PKCS1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. //
  2. // PKCS1.cs - Implements PKCS#1 primitives.
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. namespace System.Security.Cryptography {
  11. // References:
  12. // a. PKCS#1: RSA Cryptography Standard
  13. // http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/index.html
  14. internal class PKCS1 {
  15. private static bool Compare (byte[] array1, byte[] array2)
  16. {
  17. bool result = (array1.Length == array2.Length);
  18. if (result) {
  19. for (int i=0; i < array1.Length; i++)
  20. if (array1[i] != array2[i])
  21. return false;
  22. }
  23. return result;
  24. }
  25. private static byte[] xor (byte[] array1, byte[] array2)
  26. {
  27. byte[] result = new byte [array1.Length];
  28. for (int i=0; i < result.Length; i++)
  29. result[i] = (byte) (array1[i] ^ array2[i]);
  30. return result;
  31. }
  32. private static byte[] emptySHA1 = { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 };
  33. private static byte[] emptySHA256 = { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
  34. private static byte[] emptySHA384 = { 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b };
  35. private static byte[] emptySHA512 = { 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e };
  36. private static byte[] GetEmptyHash (HashAlgorithm hash)
  37. {
  38. if (hash is SHA1)
  39. return emptySHA1;
  40. else if (hash is SHA256)
  41. return emptySHA256;
  42. else if (hash is SHA384)
  43. return emptySHA384;
  44. else if (hash is SHA512)
  45. return emptySHA512;
  46. else
  47. return null;
  48. }
  49. // PKCS #1 v.2.1, Section 4.1
  50. // I2OSP converts a nonnegative integer to an octet string of a specified length.
  51. public static byte[] I2OSP (int x, int size)
  52. {
  53. byte[] array = BitConverter.GetBytes (x);
  54. return I2OSP (array, size);
  55. }
  56. public static byte[] I2OSP (byte[] x, int size)
  57. {
  58. byte[] result = new byte [size];
  59. Array.Copy (x, 0, result, (result.Length - x.Length), x.Length);
  60. return result;
  61. }
  62. // PKCS #1 v.2.1, Section 4.2
  63. // OS2IP converts an octet string to a nonnegative integer.
  64. public static byte[] OS2IP (byte[] x)
  65. {
  66. int i = 0;
  67. while ((x[i++] == 0x00) && (i < x.Length));
  68. i--;
  69. if (i > 0) {
  70. byte[] result = new byte [x.Length - i];
  71. Array.Copy (x, i, result, 0, result.Length);
  72. return result;
  73. }
  74. else
  75. return x;
  76. }
  77. // PKCS #1 v.2.1, Section 5.1.1
  78. public static byte[] RSAEP (RSA rsa, byte[] m)
  79. {
  80. // c = m^e mod n
  81. return rsa.EncryptValue (m);
  82. }
  83. // PKCS #1 v.2.1, Section 5.1.2
  84. public static byte[] RSADP (RSA rsa, byte[] c)
  85. {
  86. // m = c^d mod n
  87. // Decrypt value may apply CRT optimizations
  88. return rsa.DecryptValue (c);
  89. }
  90. // PKCS #1 v.2.1, Section 5.2.1
  91. public static byte[] RSASP1 (RSA rsa, byte[] m)
  92. {
  93. // first form: s = m^d mod n
  94. // Decrypt value may apply CRT optimizations
  95. return rsa.DecryptValue (m);
  96. }
  97. // PKCS #1 v.2.1, Section 5.2.2
  98. public static byte[] RSAVP1 (RSA rsa, byte[] s)
  99. {
  100. // m = s^e mod n
  101. return rsa.EncryptValue (s);
  102. }
  103. // PKCS #1 v.2.1, Section 7.1.1
  104. // RSAES-OAEP-ENCRYPT ((n, e), M, L)
  105. public static byte[] Encrypt_OAEP (RSA rsa, HashAlgorithm hash, RandomNumberGenerator rng, byte[] M)
  106. {
  107. int size = rsa.KeySize / 8;
  108. int hLen = hash.HashSize / 8;
  109. if (M.Length > size - 2 * hLen - 2)
  110. throw new CryptographicException ("message too long");
  111. // empty label L SHA1 hash
  112. byte[] lHash = GetEmptyHash (hash);
  113. int PSLength = (size - M.Length - 2 * hLen - 2);
  114. // DB = lHash || PS || 0x01 || M
  115. byte[] DB = new byte [lHash.Length + PSLength + 1 + M.Length];
  116. Array.Copy (lHash, 0, DB, 0, lHash.Length);
  117. DB [(lHash.Length + PSLength)] = 0x01;
  118. Array.Copy (M, 0, DB, (DB.Length - M.Length), M.Length);
  119. byte[] seed = new byte [hLen];
  120. rng.GetBytes (seed);
  121. byte[] dbMask = MGF1 (hash, seed, size - hLen - 1);
  122. byte[] maskedDB = xor (DB, dbMask);
  123. byte[] seedMask = MGF1 (hash, maskedDB, hLen);
  124. byte[] maskedSeed = xor (seed, seedMask);
  125. // EM = 0x00 || maskedSeed || maskedDB
  126. byte[] EM = new byte [maskedSeed.Length + maskedDB.Length + 1];
  127. Array.Copy (maskedSeed, 0, EM, 1, maskedSeed.Length);
  128. Array.Copy (maskedDB, 0, EM, maskedSeed.Length + 1, maskedDB.Length);
  129. byte[] m = OS2IP (EM);
  130. byte[] c = RSAEP (rsa, m);
  131. return I2OSP (c, size);
  132. }
  133. // PKCS #1 v.2.1, Section 7.1.2
  134. // RSAES-OAEP-DECRYPT (K, C, L)
  135. public static byte[] Decrypt_OAEP (RSA rsa, HashAlgorithm hash, byte[] C)
  136. {
  137. int size = rsa.KeySize / 8;
  138. int hLen = hash.HashSize / 8;
  139. if ((size < (2 * hLen + 2)) || (C.Length != size))
  140. throw new CryptographicException ("decryption error");
  141. byte[] c = OS2IP (C);
  142. byte[] m = RSADP (rsa, c);
  143. byte[] EM = I2OSP (m, size);
  144. // split EM = Y || maskedSeed || maskedDB
  145. byte[] maskedSeed = new byte [hLen];
  146. Array.Copy (EM, 1, maskedSeed, 0, maskedSeed.Length);
  147. byte[] maskedDB = new byte [size - hLen - 1];
  148. Array.Copy (EM, (EM.Length - maskedDB.Length), maskedDB, 0, maskedDB.Length);
  149. byte[] seedMask = MGF1 (hash, maskedDB, hLen);
  150. byte[] seed = xor (maskedSeed, seedMask);
  151. byte[] dbMask = MGF1 (hash, seed, size - hLen - 1);
  152. byte[] DB = xor (maskedDB, dbMask);
  153. byte[] lHash = GetEmptyHash (hash);
  154. // split DB = lHash’ || PS || 0x01 || M
  155. byte[] dbHash = new byte [lHash.Length];
  156. Array.Copy (DB, 0, dbHash, 0, dbHash.Length);
  157. bool h = Compare (lHash, dbHash);
  158. // find separator 0x01
  159. int nPos = lHash.Length;
  160. while (DB[nPos] == 0)
  161. nPos++;
  162. int Msize = DB.Length - nPos - 1;
  163. byte[] M = new byte [Msize];
  164. Array.Copy (DB, (nPos + 1), M, 0, Msize);
  165. // we could have returned EM[0] sooner but would be helping a timing attack
  166. if ((EM[0] != 0) || (!h) || (DB[nPos] != 0x01))
  167. return null;
  168. return M;
  169. }
  170. // PKCS #1 v.2.1, Section 7.2.1
  171. // RSAES-PKCS1-V1_5-ENCRYPT ((n, e), M)
  172. public static byte[] Encrypt_v15 (RSA rsa, RandomNumberGenerator rng, byte[] M)
  173. {
  174. int size = rsa.KeySize / 8;
  175. if (M.Length > size - 11)
  176. throw new CryptographicException ("message too long");
  177. int PSLength = Math.Max (8, (size - M.Length - 3));
  178. byte[] PS = new byte [PSLength];
  179. rng.GetNonZeroBytes (PS);
  180. byte[] EM = new byte [size];
  181. EM [1] = 0x02;
  182. Array.Copy (PS, 0, EM, 2, PSLength);
  183. Array.Copy (M, 0, EM, (size - M.Length), M.Length);
  184. byte[] m = OS2IP (EM);
  185. byte[] c = RSAEP (rsa, m);
  186. byte[] C = I2OSP (c, size);
  187. return C;
  188. }
  189. // PKCS #1 v.2.1, Section 7.2.2
  190. // RSAES-PKCS1-V1_5-DECRYPT (K, C)
  191. public static byte[] Decrypt_v15 (RSA rsa, byte[] C)
  192. {
  193. int size = rsa.KeySize / 8;
  194. if ((size < 11) || (C.Length != size))
  195. throw new CryptographicException ("decryption error");
  196. byte[] c = OS2IP (C);
  197. byte[] m = RSADP (rsa, c);
  198. byte[] EM = I2OSP (m, size);
  199. if ((EM [0] != 0x00) || (EM [1] != 0x02))
  200. return null;
  201. int mPos = 10;
  202. // PS is a minimum of 8 bytes + 2 bytes for header
  203. while ((EM [mPos] != 0x00) && (mPos < EM.Length))
  204. mPos++;
  205. if (EM [mPos] != 0x00)
  206. return null;
  207. mPos++;
  208. byte[] M = new byte [EM.Length - mPos];
  209. Array.Copy (EM, mPos, M, 0, M.Length);
  210. return M;
  211. }
  212. // PKCS #1 v.2.1, Section 8.2.1
  213. // RSASSA-PKCS1-V1_5-SIGN (K, M)
  214. public static byte[] Sign_v15 (RSA rsa, string oid, byte[] hash)
  215. {
  216. int size = rsa.KeySize / 8;
  217. byte[] EM = Encode_v15 (oid, hash, size);
  218. byte[] m = OS2IP (EM);
  219. byte[] s = RSASP1 (rsa, m);
  220. byte[] S = I2OSP (s, size);
  221. return S;
  222. }
  223. // PKCS #1 v.2.1, Section 8.2.2
  224. // RSASSA-PKCS1-V1_5-VERIFY ((n, e), M, S)
  225. public static bool Verify_v15 (RSA rsa, string oid, byte[] hash, byte[] signature)
  226. {
  227. int size = rsa.KeySize / 8;
  228. byte[] s = OS2IP (signature);
  229. byte[] m = RSAVP1 (rsa, signature);
  230. byte[] EM2 = I2OSP (m, size);
  231. byte[] EM = Encode_v15 (oid, hash, size);
  232. return Compare(EM, EM2);
  233. }
  234. // Note: MD2 isn't supported in .NET framework
  235. private static byte[] md2const = { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 };
  236. private static byte[] md5const = { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
  237. private static byte[] sha1const = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
  238. private static byte[] sha256const = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
  239. private static byte[] sha384const = { 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
  240. private static byte[] sha512const = { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
  241. // PKCS #1 v.2.1, Section 9.2
  242. // EMSA-PKCS1-v1_5-Encode
  243. public static byte[] Encode_v15 (string oid, byte[] hash, int emLength)
  244. {
  245. string e = "bad hash length for ";
  246. byte[] t = null;
  247. switch (oid) {
  248. case "1.3.14.3.2.26":
  249. // SHA1
  250. if (hash.Length != 20)
  251. throw new CryptographicException (e + oid);
  252. t = new byte [35];
  253. Array.Copy (sha1const, 0, t, 0, sha1const.Length);
  254. break;
  255. case "1.2.840.113549.2.2":
  256. // MD2
  257. if (hash.Length != 16)
  258. throw new CryptographicException (e + oid);
  259. t = new byte [34];
  260. Array.Copy (md2const, 0, t, 0, md2const.Length);
  261. break;
  262. case "1.2.840.113549.2.5":
  263. // MD5
  264. if (hash.Length != 16)
  265. throw new CryptographicException (e + oid);
  266. t = new byte [34];
  267. Array.Copy (md5const, 0, t, 0, md5const.Length);
  268. break;
  269. case "2.16.840.1.101.3.4.1":
  270. // SHA256
  271. if (hash.Length != 32)
  272. throw new CryptographicException (e + oid);
  273. t = new byte [51];
  274. Array.Copy (sha256const, 0, t, 0, sha256const.Length);
  275. break;
  276. case "2.16.840.1.101.3.4.2":
  277. // SHA384
  278. if (hash.Length != 48)
  279. throw new CryptographicException (e + oid);
  280. t = new byte [67];
  281. Array.Copy (sha384const, 0, t, 0, sha384const.Length);
  282. break;
  283. case "2.16.840.1.101.3.4.3":
  284. // SHA512
  285. if (hash.Length != 64)
  286. throw new CryptographicException (e + oid);
  287. t = new byte [83];
  288. Array.Copy (sha512const, 0, t, 0, sha512const.Length);
  289. break;
  290. default:
  291. return null;
  292. }
  293. Array.Copy (hash, 0, t, t.Length - hash.Length, hash.Length);
  294. int PSLength = Math.Max (8, emLength - t.Length - 3);
  295. // PS = PSLength of 0xff
  296. // EM = 0x00 | 0x01 | PS | 0x00 | T
  297. byte[] EM = new byte [PSLength + t.Length + 3];
  298. EM [1] = 0x01;
  299. for (int i=2; i < PSLength + 2; i++)
  300. EM[i] = 0xff;
  301. Array.Copy (t, 0, EM, PSLength + 3, t.Length);
  302. return EM;
  303. }
  304. // PKCS #1 v.2.1, Section B.2.1
  305. public static byte[] MGF1 (HashAlgorithm hash, byte[] mgfSeed, int maskLen)
  306. {
  307. // 1. If maskLen > 2^32 hLen, output “mask too long” and stop.
  308. // easy - this is impossible by using a int (31bits) as parameter ;-)
  309. // BUT with a signed int we do have to check for negative values!
  310. if (maskLen < 0)
  311. throw new OverflowException();
  312. int mgfSeedLength = mgfSeed.Length;
  313. int hLen = (hash.HashSize >> 3); // from bits to bytes
  314. int iterations = (maskLen / hLen);
  315. if (maskLen % hLen != 0)
  316. iterations++;
  317. // 2. Let T be the empty octet string.
  318. byte[] T = new byte [iterations * hLen];
  319. byte[] toBeHashed = new byte [mgfSeedLength + 4];
  320. int pos = 0;
  321. // 3. For counter from 0 to (maskLen / hLen) – 1, do the following:
  322. for (int counter = 0; counter < iterations; counter++) {
  323. // a. Convert counter to an octet string C of length 4 octets
  324. byte[] C = I2OSP (counter, 4);
  325. // b. Concatenate the hash of the seed mgfSeed and C to the octet string T:
  326. // T = T || Hash (mgfSeed || C)
  327. Array.Copy (mgfSeed, 0, toBeHashed, 0, mgfSeedLength);
  328. Array.Copy (C, 0, toBeHashed, mgfSeedLength, 4);
  329. byte[] output = hash.ComputeHash (toBeHashed);
  330. Array.Copy (output, 0, T, pos, hLen);
  331. pos += mgfSeedLength;
  332. }
  333. // 4. Output the leading maskLen octets of T as the octet string mask.
  334. byte[] mask = new byte [maskLen];
  335. Array.Copy (T, 0, mask, 0, maskLen);
  336. return mask;
  337. }
  338. }
  339. }