ISO9796d1Encoding.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Encodings
  8. {
  9. /**
  10. * ISO 9796-1 padding. Note in the light of recent results you should
  11. * only use this with RSA (rather than the "simpler" Rabin keys) and you
  12. * should never use it with anything other than a hash (ie. even if the
  13. * message is small don't sign the message, sign it's hash) or some "random"
  14. * value. See your favorite search engine for details.
  15. */
  16. public class ISO9796d1Encoding
  17. : IAsymmetricBlockCipher
  18. {
  19. private static readonly BigInteger Sixteen = BigInteger.ValueOf(16);
  20. private static readonly BigInteger Six = BigInteger.ValueOf(6);
  21. private static readonly byte[] shadows = { 0xe, 0x3, 0x5, 0x8, 0x9, 0x4, 0x2, 0xf,
  22. 0x0, 0xd, 0xb, 0x6, 0x7, 0xa, 0xc, 0x1 };
  23. private static readonly byte[] inverse = { 0x8, 0xf, 0x6, 0x1, 0x5, 0x2, 0xb, 0xc,
  24. 0x3, 0x4, 0xd, 0xa, 0xe, 0x9, 0x0, 0x7 };
  25. private readonly IAsymmetricBlockCipher engine;
  26. private bool forEncryption;
  27. private int bitSize;
  28. private int padBits = 0;
  29. private BigInteger modulus;
  30. public ISO9796d1Encoding(
  31. IAsymmetricBlockCipher cipher)
  32. {
  33. this.engine = cipher;
  34. }
  35. public string AlgorithmName
  36. {
  37. get { return engine.AlgorithmName + "/ISO9796-1Padding"; }
  38. }
  39. public IAsymmetricBlockCipher GetUnderlyingCipher()
  40. {
  41. return engine;
  42. }
  43. public void Init(
  44. bool forEncryption,
  45. ICipherParameters parameters)
  46. {
  47. RsaKeyParameters kParam;
  48. if (parameters is ParametersWithRandom)
  49. {
  50. ParametersWithRandom rParam = (ParametersWithRandom)parameters;
  51. kParam = (RsaKeyParameters)rParam.Parameters;
  52. }
  53. else
  54. {
  55. kParam = (RsaKeyParameters)parameters;
  56. }
  57. engine.Init(forEncryption, parameters);
  58. modulus = kParam.Modulus;
  59. bitSize = modulus.BitLength;
  60. this.forEncryption = forEncryption;
  61. }
  62. /**
  63. * return the input block size. The largest message we can process
  64. * is (key_size_in_bits + 3)/16, which in our world comes to
  65. * key_size_in_bytes / 2.
  66. */
  67. public int GetInputBlockSize()
  68. {
  69. int baseBlockSize = engine.GetInputBlockSize();
  70. if (forEncryption)
  71. {
  72. return (baseBlockSize + 1) / 2;
  73. }
  74. else
  75. {
  76. return baseBlockSize;
  77. }
  78. }
  79. /**
  80. * return the maximum possible size for the output.
  81. */
  82. public int GetOutputBlockSize()
  83. {
  84. int baseBlockSize = engine.GetOutputBlockSize();
  85. if (forEncryption)
  86. {
  87. return baseBlockSize;
  88. }
  89. else
  90. {
  91. return (baseBlockSize + 1) / 2;
  92. }
  93. }
  94. /**
  95. * set the number of bits in the next message to be treated as
  96. * pad bits.
  97. */
  98. public void SetPadBits(
  99. int padBits)
  100. {
  101. if (padBits > 7)
  102. {
  103. throw new ArgumentException("padBits > 7");
  104. }
  105. this.padBits = padBits;
  106. }
  107. /**
  108. * retrieve the number of pad bits in the last decoded message.
  109. */
  110. public int GetPadBits()
  111. {
  112. return padBits;
  113. }
  114. public byte[] ProcessBlock(
  115. byte[] input,
  116. int inOff,
  117. int length)
  118. {
  119. if (forEncryption)
  120. {
  121. return EncodeBlock(input, inOff, length);
  122. }
  123. else
  124. {
  125. return DecodeBlock(input, inOff, length);
  126. }
  127. }
  128. private byte[] EncodeBlock(
  129. byte[] input,
  130. int inOff,
  131. int inLen)
  132. {
  133. byte[] block = new byte[(bitSize + 7) / 8];
  134. int r = padBits + 1;
  135. int z = inLen;
  136. int t = (bitSize + 13) / 16;
  137. for (int i = 0; i < t; i += z)
  138. {
  139. if (i > t - z)
  140. {
  141. Array.Copy(input, inOff + inLen - (t - i),
  142. block, block.Length - t, t - i);
  143. }
  144. else
  145. {
  146. Array.Copy(input, inOff, block, block.Length - (i + z), z);
  147. }
  148. }
  149. for (int i = block.Length - 2 * t; i != block.Length; i += 2)
  150. {
  151. byte val = block[block.Length - t + i / 2];
  152. block[i] = (byte)((shadows[(uint) (val & 0xff) >> 4] << 4)
  153. | shadows[val & 0x0f]);
  154. block[i + 1] = val;
  155. }
  156. block[block.Length - 2 * z] ^= (byte) r;
  157. block[block.Length - 1] = (byte)((block[block.Length - 1] << 4) | 0x06);
  158. int maxBit = (8 - (bitSize - 1) % 8);
  159. int offSet = 0;
  160. if (maxBit != 8)
  161. {
  162. block[0] &= (byte) ((ushort) 0xff >> maxBit);
  163. block[0] |= (byte) ((ushort) 0x80 >> maxBit);
  164. }
  165. else
  166. {
  167. block[0] = 0x00;
  168. block[1] |= 0x80;
  169. offSet = 1;
  170. }
  171. return engine.ProcessBlock(block, offSet, block.Length - offSet);
  172. }
  173. /**
  174. * @exception InvalidCipherTextException if the decrypted block is not a valid ISO 9796 bit string
  175. */
  176. private byte[] DecodeBlock(
  177. byte[] input,
  178. int inOff,
  179. int inLen)
  180. {
  181. byte[] block = engine.ProcessBlock(input, inOff, inLen);
  182. int r = 1;
  183. int t = (bitSize + 13) / 16;
  184. BigInteger iS = new BigInteger(1, block);
  185. BigInteger iR;
  186. if (iS.Mod(Sixteen).Equals(Six))
  187. {
  188. iR = iS;
  189. }
  190. else
  191. {
  192. iR = modulus.Subtract(iS);
  193. if (!iR.Mod(Sixteen).Equals(Six))
  194. throw new InvalidCipherTextException("resulting integer iS or (modulus - iS) is not congruent to 6 mod 16");
  195. }
  196. block = iR.ToByteArrayUnsigned();
  197. if ((block[block.Length - 1] & 0x0f) != 0x6)
  198. throw new InvalidCipherTextException("invalid forcing byte in block");
  199. block[block.Length - 1] =
  200. (byte)(((ushort)(block[block.Length - 1] & 0xff) >> 4)
  201. | ((inverse[(block[block.Length - 2] & 0xff) >> 4]) << 4));
  202. block[0] = (byte)((shadows[(uint) (block[1] & 0xff) >> 4] << 4)
  203. | shadows[block[1] & 0x0f]);
  204. bool boundaryFound = false;
  205. int boundary = 0;
  206. for (int i = block.Length - 1; i >= block.Length - 2 * t; i -= 2)
  207. {
  208. int val = ((shadows[(uint) (block[i] & 0xff) >> 4] << 4)
  209. | shadows[block[i] & 0x0f]);
  210. if (((block[i - 1] ^ val) & 0xff) != 0)
  211. {
  212. if (!boundaryFound)
  213. {
  214. boundaryFound = true;
  215. r = (block[i - 1] ^ val) & 0xff;
  216. boundary = i - 1;
  217. }
  218. else
  219. {
  220. throw new InvalidCipherTextException("invalid tsums in block");
  221. }
  222. }
  223. }
  224. block[boundary] = 0;
  225. byte[] nblock = new byte[(block.Length - boundary) / 2];
  226. for (int i = 0; i < nblock.Length; i++)
  227. {
  228. nblock[i] = block[2 * i + boundary + 1];
  229. }
  230. padBits = r - 1;
  231. return nblock;
  232. }
  233. }
  234. }
  235. #pragma warning restore
  236. #endif