rsa_sp800_56b_test.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_Xp[] = {
  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,0xb3,0x88,
  36. 0xd4,0x4c,0xac,0xeb,0xe4,0x03,0xc6,0xca,0xcb,0xd9,0xd3,0x4e,0xf6,0x7f,0x2c,
  37. 0x27,0x1e,0x08,0x6c,0xc2,0xd6,0x45,0x1f,0x84,0xe4,0x3c,0x97,0x19,0xde,0xb8,
  38. 0x55,0xaf,0x0e,0xcf,0x9e,0xb0,0x9c,0x20,0xd3,0x1f,0xa8,0xd7,0x52,0xc2,0x95,
  39. 0x1c,0x80,0x15,0x42,0x4d,0x4f,0x19,0x16
  40. };
  41. static const unsigned char cav_Xp1[] = {
  42. 0xac,0x5f,0x7f,0x6e,0x33,0x3e,0x97,0x3a,0xb3,0x17,0x44,0xa9,0x0f,0x7a,0x54,
  43. 0x70,0x27,0x06,0x93,0xd5,0x49,0xde,0x91,0x83,0xbc,0x8a,0x7b,0x95
  44. };
  45. static const unsigned char cav_Xp2[] = {
  46. 0x0b,0xf6,0xe8,0x79,0x5a,0x81,0xae,0x90,0x1d,0xa4,0x38,0x74,0x9c,0x0e,0x6f,
  47. 0xe0,0x03,0xcf,0xc4,0x53,0x16,0x32,0x17,0xf7,0x09,0x5f,0xd9
  48. };
  49. static const unsigned char cav_Xq[] = {
  50. 0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb,
  51. 0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf,
  52. 0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68,
  53. 0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e,
  54. 0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xbd,0x9a,0x89,
  55. 0x99,0x6e,0x57,0x5e,0xd0,0x39,0x86,0xc3,0xa3,0x1b,0xc7,0xcf,0xc4,0x4f,0x47,
  56. 0x25,0x9e,0x2c,0x79,0xe1,0x2c,0xcc,0xe4,0x63,0xf4,0x02,0x84,0xf8,0xf6,0xa1,
  57. 0x5c,0x93,0x14,0xf2,0x68,0x5f,0x3a,0x90,0x2f,0x4e,0x5e,0xf9,0x16,0x05,0xcf,
  58. 0x21,0x63,0xca,0xfa,0xb0,0x08,0x02,0xc0
  59. };
  60. static const unsigned char cav_Xq1[] = {
  61. 0x9b,0x02,0xd4,0xba,0xf0,0xaa,0x14,0x99,0x6d,0xc0,0xb7,0xa5,0xe1,0xd3,0x70,
  62. 0xb6,0x5a,0xa2,0x9b,0x59,0xd5,0x8c,0x1e,0x9f,0x3f,0x9a,0xde,0xeb,0x9e,0x9c,
  63. 0x61,0xd6,0x5a,0xe1
  64. };
  65. static const unsigned char cav_Xq2[] = {
  66. 0x06,0x81,0x53,0xfd,0xa8,0x7b,0xa3,0x85,0x90,0x15,0x2c,0x97,0xb2,0xa0,0x17,
  67. 0x48,0xb0,0x7f,0x0a,0x01,0x6d
  68. };
  69. /* expected values */
  70. static const unsigned char cav_p1[] = {
  71. 0xac,0x5f,0x7f,0x6e,0x33,0x3e,0x97,0x3a,0xb3,0x17,0x44,0xa9,0x0f,0x7a,0x54,
  72. 0x70,0x27,0x06,0x93,0xd5,0x49,0xde,0x91,0x83,0xbc,0x8a,0x7b,0xc3
  73. };
  74. static const unsigned char cav_p2[] = {
  75. 0x0b,0xf6,0xe8,0x79,0x5a,0x81,0xae,0x90,0x1d,0xa4,0x38,0x74,0x9c,0x0e,0x6f,
  76. 0xe0,0x03,0xcf,0xc4,0x53,0x16,0x32,0x17,0xf7,0x09,0x5f,0xd9
  77. };
  78. static const unsigned char cav_q1[] = {
  79. 0x9b,0x02,0xd4,0xba,0xf0,0xaa,0x14,0x99,0x6d,0xc0,0xb7,0xa5,0xe1,0xd3,0x70,
  80. 0xb6,0x5a,0xa2,0x9b,0x59,0xd5,0x8c,0x1e,0x9f,0x3f,0x9a,0xde,0xeb,0x9e,0x9c,
  81. 0x61,0xd6,0x5d,0x47
  82. };
  83. static const unsigned char cav_q2[] = {
  84. 0x06,0x81,0x53,0xfd,0xa8,0x7b,0xa3,0x85,0x90,0x15,0x2c,0x97,0xb2,0xa0,0x17,
  85. 0x48,0xb0,0x7f,0x0a,0x01,0x8f
  86. };
  87. static const unsigned char cav_p[] = {
  88. 0xcf,0x72,0x1b,0x9a,0xfd,0x0d,0x22,0x1a,0x74,0x50,0x97,0x22,0x76,0xd8,0xc0,
  89. 0xc2,0xfd,0x08,0x81,0x05,0xdd,0x18,0x21,0x99,0x96,0xd6,0x5c,0x79,0xe3,0x02,
  90. 0x81,0xd7,0x0e,0x3f,0x3b,0x34,0xda,0x61,0xc9,0x2d,0x84,0x86,0x62,0x1e,0x3d,
  91. 0x5d,0xbf,0x92,0x2e,0xcd,0x35,0x3d,0x6e,0xb9,0x59,0x16,0xc9,0x82,0x50,0x41,
  92. 0x30,0x45,0x67,0xaa,0xb7,0xbe,0xec,0xea,0x4b,0x9e,0xa0,0xc3,0x05,0xbc,0x4c,
  93. 0x01,0xa5,0x4b,0xbd,0xa4,0x20,0xb5,0x20,0xd5,0x59,0x6f,0x82,0x5c,0x8f,0x4f,
  94. 0xe0,0x3a,0x4e,0x7e,0xfe,0x44,0xf3,0x3c,0xc0,0x0e,0x14,0x2b,0x32,0xe6,0x28,
  95. 0x8b,0x63,0x87,0x00,0xc3,0x53,0x4a,0x5b,0x71,0x7a,0x5b,0x28,0x40,0xc4,0x18,
  96. 0xb6,0x77,0x0b,0xab,0x59,0xa4,0x96,0x7d
  97. };
  98. static const unsigned char cav_q[] = {
  99. 0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb,
  100. 0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf,
  101. 0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68,
  102. 0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e,
  103. 0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xc4,0x44,0xdd,
  104. 0xf7,0x7e,0xda,0x47,0x4a,0x67,0x44,0x5d,0x4e,0x75,0xf0,0x4d,0x00,0x68,0xe1,
  105. 0x4a,0xec,0x1f,0x45,0xf9,0xe6,0xca,0x38,0x95,0x48,0x6f,0xdc,0x9d,0x1b,0xa3,
  106. 0x4b,0xfd,0x08,0x4b,0x54,0xcd,0xeb,0x3d,0xef,0x33,0x11,0x6e,0xce,0xe4,0x5d,
  107. 0xef,0xa9,0x58,0x5c,0x87,0x4d,0xc8,0xcf
  108. };
  109. static const unsigned char cav_n[] = {
  110. 0xce,0x5e,0x8d,0x1a,0xa3,0x08,0x7a,0x2d,0xb4,0x49,0x48,0xf0,0x06,0xb6,0xfe,
  111. 0xba,0x2f,0x39,0x7c,0x7b,0xe0,0x5d,0x09,0x2d,0x57,0x4e,0x54,0x60,0x9c,0xe5,
  112. 0x08,0x4b,0xe1,0x1a,0x73,0xc1,0x5e,0x2f,0xb6,0x46,0xd7,0x81,0xca,0xbc,0x98,
  113. 0xd2,0xf9,0xef,0x1c,0x92,0x8c,0x8d,0x99,0x85,0x28,0x52,0xd6,0xd5,0xab,0x70,
  114. 0x7e,0x9e,0xa9,0x87,0x82,0xc8,0x95,0x64,0xeb,0xf0,0x6c,0x0f,0x3f,0xe9,0x02,
  115. 0x29,0x2e,0x6d,0xa1,0xec,0xbf,0xdc,0x23,0xdf,0x82,0x4f,0xab,0x39,0x8d,0xcc,
  116. 0xac,0x21,0x51,0x14,0xf8,0xef,0xec,0x73,0x80,0x86,0xa3,0xcf,0x8f,0xd5,0xcf,
  117. 0x22,0x1f,0xcc,0x23,0x2f,0xba,0xcb,0xf6,0x17,0xcd,0x3a,0x1f,0xd9,0x84,0xb9,
  118. 0x88,0xa7,0x78,0x0f,0xaa,0xc9,0x04,0x01,0x20,0x72,0x5d,0x2a,0xfe,0x5b,0xdd,
  119. 0x16,0x5a,0xed,0x83,0x02,0x96,0x39,0x46,0x37,0x30,0xc1,0x0d,0x87,0xc2,0xc8,
  120. 0x33,0x38,0xed,0x35,0x72,0xe5,0x29,0xf8,0x1f,0x23,0x60,0xe1,0x2a,0x5b,0x1d,
  121. 0x6b,0x53,0x3f,0x07,0xc4,0xd9,0xbb,0x04,0x0c,0x5c,0x3f,0x0b,0xc4,0xd4,0x61,
  122. 0x96,0x94,0xf1,0x0f,0x4a,0x49,0xac,0xde,0xd2,0xe8,0x42,0xb3,0x4a,0x0b,0x64,
  123. 0x7a,0x32,0x5f,0x2b,0x5b,0x0f,0x8b,0x8b,0xe0,0x33,0x23,0x34,0x64,0xf8,0xb5,
  124. 0x7f,0x69,0x60,0xb8,0x71,0xe9,0xff,0x92,0x42,0xb1,0xf7,0x23,0xa8,0xa7,0x92,
  125. 0x04,0x3d,0x6b,0xff,0xf7,0xab,0xbb,0x14,0x1f,0x4c,0x10,0x97,0xd5,0x6b,0x71,
  126. 0x12,0xfd,0x93,0xa0,0x4a,0x3b,0x75,0x72,0x40,0x96,0x1c,0x5f,0x40,0x40,0x57,
  127. 0x13
  128. };
  129. static const unsigned char cav_d[] = {
  130. 0x47,0x47,0x49,0x1d,0x66,0x2a,0x4b,0x68,0xf5,0xd8,0x4a,0x24,0xfd,0x6c,0xbf,
  131. 0x56,0xb7,0x70,0xf7,0x9a,0x21,0xc8,0x80,0x9e,0xf4,0x84,0xcd,0x88,0x01,0x28,
  132. 0xea,0x50,0xab,0x13,0x63,0xdf,0xea,0x14,0x38,0xb5,0x07,0x42,0x81,0x2f,0xda,
  133. 0xe9,0x24,0x02,0x7e,0xaf,0xef,0x74,0x09,0x0e,0x80,0xfa,0xfb,0xd1,0x19,0x41,
  134. 0xe5,0xba,0x0f,0x7c,0x0a,0xa4,0x15,0x55,0xa2,0x58,0x8c,0x3a,0x48,0x2c,0xc6,
  135. 0xde,0x4a,0x76,0xfb,0x72,0xb6,0x61,0xe6,0xd2,0x10,0x44,0x4c,0x33,0xb8,0xd2,
  136. 0x74,0xb1,0x9d,0x3b,0xcd,0x2f,0xb1,0x4f,0xc3,0x98,0xbd,0x83,0xb7,0x7e,0x75,
  137. 0xe8,0xa7,0x6a,0xee,0xcc,0x51,0x8c,0x99,0x17,0x67,0x7f,0x27,0xf9,0x0d,0x6a,
  138. 0xb7,0xd4,0x80,0x17,0x89,0x39,0x9c,0xf3,0xd7,0x0f,0xdf,0xb0,0x55,0x80,0x1d,
  139. 0xaf,0x57,0x2e,0xd0,0xf0,0x4f,0x42,0x69,0x55,0xbc,0x83,0xd6,0x97,0x83,0x7a,
  140. 0xe6,0xc6,0x30,0x6d,0x3d,0xb5,0x21,0xa7,0xc4,0x62,0x0a,0x20,0xce,0x5e,0x5a,
  141. 0x17,0x98,0xb3,0x6f,0x6b,0x9a,0xeb,0x6b,0xa3,0xc4,0x75,0xd8,0x2b,0xdc,0x5c,
  142. 0x6f,0xec,0x5d,0x49,0xac,0xa8,0xa4,0x2f,0xb8,0x8c,0x4f,0x2e,0x46,0x21,0xee,
  143. 0x72,0x6a,0x0e,0x22,0x80,0x71,0xc8,0x76,0x40,0x44,0x61,0x16,0xbf,0xa5,0xf8,
  144. 0x89,0xc7,0xe9,0x87,0xdf,0xbd,0x2e,0x4b,0x4e,0xc2,0x97,0x53,0xe9,0x49,0x1c,
  145. 0x05,0xb0,0x0b,0x9b,0x9f,0x21,0x19,0x41,0xe9,0xf5,0x61,0xd7,0x33,0x2e,0x2c,
  146. 0x94,0xb8,0xa8,0x9a,0x3a,0xcc,0x6a,0x24,0x8d,0x19,0x13,0xee,0xb9,0xb0,0x48,
  147. 0x61
  148. };
  149. /* helper function */
  150. static BIGNUM *bn_load_new(const unsigned char *data, int sz)
  151. {
  152. BIGNUM *ret = BN_new();
  153. if (ret != NULL)
  154. BN_bin2bn(data, sz, ret);
  155. return ret;
  156. }
  157. /* helper function */
  158. static BIGNUM *bn_load(BN_CTX *ctx, const unsigned char *data, int sz)
  159. {
  160. BIGNUM *ret = BN_CTX_get(ctx);
  161. if (ret != NULL)
  162. BN_bin2bn(data, sz, ret);
  163. return ret;
  164. }
  165. static int test_check_public_exponent(void)
  166. {
  167. int ret = 0;
  168. BIGNUM *e = NULL;
  169. ret = TEST_ptr(e = BN_new())
  170. /* e is too small */
  171. && TEST_true(BN_set_word(e, 65535))
  172. && TEST_false(rsa_check_public_exponent(e))
  173. /* e is even will fail */
  174. && TEST_true(BN_set_word(e, 65536))
  175. && TEST_false(rsa_check_public_exponent(e))
  176. /* e is ok */
  177. && TEST_true(BN_set_word(e, 65537))
  178. && TEST_true(rsa_check_public_exponent(e))
  179. /* e = 2^256 is too big */
  180. && TEST_true(BN_lshift(e, BN_value_one(), 256))
  181. && TEST_false(rsa_check_public_exponent(e))
  182. /* e = 2^256-1 is odd and in range */
  183. && TEST_true(BN_sub(e, e, BN_value_one()))
  184. && TEST_true(rsa_check_public_exponent(e));
  185. BN_free(e);
  186. return ret;
  187. }
  188. static int test_check_prime_factor_range(void)
  189. {
  190. int ret = 0;
  191. BN_CTX *ctx = NULL;
  192. BIGNUM *p = NULL;
  193. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL, *bn_p4 = NULL;
  194. /* Some range checks that are larger than 32 bits */
  195. static const unsigned char p1[] = { 0x0B, 0x50, 0x4F, 0x33, 0x3F };
  196. static const unsigned char p2[] = { 0x10, 0x00, 0x00, 0x00, 0x00 };
  197. static const unsigned char p3[] = { 0x0B, 0x50, 0x4F, 0x33, 0x40 };
  198. static const unsigned char p4[] = { 0x0F, 0xFF, 0xFF, 0xFF, 0xFF };
  199. /* (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
  200. * For 8 bits: 0xB.504F <= p <= 0xF
  201. * for 72 bits: 0xB504F333F. <= p <= 0xF_FFFF_FFFF
  202. */
  203. ret = TEST_ptr(p = BN_new())
  204. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  205. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  206. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  207. && TEST_ptr(bn_p4 = bn_load_new(p4, sizeof(p4)))
  208. && TEST_ptr(ctx = BN_CTX_new())
  209. && TEST_true(BN_set_word(p, 0xA))
  210. && TEST_false(rsa_check_prime_factor_range(p, 8, ctx))
  211. && TEST_true(BN_set_word(p, 0x10))
  212. && TEST_false(rsa_check_prime_factor_range(p, 8, ctx))
  213. && TEST_true(BN_set_word(p, 0xB))
  214. && TEST_false(rsa_check_prime_factor_range(p, 8, ctx))
  215. && TEST_true(BN_set_word(p, 0xC))
  216. && TEST_true(rsa_check_prime_factor_range(p, 8, ctx))
  217. && TEST_true(BN_set_word(p, 0xF))
  218. && TEST_true(rsa_check_prime_factor_range(p, 8, ctx))
  219. && TEST_false(rsa_check_prime_factor_range(bn_p1, 72, ctx))
  220. && TEST_false(rsa_check_prime_factor_range(bn_p2, 72, ctx))
  221. && TEST_true(rsa_check_prime_factor_range(bn_p3, 72, ctx))
  222. && TEST_true(rsa_check_prime_factor_range(bn_p4, 72, ctx));
  223. BN_free(bn_p4);
  224. BN_free(bn_p3);
  225. BN_free(bn_p2);
  226. BN_free(bn_p1);
  227. BN_free(p);
  228. BN_CTX_free(ctx);
  229. return ret;
  230. }
  231. static int test_check_prime_factor(void)
  232. {
  233. int ret = 0;
  234. BN_CTX *ctx = NULL;
  235. BIGNUM *p = NULL, *e = NULL;
  236. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
  237. /* Some range checks that are larger than 32 bits */
  238. static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 };
  239. static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 };
  240. static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 };
  241. ret = TEST_ptr(p = BN_new())
  242. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  243. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  244. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  245. && TEST_ptr(e = BN_new())
  246. && TEST_ptr(ctx = BN_CTX_new())
  247. /* Fails the prime test */
  248. && TEST_true(BN_set_word(e, 0x1))
  249. && TEST_false(rsa_check_prime_factor(bn_p1, e, 72, ctx))
  250. /* p is prime and in range and gcd(p-1, e) = 1 */
  251. && TEST_true(rsa_check_prime_factor(bn_p2, e, 72, ctx))
  252. /* gcd(p-1,e) = 1 test fails */
  253. && TEST_true(BN_set_word(e, 0x2))
  254. && TEST_false(rsa_check_prime_factor(p, e, 72, ctx))
  255. /* p fails the range check */
  256. && TEST_true(BN_set_word(e, 0x1))
  257. && TEST_false(rsa_check_prime_factor(bn_p3, e, 72, ctx));
  258. BN_free(bn_p3);
  259. BN_free(bn_p2);
  260. BN_free(bn_p1);
  261. BN_free(e);
  262. BN_free(p);
  263. BN_CTX_free(ctx);
  264. return ret;
  265. }
  266. static int test_check_private_exponent(void)
  267. {
  268. int ret = 0;
  269. RSA *key = NULL;
  270. BN_CTX *ctx = NULL;
  271. BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;
  272. ret = TEST_ptr(key = RSA_new())
  273. && TEST_ptr(ctx = BN_CTX_new())
  274. && TEST_ptr(p = BN_new())
  275. && TEST_ptr(q = BN_new())
  276. /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
  277. && TEST_true(BN_set_word(p, 15))
  278. && TEST_true(BN_set_word(q, 17))
  279. && TEST_true(RSA_set0_factors(key, p, q));
  280. if (!ret) {
  281. BN_free(p);
  282. BN_free(q);
  283. goto end;
  284. }
  285. ret = TEST_ptr(e = BN_new())
  286. && TEST_ptr(d = BN_new())
  287. && TEST_ptr(n = BN_new())
  288. && TEST_true(BN_set_word(e, 5))
  289. && TEST_true(BN_set_word(d, 157))
  290. && TEST_true(BN_set_word(n, 15*17))
  291. && TEST_true(RSA_set0_key(key, n, e, d));
  292. if (!ret) {
  293. BN_free(e);
  294. BN_free(d);
  295. BN_free(n);
  296. goto end;
  297. }
  298. /* fails since d >= lcm(p-1, q-1) */
  299. ret = TEST_false(rsa_check_private_exponent(key, 8, ctx))
  300. && TEST_true(BN_set_word(d, 45))
  301. /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
  302. && TEST_true(rsa_check_private_exponent(key, 8, ctx))
  303. /* d is too small compared to nbits */
  304. && TEST_false(rsa_check_private_exponent(key, 16, ctx))
  305. /* d is too small compared to nbits */
  306. && TEST_true(BN_set_word(d, 16))
  307. && TEST_false(rsa_check_private_exponent(key, 8, ctx))
  308. /* fail if 1 != e.d mod lcm(p-1, q-1) */
  309. && TEST_true(BN_set_word(d, 46))
  310. && TEST_false(rsa_check_private_exponent(key, 8, ctx));
  311. end:
  312. RSA_free(key);
  313. BN_CTX_free(ctx);
  314. return ret;
  315. }
  316. static int test_check_crt_components(void)
  317. {
  318. const int P = 15;
  319. const int Q = 17;
  320. const int E = 5;
  321. const int N = P*Q;
  322. const int DP = 3;
  323. const int DQ = 13;
  324. const int QINV = 8;
  325. int ret = 0;
  326. RSA *key = NULL;
  327. BN_CTX *ctx = NULL;
  328. BIGNUM *p = NULL, *q = NULL, *e = NULL;
  329. ret = TEST_ptr(key = RSA_new())
  330. && TEST_ptr(ctx = BN_CTX_new())
  331. && TEST_ptr(p = BN_new())
  332. && TEST_ptr(q = BN_new())
  333. && TEST_ptr(e = BN_new())
  334. && TEST_true(BN_set_word(p, P))
  335. && TEST_true(BN_set_word(q, Q))
  336. && TEST_true(BN_set_word(e, E))
  337. && TEST_true(RSA_set0_factors(key, p, q));
  338. if (!ret) {
  339. BN_free(p);
  340. BN_free(q);
  341. goto end;
  342. }
  343. ret = TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
  344. && TEST_BN_eq_word(key->n, N)
  345. && TEST_BN_eq_word(key->dmp1, DP)
  346. && TEST_BN_eq_word(key->dmq1, DQ)
  347. && TEST_BN_eq_word(key->iqmp, QINV)
  348. && TEST_true(rsa_check_crt_components(key, ctx))
  349. /* (a) 1 < dP < (p – 1). */
  350. && TEST_true(BN_set_word(key->dmp1, 1))
  351. && TEST_false(rsa_check_crt_components(key, ctx))
  352. && TEST_true(BN_set_word(key->dmp1, P-1))
  353. && TEST_false(rsa_check_crt_components(key, ctx))
  354. && TEST_true(BN_set_word(key->dmp1, DP))
  355. /* (b) 1 < dQ < (q - 1). */
  356. && TEST_true(BN_set_word(key->dmq1, 1))
  357. && TEST_false(rsa_check_crt_components(key, ctx))
  358. && TEST_true(BN_set_word(key->dmq1, Q-1))
  359. && TEST_false(rsa_check_crt_components(key, ctx))
  360. && TEST_true(BN_set_word(key->dmq1, DQ))
  361. /* (c) 1 < qInv < p */
  362. && TEST_true(BN_set_word(key->iqmp, 1))
  363. && TEST_false(rsa_check_crt_components(key, ctx))
  364. && TEST_true(BN_set_word(key->iqmp, P))
  365. && TEST_false(rsa_check_crt_components(key, ctx))
  366. && TEST_true(BN_set_word(key->iqmp, QINV))
  367. /* (d) 1 = (dP . e) mod (p - 1)*/
  368. && TEST_true(BN_set_word(key->dmp1, DP+1))
  369. && TEST_false(rsa_check_crt_components(key, ctx))
  370. && TEST_true(BN_set_word(key->dmp1, DP))
  371. /* (e) 1 = (dQ . e) mod (q - 1) */
  372. && TEST_true(BN_set_word(key->dmq1, DQ-1))
  373. && TEST_false(rsa_check_crt_components(key, ctx))
  374. && TEST_true(BN_set_word(key->dmq1, DQ))
  375. /* (f) 1 = (qInv . q) mod p */
  376. && TEST_true(BN_set_word(key->iqmp, QINV+1))
  377. && TEST_false(rsa_check_crt_components(key, ctx))
  378. && TEST_true(BN_set_word(key->iqmp, QINV))
  379. /* check defaults are still valid */
  380. && TEST_true(rsa_check_crt_components(key, ctx));
  381. end:
  382. BN_free(e);
  383. RSA_free(key);
  384. BN_CTX_free(ctx);
  385. return ret;
  386. }
  387. static int test_pq_diff(void)
  388. {
  389. int ret = 0;
  390. BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
  391. ret = TEST_ptr(tmp = BN_new())
  392. && TEST_ptr(p = BN_new())
  393. && TEST_ptr(q = BN_new())
  394. /* |1-(2+1)| > 2^1 */
  395. && TEST_true(BN_set_word(p, 1))
  396. && TEST_true(BN_set_word(q, 1+2))
  397. && TEST_false(rsa_check_pminusq_diff(tmp, p, q, 202))
  398. /* Check |p - q| > 2^(nbits/2 - 100) */
  399. && TEST_true(BN_set_word(q, 1+3))
  400. && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202))
  401. && TEST_true(BN_set_word(p, 1+3))
  402. && TEST_true(BN_set_word(q, 1))
  403. && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202));
  404. BN_free(p);
  405. BN_free(q);
  406. BN_free(tmp);
  407. return ret;
  408. }
  409. static int test_invalid_keypair(void)
  410. {
  411. int ret = 0;
  412. RSA *key = NULL;
  413. BN_CTX *ctx = NULL;
  414. BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
  415. ret = TEST_ptr(key = RSA_new())
  416. && TEST_ptr(ctx = BN_CTX_new())
  417. /* NULL parameters */
  418. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  419. /* load key */
  420. && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
  421. && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
  422. && TEST_true(RSA_set0_factors(key, p, q));
  423. if (!ret) {
  424. BN_free(p);
  425. BN_free(q);
  426. goto end;
  427. }
  428. ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  429. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  430. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  431. && TEST_true(RSA_set0_key(key, n, e, d));
  432. if (!ret) {
  433. BN_free(e);
  434. BN_free(n);
  435. BN_free(d);
  436. goto end;
  437. }
  438. /* bad strength/key size */
  439. ret = TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 100, 2048))
  440. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 112, 1024))
  441. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 128, 2048))
  442. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 140, 3072))
  443. /* mismatching exponent */
  444. && TEST_false(rsa_sp800_56b_check_keypair(key, BN_value_one(), -1,
  445. 2048))
  446. /* bad exponent */
  447. && TEST_true(BN_add_word(e, 1))
  448. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1,
  449. 2048))
  450. && TEST_true(BN_sub_word(e, 1))
  451. /* mismatch between bits and modulus */
  452. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
  453. && TEST_true(rsa_sp800_56b_check_keypair(key, e, 112, 2048))
  454. /* check n == pq failure */
  455. && TEST_true(BN_add_word(n, 1))
  456. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  457. && TEST_true(BN_sub_word(n, 1))
  458. /* check p */
  459. && TEST_true(BN_sub_word(p, 2))
  460. && TEST_true(BN_mul(n, p, q, ctx))
  461. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  462. && TEST_true(BN_add_word(p, 2))
  463. && TEST_true(BN_mul(n, p, q, ctx))
  464. /* check q */
  465. && TEST_true(BN_sub_word(q, 2))
  466. && TEST_true(BN_mul(n, p, q, ctx))
  467. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  468. && TEST_true(BN_add_word(q, 2))
  469. && TEST_true(BN_mul(n, p, q, ctx));
  470. end:
  471. RSA_free(key);
  472. BN_CTX_free(ctx);
  473. return ret;
  474. }
  475. static int test_fips1864_keygen_kat(void)
  476. {
  477. int ret = 0;
  478. RSA *key = NULL;
  479. BN_CTX *ctx = NULL;
  480. BIGNUM *e, *Xp, *Xp1, *Xp2, *Xq, *Xq1, *Xq2;
  481. BIGNUM *p1, *p2, *q1, *q2;
  482. BIGNUM *p1_exp, *p2_exp, *q1_exp, *q2_exp;
  483. BIGNUM *p_exp, *q_exp, *n_exp, *d_exp;
  484. const BIGNUM *p, *q, *n, *d, *e2;
  485. if (!(TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new())))
  486. goto err;
  487. BN_CTX_start(ctx);
  488. e = bn_load(ctx, cav_e, sizeof(cav_e));
  489. Xp = bn_load(ctx, cav_Xp, sizeof(cav_Xp));
  490. Xp1 = bn_load(ctx, cav_Xp1, sizeof(cav_Xp1));
  491. Xp2 = bn_load(ctx, cav_Xp2, sizeof(cav_Xp2));
  492. Xq = bn_load(ctx, cav_Xq, sizeof(cav_Xq));
  493. Xq1 = bn_load(ctx, cav_Xq1, sizeof(cav_Xq1));
  494. Xq2 = bn_load(ctx, cav_Xq2, sizeof(cav_Xq2));
  495. p1_exp = bn_load(ctx, cav_p1, sizeof(cav_p1));
  496. p2_exp = bn_load(ctx, cav_p2, sizeof(cav_p2));
  497. q1_exp = bn_load(ctx, cav_q1, sizeof(cav_q1));
  498. q2_exp = bn_load(ctx, cav_q2, sizeof(cav_q2));
  499. p_exp = bn_load(ctx, cav_p, sizeof(cav_p));
  500. q_exp = bn_load(ctx, cav_q, sizeof(cav_q));
  501. n_exp = bn_load(ctx, cav_n, sizeof(cav_n));
  502. d_exp = bn_load(ctx, cav_d, sizeof(cav_d));
  503. p1 = BN_CTX_get(ctx);
  504. p2 = BN_CTX_get(ctx);
  505. q1 = BN_CTX_get(ctx);
  506. q2 = BN_CTX_get(ctx);
  507. ret = TEST_ptr(q2)
  508. && TEST_true(rsa_fips186_4_gen_prob_primes(key, p1, p2, NULL, Xp, Xp1,
  509. Xp2, q1, q2, NULL, Xq, Xq1,
  510. Xq2, 2048, e, ctx, NULL))
  511. && TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 2048, e, ctx))
  512. && TEST_BN_eq(p1_exp, p1)
  513. && TEST_BN_eq(p2_exp, p2)
  514. && TEST_BN_eq(q1_exp, q1)
  515. && TEST_BN_eq(q2_exp, q2);
  516. if (!ret)
  517. goto err;
  518. RSA_get0_key(key, &n, &e2, &d);
  519. RSA_get0_factors(key, &p, &q);
  520. ret = TEST_BN_eq(e, e2)
  521. && TEST_BN_eq(p_exp, p)
  522. && TEST_BN_eq(q_exp, q)
  523. && TEST_BN_eq(n_exp, n)
  524. && TEST_BN_eq(d_exp, d);
  525. err:
  526. RSA_free(key);
  527. BN_CTX_end(ctx);
  528. BN_CTX_free(ctx);
  529. return ret;
  530. }
  531. static int keygen_size[] =
  532. {
  533. 2048, 3072
  534. };
  535. static int test_sp80056b_keygen(int id)
  536. {
  537. RSA *key = NULL;
  538. int ret;
  539. int sz = keygen_size[id];
  540. ret = TEST_ptr(key = RSA_new())
  541. && TEST_true(rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
  542. && TEST_true(rsa_sp800_56b_check_public(key))
  543. && TEST_true(rsa_sp800_56b_check_private(key))
  544. && TEST_true(rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
  545. RSA_free(key);
  546. return ret;
  547. }
  548. static int test_check_private_key(void)
  549. {
  550. int ret = 0;
  551. BIGNUM *n = NULL, *d = NULL, *e = NULL;
  552. RSA *key = NULL;
  553. ret = TEST_ptr(key = RSA_new())
  554. /* check NULL pointers fail */
  555. && TEST_false(rsa_sp800_56b_check_private(key))
  556. /* load private key */
  557. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  558. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  559. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  560. && TEST_true(RSA_set0_key(key, n, e, d));
  561. if (!ret) {
  562. BN_free(n);
  563. BN_free(e);
  564. BN_free(d);
  565. goto end;
  566. }
  567. /* check d is in range */
  568. ret = TEST_true(rsa_sp800_56b_check_private(key))
  569. /* check d is too low */
  570. && TEST_true(BN_set_word(d, 0))
  571. && TEST_false(rsa_sp800_56b_check_private(key))
  572. /* check d is too high */
  573. && TEST_ptr(BN_copy(d, n))
  574. && TEST_false(rsa_sp800_56b_check_private(key));
  575. end:
  576. RSA_free(key);
  577. return ret;
  578. }
  579. static int test_check_public_key(void)
  580. {
  581. int ret = 0;
  582. BIGNUM *n = NULL, *e = NULL;
  583. RSA *key = NULL;
  584. ret = TEST_ptr(key = RSA_new())
  585. /* check NULL pointers fail */
  586. && TEST_false(rsa_sp800_56b_check_public(key))
  587. /* load public key */
  588. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  589. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  590. && TEST_true(RSA_set0_key(key, n, e, NULL));
  591. if (!ret) {
  592. BN_free(e);
  593. BN_free(n);
  594. goto end;
  595. }
  596. /* check public key is valid */
  597. ret = TEST_true(rsa_sp800_56b_check_public(key))
  598. /* check fail if n is even */
  599. && TEST_true(BN_add_word(n, 1))
  600. && TEST_false(rsa_sp800_56b_check_public(key))
  601. && TEST_true(BN_sub_word(n, 1))
  602. /* check fail if n is wrong number of bits */
  603. && TEST_true(BN_lshift1(n, n))
  604. && TEST_false(rsa_sp800_56b_check_public(key))
  605. && TEST_true(BN_rshift1(n, n))
  606. /* test odd exponent fails */
  607. && TEST_true(BN_add_word(e, 1))
  608. && TEST_false(rsa_sp800_56b_check_public(key))
  609. && TEST_true(BN_sub_word(e, 1))
  610. /* modulus fails composite check */
  611. && TEST_true(BN_add_word(n, 2))
  612. && TEST_false(rsa_sp800_56b_check_public(key));
  613. end:
  614. RSA_free(key);
  615. return ret;
  616. }
  617. int setup_tests(void)
  618. {
  619. ADD_TEST(test_check_public_exponent);
  620. ADD_TEST(test_check_prime_factor_range);
  621. ADD_TEST(test_check_prime_factor);
  622. ADD_TEST(test_check_private_exponent);
  623. ADD_TEST(test_check_crt_components);
  624. ADD_TEST(test_check_private_key);
  625. ADD_TEST(test_check_public_key);
  626. ADD_TEST(test_invalid_keypair);
  627. ADD_TEST(test_pq_diff);
  628. ADD_TEST(test_fips1864_keygen_kat);
  629. ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));
  630. return 1;
  631. }
  632. #endif