rsa_sp800_56b_test.c 21 KB

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