crypto_rsa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <openssl/err.h>
  7. #include <openssl/rsa.h>
  8. #include "crypto_compat.h"
  9. #include "crypto_entropy.h"
  10. #include "crypto_verify_bytes.h"
  11. #include "sysendian.h"
  12. #include "warnp.h"
  13. #include "crypto_internal.h"
  14. #include "crypto.h"
  15. /**
  16. * crypto_MGF1(seed, seedlen, buf, buflen):
  17. * The MGF1 mask generation function, as specified in RFC 3447 section B.2.1,
  18. * using SHA256 as the hash function.
  19. */
  20. void
  21. crypto_MGF1(uint8_t * seed, size_t seedlen, uint8_t * buf, size_t buflen)
  22. {
  23. uint8_t hbuf[32];
  24. size_t pos;
  25. uint32_t i;
  26. uint8_t C[4];
  27. /* Sanity check for I2OSP function. */
  28. assert(((buflen - 1) / 32) <= UINT32_MAX);
  29. /* Iterate through the buffer. */
  30. for (pos = 0; pos < buflen; pos += 32) {
  31. /* The ith block starts at position i * 32. */
  32. i = (uint32_t)(pos / 32);
  33. /* Convert counter to big-endian format. */
  34. be32enc(C, i);
  35. /* Compute the hash of (seed || C). */
  36. if (crypto_hash_data_2(CRYPTO_KEY_HMAC_SHA256, seed, seedlen,
  37. C, 4, hbuf)) {
  38. warn0("Programmer error: "
  39. "SHA256 should never fail");
  40. abort();
  41. }
  42. /* Copy as much data as needed. */
  43. if (buflen - pos > 32)
  44. memcpy(buf + pos, hbuf, 32);
  45. else
  46. memcpy(buf + pos, hbuf, buflen - pos);
  47. }
  48. }
  49. /**
  50. * crypto_rsa_sign(key, data, len, sig, siglen):
  51. * Sign the provided ${data} with the specified ${key}, writing the signature
  52. * into ${sig}.
  53. */
  54. int
  55. crypto_rsa_sign(int key, const uint8_t * data, size_t len,
  56. uint8_t * sig, size_t siglen)
  57. {
  58. RSA * rsa; /* RSA key used for signing. */
  59. uint8_t mHash[32];
  60. uint8_t salt[32];
  61. uint8_t Mprime[72];
  62. uint8_t H[32];
  63. uint8_t DB[223];
  64. uint8_t dbMask[223];
  65. uint8_t maskedDB[223];
  66. uint8_t EM[256];
  67. size_t i;
  68. /* Find the required key. */
  69. if ((rsa = crypto_keys_lookup_RSA(key)) == NULL)
  70. goto err0;
  71. /* Make sure the key and signature buffer are the correct size. */
  72. if (!crypto_compat_RSA_valid_size(rsa)) {
  73. warn0("RSA key is incorrect size");
  74. goto err0;
  75. }
  76. if (siglen != 256) {
  77. warn0("Programmer error: "
  78. "signature buffer is incorrect length");
  79. goto err0;
  80. }
  81. /* Generate mHash as specified in EMSA-PSS-ENCODE from RFC 3447. */
  82. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, data, len, mHash)) {
  83. warn0("Programmer error: "
  84. "SHA256 should never fail");
  85. goto err0;
  86. }
  87. /* Generate random salt. */
  88. if (crypto_entropy_read(salt, 32)) {
  89. warnp("Could not obtain sufficient entropy");
  90. goto err0;
  91. }
  92. /* Construct M'. */
  93. memset(Mprime, 0, 8);
  94. memcpy(Mprime + 8, mHash, 32);
  95. memcpy(Mprime + 40, salt, 32);
  96. /* Construct H. */
  97. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, Mprime, 72, H)) {
  98. warn0("Programmer error: "
  99. "SHA256 should never fail");
  100. goto err0;
  101. }
  102. /* Construct DB. */
  103. memset(DB, 0, 190);
  104. memset(DB + 190, 1, 1);
  105. memcpy(DB + 191, salt, 32);
  106. /* Construct dbMask and maskedDB. */
  107. crypto_MGF1(H, 32, dbMask, 223);
  108. for (i = 0; i < 223; i++)
  109. maskedDB[i] = DB[i] ^ dbMask[i];
  110. maskedDB[0] &= 0x7f;
  111. /* Construct EM. */
  112. memcpy(EM, maskedDB, 223);
  113. memcpy(EM + 223, H, 32);
  114. memset(EM + 255, 0xbc, 1);
  115. /* Convert EM to a signature, via RSA. */
  116. if (RSA_private_encrypt(256, EM, sig, rsa, RSA_NO_PADDING) != 256) {
  117. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  118. goto err0;
  119. }
  120. /* Success! */
  121. return (0);
  122. err0:
  123. /* Failure! */
  124. return (-1);
  125. }
  126. /**
  127. * crypto_rsa_verify(key, data, len, sig, siglen):
  128. * Verify that the provided signature ${sig} matches the provided ${data}.
  129. * Return 0 if the signature is valid, 1 if the signature is invalid, or -1
  130. * on error.
  131. */
  132. int
  133. crypto_rsa_verify(int key, const uint8_t * data, size_t len,
  134. const uint8_t * sig, size_t siglen)
  135. {
  136. RSA * rsa;
  137. uint8_t EM[256];
  138. uint8_t mHash[32];
  139. uint8_t maskedDB[223];
  140. uint8_t H[32];
  141. uint8_t dbMask[223];
  142. uint8_t DB[223];
  143. uint8_t salt[32];
  144. uint8_t Mprime[72];
  145. uint8_t Hprime[32];
  146. size_t i;
  147. unsigned long rsaerr;
  148. /* Sanity check. */
  149. assert(siglen < INT_MAX);
  150. /* Find the required key. */
  151. if ((rsa = crypto_keys_lookup_RSA(key)) == NULL)
  152. goto err0;
  153. /* Make sure the key and signature buffer are the correct size. */
  154. if (!crypto_compat_RSA_valid_size(rsa)) {
  155. warn0("RSA key is incorrect size");
  156. goto err0;
  157. }
  158. if (siglen != 256) {
  159. warn0("Programmer error: "
  160. "signature buffer is incorrect length");
  161. goto err0;
  162. }
  163. /* Convert the signature to EM, via RSA. */
  164. if (RSA_public_decrypt((int)siglen, sig, EM, rsa, RSA_NO_PADDING)
  165. != 256) {
  166. /*
  167. * We can only distinguish between a bad signature and an
  168. * internal error in OpenSSL by looking at the error code.
  169. */
  170. rsaerr = ERR_get_error();
  171. if (rsaerr == RSA_R_DATA_TOO_LARGE_FOR_MODULUS)
  172. goto bad;
  173. /* Anything else is an internal error in OpenSSL. */
  174. warn0("%s", ERR_error_string(rsaerr, NULL));
  175. goto err0;
  176. }
  177. /* Generate mHash as specified in EMSA-PSS-VERIFY from RFC 3447. */
  178. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, data, len, mHash)) {
  179. warn0("Programmer error: "
  180. "SHA256 should never fail");
  181. goto err0;
  182. }
  183. /* Verify rightmost octet of EM. */
  184. if (EM[255] != 0xbc)
  185. goto bad;
  186. /* Construct maskedDB and H. */
  187. memcpy(maskedDB, EM, 223);
  188. memcpy(H, EM + 223, 32);
  189. /* Verify high bit of leftmost octet of maskedDB. */
  190. if (maskedDB[0] & 0x80)
  191. goto bad;
  192. /* Construct dbMask and DB. */
  193. crypto_MGF1(H, 32, dbMask, 223);
  194. for (i = 0; i < 223; i++)
  195. DB[i] = maskedDB[i] ^ dbMask[i];
  196. /* Set high bit of leftmost octet of DB to zero. */
  197. DB[0] &= 0x7f;
  198. /* Verify padding in DB. */
  199. for (i = 0; i < 190; i++)
  200. if (DB[i] != 0)
  201. goto bad;
  202. if (DB[190] != 1)
  203. goto bad;
  204. /* Construct salt. */
  205. memcpy(salt, DB + 191, 32);
  206. /* Construct M'. */
  207. memset(Mprime, 0, 8);
  208. memcpy(Mprime + 8, mHash, 32);
  209. memcpy(Mprime + 40, salt, 32);
  210. /* Construct H'. */
  211. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, Mprime, 72, Hprime)) {
  212. warn0("Programmer error: "
  213. "SHA256 should never fail");
  214. goto err0;
  215. }
  216. /* Verify that H' == H. */
  217. if (crypto_verify_bytes(H, Hprime, 32))
  218. goto bad;
  219. /* Success! */
  220. return (0);
  221. bad:
  222. /* Bad signature. */
  223. return (1);
  224. err0:
  225. /* Failure! */
  226. return (-1);
  227. }
  228. /**
  229. * crypto_rsa_encrypt(key, data, len, out, outlen):
  230. * Encrypt the provided ${data} with the specified ${key}, writing the
  231. * ciphertext into ${out} (of length ${outlen}).
  232. */
  233. int
  234. crypto_rsa_encrypt(int key, const uint8_t * data, size_t len,
  235. uint8_t * out, size_t outlen)
  236. {
  237. RSA * rsa;
  238. uint8_t lHash[32];
  239. uint8_t DB[223];
  240. uint8_t seed[32];
  241. uint8_t dbMask[223];
  242. uint8_t maskedDB[223];
  243. uint8_t seedMask[32];
  244. uint8_t maskedSeed[32];
  245. uint8_t EM[256];
  246. size_t i;
  247. /* Find the required key. */
  248. if ((rsa = crypto_keys_lookup_RSA(key)) == NULL)
  249. goto err0;
  250. /* Make sure the key and ciphertext buffer are the correct size. */
  251. if (!crypto_compat_RSA_valid_size(rsa)) {
  252. warn0("RSA key is incorrect size");
  253. goto err0;
  254. }
  255. if (outlen != 256) {
  256. warn0("Programmer error: "
  257. "ciphertext buffer is incorrect length");
  258. goto err0;
  259. }
  260. /* Make sure the input is not too long. */
  261. if (len > 190) {
  262. warn0("Programmer error: "
  263. "input to crypto_rsa_encrypt is too long");
  264. goto err0;
  265. }
  266. /* Construct lHash as specified in RSAES-OAEP-ENCRYPT in RFC 3447. */
  267. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, NULL, 0, lHash)) {
  268. warn0("Programmer error: "
  269. "SHA256 should never fail");
  270. goto err0;
  271. }
  272. /* Construct DB. */
  273. memcpy(DB, lHash, 32);
  274. memset(DB + 32, 0, 190 - len);
  275. memset(DB + 222 - len, 1, 1);
  276. memcpy(DB + 223 - len, data, len);
  277. /* Generate random seed. */
  278. if (crypto_entropy_read(seed, 32)) {
  279. warnp("Could not obtain sufficient entropy");
  280. goto err0;
  281. }
  282. /* Construct dbMask and maskedDB. */
  283. crypto_MGF1(seed, 32, dbMask, 223);
  284. for (i = 0; i < 223; i++)
  285. maskedDB[i] = DB[i] ^ dbMask[i];
  286. /* Construct seedMask and maskedSeed. */
  287. crypto_MGF1(maskedDB, 223, seedMask, 32);
  288. for (i = 0; i < 32; i++)
  289. maskedSeed[i] = seed[i] ^ seedMask[i];
  290. /* Construct EM. */
  291. memset(EM, 0, 1);
  292. memcpy(EM + 1, maskedSeed, 32);
  293. memcpy(EM + 33, maskedDB, 223);
  294. /* Convert EM to ciphertext, via RSA. */
  295. if (RSA_public_encrypt(256, EM, out, rsa, RSA_NO_PADDING) != 256) {
  296. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  297. goto err0;
  298. }
  299. /* Success! */
  300. return (0);
  301. err0:
  302. /* Failure! */
  303. return (-1);
  304. }
  305. /**
  306. * crypto_rsa_decrypt(key, data, len, out, outlen):
  307. * Decrypt the provided ${data} with the specified ${key}, writing the
  308. * ciphertext into ${out} (of length ${*outlen}). Set ${*outlen} to the
  309. * length of the plaintext, and return 0 on success, 1 if the ciphertext is
  310. * invalid, or 1 on error.
  311. */
  312. int
  313. crypto_rsa_decrypt(int key, const uint8_t * data, size_t len,
  314. uint8_t * out, size_t * outlen)
  315. {
  316. RSA * rsa;
  317. uint8_t EM[256];
  318. uint8_t lHash[32];
  319. uint8_t baddata, paddingmask;
  320. uint8_t maskedSeed[32];
  321. uint8_t maskedDB[223];
  322. uint8_t seedMask[32];
  323. uint8_t seed[32];
  324. uint8_t dbMask[223];
  325. uint8_t DB[223];
  326. size_t msglen;
  327. size_t i;
  328. unsigned long rsaerr;
  329. /* Sanity check. */
  330. assert(len < INT_MAX);
  331. /* Find the required key. */
  332. if ((rsa = crypto_keys_lookup_RSA(key)) == NULL)
  333. goto err0;
  334. /* Make sure the key and ciphertext buffer are the correct size. */
  335. if (!crypto_compat_RSA_valid_size(rsa)) {
  336. warn0("RSA key is incorrect size");
  337. goto err0;
  338. }
  339. if (len != 256) {
  340. warn0("Programmer error: "
  341. "ciphertext buffer is incorrect length");
  342. goto err0;
  343. }
  344. /* Make sure the plaintext buffer is large enough. */
  345. if (*outlen < 256) {
  346. warn0("Programmer error: "
  347. "plaintext buffer is too small");
  348. goto err0;
  349. }
  350. /* Convert the ciphertext to EM, via RSA. */
  351. if (RSA_private_decrypt((int)len, data, EM, rsa, RSA_NO_PADDING)
  352. != 256) {
  353. /*
  354. * We can only distinguish between bad ciphertext and an
  355. * internal error in OpenSSL by looking at the error code.
  356. */
  357. rsaerr = ERR_get_error();
  358. if (rsaerr == RSA_R_DATA_TOO_LARGE_FOR_MODULUS)
  359. goto bad;
  360. /* Anything else is an internal error in OpenSSL. */
  361. warn0("%s", ERR_error_string(rsaerr, NULL));
  362. goto err0;
  363. }
  364. /* Construct lHash as specified in RSAES-OAEP-DECRYPT in RFC 3447. */
  365. if (crypto_hash_data(CRYPTO_KEY_HMAC_SHA256, NULL, 0, lHash)) {
  366. warn0("Programmer error: "
  367. "SHA256 should never fail");
  368. goto err0;
  369. }
  370. /*
  371. * The high byte of EM must be zero. We test this later to avoid
  372. * timing side channel attacks.
  373. */
  374. baddata = EM[0];
  375. /* Construct maskedSeed and maskedDB. */
  376. memcpy(maskedSeed, EM + 1, 32);
  377. memcpy(maskedDB, EM + 33, 223);
  378. /* Construct seedMask and seed. */
  379. crypto_MGF1(maskedDB, 223, seedMask, 32);
  380. for (i = 0; i < 32; i++)
  381. seed[i] = maskedSeed[i] ^ seedMask[i];
  382. /* Construct dbMask and DB. */
  383. crypto_MGF1(seed, 32, dbMask, 223);
  384. for (i = 0; i < 223; i++)
  385. DB[i] = maskedDB[i] ^ dbMask[i];
  386. /*
  387. * The leading 32 bytes of DB must be equal to lHash. Test them all
  388. * at once, simultaneous with other tests, in order to avoid timing
  389. * side channel attacks.
  390. */
  391. baddata = baddata | crypto_verify_bytes(DB, lHash, 32);
  392. /*
  393. * Bytes 33 -- 223 of DB must be zero bytes followed by a one byte
  394. * followed by the real data. The following code will set baddata
  395. * to a non-zero value if there are non-{0, 1} bytes which are not
  396. * separated from the start by a 1 byte.
  397. */
  398. paddingmask = 0xff;
  399. msglen = 191;
  400. for (i = 32; i < 223; i++) {
  401. /* If we're still doing padding, DB[i] should be 0 or 1. */
  402. baddata = baddata | (paddingmask & DB[i] & 0xfe);
  403. /*
  404. * If baddata is still 0, paddingmask is either 0xff or 0x00
  405. * depending upon whether the current byte is padding or not.
  406. * Treating it as a signed integer and adding it to msglen
  407. * will result in msglen holding the length of the message
  408. * after the padding is removed.
  409. */
  410. msglen += (size_t)((int8_t)(paddingmask));
  411. /*-
  412. * If baddata is still 0, there are 3 cases:
  413. * 1. We're no longer looking at padding, and paddingmask is
  414. * 0x00, so &ing it with something won't change it.
  415. * 2. We're looking at a 0 byte of padding, paddingmask is
  416. * 0xff, and we want it to remain 0xff.
  417. * 3. We're looking at a 1 byte of padding, paddingmask is
  418. * 0xff, and we want it to become 0x00.
  419. * In all three cases, &ing the byte minus one does what we
  420. * want.
  421. */
  422. paddingmask = paddingmask & (DB[i] - 1);
  423. }
  424. /* Once we hit the end, the padding should be over. */
  425. baddata = baddata | paddingmask;
  426. /* Is the data bad? */
  427. if (baddata)
  428. goto bad;
  429. /* Sanity check the message length. */
  430. if (msglen > *outlen) {
  431. warn0("Programmer error: "
  432. "decrypted message length is insane");
  433. goto err0;
  434. }
  435. /* Copy the message into the output buffer. */
  436. memcpy(out, DB + 223 - msglen, msglen);
  437. *outlen = msglen;
  438. /* Success! */
  439. return (0);
  440. bad:
  441. /* Bad signature. */
  442. return (1);
  443. err0:
  444. /* Failure! */
  445. return (-1);
  446. }