rsa_sp800_56b_test.c 21 KB

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