OaepEncoding.cs 9.4 KB

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