FpeFf3_1Engine.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.Crypto.Engines;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Fpe
  9. {
  10. public class FpeFf3_1Engine
  11. : FpeEngine
  12. {
  13. public FpeFf3_1Engine()
  14. : this(new AesEngine())
  15. {
  16. }
  17. public FpeFf3_1Engine(IBlockCipher baseCipher)
  18. : base(baseCipher)
  19. {
  20. if (IsOverrideSet(SP80038G.FPE_DISABLED))
  21. {
  22. throw new InvalidOperationException("FPE disabled");
  23. }
  24. }
  25. public override void Init(bool forEncryption, ICipherParameters parameters)
  26. {
  27. this.forEncryption = forEncryption;
  28. this.fpeParameters = (FpeParameters)parameters;
  29. baseCipher.Init(!fpeParameters.UseInverseFunction, new KeyParameter(Arrays.Reverse(fpeParameters.Key.GetKey())));
  30. if (fpeParameters.GetTweak().Length != 7)
  31. throw new ArgumentException("tweak should be 56 bits");
  32. }
  33. protected override int EncryptBlock(byte[] inBuf, int inOff, int length, byte[] outBuf, int outOff)
  34. {
  35. byte[] enc;
  36. if (fpeParameters.Radix > 256)
  37. {
  38. if ((length & 1) != 0)
  39. throw new ArgumentException("input must be an even number of bytes for a wide radix");
  40. ushort[] u16In = Pack.BE_To_UInt16(inBuf, inOff, length);
  41. ushort[] u16Out = SP80038G.EncryptFF3_1w(baseCipher, fpeParameters.Radix, fpeParameters.GetTweak(),
  42. u16In, 0, u16In.Length);
  43. enc = Pack.UInt16_To_BE(u16Out, 0, u16Out.Length);
  44. }
  45. else
  46. {
  47. enc = SP80038G.EncryptFF3_1(baseCipher, fpeParameters.Radix, fpeParameters.GetTweak(), inBuf, inOff, length);
  48. }
  49. Array.Copy(enc, 0, outBuf, outOff, length);
  50. return length;
  51. }
  52. protected override int DecryptBlock(byte[] inBuf, int inOff, int length, byte[] outBuf, int outOff)
  53. {
  54. byte[] dec;
  55. if (fpeParameters.Radix > 256)
  56. {
  57. if ((length & 1) != 0)
  58. throw new ArgumentException("input must be an even number of bytes for a wide radix");
  59. ushort[] u16In = Pack.BE_To_UInt16(inBuf, inOff, length);
  60. ushort[] u16Out = SP80038G.DecryptFF3_1w(baseCipher, fpeParameters.Radix, fpeParameters.GetTweak(),
  61. u16In, 0, u16In.Length);
  62. dec = Pack.UInt16_To_BE(u16Out, 0, u16Out.Length);
  63. }
  64. else
  65. {
  66. dec = SP80038G.DecryptFF3_1(baseCipher, fpeParameters.Radix, fpeParameters.GetTweak(), inBuf, inOff, length);
  67. }
  68. Array.Copy(dec, 0, outBuf, outOff, length);
  69. return length;
  70. }
  71. }
  72. }
  73. #pragma warning restore
  74. #endif