RSABlindedEngine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  9. {
  10. /**
  11. * this does your basic RSA algorithm with blinding
  12. */
  13. public class RsaBlindedEngine
  14. : IAsymmetricBlockCipher
  15. {
  16. private readonly IRsa core;
  17. private RsaKeyParameters key;
  18. private SecureRandom random;
  19. public RsaBlindedEngine()
  20. : this(new RsaCoreEngine())
  21. {
  22. }
  23. public RsaBlindedEngine(IRsa rsa)
  24. {
  25. this.core = rsa;
  26. }
  27. public virtual string AlgorithmName
  28. {
  29. get { return "RSA"; }
  30. }
  31. /**
  32. * initialise the RSA engine.
  33. *
  34. * @param forEncryption true if we are encrypting, false otherwise.
  35. * @param param the necessary RSA key parameters.
  36. */
  37. public virtual void Init(bool forEncryption, ICipherParameters param)
  38. {
  39. core.Init(forEncryption, param);
  40. if (param is ParametersWithRandom rParam)
  41. {
  42. this.key = (RsaKeyParameters)rParam.Parameters;
  43. if (key is RsaPrivateCrtKeyParameters)
  44. {
  45. this.random = rParam.Random;
  46. }
  47. else
  48. {
  49. this.random = null;
  50. }
  51. }
  52. else
  53. {
  54. this.key = (RsaKeyParameters)param;
  55. if (key is RsaPrivateCrtKeyParameters)
  56. {
  57. this.random = CryptoServicesRegistrar.GetSecureRandom();
  58. }
  59. else
  60. {
  61. this.random = null;
  62. }
  63. }
  64. }
  65. /**
  66. * Return the maximum size for an input block to this engine.
  67. * For RSA this is always one byte less than the key size on
  68. * encryption, and the same length as the key size on decryption.
  69. *
  70. * @return maximum size for an input block.
  71. */
  72. public virtual int GetInputBlockSize()
  73. {
  74. return core.GetInputBlockSize();
  75. }
  76. /**
  77. * Return the maximum size for an output block to this engine.
  78. * For RSA this is always one byte less than the key size on
  79. * decryption, and the same length as the key size on encryption.
  80. *
  81. * @return maximum size for an output block.
  82. */
  83. public virtual int GetOutputBlockSize()
  84. {
  85. return core.GetOutputBlockSize();
  86. }
  87. /**
  88. * Process a single block using the basic RSA algorithm.
  89. *
  90. * @param inBuf the input array.
  91. * @param inOff the offset into the input buffer where the data starts.
  92. * @param inLen the length of the data to be processed.
  93. * @return the result of the RSA process.
  94. * @exception DataLengthException the input block is too large.
  95. */
  96. public virtual byte[] ProcessBlock(
  97. byte[] inBuf,
  98. int inOff,
  99. int inLen)
  100. {
  101. if (key == null)
  102. throw new InvalidOperationException("RSA engine not initialised");
  103. BigInteger input = core.ConvertInput(inBuf, inOff, inLen);
  104. BigInteger result;
  105. if (key is RsaPrivateCrtKeyParameters)
  106. {
  107. RsaPrivateCrtKeyParameters k = (RsaPrivateCrtKeyParameters)key;
  108. BigInteger e = k.PublicExponent;
  109. if (e != null) // can't do blinding without a public exponent
  110. {
  111. BigInteger m = k.Modulus;
  112. BigInteger r = BigIntegers.CreateRandomInRange(
  113. BigInteger.One, m.Subtract(BigInteger.One), random);
  114. BigInteger blindedInput = r.ModPow(e, m).Multiply(input).Mod(m);
  115. BigInteger blindedResult = core.ProcessBlock(blindedInput);
  116. BigInteger rInv = BigIntegers.ModOddInverse(m, r);
  117. result = blindedResult.Multiply(rInv).Mod(m);
  118. // defence against Arjen Lenstra�s CRT attack
  119. if (!input.Equals(result.ModPow(e, m)))
  120. throw new InvalidOperationException("RSA engine faulty decryption/signing detected");
  121. }
  122. else
  123. {
  124. result = core.ProcessBlock(input);
  125. }
  126. }
  127. else
  128. {
  129. result = core.ProcessBlock(input);
  130. }
  131. return core.ConvertOutput(result);
  132. }
  133. }
  134. }
  135. #pragma warning restore
  136. #endif