FpeEngine.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Fpe
  7. {
  8. /// <summary>Base class for format-preserving encryption.</summary>
  9. public abstract class FpeEngine
  10. {
  11. protected readonly IBlockCipher baseCipher;
  12. protected bool forEncryption;
  13. protected FpeParameters fpeParameters;
  14. protected FpeEngine(IBlockCipher baseCipher)
  15. {
  16. this.baseCipher = baseCipher;
  17. }
  18. /// <summary>
  19. /// Process length bytes from inBuf, writing the output to outBuf.
  20. /// </summary>
  21. /// <returns>number of bytes output.</returns>
  22. /// <param name="inBuf">input data.</param>
  23. /// <param name="inOff">offset in input data to start at.</param>
  24. /// <param name="length">number of bytes to process.</param>
  25. /// <param name="outBuf">destination buffer.</param>
  26. /// <param name="outOff">offset to start writing at in destination buffer.</param>
  27. public virtual int ProcessBlock(byte[] inBuf, int inOff, int length, byte[] outBuf, int outOff)
  28. {
  29. if (fpeParameters == null)
  30. throw new InvalidOperationException("FPE engine not initialized");
  31. if (length < 0)
  32. throw new ArgumentException("cannot be negative", "length");
  33. if (inBuf == null)
  34. throw new ArgumentNullException("inBuf");
  35. if (outBuf == null)
  36. throw new ArgumentNullException("outBuf");
  37. Check.DataLength(inBuf, inOff, length, "input buffer too short");
  38. Check.OutputLength(outBuf, outOff, length, "output buffer too short");
  39. if (forEncryption)
  40. {
  41. return EncryptBlock(inBuf, inOff, length, outBuf, outOff);
  42. }
  43. else
  44. {
  45. return DecryptBlock(inBuf, inOff, length, outBuf, outOff);
  46. }
  47. }
  48. protected static bool IsOverrideSet(string propName)
  49. {
  50. string propValue = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetEnvironmentVariable(propName);
  51. return propValue != null && BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.EqualsIgnoreCase("true", propValue);
  52. }
  53. /// <summary>
  54. /// Initialize the FPE engine for encryption/decryption.
  55. /// </summary>
  56. /// <returns>number of bytes output.</returns>
  57. /// <param name="forEncryption">true if initialising for encryption, false otherwise.</param>
  58. /// <param name="parameters ">the key and other parameters to use to set the engine up.</param>
  59. public abstract void Init(bool forEncryption, ICipherParameters parameters);
  60. protected abstract int EncryptBlock(byte[] inBuf, int inOff, int length, byte[] outBuf, int outOff);
  61. protected abstract int DecryptBlock(byte[] inBuf, int inOff, int length, byte[] outBuf, int outOff);
  62. }
  63. }
  64. #pragma warning restore
  65. #endif