rsa_sp800_56b_test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * Copyright 2018-2021 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. /* Check that small rsa exponents are allowed in non FIPS mode */
  99. static int test_check_public_exponent(void)
  100. {
  101. int ret = 0;
  102. BIGNUM *e = NULL;
  103. ret = TEST_ptr(e = BN_new())
  104. /* e is too small will fail */
  105. && TEST_true(BN_set_word(e, 1))
  106. && TEST_false(ossl_rsa_check_public_exponent(e))
  107. /* e is even will fail */
  108. && TEST_true(BN_set_word(e, 65536))
  109. && TEST_false(ossl_rsa_check_public_exponent(e))
  110. /* e is ok */
  111. && TEST_true(BN_set_word(e, 3))
  112. && TEST_true(ossl_rsa_check_public_exponent(e))
  113. && TEST_true(BN_set_word(e, 17))
  114. && TEST_true(ossl_rsa_check_public_exponent(e))
  115. && TEST_true(BN_set_word(e, 65537))
  116. && TEST_true(ossl_rsa_check_public_exponent(e))
  117. /* e = 2^256 + 1 is ok */
  118. && TEST_true(BN_lshift(e, BN_value_one(), 256))
  119. && TEST_true(BN_add(e, e, BN_value_one()))
  120. && TEST_true(ossl_rsa_check_public_exponent(e));
  121. BN_free(e);
  122. return ret;
  123. }
  124. static int test_check_prime_factor_range(void)
  125. {
  126. int ret = 0;
  127. BN_CTX *ctx = NULL;
  128. BIGNUM *p = NULL;
  129. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL, *bn_p4 = NULL;
  130. /* Some range checks that are larger than 32 bits */
  131. static const unsigned char p1[] = { 0x0B, 0x50, 0x4F, 0x33, 0x3F };
  132. static const unsigned char p2[] = { 0x10, 0x00, 0x00, 0x00, 0x00 };
  133. static const unsigned char p3[] = { 0x0B, 0x50, 0x4F, 0x33, 0x40 };
  134. static const unsigned char p4[] = { 0x0F, 0xFF, 0xFF, 0xFF, 0xFF };
  135. /* (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
  136. * For 8 bits: 0xB.504F <= p <= 0xF
  137. * for 72 bits: 0xB504F333F. <= p <= 0xF_FFFF_FFFF
  138. */
  139. ret = TEST_ptr(p = BN_new())
  140. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  141. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  142. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  143. && TEST_ptr(bn_p4 = bn_load_new(p4, sizeof(p4)))
  144. && TEST_ptr(ctx = BN_CTX_new())
  145. && TEST_true(BN_set_word(p, 0xA))
  146. && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  147. && TEST_true(BN_set_word(p, 0x10))
  148. && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  149. && TEST_true(BN_set_word(p, 0xB))
  150. && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  151. && TEST_true(BN_set_word(p, 0xC))
  152. && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  153. && TEST_true(BN_set_word(p, 0xF))
  154. && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx))
  155. && TEST_false(ossl_rsa_check_prime_factor_range(bn_p1, 72, ctx))
  156. && TEST_false(ossl_rsa_check_prime_factor_range(bn_p2, 72, ctx))
  157. && TEST_true(ossl_rsa_check_prime_factor_range(bn_p3, 72, ctx))
  158. && TEST_true(ossl_rsa_check_prime_factor_range(bn_p4, 72, ctx));
  159. BN_free(bn_p4);
  160. BN_free(bn_p3);
  161. BN_free(bn_p2);
  162. BN_free(bn_p1);
  163. BN_free(p);
  164. BN_CTX_free(ctx);
  165. return ret;
  166. }
  167. static int test_check_prime_factor(void)
  168. {
  169. int ret = 0;
  170. BN_CTX *ctx = NULL;
  171. BIGNUM *p = NULL, *e = NULL;
  172. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
  173. /* Some range checks that are larger than 32 bits */
  174. static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 };
  175. static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 };
  176. static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 };
  177. ret = TEST_ptr(p = BN_new())
  178. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  179. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  180. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  181. && TEST_ptr(e = BN_new())
  182. && TEST_ptr(ctx = BN_CTX_new())
  183. /* Fails the prime test */
  184. && TEST_true(BN_set_word(e, 0x1))
  185. && TEST_false(ossl_rsa_check_prime_factor(bn_p1, e, 72, ctx))
  186. /* p is prime and in range and gcd(p-1, e) = 1 */
  187. && TEST_true(ossl_rsa_check_prime_factor(bn_p2, e, 72, ctx))
  188. /* gcd(p-1,e) = 1 test fails */
  189. && TEST_true(BN_set_word(e, 0x2))
  190. && TEST_false(ossl_rsa_check_prime_factor(p, e, 72, ctx))
  191. /* p fails the range check */
  192. && TEST_true(BN_set_word(e, 0x1))
  193. && TEST_false(ossl_rsa_check_prime_factor(bn_p3, e, 72, ctx));
  194. BN_free(bn_p3);
  195. BN_free(bn_p2);
  196. BN_free(bn_p1);
  197. BN_free(e);
  198. BN_free(p);
  199. BN_CTX_free(ctx);
  200. return ret;
  201. }
  202. /* This test uses legacy functions because they can take invalid numbers */
  203. static int test_check_private_exponent(void)
  204. {
  205. int ret = 0;
  206. RSA *key = NULL;
  207. BN_CTX *ctx = NULL;
  208. BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;
  209. ret = TEST_ptr(key = RSA_new())
  210. && TEST_ptr(ctx = BN_CTX_new())
  211. && TEST_ptr(p = BN_new())
  212. && TEST_ptr(q = BN_new())
  213. /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
  214. && TEST_true(BN_set_word(p, 15))
  215. && TEST_true(BN_set_word(q, 17))
  216. && TEST_true(RSA_set0_factors(key, p, q));
  217. if (!ret) {
  218. BN_free(p);
  219. BN_free(q);
  220. goto end;
  221. }
  222. ret = TEST_ptr(e = BN_new())
  223. && TEST_ptr(d = BN_new())
  224. && TEST_ptr(n = BN_new())
  225. && TEST_true(BN_set_word(e, 5))
  226. && TEST_true(BN_set_word(d, 157))
  227. && TEST_true(BN_set_word(n, 15*17))
  228. && TEST_true(RSA_set0_key(key, n, e, d));
  229. if (!ret) {
  230. BN_free(e);
  231. BN_free(d);
  232. BN_free(n);
  233. goto end;
  234. }
  235. /* fails since d >= lcm(p-1, q-1) */
  236. ret = TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx))
  237. && TEST_true(BN_set_word(d, 45))
  238. /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
  239. && TEST_true(ossl_rsa_check_private_exponent(key, 8, ctx))
  240. /* d is too small compared to nbits */
  241. && TEST_false(ossl_rsa_check_private_exponent(key, 16, ctx))
  242. /* d is too small compared to nbits */
  243. && TEST_true(BN_set_word(d, 16))
  244. && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx))
  245. /* fail if 1 != e.d mod lcm(p-1, q-1) */
  246. && TEST_true(BN_set_word(d, 46))
  247. && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx));
  248. end:
  249. RSA_free(key);
  250. BN_CTX_free(ctx);
  251. return ret;
  252. }
  253. static int test_check_crt_components(void)
  254. {
  255. const int P = 15;
  256. const int Q = 17;
  257. const int E = 5;
  258. const int N = P*Q;
  259. const int DP = 3;
  260. const int DQ = 13;
  261. const int QINV = 8;
  262. int ret = 0;
  263. RSA *key = NULL;
  264. BN_CTX *ctx = NULL;
  265. BIGNUM *p = NULL, *q = NULL, *e = NULL;
  266. ret = TEST_ptr(key = RSA_new())
  267. && TEST_ptr(ctx = BN_CTX_new())
  268. && TEST_ptr(p = BN_new())
  269. && TEST_ptr(q = BN_new())
  270. && TEST_ptr(e = BN_new())
  271. && TEST_true(BN_set_word(p, P))
  272. && TEST_true(BN_set_word(q, Q))
  273. && TEST_true(BN_set_word(e, E))
  274. && TEST_true(RSA_set0_factors(key, p, q));
  275. if (!ret) {
  276. BN_free(p);
  277. BN_free(q);
  278. goto end;
  279. }
  280. ret = TEST_true(ossl_rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
  281. && TEST_BN_eq_word(key->n, N)
  282. && TEST_BN_eq_word(key->dmp1, DP)
  283. && TEST_BN_eq_word(key->dmq1, DQ)
  284. && TEST_BN_eq_word(key->iqmp, QINV)
  285. && TEST_true(ossl_rsa_check_crt_components(key, ctx))
  286. /* (a) 1 < dP < (p – 1). */
  287. && TEST_true(BN_set_word(key->dmp1, 1))
  288. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  289. && TEST_true(BN_set_word(key->dmp1, P-1))
  290. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  291. && TEST_true(BN_set_word(key->dmp1, DP))
  292. /* (b) 1 < dQ < (q - 1). */
  293. && TEST_true(BN_set_word(key->dmq1, 1))
  294. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  295. && TEST_true(BN_set_word(key->dmq1, Q-1))
  296. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  297. && TEST_true(BN_set_word(key->dmq1, DQ))
  298. /* (c) 1 < qInv < p */
  299. && TEST_true(BN_set_word(key->iqmp, 1))
  300. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  301. && TEST_true(BN_set_word(key->iqmp, P))
  302. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  303. && TEST_true(BN_set_word(key->iqmp, QINV))
  304. /* (d) 1 = (dP . e) mod (p - 1)*/
  305. && TEST_true(BN_set_word(key->dmp1, DP+1))
  306. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  307. && TEST_true(BN_set_word(key->dmp1, DP))
  308. /* (e) 1 = (dQ . e) mod (q - 1) */
  309. && TEST_true(BN_set_word(key->dmq1, DQ-1))
  310. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  311. && TEST_true(BN_set_word(key->dmq1, DQ))
  312. /* (f) 1 = (qInv . q) mod p */
  313. && TEST_true(BN_set_word(key->iqmp, QINV+1))
  314. && TEST_false(ossl_rsa_check_crt_components(key, ctx))
  315. && TEST_true(BN_set_word(key->iqmp, QINV))
  316. /* check defaults are still valid */
  317. && TEST_true(ossl_rsa_check_crt_components(key, ctx));
  318. end:
  319. BN_free(e);
  320. RSA_free(key);
  321. BN_CTX_free(ctx);
  322. return ret;
  323. }
  324. static int test_pq_diff(void)
  325. {
  326. int ret = 0;
  327. BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
  328. ret = TEST_ptr(tmp = BN_new())
  329. && TEST_ptr(p = BN_new())
  330. && TEST_ptr(q = BN_new())
  331. /* |1-(2+1)| > 2^1 */
  332. && TEST_true(BN_set_word(p, 1))
  333. && TEST_true(BN_set_word(q, 1+2))
  334. && TEST_false(ossl_rsa_check_pminusq_diff(tmp, p, q, 202))
  335. /* Check |p - q| > 2^(nbits/2 - 100) */
  336. && TEST_true(BN_set_word(q, 1+3))
  337. && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202))
  338. && TEST_true(BN_set_word(p, 1+3))
  339. && TEST_true(BN_set_word(q, 1))
  340. && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202));
  341. BN_free(p);
  342. BN_free(q);
  343. BN_free(tmp);
  344. return ret;
  345. }
  346. static int test_invalid_keypair(void)
  347. {
  348. int ret = 0;
  349. RSA *key = NULL;
  350. BN_CTX *ctx = NULL;
  351. BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
  352. ret = TEST_ptr(key = RSA_new())
  353. && TEST_ptr(ctx = BN_CTX_new())
  354. /* NULL parameters */
  355. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  356. /* load key */
  357. && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
  358. && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
  359. && TEST_true(RSA_set0_factors(key, p, q));
  360. if (!ret) {
  361. BN_free(p);
  362. BN_free(q);
  363. goto end;
  364. }
  365. ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  366. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  367. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  368. && TEST_true(RSA_set0_key(key, n, e, d));
  369. if (!ret) {
  370. BN_free(e);
  371. BN_free(n);
  372. BN_free(d);
  373. goto end;
  374. }
  375. /* bad strength/key size */
  376. ret = TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 100, 2048))
  377. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 112, 1024))
  378. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 128, 2048))
  379. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 140, 3072))
  380. /* mismatching exponent */
  381. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, BN_value_one(),
  382. -1, 2048))
  383. /* bad exponent */
  384. && TEST_true(BN_add_word(e, 1))
  385. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  386. && TEST_true(BN_sub_word(e, 1))
  387. /* mismatch between bits and modulus */
  388. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
  389. && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, e, 112, 2048))
  390. /* check n == pq failure */
  391. && TEST_true(BN_add_word(n, 1))
  392. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  393. && TEST_true(BN_sub_word(n, 1))
  394. /* check p */
  395. && TEST_true(BN_sub_word(p, 2))
  396. && TEST_true(BN_mul(n, p, q, ctx))
  397. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  398. && TEST_true(BN_add_word(p, 2))
  399. && TEST_true(BN_mul(n, p, q, ctx))
  400. /* check q */
  401. && TEST_true(BN_sub_word(q, 2))
  402. && TEST_true(BN_mul(n, p, q, ctx))
  403. && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  404. && TEST_true(BN_add_word(q, 2))
  405. && TEST_true(BN_mul(n, p, q, ctx));
  406. end:
  407. RSA_free(key);
  408. BN_CTX_free(ctx);
  409. return ret;
  410. }
  411. static int keygen_size[] =
  412. {
  413. 2048, 3072
  414. };
  415. static int test_sp80056b_keygen(int id)
  416. {
  417. RSA *key = NULL;
  418. int ret;
  419. int sz = keygen_size[id];
  420. ret = TEST_ptr(key = RSA_new())
  421. && TEST_true(ossl_rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
  422. && TEST_true(ossl_rsa_sp800_56b_check_public(key))
  423. && TEST_true(ossl_rsa_sp800_56b_check_private(key))
  424. && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
  425. RSA_free(key);
  426. return ret;
  427. }
  428. static int test_check_private_key(void)
  429. {
  430. int ret = 0;
  431. BIGNUM *n = NULL, *d = NULL, *e = NULL;
  432. RSA *key = NULL;
  433. ret = TEST_ptr(key = RSA_new())
  434. /* check NULL pointers fail */
  435. && TEST_false(ossl_rsa_sp800_56b_check_private(key))
  436. /* load private key */
  437. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  438. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  439. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  440. && TEST_true(RSA_set0_key(key, n, e, d));
  441. if (!ret) {
  442. BN_free(n);
  443. BN_free(e);
  444. BN_free(d);
  445. goto end;
  446. }
  447. /* check d is in range */
  448. ret = TEST_true(ossl_rsa_sp800_56b_check_private(key))
  449. /* check d is too low */
  450. && TEST_true(BN_set_word(d, 0))
  451. && TEST_false(ossl_rsa_sp800_56b_check_private(key))
  452. /* check d is too high */
  453. && TEST_ptr(BN_copy(d, n))
  454. && TEST_false(ossl_rsa_sp800_56b_check_private(key));
  455. end:
  456. RSA_free(key);
  457. return ret;
  458. }
  459. static int test_check_public_key(void)
  460. {
  461. int ret = 0;
  462. BIGNUM *n = NULL, *e = NULL;
  463. RSA *key = NULL;
  464. ret = TEST_ptr(key = RSA_new())
  465. /* check NULL pointers fail */
  466. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  467. /* load public key */
  468. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  469. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  470. && TEST_true(RSA_set0_key(key, n, e, NULL));
  471. if (!ret) {
  472. BN_free(e);
  473. BN_free(n);
  474. goto end;
  475. }
  476. /* check public key is valid */
  477. ret = TEST_true(ossl_rsa_sp800_56b_check_public(key))
  478. /* check fail if n is even */
  479. && TEST_true(BN_add_word(n, 1))
  480. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  481. && TEST_true(BN_sub_word(n, 1))
  482. /* check fail if n is wrong number of bits */
  483. && TEST_true(BN_lshift1(n, n))
  484. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  485. && TEST_true(BN_rshift1(n, n))
  486. /* test odd exponent fails */
  487. && TEST_true(BN_add_word(e, 1))
  488. && TEST_false(ossl_rsa_sp800_56b_check_public(key))
  489. && TEST_true(BN_sub_word(e, 1))
  490. /* modulus fails composite check */
  491. && TEST_true(BN_add_word(n, 2))
  492. && TEST_false(ossl_rsa_sp800_56b_check_public(key));
  493. end:
  494. RSA_free(key);
  495. return ret;
  496. }
  497. int setup_tests(void)
  498. {
  499. ADD_TEST(test_check_public_exponent);
  500. ADD_TEST(test_check_prime_factor_range);
  501. ADD_TEST(test_check_prime_factor);
  502. ADD_TEST(test_check_private_exponent);
  503. ADD_TEST(test_check_crt_components);
  504. ADD_TEST(test_check_private_key);
  505. ADD_TEST(test_check_public_key);
  506. ADD_TEST(test_invalid_keypair);
  507. ADD_TEST(test_pq_diff);
  508. ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));
  509. return 1;
  510. }