RSABlindedEngine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.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(
  38. bool forEncryption,
  39. ICipherParameters param)
  40. {
  41. core.Init(forEncryption, param);
  42. if (param is ParametersWithRandom)
  43. {
  44. ParametersWithRandom rParam = (ParametersWithRandom)param;
  45. this.key = (RsaKeyParameters)rParam.Parameters;
  46. if (key is RsaPrivateCrtKeyParameters)
  47. {
  48. this.random = rParam.Random;
  49. }
  50. else
  51. {
  52. this.random = null;
  53. }
  54. }
  55. else
  56. {
  57. this.key = (RsaKeyParameters)param;
  58. if (key is RsaPrivateCrtKeyParameters)
  59. {
  60. this.random = new SecureRandom();
  61. }
  62. else
  63. {
  64. this.random = null;
  65. }
  66. }
  67. }
  68. /**
  69. * Return the maximum size for an input block to this engine.
  70. * For RSA this is always one byte less than the key size on
  71. * encryption, and the same length as the key size on decryption.
  72. *
  73. * @return maximum size for an input block.
  74. */
  75. public virtual int GetInputBlockSize()
  76. {
  77. return core.GetInputBlockSize();
  78. }
  79. /**
  80. * Return the maximum size for an output block to this engine.
  81. * For RSA this is always one byte less than the key size on
  82. * decryption, and the same length as the key size on encryption.
  83. *
  84. * @return maximum size for an output block.
  85. */
  86. public virtual int GetOutputBlockSize()
  87. {
  88. return core.GetOutputBlockSize();
  89. }
  90. /**
  91. * Process a single block using the basic RSA algorithm.
  92. *
  93. * @param inBuf the input array.
  94. * @param inOff the offset into the input buffer where the data starts.
  95. * @param inLen the length of the data to be processed.
  96. * @return the result of the RSA process.
  97. * @exception DataLengthException the input block is too large.
  98. */
  99. public virtual byte[] ProcessBlock(
  100. byte[] inBuf,
  101. int inOff,
  102. int inLen)
  103. {
  104. if (key == null)
  105. throw new InvalidOperationException("RSA engine not initialised");
  106. BigInteger input = core.ConvertInput(inBuf, inOff, inLen);
  107. BigInteger result;
  108. if (key is RsaPrivateCrtKeyParameters)
  109. {
  110. RsaPrivateCrtKeyParameters k = (RsaPrivateCrtKeyParameters)key;
  111. BigInteger e = k.PublicExponent;
  112. if (e != null) // can't do blinding without a public exponent
  113. {
  114. BigInteger m = k.Modulus;
  115. BigInteger r = BigIntegers.CreateRandomInRange(
  116. BigInteger.One, m.Subtract(BigInteger.One), random);
  117. BigInteger blindedInput = r.ModPow(e, m).Multiply(input).Mod(m);
  118. BigInteger blindedResult = core.ProcessBlock(blindedInput);
  119. BigInteger rInv = BigIntegers.ModOddInverse(m, r);
  120. result = blindedResult.Multiply(rInv).Mod(m);
  121. // defence against Arjen Lenstra�s CRT attack
  122. if (!input.Equals(result.ModPow(e, m)))
  123. throw new InvalidOperationException("RSA engine faulty decryption/signing detected");
  124. }
  125. else
  126. {
  127. result = core.ProcessBlock(input);
  128. }
  129. }
  130. else
  131. {
  132. result = core.ProcessBlock(input);
  133. }
  134. return core.ConvertOutput(result);
  135. }
  136. }
  137. }
  138. #pragma warning restore
  139. #endif