OaepEncoding.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Encodings
  9. {
  10. /**
  11. * Optimal Asymmetric Encryption Padding (OAEP) - see PKCS 1 V 2.
  12. */
  13. public class OaepEncoding
  14. : IAsymmetricBlockCipher
  15. {
  16. private byte[] defHash;
  17. private IDigest mgf1Hash;
  18. private IAsymmetricBlockCipher engine;
  19. private SecureRandom random;
  20. private bool forEncryption;
  21. public OaepEncoding(
  22. IAsymmetricBlockCipher cipher)
  23. : this(cipher, new Sha1Digest(), null)
  24. {
  25. }
  26. public OaepEncoding(
  27. IAsymmetricBlockCipher cipher,
  28. IDigest hash)
  29. : this(cipher, hash, null)
  30. {
  31. }
  32. public OaepEncoding(
  33. IAsymmetricBlockCipher cipher,
  34. IDigest hash,
  35. byte[] encodingParams)
  36. : this(cipher, hash, hash, encodingParams)
  37. {
  38. }
  39. public OaepEncoding(
  40. IAsymmetricBlockCipher cipher,
  41. IDigest hash,
  42. IDigest mgf1Hash,
  43. byte[] encodingParams)
  44. {
  45. this.engine = cipher;
  46. this.mgf1Hash = mgf1Hash;
  47. this.defHash = new byte[hash.GetDigestSize()];
  48. hash.Reset();
  49. if (encodingParams != null)
  50. {
  51. hash.BlockUpdate(encodingParams, 0, encodingParams.Length);
  52. }
  53. hash.DoFinal(defHash, 0);
  54. }
  55. public IAsymmetricBlockCipher GetUnderlyingCipher()
  56. {
  57. return engine;
  58. }
  59. public string AlgorithmName
  60. {
  61. get { return engine.AlgorithmName + "/OAEPPadding"; }
  62. }
  63. public void Init(
  64. bool forEncryption,
  65. ICipherParameters param)
  66. {
  67. if (param is ParametersWithRandom)
  68. {
  69. ParametersWithRandom rParam = (ParametersWithRandom)param;
  70. this.random = rParam.Random;
  71. }
  72. else
  73. {
  74. this.random = new SecureRandom();
  75. }
  76. engine.Init(forEncryption, param);
  77. this.forEncryption = forEncryption;
  78. }
  79. public int GetInputBlockSize()
  80. {
  81. int baseBlockSize = engine.GetInputBlockSize();
  82. if (forEncryption)
  83. {
  84. return baseBlockSize - 1 - 2 * defHash.Length;
  85. }
  86. else
  87. {
  88. return baseBlockSize;
  89. }
  90. }
  91. public int GetOutputBlockSize()
  92. {
  93. int baseBlockSize = engine.GetOutputBlockSize();
  94. if (forEncryption)
  95. {
  96. return baseBlockSize;
  97. }
  98. else
  99. {
  100. return baseBlockSize - 1 - 2 * defHash.Length;
  101. }
  102. }
  103. public byte[] ProcessBlock(
  104. byte[] inBytes,
  105. int inOff,
  106. int inLen)
  107. {
  108. if (forEncryption)
  109. {
  110. return EncodeBlock(inBytes, inOff, inLen);
  111. }
  112. else
  113. {
  114. return DecodeBlock(inBytes, inOff, inLen);
  115. }
  116. }
  117. private byte[] EncodeBlock(
  118. byte[] inBytes,
  119. int inOff,
  120. int inLen)
  121. {
  122. Check.DataLength(inLen > GetInputBlockSize(), "input data too long");
  123. byte[] block = new byte[GetInputBlockSize() + 1 + 2 * defHash.Length];
  124. //
  125. // copy in the message
  126. //
  127. Array.Copy(inBytes, inOff, block, block.Length - inLen, inLen);
  128. //
  129. // add sentinel
  130. //
  131. block[block.Length - inLen - 1] = 0x01;
  132. //
  133. // as the block is already zeroed - there's no need to add PS (the >= 0 pad of 0)
  134. //
  135. //
  136. // add the hash of the encoding params.
  137. //
  138. Array.Copy(defHash, 0, block, defHash.Length, defHash.Length);
  139. //
  140. // generate the seed.
  141. //
  142. byte[] seed = SecureRandom.GetNextBytes(random, defHash.Length);
  143. //
  144. // mask the message block.
  145. //
  146. byte[] mask = maskGeneratorFunction1(seed, 0, seed.Length, block.Length - defHash.Length);
  147. for (int i = defHash.Length; i != block.Length; i++)
  148. {
  149. block[i] ^= mask[i - defHash.Length];
  150. }
  151. //
  152. // add in the seed
  153. //
  154. Array.Copy(seed, 0, block, 0, defHash.Length);
  155. //
  156. // mask the seed.
  157. //
  158. mask = maskGeneratorFunction1(
  159. block, defHash.Length, block.Length - defHash.Length, defHash.Length);
  160. for (int i = 0; i != defHash.Length; i++)
  161. {
  162. block[i] ^= mask[i];
  163. }
  164. return engine.ProcessBlock(block, 0, block.Length);
  165. }
  166. /**
  167. * @exception InvalidCipherTextException if the decrypted block turns out to
  168. * be badly formatted.
  169. */
  170. private byte[] DecodeBlock(
  171. byte[] inBytes,
  172. int inOff,
  173. int inLen)
  174. {
  175. byte[] data = engine.ProcessBlock(inBytes, inOff, inLen);
  176. byte[] block = new byte[engine.GetOutputBlockSize()];
  177. //
  178. // as we may have zeros in our leading bytes for the block we produced
  179. // on encryption, we need to make sure our decrypted block comes back
  180. // the same size.
  181. //
  182. bool wrongData = (block.Length < (2 * defHash.Length) + 1);
  183. if (data.Length <= block.Length)
  184. {
  185. Array.Copy(data, 0, block, block.Length - data.Length, data.Length);
  186. }
  187. else
  188. {
  189. Array.Copy(data, 0, block, 0, block.Length);
  190. wrongData = true;
  191. }
  192. //
  193. // unmask the seed.
  194. //
  195. byte[] mask = maskGeneratorFunction1(
  196. block, defHash.Length, block.Length - defHash.Length, defHash.Length);
  197. for (int i = 0; i != defHash.Length; i++)
  198. {
  199. block[i] ^= mask[i];
  200. }
  201. //
  202. // unmask the message block.
  203. //
  204. mask = maskGeneratorFunction1(block, 0, defHash.Length, block.Length - defHash.Length);
  205. for (int i = defHash.Length; i != block.Length; i++)
  206. {
  207. block[i] ^= mask[i - defHash.Length];
  208. }
  209. //
  210. // check the hash of the encoding params.
  211. // long check to try to avoid this been a source of a timing attack.
  212. //
  213. bool defHashWrong = false;
  214. for (int i = 0; i != defHash.Length; i++)
  215. {
  216. if (defHash[i] != block[defHash.Length + i])
  217. {
  218. defHashWrong = true;
  219. }
  220. }
  221. //
  222. // find the data block
  223. //
  224. int start = block.Length;
  225. for (int index = 2 * defHash.Length; index != block.Length; index++)
  226. {
  227. if (block[index] != 0 & start == block.Length)
  228. {
  229. start = index;
  230. }
  231. }
  232. bool dataStartWrong = (start > (block.Length - 1) | block[start] != 1);
  233. start++;
  234. if (defHashWrong | wrongData | dataStartWrong)
  235. {
  236. Arrays.Fill(block, 0);
  237. throw new InvalidCipherTextException("data wrong");
  238. }
  239. //
  240. // extract the data block
  241. //
  242. byte[] output = new byte[block.Length - start];
  243. Array.Copy(block, start, output, 0, output.Length);
  244. return output;
  245. }
  246. /**
  247. * int to octet string.
  248. */
  249. private void ItoOSP(
  250. int i,
  251. byte[] sp)
  252. {
  253. sp[0] = (byte)((uint)i >> 24);
  254. sp[1] = (byte)((uint)i >> 16);
  255. sp[2] = (byte)((uint)i >> 8);
  256. sp[3] = (byte)((uint)i >> 0);
  257. }
  258. /**
  259. * mask generator function, as described in PKCS1v2.
  260. */
  261. private byte[] maskGeneratorFunction1(
  262. byte[] Z,
  263. int zOff,
  264. int zLen,
  265. int length)
  266. {
  267. byte[] mask = new byte[length];
  268. byte[] hashBuf = new byte[mgf1Hash.GetDigestSize()];
  269. byte[] C = new byte[4];
  270. int counter = 0;
  271. mgf1Hash.Reset();
  272. while (counter < (length / hashBuf.Length))
  273. {
  274. ItoOSP(counter, C);
  275. mgf1Hash.BlockUpdate(Z, zOff, zLen);
  276. mgf1Hash.BlockUpdate(C, 0, C.Length);
  277. mgf1Hash.DoFinal(hashBuf, 0);
  278. Array.Copy(hashBuf, 0, mask, counter * hashBuf.Length, hashBuf.Length);
  279. counter++;
  280. }
  281. if ((counter * hashBuf.Length) < length)
  282. {
  283. ItoOSP(counter, C);
  284. mgf1Hash.BlockUpdate(Z, zOff, zLen);
  285. mgf1Hash.BlockUpdate(C, 0, C.Length);
  286. mgf1Hash.DoFinal(hashBuf, 0);
  287. Array.Copy(hashBuf, 0, mask, counter * hashBuf.Length, mask.Length - (counter * hashBuf.Length));
  288. }
  289. return mask;
  290. }
  291. }
  292. }
  293. #pragma warning restore
  294. #endif