rsa_sp800_56b_test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "internal/nelem.h"
  17. #include <openssl/crypto.h>
  18. #include <openssl/err.h>
  19. #include <openssl/rand.h>
  20. #include <openssl/bn.h>
  21. #include "testutil.h"
  22. #include "rsa_local.h"
  23. #include <openssl/rsa.h>
  24. /* taken from RSA2 cavs data */
  25. static const unsigned char cav_e[] = {
  26. 0x01,0x00,0x01
  27. };
  28. static const unsigned char cav_p[] = {
  29. 0xcf,0x72,0x1b,0x9a,0xfd,0x0d,0x22,0x1a,0x74,0x50,0x97,0x22,0x76,0xd8,0xc0,
  30. 0xc2,0xfd,0x08,0x81,0x05,0xdd,0x18,0x21,0x99,0x96,0xd6,0x5c,0x79,0xe3,0x02,
  31. 0x81,0xd7,0x0e,0x3f,0x3b,0x34,0xda,0x61,0xc9,0x2d,0x84,0x86,0x62,0x1e,0x3d,
  32. 0x5d,0xbf,0x92,0x2e,0xcd,0x35,0x3d,0x6e,0xb9,0x59,0x16,0xc9,0x82,0x50,0x41,
  33. 0x30,0x45,0x67,0xaa,0xb7,0xbe,0xec,0xea,0x4b,0x9e,0xa0,0xc3,0x05,0xbc,0x4c,
  34. 0x01,0xa5,0x4b,0xbd,0xa4,0x20,0xb5,0x20,0xd5,0x59,0x6f,0x82,0x5c,0x8f,0x4f,
  35. 0xe0,0x3a,0x4e,0x7e,0xfe,0x44,0xf3,0x3c,0xc0,0x0e,0x14,0x2b,0x32,0xe6,0x28,
  36. 0x8b,0x63,0x87,0x00,0xc3,0x53,0x4a,0x5b,0x71,0x7a,0x5b,0x28,0x40,0xc4,0x18,
  37. 0xb6,0x77,0x0b,0xab,0x59,0xa4,0x96,0x7d
  38. };
  39. static const unsigned char cav_q[] = {
  40. 0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb,
  41. 0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf,
  42. 0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68,
  43. 0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e,
  44. 0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xc4,0x44,0xdd,
  45. 0xf7,0x7e,0xda,0x47,0x4a,0x67,0x44,0x5d,0x4e,0x75,0xf0,0x4d,0x00,0x68,0xe1,
  46. 0x4a,0xec,0x1f,0x45,0xf9,0xe6,0xca,0x38,0x95,0x48,0x6f,0xdc,0x9d,0x1b,0xa3,
  47. 0x4b,0xfd,0x08,0x4b,0x54,0xcd,0xeb,0x3d,0xef,0x33,0x11,0x6e,0xce,0xe4,0x5d,
  48. 0xef,0xa9,0x58,0x5c,0x87,0x4d,0xc8,0xcf
  49. };
  50. static const unsigned char cav_n[] = {
  51. 0xce,0x5e,0x8d,0x1a,0xa3,0x08,0x7a,0x2d,0xb4,0x49,0x48,0xf0,0x06,0xb6,0xfe,
  52. 0xba,0x2f,0x39,0x7c,0x7b,0xe0,0x5d,0x09,0x2d,0x57,0x4e,0x54,0x60,0x9c,0xe5,
  53. 0x08,0x4b,0xe1,0x1a,0x73,0xc1,0x5e,0x2f,0xb6,0x46,0xd7,0x81,0xca,0xbc,0x98,
  54. 0xd2,0xf9,0xef,0x1c,0x92,0x8c,0x8d,0x99,0x85,0x28,0x52,0xd6,0xd5,0xab,0x70,
  55. 0x7e,0x9e,0xa9,0x87,0x82,0xc8,0x95,0x64,0xeb,0xf0,0x6c,0x0f,0x3f,0xe9,0x02,
  56. 0x29,0x2e,0x6d,0xa1,0xec,0xbf,0xdc,0x23,0xdf,0x82,0x4f,0xab,0x39,0x8d,0xcc,
  57. 0xac,0x21,0x51,0x14,0xf8,0xef,0xec,0x73,0x80,0x86,0xa3,0xcf,0x8f,0xd5,0xcf,
  58. 0x22,0x1f,0xcc,0x23,0x2f,0xba,0xcb,0xf6,0x17,0xcd,0x3a,0x1f,0xd9,0x84,0xb9,
  59. 0x88,0xa7,0x78,0x0f,0xaa,0xc9,0x04,0x01,0x20,0x72,0x5d,0x2a,0xfe,0x5b,0xdd,
  60. 0x16,0x5a,0xed,0x83,0x02,0x96,0x39,0x46,0x37,0x30,0xc1,0x0d,0x87,0xc2,0xc8,
  61. 0x33,0x38,0xed,0x35,0x72,0xe5,0x29,0xf8,0x1f,0x23,0x60,0xe1,0x2a,0x5b,0x1d,
  62. 0x6b,0x53,0x3f,0x07,0xc4,0xd9,0xbb,0x04,0x0c,0x5c,0x3f,0x0b,0xc4,0xd4,0x61,
  63. 0x96,0x94,0xf1,0x0f,0x4a,0x49,0xac,0xde,0xd2,0xe8,0x42,0xb3,0x4a,0x0b,0x64,
  64. 0x7a,0x32,0x5f,0x2b,0x5b,0x0f,0x8b,0x8b,0xe0,0x33,0x23,0x34,0x64,0xf8,0xb5,
  65. 0x7f,0x69,0x60,0xb8,0x71,0xe9,0xff,0x92,0x42,0xb1,0xf7,0x23,0xa8,0xa7,0x92,
  66. 0x04,0x3d,0x6b,0xff,0xf7,0xab,0xbb,0x14,0x1f,0x4c,0x10,0x97,0xd5,0x6b,0x71,
  67. 0x12,0xfd,0x93,0xa0,0x4a,0x3b,0x75,0x72,0x40,0x96,0x1c,0x5f,0x40,0x40,0x57,
  68. 0x13
  69. };
  70. static const unsigned char cav_d[] = {
  71. 0x47,0x47,0x49,0x1d,0x66,0x2a,0x4b,0x68,0xf5,0xd8,0x4a,0x24,0xfd,0x6c,0xbf,
  72. 0x56,0xb7,0x70,0xf7,0x9a,0x21,0xc8,0x80,0x9e,0xf4,0x84,0xcd,0x88,0x01,0x28,
  73. 0xea,0x50,0xab,0x13,0x63,0xdf,0xea,0x14,0x38,0xb5,0x07,0x42,0x81,0x2f,0xda,
  74. 0xe9,0x24,0x02,0x7e,0xaf,0xef,0x74,0x09,0x0e,0x80,0xfa,0xfb,0xd1,0x19,0x41,
  75. 0xe5,0xba,0x0f,0x7c,0x0a,0xa4,0x15,0x55,0xa2,0x58,0x8c,0x3a,0x48,0x2c,0xc6,
  76. 0xde,0x4a,0x76,0xfb,0x72,0xb6,0x61,0xe6,0xd2,0x10,0x44,0x4c,0x33,0xb8,0xd2,
  77. 0x74,0xb1,0x9d,0x3b,0xcd,0x2f,0xb1,0x4f,0xc3,0x98,0xbd,0x83,0xb7,0x7e,0x75,
  78. 0xe8,0xa7,0x6a,0xee,0xcc,0x51,0x8c,0x99,0x17,0x67,0x7f,0x27,0xf9,0x0d,0x6a,
  79. 0xb7,0xd4,0x80,0x17,0x89,0x39,0x9c,0xf3,0xd7,0x0f,0xdf,0xb0,0x55,0x80,0x1d,
  80. 0xaf,0x57,0x2e,0xd0,0xf0,0x4f,0x42,0x69,0x55,0xbc,0x83,0xd6,0x97,0x83,0x7a,
  81. 0xe6,0xc6,0x30,0x6d,0x3d,0xb5,0x21,0xa7,0xc4,0x62,0x0a,0x20,0xce,0x5e,0x5a,
  82. 0x17,0x98,0xb3,0x6f,0x6b,0x9a,0xeb,0x6b,0xa3,0xc4,0x75,0xd8,0x2b,0xdc,0x5c,
  83. 0x6f,0xec,0x5d,0x49,0xac,0xa8,0xa4,0x2f,0xb8,0x8c,0x4f,0x2e,0x46,0x21,0xee,
  84. 0x72,0x6a,0x0e,0x22,0x80,0x71,0xc8,0x76,0x40,0x44,0x61,0x16,0xbf,0xa5,0xf8,
  85. 0x89,0xc7,0xe9,0x87,0xdf,0xbd,0x2e,0x4b,0x4e,0xc2,0x97,0x53,0xe9,0x49,0x1c,
  86. 0x05,0xb0,0x0b,0x9b,0x9f,0x21,0x19,0x41,0xe9,0xf5,0x61,0xd7,0x33,0x2e,0x2c,
  87. 0x94,0xb8,0xa8,0x9a,0x3a,0xcc,0x6a,0x24,0x8d,0x19,0x13,0xee,0xb9,0xb0,0x48,
  88. 0x61
  89. };
  90. /* helper function */
  91. static BIGNUM *bn_load_new(const unsigned char *data, int sz)
  92. {
  93. BIGNUM *ret = BN_new();
  94. if (ret != NULL)
  95. BN_bin2bn(data, sz, ret);
  96. return ret;
  97. }
  98. static int test_check_public_exponent(void)
  99. {
  100. int ret = 0;
  101. BIGNUM *e = NULL;
  102. ret = TEST_ptr(e = BN_new())
  103. /* e is too small */
  104. && TEST_true(BN_set_word(e, 65535))
  105. && TEST_false(ossl_rsa_check_public_exponent(e))
  106. /* e is even will fail */
  107. && TEST_true(BN_set_word(e, 65536))
  108. && TEST_false(ossl_rsa_check_public_exponent(e))
  109. /* e is ok */
  110. && TEST_true(BN_set_word(e, 65537))
  111. && TEST_true(ossl_rsa_check_public_exponent(e))
  112. /* e = 2^256 is too big */
  113. && TEST_true(BN_lshift(e, BN_value_one(), 256))
  114. && TEST_false(ossl_rsa_check_public_exponent(e))
  115. /* e = 2^256-1 is odd and in range */
  116. && TEST_true(BN_sub(e, e, BN_value_one()))
  117. && TEST_true(ossl_rsa_check_public_exponent(e));
  118. BN_free(e);
  119. return ret;
  120. }
  121. static int test_check_prime_factor_range(void)
  122. {
  123. int ret = 0;
  124. BN_CTX *ctx = NULL;
  125. BIGNUM *p = NULL;
  126. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL, *bn_p4 = NULL;
  127. /* Some range checks that are larger than 32 bits */
  128. static const unsigned char p1[] = { 0x0B, 0x50, 0x4F, 0x33, 0x3F };
  129. static const unsigned char p2[] = { 0x10, 0x00, 0x00, 0x00, 0x00 };
  130. static const unsigned char p3[] = { 0x0B, 0x50, 0x4F, 0x33, 0x40 };
  131. static const unsigned char p4[] = { 0x0F, 0xFF, 0xFF, 0xFF, 0xFF };
  132. /* (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
  133. * For 8 bits: 0xB.504F <= p <= 0xF
  134. * for 72 bits: 0xB504F333F. <= p <= 0xF_FFFF_FFFF
  135. */
  136. ret = TEST_ptr(p = BN_new())
  137. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  138. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  139. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  140. && TEST_ptr(bn_p4 = bn_load_new(p4, sizeof(p4)))
  141. && TEST_ptr(ctx = BN_CTX_new())
  142. && TEST_true(BN_set_word(p, 0xA))
  143. && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  144. && TEST_true(BN_set_word(p, 0x10))
  145. && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  146. && TEST_true(BN_set_word(p, 0xB))
  147. && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  148. && TEST_true(BN_set_word(p, 0xC))
  149. && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  150. && TEST_true(BN_set_word(p, 0xF))
  151. && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  152. && TEST_false(ossl_rsa_check_prime_factor_range(bn_p1, 72, ctx))
  153. && TEST_false(ossl_rsa_check_prime_factor_range(bn_p2, 72, ctx))
  154. && TEST_true(ossl_rsa_check_prime_factor_range(bn_p3, 72, ctx))
  155. && TEST_true(ossl_rsa_check_prime_factor_range(bn_p4, 72, ctx));
  156. BN_free(bn_p4);
  157. BN_free(bn_p3);
  158. BN_free(bn_p2);
  159. BN_free(bn_p1);
  160. BN_free(p);
  161. BN_CTX_free(ctx);
  162. return ret;
  163. }
  164. static int test_check_prime_factor(void)
  165. {
  166. int ret = 0;
  167. BN_CTX *ctx = NULL;
  168. BIGNUM *p = NULL, *e = NULL;
  169. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
  170. /* Some range checks that are larger than 32 bits */
  171. static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 };
  172. static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 };
  173. static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 };
  174. ret = TEST_ptr(p = BN_new())
  175. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  176. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  177. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  178. && TEST_ptr(e = BN_new())
  179. && TEST_ptr(ctx = BN_CTX_new())
  180. /* Fails the prime test */
  181. && TEST_true(BN_set_word(e, 0x1))
  182. && TEST_false(ossl_rsa_check_prime_factor(bn_p1, e, 72, ctx))
  183. /* p is prime and in range and gcd(p-1, e) = 1 */
  184. && TEST_true(ossl_rsa_check_prime_factor(bn_p2, e, 72, ctx))
  185. /* gcd(p-1,e) = 1 test fails */
  186. && TEST_true(BN_set_word(e, 0x2))
  187. && TEST_false(ossl_rsa_check_prime_factor(p, e, 72, ctx))
  188. /* p fails the range check */
  189. && TEST_true(BN_set_word(e, 0x1))
  190. && TEST_false(ossl_rsa_check_prime_factor(bn_p3, e, 72, ctx));
  191. BN_free(bn_p3);
  192. BN_free(bn_p2);
  193. BN_free(bn_p1);
  194. BN_free(e);
  195. BN_free(p);
  196. BN_CTX_free(ctx);
  197. return ret;
  198. }
  199. /* This test uses legacy functions because they can take invalid numbers */
  200. static int test_check_private_exponent(void)
  201. {
  202. int ret = 0;
  203. RSA *key = NULL;
  204. BN_CTX *ctx = NULL;
  205. BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;
  206. ret = TEST_ptr(key = RSA_new())
  207. && TEST_ptr(ctx = BN_CTX_new())
  208. && TEST_ptr(p = BN_new())
  209. && TEST_ptr(q = BN_new())
  210. /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
  211. && TEST_true(BN_set_word(p, 15))
  212. && TEST_true(BN_set_word(q, 17))
  213. && TEST_true(RSA_set0_factors(key, p, q));
  214. if (!ret) {
  215. BN_free(p);
  216. BN_free(q);
  217. goto end;
  218. }
  219. ret = TEST_ptr(e = BN_new())
  220. && TEST_ptr(d = BN_new())
  221. && TEST_ptr(n = BN_new())
  222. && TEST_true(BN_set_word(e, 5))
  223. && TEST_true(BN_set_word(d, 157))
  224. && TEST_true(BN_set_word(n, 15*17))
  225. && TEST_true(RSA_set0_key(key, n, e, d));
  226. if (!ret) {
  227. BN_free(e);
  228. BN_free(d);
  229. BN_free(n);
  230. goto end;
  231. }
  232. /* fails since d >= lcm(p-1, q-1) */
  233. ret = TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx))
  234. && TEST_true(BN_set_word(d, 45))
  235. /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
  236. && TEST_true(ossl_rsa_check_private_exponent(key, 8, ctx))
  237. /* d is too small compared to nbits */
  238. && TEST_false(ossl_rsa_check_private_exponent(key, 16, ctx))
  239. /* d is too small compared to nbits */
  240. && TEST_true(BN_set_word(d, 16))
  241. && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx))
  242. /* fail if 1 != e.d mod lcm(p-1, q-1) */
  243. && TEST_true(BN_set_word(d, 46))
  244. && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx));
  245. end:
  246. RSA_free(key);
  247. BN_CTX_free(ctx);
  248. return ret;
  249. }
  250. static int test_check_crt_components(void)
  251. {
  252. const int P = 15;
  253. const int Q = 17;
  254. const int E = 5;
  255. const int N = P*Q;
  256. const int DP = 3;
  257. const int DQ = 13;
  258. const int QINV = 8;
  259. int ret = 0;
  260. RSA *key = NULL;
  261. BN_CTX *ctx = NULL;
  262. BIGNUM *p = NULL, *q = NULL, *e = NULL;
  263. ret = TEST_ptr(key = RSA_new())
  264. && TEST_ptr(ctx = BN_CTX_new())
  265. && TEST_ptr(p = BN_new())
  266. && TEST_ptr(q = BN_new())
  267. && TEST_ptr(e = BN_new())
  268. && TEST_true(BN_set_word(p, P))
  269. && TEST_true(BN_set_word(q, Q))
  270. && TEST_true(BN_set_word(e, E))
  271. && TEST_true(RSA_set0_factors(key, p, q));
  272. if (!ret) {
  273. BN_free(p);
  274. BN_free(q);
  275. goto end;
  276. }
  277. ret = TEST_true(ossl_rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
  278. && TEST_BN_eq_word(key->n, N)
  279. && TEST_BN_eq_word(key->dmp1, DP)
  280. && TEST_BN_eq_word(key->dmq1, DQ)
  281. && TEST_BN_eq_word(key->iqmp, QINV)
  282. && TEST_true(ossl_rsa_check_crt_components(key, ctx))
  283. /* (a) 1 < dP < (p – 1). */
  284. && TEST_true(BN_set_word(key->dmp1, 1))
  285. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  286. && TEST_true(BN_set_word(key->dmp1, P-1))
  287. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  288. && TEST_true(BN_set_word(key->dmp1, DP))
  289. /* (b) 1 < dQ < (q - 1). */
  290. && TEST_true(BN_set_word(key->dmq1, 1))
  291. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  292. && TEST_true(BN_set_word(key->dmq1, Q-1))
  293. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  294. && TEST_true(BN_set_word(key->dmq1, DQ))
  295. /* (c) 1 < qInv < p */
  296. && TEST_true(BN_set_word(key->iqmp, 1))
  297. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  298. && TEST_true(BN_set_word(key->iqmp, P))
  299. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  300. && TEST_true(BN_set_word(key->iqmp, QINV))
  301. /* (d) 1 = (dP . e) mod (p - 1)*/
  302. && TEST_true(BN_set_word(key->dmp1, DP+1))
  303. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  304. && TEST_true(BN_set_word(key->dmp1, DP))
  305. /* (e) 1 = (dQ . e) mod (q - 1) */
  306. && TEST_true(BN_set_word(key->dmq1, DQ-1))
  307. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  308. && TEST_true(BN_set_word(key->dmq1, DQ))
  309. /* (f) 1 = (qInv . q) mod p */
  310. && TEST_true(BN_set_word(key->iqmp, QINV+1))
  311. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  312. && TEST_true(BN_set_word(key->iqmp, QINV))
  313. /* check defaults are still valid */
  314. && TEST_true(ossl_rsa_check_crt_components(key, ctx));
  315. end:
  316. BN_free(e);
  317. RSA_free(key);
  318. BN_CTX_free(ctx);
  319. return ret;
  320. }
  321. static int test_pq_diff(void)
  322. {
  323. int ret = 0;
  324. BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
  325. ret = TEST_ptr(tmp = BN_new())
  326. && TEST_ptr(p = BN_new())
  327. && TEST_ptr(q = BN_new())
  328. /* |1-(2+1)| > 2^1 */
  329. && TEST_true(BN_set_word(p, 1))
  330. && TEST_true(BN_set_word(q, 1+2))
  331. && TEST_false(ossl_rsa_check_pminusq_diff(tmp, p, q, 202))
  332. /* Check |p - q| > 2^(nbits/2 - 100) */
  333. && TEST_true(BN_set_word(q, 1+3))
  334. && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202))
  335. && TEST_true(BN_set_word(p, 1+3))
  336. && TEST_true(BN_set_word(q, 1))
  337. && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202));
  338. BN_free(p);
  339. BN_free(q);
  340. BN_free(tmp);
  341. return ret;
  342. }
  343. static int test_invalid_keypair(void)
  344. {
  345. int ret = 0;
  346. RSA *key = NULL;
  347. BN_CTX *ctx = NULL;
  348. BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
  349. ret = TEST_ptr(key = RSA_new())
  350. && TEST_ptr(ctx = BN_CTX_new())
  351. /* NULL parameters */
  352. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  353. /* load key */
  354. && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
  355. && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
  356. && TEST_true(RSA_set0_factors(key, p, q));
  357. if (!ret) {
  358. BN_free(p);
  359. BN_free(q);
  360. goto end;
  361. }
  362. ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  363. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  364. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  365. && TEST_true(RSA_set0_key(key, n, e, d));
  366. if (!ret) {
  367. BN_free(e);
  368. BN_free(n);
  369. BN_free(d);
  370. goto end;
  371. }
  372. /* bad strength/key size */
  373. ret = TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 100, 2048))
  374. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 112, 1024))
  375. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 128, 2048))
  376. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 140, 3072))
  377. /* mismatching exponent */
  378. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, BN_value_one(),
  379. -1, 2048))
  380. /* bad exponent */
  381. && TEST_true(BN_add_word(e, 1))
  382. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  383. && TEST_true(BN_sub_word(e, 1))
  384. /* mismatch between bits and modulus */
  385. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
  386. && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, e, 112, 2048))
  387. /* check n == pq failure */
  388. && TEST_true(BN_add_word(n, 1))
  389. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  390. && TEST_true(BN_sub_word(n, 1))
  391. /* check p */
  392. && TEST_true(BN_sub_word(p, 2))
  393. && TEST_true(BN_mul(n, p, q, ctx))
  394. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  395. && TEST_true(BN_add_word(p, 2))
  396. && TEST_true(BN_mul(n, p, q, ctx))
  397. /* check q */
  398. && TEST_true(BN_sub_word(q, 2))
  399. && TEST_true(BN_mul(n, p, q, ctx))
  400. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  401. && TEST_true(BN_add_word(q, 2))
  402. && TEST_true(BN_mul(n, p, q, ctx));
  403. end:
  404. RSA_free(key);
  405. BN_CTX_free(ctx);
  406. return ret;
  407. }
  408. static int keygen_size[] =
  409. {
  410. 2048, 3072
  411. };
  412. static int test_sp80056b_keygen(int id)
  413. {
  414. RSA *key = NULL;
  415. int ret;
  416. int sz = keygen_size[id];
  417. ret = TEST_ptr(key = RSA_new())
  418. && TEST_true(ossl_rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
  419. && TEST_true(ossl_rsa_sp800_56b_check_public(key))
  420. && TEST_true(ossl_rsa_sp800_56b_check_private(key))
  421. && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
  422. RSA_free(key);
  423. return ret;
  424. }
  425. static int test_check_private_key(void)
  426. {
  427. int ret = 0;
  428. BIGNUM *n = NULL, *d = NULL, *e = NULL;
  429. RSA *key = NULL;
  430. ret = TEST_ptr(key = RSA_new())
  431. /* check NULL pointers fail */
  432. && TEST_false(ossl_rsa_sp800_56b_check_private(key))
  433. /* load private key */
  434. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  435. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  436. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  437. && TEST_true(RSA_set0_key(key, n, e, d));
  438. if (!ret) {
  439. BN_free(n);
  440. BN_free(e);
  441. BN_free(d);
  442. goto end;
  443. }
  444. /* check d is in range */
  445. ret = TEST_true(ossl_rsa_sp800_56b_check_private(key))
  446. /* check d is too low */
  447. && TEST_true(BN_set_word(d, 0))
  448. && TEST_false(ossl_rsa_sp800_56b_check_private(key))
  449. /* check d is too high */
  450. && TEST_ptr(BN_copy(d, n))
  451. && TEST_false(ossl_rsa_sp800_56b_check_private(key));
  452. end:
  453. RSA_free(key);
  454. return ret;
  455. }
  456. static int test_check_public_key(void)
  457. {
  458. int ret = 0;
  459. BIGNUM *n = NULL, *e = NULL;
  460. RSA *key = NULL;
  461. ret = TEST_ptr(key = RSA_new())
  462. /* check NULL pointers fail */
  463. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  464. /* load public key */
  465. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  466. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  467. && TEST_true(RSA_set0_key(key, n, e, NULL));
  468. if (!ret) {
  469. BN_free(e);
  470. BN_free(n);
  471. goto end;
  472. }
  473. /* check public key is valid */
  474. ret = TEST_true(ossl_rsa_sp800_56b_check_public(key))
  475. /* check fail if n is even */
  476. && TEST_true(BN_add_word(n, 1))
  477. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  478. && TEST_true(BN_sub_word(n, 1))
  479. /* check fail if n is wrong number of bits */
  480. && TEST_true(BN_lshift1(n, n))
  481. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  482. && TEST_true(BN_rshift1(n, n))
  483. /* test odd exponent fails */
  484. && TEST_true(BN_add_word(e, 1))
  485. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  486. && TEST_true(BN_sub_word(e, 1))
  487. /* modulus fails composite check */
  488. && TEST_true(BN_add_word(n, 2))
  489. && TEST_false(ossl_rsa_sp800_56b_check_public(key));
  490. end:
  491. RSA_free(key);
  492. return ret;
  493. }
  494. int setup_tests(void)
  495. {
  496. ADD_TEST(test_check_public_exponent);
  497. ADD_TEST(test_check_prime_factor_range);
  498. ADD_TEST(test_check_prime_factor);
  499. ADD_TEST(test_check_private_exponent);
  500. ADD_TEST(test_check_crt_components);
  501. ADD_TEST(test_check_private_key);
  502. ADD_TEST(test_check_public_key);
  503. ADD_TEST(test_invalid_keypair);
  504. ADD_TEST(test_pq_diff);
  505. ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));
  506. return 1;
  507. }