RSABlindedEngine.cs 4.5 KB

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