rsa_sp800_56b_test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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_locl.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_true(rsa_check_prime_factor_range(p, 8, ctx))
  215. && TEST_true(BN_set_word(p, 0xF))
  216. && TEST_true(rsa_check_prime_factor_range(p, 8, ctx))
  217. && TEST_false(rsa_check_prime_factor_range(bn_p1, 72, ctx))
  218. && TEST_false(rsa_check_prime_factor_range(bn_p2, 72, ctx))
  219. && TEST_true(rsa_check_prime_factor_range(bn_p3, 72, ctx))
  220. && TEST_true(rsa_check_prime_factor_range(bn_p4, 72, ctx));
  221. BN_free(bn_p4);
  222. BN_free(bn_p3);
  223. BN_free(bn_p2);
  224. BN_free(bn_p1);
  225. BN_free(p);
  226. BN_CTX_free(ctx);
  227. return ret;
  228. }
  229. static int test_check_prime_factor(void)
  230. {
  231. int ret = 0;
  232. BN_CTX *ctx = NULL;
  233. BIGNUM *p = NULL, *e = NULL;
  234. BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
  235. /* Some range checks that are larger than 32 bits */
  236. static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 };
  237. static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 };
  238. static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 };
  239. ret = TEST_ptr(p = BN_new())
  240. && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
  241. && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
  242. && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
  243. && TEST_ptr(e = BN_new())
  244. && TEST_ptr(ctx = BN_CTX_new())
  245. /* Fails the prime test */
  246. && TEST_true(BN_set_word(e, 0x1))
  247. && TEST_false(rsa_check_prime_factor(bn_p1, e, 72, ctx))
  248. /* p is prime and in range and gcd(p-1, e) = 1 */
  249. && TEST_true(rsa_check_prime_factor(bn_p2, e, 72, ctx))
  250. /* gcd(p-1,e) = 1 test fails */
  251. && TEST_true(BN_set_word(e, 0x2))
  252. && TEST_false(rsa_check_prime_factor(p, e, 72, ctx))
  253. /* p fails the range check */
  254. && TEST_true(BN_set_word(e, 0x1))
  255. && TEST_false(rsa_check_prime_factor(bn_p3, e, 72, ctx));
  256. BN_free(bn_p3);
  257. BN_free(bn_p2);
  258. BN_free(bn_p1);
  259. BN_free(e);
  260. BN_free(p);
  261. BN_CTX_free(ctx);
  262. return ret;
  263. }
  264. static int test_check_private_exponent(void)
  265. {
  266. int ret = 0;
  267. RSA *key = NULL;
  268. BN_CTX *ctx = NULL;
  269. BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = 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. /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
  275. && TEST_true(BN_set_word(p, 15))
  276. && TEST_true(BN_set_word(q, 17))
  277. && TEST_true(RSA_set0_factors(key, p, q));
  278. if (!ret) {
  279. BN_free(p);
  280. BN_free(q);
  281. goto end;
  282. }
  283. ret = TEST_ptr(e = BN_new())
  284. && TEST_ptr(d = BN_new())
  285. && TEST_ptr(n = BN_new())
  286. && TEST_true(BN_set_word(e, 5))
  287. && TEST_true(BN_set_word(d, 157))
  288. && TEST_true(BN_set_word(n, 15*17))
  289. && TEST_true(RSA_set0_key(key, n, e, d));
  290. if (!ret) {
  291. BN_free(e);
  292. BN_free(d);
  293. BN_free(n);
  294. goto end;
  295. }
  296. /* fails since d >= lcm(p-1, q-1) */
  297. ret = TEST_false(rsa_check_private_exponent(key, 8, ctx))
  298. && TEST_true(BN_set_word(d, 45))
  299. /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
  300. && TEST_true(rsa_check_private_exponent(key, 8, ctx))
  301. /* d is too small compared to nbits */
  302. && TEST_false(rsa_check_private_exponent(key, 16, ctx))
  303. /* d is too small compared to nbits */
  304. && TEST_true(BN_set_word(d, 16))
  305. && TEST_false(rsa_check_private_exponent(key, 8, ctx))
  306. /* fail if 1 != e.d mod lcm(p-1, q-1) */
  307. && TEST_true(BN_set_word(d, 46))
  308. && TEST_false(rsa_check_private_exponent(key, 8, ctx));
  309. end:
  310. RSA_free(key);
  311. BN_CTX_free(ctx);
  312. return ret;
  313. }
  314. static int test_check_crt_components(void)
  315. {
  316. const int P = 15;
  317. const int Q = 17;
  318. const int E = 5;
  319. const int N = P*Q;
  320. const int DP = 3;
  321. const int DQ = 13;
  322. const int QINV = 8;
  323. int ret = 0;
  324. RSA *key = NULL;
  325. BN_CTX *ctx = NULL;
  326. BIGNUM *p = NULL, *q = NULL, *e = NULL;
  327. ret = TEST_ptr(key = RSA_new())
  328. && TEST_ptr(ctx = BN_CTX_new())
  329. && TEST_ptr(p = BN_new())
  330. && TEST_ptr(q = BN_new())
  331. && TEST_ptr(e = BN_new())
  332. && TEST_true(BN_set_word(p, P))
  333. && TEST_true(BN_set_word(q, Q))
  334. && TEST_true(BN_set_word(e, E))
  335. && TEST_true(RSA_set0_factors(key, p, q));
  336. if (!ret) {
  337. BN_free(p);
  338. BN_free(q);
  339. goto end;
  340. }
  341. ret = TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
  342. && TEST_BN_eq_word(key->n, N)
  343. && TEST_BN_eq_word(key->dmp1, DP)
  344. && TEST_BN_eq_word(key->dmq1, DQ)
  345. && TEST_BN_eq_word(key->iqmp, QINV)
  346. && TEST_true(rsa_check_crt_components(key, ctx))
  347. /* (a) 1 < dP < (p – 1). */
  348. && TEST_true(BN_set_word(key->dmp1, 1))
  349. && TEST_false(rsa_check_crt_components(key, ctx))
  350. && TEST_true(BN_set_word(key->dmp1, P-1))
  351. && TEST_false(rsa_check_crt_components(key, ctx))
  352. && TEST_true(BN_set_word(key->dmp1, DP))
  353. /* (b) 1 < dQ < (q - 1). */
  354. && TEST_true(BN_set_word(key->dmq1, 1))
  355. && TEST_false(rsa_check_crt_components(key, ctx))
  356. && TEST_true(BN_set_word(key->dmq1, Q-1))
  357. && TEST_false(rsa_check_crt_components(key, ctx))
  358. && TEST_true(BN_set_word(key->dmq1, DQ))
  359. /* (c) 1 < qInv < p */
  360. && TEST_true(BN_set_word(key->iqmp, 1))
  361. && TEST_false(rsa_check_crt_components(key, ctx))
  362. && TEST_true(BN_set_word(key->iqmp, P))
  363. && TEST_false(rsa_check_crt_components(key, ctx))
  364. && TEST_true(BN_set_word(key->iqmp, QINV))
  365. /* (d) 1 = (dP . e) mod (p - 1)*/
  366. && TEST_true(BN_set_word(key->dmp1, DP+1))
  367. && TEST_false(rsa_check_crt_components(key, ctx))
  368. && TEST_true(BN_set_word(key->dmp1, DP))
  369. /* (e) 1 = (dQ . e) mod (q - 1) */
  370. && TEST_true(BN_set_word(key->dmq1, DQ-1))
  371. && TEST_false(rsa_check_crt_components(key, ctx))
  372. && TEST_true(BN_set_word(key->dmq1, DQ))
  373. /* (f) 1 = (qInv . q) mod p */
  374. && TEST_true(BN_set_word(key->iqmp, QINV+1))
  375. && TEST_false(rsa_check_crt_components(key, ctx))
  376. && TEST_true(BN_set_word(key->iqmp, QINV))
  377. /* check defaults are still valid */
  378. && TEST_true(rsa_check_crt_components(key, ctx));
  379. end:
  380. BN_free(e);
  381. RSA_free(key);
  382. BN_CTX_free(ctx);
  383. return ret;
  384. }
  385. static int test_pq_diff(void)
  386. {
  387. int ret = 0;
  388. BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
  389. ret = TEST_ptr(tmp = BN_new())
  390. && TEST_ptr(p = BN_new())
  391. && TEST_ptr(q = BN_new())
  392. /* |1-(2+1)| > 2^1 */
  393. && TEST_true(BN_set_word(p, 1))
  394. && TEST_true(BN_set_word(q, 1+2))
  395. && TEST_false(rsa_check_pminusq_diff(tmp, p, q, 202))
  396. /* Check |p - q| > 2^(nbits/2 - 100) */
  397. && TEST_true(BN_set_word(q, 1+3))
  398. && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202))
  399. && TEST_true(BN_set_word(p, 1+3))
  400. && TEST_true(BN_set_word(q, 1))
  401. && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202));
  402. BN_free(p);
  403. BN_free(q);
  404. BN_free(tmp);
  405. return ret;
  406. }
  407. static int test_invalid_keypair(void)
  408. {
  409. int ret = 0;
  410. RSA *key = NULL;
  411. BN_CTX *ctx = NULL;
  412. BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
  413. ret = TEST_ptr(key = RSA_new())
  414. && TEST_ptr(ctx = BN_CTX_new())
  415. /* NULL parameters */
  416. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  417. /* load key */
  418. && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
  419. && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
  420. && TEST_true(RSA_set0_factors(key, p, q));
  421. if (!ret) {
  422. BN_free(p);
  423. BN_free(q);
  424. goto end;
  425. }
  426. ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  427. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  428. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  429. && TEST_true(RSA_set0_key(key, n, e, d));
  430. if (!ret) {
  431. BN_free(e);
  432. BN_free(n);
  433. BN_free(d);
  434. goto end;
  435. }
  436. /* bad strength/key size */
  437. ret = TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 100, 2048))
  438. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 112, 1024))
  439. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 128, 2048))
  440. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 140, 3072))
  441. /* mismatching exponent */
  442. && TEST_false(rsa_sp800_56b_check_keypair(key, BN_value_one(), -1,
  443. 2048))
  444. /* bad exponent */
  445. && TEST_true(BN_add_word(e, 1))
  446. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1,
  447. 2048))
  448. && TEST_true(BN_sub_word(e, 1))
  449. /* mismatch between bits and modulus */
  450. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
  451. && TEST_true(rsa_sp800_56b_check_keypair(key, e, 112, 2048))
  452. /* check n == pq failure */
  453. && TEST_true(BN_add_word(n, 1))
  454. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  455. && TEST_true(BN_sub_word(n, 1))
  456. /* check p */
  457. && TEST_true(BN_sub_word(p, 2))
  458. && TEST_true(BN_mul(n, p, q, ctx))
  459. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  460. && TEST_true(BN_add_word(p, 2))
  461. && TEST_true(BN_mul(n, p, q, ctx))
  462. /* check q */
  463. && TEST_true(BN_sub_word(q, 2))
  464. && TEST_true(BN_mul(n, p, q, ctx))
  465. && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
  466. && TEST_true(BN_add_word(q, 2))
  467. && TEST_true(BN_mul(n, p, q, ctx));
  468. end:
  469. RSA_free(key);
  470. BN_CTX_free(ctx);
  471. return ret;
  472. }
  473. static int test_fips1864_keygen_kat(void)
  474. {
  475. int ret = 0;
  476. RSA *key = NULL;
  477. BN_CTX *ctx = NULL;
  478. BIGNUM *e, *Xp, *Xp1, *Xp2, *Xq, *Xq1, *Xq2;
  479. BIGNUM *p1, *p2, *q1, *q2;
  480. BIGNUM *p1_exp, *p2_exp, *q1_exp, *q2_exp;
  481. BIGNUM *p_exp, *q_exp, *n_exp, *d_exp;
  482. const BIGNUM *p, *q, *n, *d, *e2;
  483. if (!(TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new())))
  484. goto err;
  485. BN_CTX_start(ctx);
  486. e = bn_load(ctx, cav_e, sizeof(cav_e));
  487. Xp = bn_load(ctx, cav_Xp, sizeof(cav_Xp));
  488. Xp1 = bn_load(ctx, cav_Xp1, sizeof(cav_Xp1));
  489. Xp2 = bn_load(ctx, cav_Xp2, sizeof(cav_Xp2));
  490. Xq = bn_load(ctx, cav_Xq, sizeof(cav_Xq));
  491. Xq1 = bn_load(ctx, cav_Xq1, sizeof(cav_Xq1));
  492. Xq2 = bn_load(ctx, cav_Xq2, sizeof(cav_Xq2));
  493. p1_exp = bn_load(ctx, cav_p1, sizeof(cav_p1));
  494. p2_exp = bn_load(ctx, cav_p2, sizeof(cav_p2));
  495. q1_exp = bn_load(ctx, cav_q1, sizeof(cav_q1));
  496. q2_exp = bn_load(ctx, cav_q2, sizeof(cav_q2));
  497. p_exp = bn_load(ctx, cav_p, sizeof(cav_p));
  498. q_exp = bn_load(ctx, cav_q, sizeof(cav_q));
  499. n_exp = bn_load(ctx, cav_n, sizeof(cav_n));
  500. d_exp = bn_load(ctx, cav_d, sizeof(cav_d));
  501. p1 = BN_CTX_get(ctx);
  502. p2 = BN_CTX_get(ctx);
  503. q1 = BN_CTX_get(ctx);
  504. q2 = BN_CTX_get(ctx);
  505. ret = TEST_ptr(q2)
  506. && TEST_true(rsa_fips186_4_gen_prob_primes(key, p1, p2, NULL, Xp, Xp1,
  507. Xp2, q1, q2, NULL, Xq, Xq1,
  508. Xq2, 2048, e, ctx, NULL))
  509. && TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 2048, e, ctx))
  510. && TEST_BN_eq(p1_exp, p1)
  511. && TEST_BN_eq(p2_exp, p2)
  512. && TEST_BN_eq(q1_exp, q1)
  513. && TEST_BN_eq(q2_exp, q2);
  514. if (!ret)
  515. goto err;
  516. RSA_get0_key(key, &n, &e2, &d);
  517. RSA_get0_factors(key, &p, &q);
  518. ret = TEST_BN_eq(e, e2)
  519. && TEST_BN_eq(p_exp, p)
  520. && TEST_BN_eq(q_exp, q)
  521. && TEST_BN_eq(n_exp, n)
  522. && TEST_BN_eq(d_exp, d);
  523. err:
  524. RSA_free(key);
  525. BN_CTX_end(ctx);
  526. BN_CTX_free(ctx);
  527. return ret;
  528. }
  529. static int keygen_size[] =
  530. {
  531. 2048, 3072
  532. };
  533. static int test_sp80056b_keygen(int id)
  534. {
  535. RSA *key = NULL;
  536. int ret;
  537. int sz = keygen_size[id];
  538. ret = TEST_ptr(key = RSA_new())
  539. && TEST_true(rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
  540. && TEST_true(rsa_sp800_56b_check_public(key))
  541. && TEST_true(rsa_sp800_56b_check_private(key))
  542. && TEST_true(rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
  543. RSA_free(key);
  544. return ret;
  545. }
  546. static int test_check_private_key(void)
  547. {
  548. int ret = 0;
  549. BIGNUM *n = NULL, *d = NULL, *e = NULL;
  550. RSA *key = NULL;
  551. ret = TEST_ptr(key = RSA_new())
  552. /* check NULL pointers fail */
  553. && TEST_false(rsa_sp800_56b_check_private(key))
  554. /* load private key */
  555. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  556. && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
  557. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  558. && TEST_true(RSA_set0_key(key, n, e, d));
  559. if (!ret) {
  560. BN_free(n);
  561. BN_free(e);
  562. BN_free(d);
  563. goto end;
  564. }
  565. /* check d is in range */
  566. ret = TEST_true(rsa_sp800_56b_check_private(key))
  567. /* check d is too low */
  568. && TEST_true(BN_set_word(d, 0))
  569. && TEST_false(rsa_sp800_56b_check_private(key))
  570. /* check d is too high */
  571. && TEST_ptr(BN_copy(d, n))
  572. && TEST_false(rsa_sp800_56b_check_private(key));
  573. end:
  574. RSA_free(key);
  575. return ret;
  576. }
  577. static int test_check_public_key(void)
  578. {
  579. int ret = 0;
  580. BIGNUM *n = NULL, *e = NULL;
  581. RSA *key = NULL;
  582. ret = TEST_ptr(key = RSA_new())
  583. /* check NULL pointers fail */
  584. && TEST_false(rsa_sp800_56b_check_public(key))
  585. /* load public key */
  586. && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
  587. && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
  588. && TEST_true(RSA_set0_key(key, n, e, NULL));
  589. if (!ret) {
  590. BN_free(e);
  591. BN_free(n);
  592. goto end;
  593. }
  594. /* check public key is valid */
  595. ret = TEST_true(rsa_sp800_56b_check_public(key))
  596. /* check fail if n is even */
  597. && TEST_true(BN_add_word(n, 1))
  598. && TEST_false(rsa_sp800_56b_check_public(key))
  599. && TEST_true(BN_sub_word(n, 1))
  600. /* check fail if n is wrong number of bits */
  601. && TEST_true(BN_lshift1(n, n))
  602. && TEST_false(rsa_sp800_56b_check_public(key))
  603. && TEST_true(BN_rshift1(n, n))
  604. /* test odd exponent fails */
  605. && TEST_true(BN_add_word(e, 1))
  606. && TEST_false(rsa_sp800_56b_check_public(key))
  607. && TEST_true(BN_sub_word(e, 1))
  608. /* modulus fails composite check */
  609. && TEST_true(BN_add_word(n, 2))
  610. && TEST_false(rsa_sp800_56b_check_public(key));
  611. end:
  612. RSA_free(key);
  613. return ret;
  614. }
  615. int setup_tests(void)
  616. {
  617. ADD_TEST(test_check_public_exponent);
  618. ADD_TEST(test_check_prime_factor_range);
  619. ADD_TEST(test_check_prime_factor);
  620. ADD_TEST(test_check_private_exponent);
  621. ADD_TEST(test_check_crt_components);
  622. ADD_TEST(test_check_private_key);
  623. ADD_TEST(test_check_public_key);
  624. ADD_TEST(test_invalid_keypair);
  625. ADD_TEST(test_pq_diff);
  626. ADD_TEST(test_fips1864_keygen_kat);
  627. ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));
  628. return 1;
  629. }
  630. #endif