RSABlindingEngine.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  7. {
  8. /**
  9. * This does your basic RSA Chaum's blinding and unblinding as outlined in
  10. * "Handbook of Applied Cryptography", page 475. You need to use this if you are
  11. * trying to get another party to generate signatures without them being aware
  12. * of the message they are signing.
  13. */
  14. public class RsaBlindingEngine
  15. : IAsymmetricBlockCipher
  16. {
  17. private readonly IRsa core;
  18. private RsaKeyParameters key;
  19. private BigInteger blindingFactor;
  20. private bool forEncryption;
  21. public RsaBlindingEngine()
  22. : this(new RsaCoreEngine())
  23. {
  24. }
  25. public RsaBlindingEngine(IRsa rsa)
  26. {
  27. this.core = rsa;
  28. }
  29. public virtual string AlgorithmName
  30. {
  31. get { return "RSA"; }
  32. }
  33. /**
  34. * Initialise the blinding engine.
  35. *
  36. * @param forEncryption true if we are encrypting (blinding), false otherwise.
  37. * @param param the necessary RSA key parameters.
  38. */
  39. public virtual void Init(
  40. bool forEncryption,
  41. ICipherParameters param)
  42. {
  43. RsaBlindingParameters p;
  44. if (param is ParametersWithRandom)
  45. {
  46. ParametersWithRandom rParam = (ParametersWithRandom)param;
  47. p = (RsaBlindingParameters)rParam.Parameters;
  48. }
  49. else
  50. {
  51. p = (RsaBlindingParameters)param;
  52. }
  53. core.Init(forEncryption, p.PublicKey);
  54. this.forEncryption = forEncryption;
  55. this.key = p.PublicKey;
  56. this.blindingFactor = p.BlindingFactor;
  57. }
  58. /**
  59. * Return the maximum size for an input block to this engine.
  60. * For RSA this is always one byte less than the key size on
  61. * encryption, and the same length as the key size on decryption.
  62. *
  63. * @return maximum size for an input block.
  64. */
  65. public virtual int GetInputBlockSize()
  66. {
  67. return core.GetInputBlockSize();
  68. }
  69. /**
  70. * Return the maximum size for an output block to this engine.
  71. * For RSA this is always one byte less than the key size on
  72. * decryption, and the same length as the key size on encryption.
  73. *
  74. * @return maximum size for an output block.
  75. */
  76. public virtual int GetOutputBlockSize()
  77. {
  78. return core.GetOutputBlockSize();
  79. }
  80. /**
  81. * Process a single block using the RSA blinding algorithm.
  82. *
  83. * @param in the input array.
  84. * @param inOff the offset into the input buffer where the data starts.
  85. * @param inLen the length of the data to be processed.
  86. * @return the result of the RSA process.
  87. * @throws DataLengthException the input block is too large.
  88. */
  89. public virtual byte[] ProcessBlock(
  90. byte[] inBuf,
  91. int inOff,
  92. int inLen)
  93. {
  94. BigInteger msg = core.ConvertInput(inBuf, inOff, inLen);
  95. if (forEncryption)
  96. {
  97. msg = BlindMessage(msg);
  98. }
  99. else
  100. {
  101. msg = UnblindMessage(msg);
  102. }
  103. return core.ConvertOutput(msg);
  104. }
  105. /*
  106. * Blind message with the blind factor.
  107. */
  108. private BigInteger BlindMessage(
  109. BigInteger msg)
  110. {
  111. BigInteger blindMsg = blindingFactor;
  112. blindMsg = msg.Multiply(blindMsg.ModPow(key.Exponent, key.Modulus));
  113. blindMsg = blindMsg.Mod(key.Modulus);
  114. return blindMsg;
  115. }
  116. /*
  117. * Unblind the message blinded with the blind factor.
  118. */
  119. private BigInteger UnblindMessage(
  120. BigInteger blindedMsg)
  121. {
  122. BigInteger m = key.Modulus;
  123. BigInteger msg = blindedMsg;
  124. BigInteger blindFactorInverse = blindingFactor.ModInverse(m);
  125. msg = msg.Multiply(blindFactorInverse);
  126. msg = msg.Mod(m);
  127. return msg;
  128. }
  129. }
  130. }
  131. #pragma warning restore
  132. #endif