2 * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include "internal/nelem.h"
15 #include <openssl/crypto.h>
16 #include <openssl/err.h>
17 #include <openssl/rand.h>
18 #include <openssl/bn.h>
29 # include "rsa_local.h"
30 # include <openssl/rsa.h>
32 /* taken from RSA2 cavs data */
33 static const unsigned char cav_e[] = {
36 static const unsigned char cav_Xp[] = {
37 0xcf,0x72,0x1b,0x9a,0xfd,0x0d,0x22,0x1a,0x74,0x50,0x97,0x22,0x76,0xd8,0xc0,
38 0xc2,0xfd,0x08,0x81,0x05,0xdd,0x18,0x21,0x99,0x96,0xd6,0x5c,0x79,0xe3,0x02,
39 0x81,0xd7,0x0e,0x3f,0x3b,0x34,0xda,0x61,0xc9,0x2d,0x84,0x86,0x62,0x1e,0x3d,
40 0x5d,0xbf,0x92,0x2e,0xcd,0x35,0x3d,0x6e,0xb9,0x59,0x16,0xc9,0x82,0x50,0x41,
41 0x30,0x45,0x67,0xaa,0xb7,0xbe,0xec,0xea,0x4b,0x9e,0xa0,0xc3,0x05,0xb3,0x88,
42 0xd4,0x4c,0xac,0xeb,0xe4,0x03,0xc6,0xca,0xcb,0xd9,0xd3,0x4e,0xf6,0x7f,0x2c,
43 0x27,0x1e,0x08,0x6c,0xc2,0xd6,0x45,0x1f,0x84,0xe4,0x3c,0x97,0x19,0xde,0xb8,
44 0x55,0xaf,0x0e,0xcf,0x9e,0xb0,0x9c,0x20,0xd3,0x1f,0xa8,0xd7,0x52,0xc2,0x95,
45 0x1c,0x80,0x15,0x42,0x4d,0x4f,0x19,0x16
47 static const unsigned char cav_Xp1[] = {
48 0xac,0x5f,0x7f,0x6e,0x33,0x3e,0x97,0x3a,0xb3,0x17,0x44,0xa9,0x0f,0x7a,0x54,
49 0x70,0x27,0x06,0x93,0xd5,0x49,0xde,0x91,0x83,0xbc,0x8a,0x7b,0x95
51 static const unsigned char cav_Xp2[] = {
52 0x0b,0xf6,0xe8,0x79,0x5a,0x81,0xae,0x90,0x1d,0xa4,0x38,0x74,0x9c,0x0e,0x6f,
53 0xe0,0x03,0xcf,0xc4,0x53,0x16,0x32,0x17,0xf7,0x09,0x5f,0xd9
55 static const unsigned char cav_Xq[] = {
56 0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb,
57 0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf,
58 0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68,
59 0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e,
60 0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xbd,0x9a,0x89,
61 0x99,0x6e,0x57,0x5e,0xd0,0x39,0x86,0xc3,0xa3,0x1b,0xc7,0xcf,0xc4,0x4f,0x47,
62 0x25,0x9e,0x2c,0x79,0xe1,0x2c,0xcc,0xe4,0x63,0xf4,0x02,0x84,0xf8,0xf6,0xa1,
63 0x5c,0x93,0x14,0xf2,0x68,0x5f,0x3a,0x90,0x2f,0x4e,0x5e,0xf9,0x16,0x05,0xcf,
64 0x21,0x63,0xca,0xfa,0xb0,0x08,0x02,0xc0
66 static const unsigned char cav_Xq1[] = {
67 0x9b,0x02,0xd4,0xba,0xf0,0xaa,0x14,0x99,0x6d,0xc0,0xb7,0xa5,0xe1,0xd3,0x70,
68 0xb6,0x5a,0xa2,0x9b,0x59,0xd5,0x8c,0x1e,0x9f,0x3f,0x9a,0xde,0xeb,0x9e,0x9c,
71 static const unsigned char cav_Xq2[] = {
72 0x06,0x81,0x53,0xfd,0xa8,0x7b,0xa3,0x85,0x90,0x15,0x2c,0x97,0xb2,0xa0,0x17,
73 0x48,0xb0,0x7f,0x0a,0x01,0x6d
76 static const unsigned char cav_p1[] = {
77 0xac,0x5f,0x7f,0x6e,0x33,0x3e,0x97,0x3a,0xb3,0x17,0x44,0xa9,0x0f,0x7a,0x54,
78 0x70,0x27,0x06,0x93,0xd5,0x49,0xde,0x91,0x83,0xbc,0x8a,0x7b,0xc3
80 static const unsigned char cav_p2[] = {
81 0x0b,0xf6,0xe8,0x79,0x5a,0x81,0xae,0x90,0x1d,0xa4,0x38,0x74,0x9c,0x0e,0x6f,
82 0xe0,0x03,0xcf,0xc4,0x53,0x16,0x32,0x17,0xf7,0x09,0x5f,0xd9
84 static const unsigned char cav_q1[] = {
85 0x9b,0x02,0xd4,0xba,0xf0,0xaa,0x14,0x99,0x6d,0xc0,0xb7,0xa5,0xe1,0xd3,0x70,
86 0xb6,0x5a,0xa2,0x9b,0x59,0xd5,0x8c,0x1e,0x9f,0x3f,0x9a,0xde,0xeb,0x9e,0x9c,
89 static const unsigned char cav_q2[] = {
90 0x06,0x81,0x53,0xfd,0xa8,0x7b,0xa3,0x85,0x90,0x15,0x2c,0x97,0xb2,0xa0,0x17,
91 0x48,0xb0,0x7f,0x0a,0x01,0x8f
93 static const unsigned char cav_p[] = {
94 0xcf,0x72,0x1b,0x9a,0xfd,0x0d,0x22,0x1a,0x74,0x50,0x97,0x22,0x76,0xd8,0xc0,
95 0xc2,0xfd,0x08,0x81,0x05,0xdd,0x18,0x21,0x99,0x96,0xd6,0x5c,0x79,0xe3,0x02,
96 0x81,0xd7,0x0e,0x3f,0x3b,0x34,0xda,0x61,0xc9,0x2d,0x84,0x86,0x62,0x1e,0x3d,
97 0x5d,0xbf,0x92,0x2e,0xcd,0x35,0x3d,0x6e,0xb9,0x59,0x16,0xc9,0x82,0x50,0x41,
98 0x30,0x45,0x67,0xaa,0xb7,0xbe,0xec,0xea,0x4b,0x9e,0xa0,0xc3,0x05,0xbc,0x4c,
99 0x01,0xa5,0x4b,0xbd,0xa4,0x20,0xb5,0x20,0xd5,0x59,0x6f,0x82,0x5c,0x8f,0x4f,
100 0xe0,0x3a,0x4e,0x7e,0xfe,0x44,0xf3,0x3c,0xc0,0x0e,0x14,0x2b,0x32,0xe6,0x28,
101 0x8b,0x63,0x87,0x00,0xc3,0x53,0x4a,0x5b,0x71,0x7a,0x5b,0x28,0x40,0xc4,0x18,
102 0xb6,0x77,0x0b,0xab,0x59,0xa4,0x96,0x7d
104 static const unsigned char cav_q[] = {
105 0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb,
106 0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf,
107 0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68,
108 0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e,
109 0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xc4,0x44,0xdd,
110 0xf7,0x7e,0xda,0x47,0x4a,0x67,0x44,0x5d,0x4e,0x75,0xf0,0x4d,0x00,0x68,0xe1,
111 0x4a,0xec,0x1f,0x45,0xf9,0xe6,0xca,0x38,0x95,0x48,0x6f,0xdc,0x9d,0x1b,0xa3,
112 0x4b,0xfd,0x08,0x4b,0x54,0xcd,0xeb,0x3d,0xef,0x33,0x11,0x6e,0xce,0xe4,0x5d,
113 0xef,0xa9,0x58,0x5c,0x87,0x4d,0xc8,0xcf
115 static const unsigned char cav_n[] = {
116 0xce,0x5e,0x8d,0x1a,0xa3,0x08,0x7a,0x2d,0xb4,0x49,0x48,0xf0,0x06,0xb6,0xfe,
117 0xba,0x2f,0x39,0x7c,0x7b,0xe0,0x5d,0x09,0x2d,0x57,0x4e,0x54,0x60,0x9c,0xe5,
118 0x08,0x4b,0xe1,0x1a,0x73,0xc1,0x5e,0x2f,0xb6,0x46,0xd7,0x81,0xca,0xbc,0x98,
119 0xd2,0xf9,0xef,0x1c,0x92,0x8c,0x8d,0x99,0x85,0x28,0x52,0xd6,0xd5,0xab,0x70,
120 0x7e,0x9e,0xa9,0x87,0x82,0xc8,0x95,0x64,0xeb,0xf0,0x6c,0x0f,0x3f,0xe9,0x02,
121 0x29,0x2e,0x6d,0xa1,0xec,0xbf,0xdc,0x23,0xdf,0x82,0x4f,0xab,0x39,0x8d,0xcc,
122 0xac,0x21,0x51,0x14,0xf8,0xef,0xec,0x73,0x80,0x86,0xa3,0xcf,0x8f,0xd5,0xcf,
123 0x22,0x1f,0xcc,0x23,0x2f,0xba,0xcb,0xf6,0x17,0xcd,0x3a,0x1f,0xd9,0x84,0xb9,
124 0x88,0xa7,0x78,0x0f,0xaa,0xc9,0x04,0x01,0x20,0x72,0x5d,0x2a,0xfe,0x5b,0xdd,
125 0x16,0x5a,0xed,0x83,0x02,0x96,0x39,0x46,0x37,0x30,0xc1,0x0d,0x87,0xc2,0xc8,
126 0x33,0x38,0xed,0x35,0x72,0xe5,0x29,0xf8,0x1f,0x23,0x60,0xe1,0x2a,0x5b,0x1d,
127 0x6b,0x53,0x3f,0x07,0xc4,0xd9,0xbb,0x04,0x0c,0x5c,0x3f,0x0b,0xc4,0xd4,0x61,
128 0x96,0x94,0xf1,0x0f,0x4a,0x49,0xac,0xde,0xd2,0xe8,0x42,0xb3,0x4a,0x0b,0x64,
129 0x7a,0x32,0x5f,0x2b,0x5b,0x0f,0x8b,0x8b,0xe0,0x33,0x23,0x34,0x64,0xf8,0xb5,
130 0x7f,0x69,0x60,0xb8,0x71,0xe9,0xff,0x92,0x42,0xb1,0xf7,0x23,0xa8,0xa7,0x92,
131 0x04,0x3d,0x6b,0xff,0xf7,0xab,0xbb,0x14,0x1f,0x4c,0x10,0x97,0xd5,0x6b,0x71,
132 0x12,0xfd,0x93,0xa0,0x4a,0x3b,0x75,0x72,0x40,0x96,0x1c,0x5f,0x40,0x40,0x57,
135 static const unsigned char cav_d[] = {
136 0x47,0x47,0x49,0x1d,0x66,0x2a,0x4b,0x68,0xf5,0xd8,0x4a,0x24,0xfd,0x6c,0xbf,
137 0x56,0xb7,0x70,0xf7,0x9a,0x21,0xc8,0x80,0x9e,0xf4,0x84,0xcd,0x88,0x01,0x28,
138 0xea,0x50,0xab,0x13,0x63,0xdf,0xea,0x14,0x38,0xb5,0x07,0x42,0x81,0x2f,0xda,
139 0xe9,0x24,0x02,0x7e,0xaf,0xef,0x74,0x09,0x0e,0x80,0xfa,0xfb,0xd1,0x19,0x41,
140 0xe5,0xba,0x0f,0x7c,0x0a,0xa4,0x15,0x55,0xa2,0x58,0x8c,0x3a,0x48,0x2c,0xc6,
141 0xde,0x4a,0x76,0xfb,0x72,0xb6,0x61,0xe6,0xd2,0x10,0x44,0x4c,0x33,0xb8,0xd2,
142 0x74,0xb1,0x9d,0x3b,0xcd,0x2f,0xb1,0x4f,0xc3,0x98,0xbd,0x83,0xb7,0x7e,0x75,
143 0xe8,0xa7,0x6a,0xee,0xcc,0x51,0x8c,0x99,0x17,0x67,0x7f,0x27,0xf9,0x0d,0x6a,
144 0xb7,0xd4,0x80,0x17,0x89,0x39,0x9c,0xf3,0xd7,0x0f,0xdf,0xb0,0x55,0x80,0x1d,
145 0xaf,0x57,0x2e,0xd0,0xf0,0x4f,0x42,0x69,0x55,0xbc,0x83,0xd6,0x97,0x83,0x7a,
146 0xe6,0xc6,0x30,0x6d,0x3d,0xb5,0x21,0xa7,0xc4,0x62,0x0a,0x20,0xce,0x5e,0x5a,
147 0x17,0x98,0xb3,0x6f,0x6b,0x9a,0xeb,0x6b,0xa3,0xc4,0x75,0xd8,0x2b,0xdc,0x5c,
148 0x6f,0xec,0x5d,0x49,0xac,0xa8,0xa4,0x2f,0xb8,0x8c,0x4f,0x2e,0x46,0x21,0xee,
149 0x72,0x6a,0x0e,0x22,0x80,0x71,0xc8,0x76,0x40,0x44,0x61,0x16,0xbf,0xa5,0xf8,
150 0x89,0xc7,0xe9,0x87,0xdf,0xbd,0x2e,0x4b,0x4e,0xc2,0x97,0x53,0xe9,0x49,0x1c,
151 0x05,0xb0,0x0b,0x9b,0x9f,0x21,0x19,0x41,0xe9,0xf5,0x61,0xd7,0x33,0x2e,0x2c,
152 0x94,0xb8,0xa8,0x9a,0x3a,0xcc,0x6a,0x24,0x8d,0x19,0x13,0xee,0xb9,0xb0,0x48,
156 /* helper function */
157 static BIGNUM *bn_load_new(const unsigned char *data, int sz)
159 BIGNUM *ret = BN_new();
161 BN_bin2bn(data, sz, ret);
165 /* helper function */
166 static BIGNUM *bn_load(BN_CTX *ctx, const unsigned char *data, int sz)
168 BIGNUM *ret = BN_CTX_get(ctx);
170 BN_bin2bn(data, sz, ret);
174 static int test_check_public_exponent(void)
179 ret = TEST_ptr(e = BN_new())
181 && TEST_true(BN_set_word(e, 65535))
182 && TEST_false(rsa_check_public_exponent(e))
183 /* e is even will fail */
184 && TEST_true(BN_set_word(e, 65536))
185 && TEST_false(rsa_check_public_exponent(e))
187 && TEST_true(BN_set_word(e, 65537))
188 && TEST_true(rsa_check_public_exponent(e))
189 /* e = 2^256 is too big */
190 && TEST_true(BN_lshift(e, BN_value_one(), 256))
191 && TEST_false(rsa_check_public_exponent(e))
192 /* e = 2^256-1 is odd and in range */
193 && TEST_true(BN_sub(e, e, BN_value_one()))
194 && TEST_true(rsa_check_public_exponent(e));
199 static int test_check_prime_factor_range(void)
204 BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL, *bn_p4 = NULL;
205 /* Some range checks that are larger than 32 bits */
206 static const unsigned char p1[] = { 0x0B, 0x50, 0x4F, 0x33, 0x3F };
207 static const unsigned char p2[] = { 0x10, 0x00, 0x00, 0x00, 0x00 };
208 static const unsigned char p3[] = { 0x0B, 0x50, 0x4F, 0x33, 0x40 };
209 static const unsigned char p4[] = { 0x0F, 0xFF, 0xFF, 0xFF, 0xFF };
211 /* (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
212 * For 8 bits: 0xB.504F <= p <= 0xF
213 * for 72 bits: 0xB504F333F. <= p <= 0xF_FFFF_FFFF
215 ret = TEST_ptr(p = BN_new())
216 && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
217 && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
218 && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
219 && TEST_ptr(bn_p4 = bn_load_new(p4, sizeof(p4)))
220 && TEST_ptr(ctx = BN_CTX_new())
221 && TEST_true(BN_set_word(p, 0xA))
222 && TEST_false(rsa_check_prime_factor_range(p, 8, ctx))
223 && TEST_true(BN_set_word(p, 0x10))
224 && TEST_false(rsa_check_prime_factor_range(p, 8, ctx))
225 && TEST_true(BN_set_word(p, 0xB))
226 && TEST_true(rsa_check_prime_factor_range(p, 8, ctx))
227 && TEST_true(BN_set_word(p, 0xF))
228 && TEST_true(rsa_check_prime_factor_range(p, 8, ctx))
229 && TEST_false(rsa_check_prime_factor_range(bn_p1, 72, ctx))
230 && TEST_false(rsa_check_prime_factor_range(bn_p2, 72, ctx))
231 && TEST_true(rsa_check_prime_factor_range(bn_p3, 72, ctx))
232 && TEST_true(rsa_check_prime_factor_range(bn_p4, 72, ctx));
243 static int test_check_prime_factor(void)
247 BIGNUM *p = NULL, *e = NULL;
248 BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
250 /* Some range checks that are larger than 32 bits */
251 static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 };
252 static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 };
253 static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 };
255 ret = TEST_ptr(p = BN_new())
256 && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
257 && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
258 && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
259 && TEST_ptr(e = BN_new())
260 && TEST_ptr(ctx = BN_CTX_new())
261 /* Fails the prime test */
262 && TEST_true(BN_set_word(e, 0x1))
263 && TEST_false(rsa_check_prime_factor(bn_p1, e, 72, ctx))
264 /* p is prime and in range and gcd(p-1, e) = 1 */
265 && TEST_true(rsa_check_prime_factor(bn_p2, e, 72, ctx))
266 /* gcd(p-1,e) = 1 test fails */
267 && TEST_true(BN_set_word(e, 0x2))
268 && TEST_false(rsa_check_prime_factor(p, e, 72, ctx))
269 /* p fails the range check */
270 && TEST_true(BN_set_word(e, 0x1))
271 && TEST_false(rsa_check_prime_factor(bn_p3, e, 72, ctx));
282 static int test_check_private_exponent(void)
287 BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;
289 ret = TEST_ptr(key = RSA_new())
290 && TEST_ptr(ctx = BN_CTX_new())
291 && TEST_ptr(p = BN_new())
292 && TEST_ptr(q = BN_new())
293 /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
294 && TEST_true(BN_set_word(p, 15))
295 && TEST_true(BN_set_word(q, 17))
296 && TEST_true(RSA_set0_factors(key, p, q));
303 ret = TEST_ptr(e = BN_new())
304 && TEST_ptr(d = BN_new())
305 && TEST_ptr(n = BN_new())
306 && TEST_true(BN_set_word(e, 5))
307 && TEST_true(BN_set_word(d, 157))
308 && TEST_true(BN_set_word(n, 15*17))
309 && TEST_true(RSA_set0_key(key, n, e, d));
316 /* fails since d >= lcm(p-1, q-1) */
317 ret = TEST_false(rsa_check_private_exponent(key, 8, ctx))
318 && TEST_true(BN_set_word(d, 45))
319 /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
320 && TEST_true(rsa_check_private_exponent(key, 8, ctx))
321 /* d is too small compared to nbits */
322 && TEST_false(rsa_check_private_exponent(key, 16, ctx))
323 /* d is too small compared to nbits */
324 && TEST_true(BN_set_word(d, 16))
325 && TEST_false(rsa_check_private_exponent(key, 8, ctx))
326 /* fail if 1 != e.d mod lcm(p-1, q-1) */
327 && TEST_true(BN_set_word(d, 46))
328 && TEST_false(rsa_check_private_exponent(key, 8, ctx));
335 static int test_check_crt_components(void)
348 BIGNUM *p = NULL, *q = NULL, *e = NULL;
350 ret = TEST_ptr(key = RSA_new())
351 && TEST_ptr(ctx = BN_CTX_new())
352 && TEST_ptr(p = BN_new())
353 && TEST_ptr(q = BN_new())
354 && TEST_ptr(e = BN_new())
355 && TEST_true(BN_set_word(p, P))
356 && TEST_true(BN_set_word(q, Q))
357 && TEST_true(BN_set_word(e, E))
358 && TEST_true(RSA_set0_factors(key, p, q));
364 ret = TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
365 && TEST_BN_eq_word(key->n, N)
366 && TEST_BN_eq_word(key->dmp1, DP)
367 && TEST_BN_eq_word(key->dmq1, DQ)
368 && TEST_BN_eq_word(key->iqmp, QINV)
369 && TEST_true(rsa_check_crt_components(key, ctx))
370 /* (a) 1 < dP < (p – 1). */
371 && TEST_true(BN_set_word(key->dmp1, 1))
372 && TEST_false(rsa_check_crt_components(key, ctx))
373 && TEST_true(BN_set_word(key->dmp1, P-1))
374 && TEST_false(rsa_check_crt_components(key, ctx))
375 && TEST_true(BN_set_word(key->dmp1, DP))
376 /* (b) 1 < dQ < (q - 1). */
377 && TEST_true(BN_set_word(key->dmq1, 1))
378 && TEST_false(rsa_check_crt_components(key, ctx))
379 && TEST_true(BN_set_word(key->dmq1, Q-1))
380 && TEST_false(rsa_check_crt_components(key, ctx))
381 && TEST_true(BN_set_word(key->dmq1, DQ))
382 /* (c) 1 < qInv < p */
383 && TEST_true(BN_set_word(key->iqmp, 1))
384 && TEST_false(rsa_check_crt_components(key, ctx))
385 && TEST_true(BN_set_word(key->iqmp, P))
386 && TEST_false(rsa_check_crt_components(key, ctx))
387 && TEST_true(BN_set_word(key->iqmp, QINV))
388 /* (d) 1 = (dP . e) mod (p - 1)*/
389 && TEST_true(BN_set_word(key->dmp1, DP+1))
390 && TEST_false(rsa_check_crt_components(key, ctx))
391 && TEST_true(BN_set_word(key->dmp1, DP))
392 /* (e) 1 = (dQ . e) mod (q - 1) */
393 && TEST_true(BN_set_word(key->dmq1, DQ-1))
394 && TEST_false(rsa_check_crt_components(key, ctx))
395 && TEST_true(BN_set_word(key->dmq1, DQ))
396 /* (f) 1 = (qInv . q) mod p */
397 && TEST_true(BN_set_word(key->iqmp, QINV+1))
398 && TEST_false(rsa_check_crt_components(key, ctx))
399 && TEST_true(BN_set_word(key->iqmp, QINV))
400 /* check defaults are still valid */
401 && TEST_true(rsa_check_crt_components(key, ctx));
409 static int test_pq_diff(void)
412 BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
414 ret = TEST_ptr(tmp = BN_new())
415 && TEST_ptr(p = BN_new())
416 && TEST_ptr(q = BN_new())
417 /* |1-(2+1)| > 2^1 */
418 && TEST_true(BN_set_word(p, 1))
419 && TEST_true(BN_set_word(q, 1+2))
420 && TEST_false(rsa_check_pminusq_diff(tmp, p, q, 202))
421 /* Check |p - q| > 2^(nbits/2 - 100) */
422 && TEST_true(BN_set_word(q, 1+3))
423 && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202))
424 && TEST_true(BN_set_word(p, 1+3))
425 && TEST_true(BN_set_word(q, 1))
426 && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202));
433 static int test_invalid_keypair(void)
438 BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
440 ret = TEST_ptr(key = RSA_new())
441 && TEST_ptr(ctx = BN_CTX_new())
442 /* NULL parameters */
443 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
445 && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
446 && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
447 && TEST_true(RSA_set0_factors(key, p, q));
454 ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
455 && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
456 && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
457 && TEST_true(RSA_set0_key(key, n, e, d));
464 /* bad strength/key size */
465 ret = TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 100, 2048))
466 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 112, 1024))
467 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 128, 2048))
468 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, 140, 3072))
469 /* mismatching exponent */
470 && TEST_false(rsa_sp800_56b_check_keypair(key, BN_value_one(), -1,
473 && TEST_true(BN_add_word(e, 1))
474 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1,
476 && TEST_true(BN_sub_word(e, 1))
478 /* mismatch between bits and modulus */
479 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
480 && TEST_true(rsa_sp800_56b_check_keypair(key, e, 112, 2048))
481 /* check n == pq failure */
482 && TEST_true(BN_add_word(n, 1))
483 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
484 && TEST_true(BN_sub_word(n, 1))
486 && TEST_true(BN_sub_word(p, 2))
487 && TEST_true(BN_mul(n, p, q, ctx))
488 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
489 && TEST_true(BN_add_word(p, 2))
490 && TEST_true(BN_mul(n, p, q, ctx))
492 && TEST_true(BN_sub_word(q, 2))
493 && TEST_true(BN_mul(n, p, q, ctx))
494 && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
495 && TEST_true(BN_add_word(q, 2))
496 && TEST_true(BN_mul(n, p, q, ctx));
503 static int test_fips1864_keygen_kat(void)
508 BIGNUM *e, *Xp, *Xp1, *Xp2, *Xq, *Xq1, *Xq2;
509 BIGNUM *p1, *p2, *q1, *q2;
510 BIGNUM *p1_exp, *p2_exp, *q1_exp, *q2_exp;
511 BIGNUM *p_exp, *q_exp, *n_exp, *d_exp;
512 const BIGNUM *p, *q, *n, *d, *e2;
514 if (!(TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new())))
518 e = bn_load(ctx, cav_e, sizeof(cav_e));
519 Xp = bn_load(ctx, cav_Xp, sizeof(cav_Xp));
520 Xp1 = bn_load(ctx, cav_Xp1, sizeof(cav_Xp1));
521 Xp2 = bn_load(ctx, cav_Xp2, sizeof(cav_Xp2));
522 Xq = bn_load(ctx, cav_Xq, sizeof(cav_Xq));
523 Xq1 = bn_load(ctx, cav_Xq1, sizeof(cav_Xq1));
524 Xq2 = bn_load(ctx, cav_Xq2, sizeof(cav_Xq2));
525 p1_exp = bn_load(ctx, cav_p1, sizeof(cav_p1));
526 p2_exp = bn_load(ctx, cav_p2, sizeof(cav_p2));
527 q1_exp = bn_load(ctx, cav_q1, sizeof(cav_q1));
528 q2_exp = bn_load(ctx, cav_q2, sizeof(cav_q2));
529 p_exp = bn_load(ctx, cav_p, sizeof(cav_p));
530 q_exp = bn_load(ctx, cav_q, sizeof(cav_q));
531 n_exp = bn_load(ctx, cav_n, sizeof(cav_n));
532 d_exp = bn_load(ctx, cav_d, sizeof(cav_d));
533 p1 = BN_CTX_get(ctx);
534 p2 = BN_CTX_get(ctx);
535 q1 = BN_CTX_get(ctx);
536 q2 = BN_CTX_get(ctx);
538 && TEST_true(rsa_fips186_4_gen_prob_primes(key, p1, p2, NULL, Xp, Xp1,
539 Xp2, q1, q2, NULL, Xq, Xq1,
540 Xq2, 2048, e, ctx, NULL))
541 && TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 2048, e, ctx))
542 && TEST_BN_eq(p1_exp, p1)
543 && TEST_BN_eq(p2_exp, p2)
544 && TEST_BN_eq(q1_exp, q1)
545 && TEST_BN_eq(q2_exp, q2);
549 RSA_get0_key(key, &n, &e2, &d);
550 RSA_get0_factors(key, &p, &q);
551 ret = TEST_BN_eq(e, e2)
552 && TEST_BN_eq(p_exp, p)
553 && TEST_BN_eq(q_exp, q)
554 && TEST_BN_eq(n_exp, n)
555 && TEST_BN_eq(d_exp, d);
564 static int keygen_size[] =
569 static int test_sp80056b_keygen(int id)
573 int sz = keygen_size[id];
575 ret = TEST_ptr(key = RSA_new())
576 && TEST_true(rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
577 && TEST_true(rsa_sp800_56b_check_public(key))
578 && TEST_true(rsa_sp800_56b_check_private(key))
579 && TEST_true(rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
585 static int test_check_private_key(void)
588 BIGNUM *n = NULL, *d = NULL, *e = NULL;
591 ret = TEST_ptr(key = RSA_new())
592 /* check NULL pointers fail */
593 && TEST_false(rsa_sp800_56b_check_private(key))
594 /* load private key */
595 && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
596 && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
597 && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
598 && TEST_true(RSA_set0_key(key, n, e, d));
605 /* check d is in range */
606 ret = TEST_true(rsa_sp800_56b_check_private(key))
607 /* check d is too low */
608 && TEST_true(BN_set_word(d, 0))
609 && TEST_false(rsa_sp800_56b_check_private(key))
610 /* check d is too high */
611 && TEST_ptr(BN_copy(d, n))
612 && TEST_false(rsa_sp800_56b_check_private(key));
618 static int test_check_public_key(void)
621 BIGNUM *n = NULL, *e = NULL;
624 ret = TEST_ptr(key = RSA_new())
625 /* check NULL pointers fail */
626 && TEST_false(rsa_sp800_56b_check_public(key))
627 /* load public key */
628 && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
629 && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
630 && TEST_true(RSA_set0_key(key, n, e, NULL));
636 /* check public key is valid */
637 ret = TEST_true(rsa_sp800_56b_check_public(key))
638 /* check fail if n is even */
639 && TEST_true(BN_add_word(n, 1))
640 && TEST_false(rsa_sp800_56b_check_public(key))
641 && TEST_true(BN_sub_word(n, 1))
642 /* check fail if n is wrong number of bits */
643 && TEST_true(BN_lshift1(n, n))
644 && TEST_false(rsa_sp800_56b_check_public(key))
645 && TEST_true(BN_rshift1(n, n))
646 /* test odd exponent fails */
647 && TEST_true(BN_add_word(e, 1))
648 && TEST_false(rsa_sp800_56b_check_public(key))
649 && TEST_true(BN_sub_word(e, 1))
650 /* modulus fails composite check */
651 && TEST_true(BN_add_word(n, 2))
652 && TEST_false(rsa_sp800_56b_check_public(key));
658 int setup_tests(void)
660 ADD_TEST(test_check_public_exponent);
661 ADD_TEST(test_check_prime_factor_range);
662 ADD_TEST(test_check_prime_factor);
663 ADD_TEST(test_check_private_exponent);
664 ADD_TEST(test_check_crt_components);
665 ADD_TEST(test_check_private_key);
666 ADD_TEST(test_check_public_key);
667 ADD_TEST(test_invalid_keypair);
668 ADD_TEST(test_pq_diff);
669 ADD_TEST(test_fips1864_keygen_kat);
670 ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));