test/params_test.c: Add API test case, and mixed methods
[openssl.git] / test / rsa_sp800_56b_test.c
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
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "internal/nelem.h"
14
15 #include <openssl/crypto.h>
16 #include <openssl/err.h>
17 #include <openssl/rand.h>
18 #include <openssl/bn.h>
19
20 #include "testutil.h"
21
22 #ifdef OPENSSL_NO_RSA
23 int setup_tests(void)
24 {
25     /* No tests */
26     return 1;
27 }
28 #else
29 # include "rsa_locl.h"
30 # include <openssl/rsa.h>
31
32 /* taken from RSA2 cavs data */
33 static const unsigned char cav_e[] = {
34     0x01,0x00,0x01
35 };
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
46 };
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
50 };
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
54 };
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
65 };
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,
69     0x61,0xd6,0x5a,0xe1
70 };
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
74 };
75 /* expected values */
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
79 };
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
83 };
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,
87     0x61,0xd6,0x5d,0x47
88 };
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
92 };
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
103 };
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
114 };
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,
133     0x13
134 };
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,
153     0x61
154 };
155
156 /* helper function */
157 static BIGNUM *bn_load_new(const unsigned char *data, int sz)
158 {
159     BIGNUM *ret = BN_new();
160     if (ret != NULL)
161         BN_bin2bn(data, sz, ret);
162     return ret;
163 }
164
165 /* helper function */
166 static BIGNUM *bn_load(BN_CTX *ctx, const unsigned char *data, int sz)
167 {
168     BIGNUM *ret = BN_CTX_get(ctx);
169     if (ret != NULL)
170         BN_bin2bn(data, sz, ret);
171     return ret;
172 }
173
174 static int test_check_public_exponent(void)
175 {
176     int ret = 0;
177     BIGNUM *e = NULL;
178
179     ret = TEST_ptr(e = BN_new())
180           /* e is too small */
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))
186           /* e is ok */
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));
195     BN_free(e);
196     return ret;
197 }
198
199 static int test_check_prime_factor_range(void)
200 {
201     int ret = 0;
202     BN_CTX *ctx = NULL;
203     BIGNUM *p = NULL;
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 };
210
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
214      */
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));
233
234     BN_free(bn_p4);
235     BN_free(bn_p3);
236     BN_free(bn_p2);
237     BN_free(bn_p1);
238     BN_free(p);
239     BN_CTX_free(ctx);
240     return ret;
241 }
242
243 static int test_check_prime_factor(void)
244 {
245     int ret = 0;
246     BN_CTX *ctx = NULL;
247     BIGNUM *p = NULL, *e = NULL;
248     BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
249
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 };
254
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));
272
273     BN_free(bn_p3);
274     BN_free(bn_p2);
275     BN_free(bn_p1);
276     BN_free(e);
277     BN_free(p);
278     BN_CTX_free(ctx);
279     return ret;
280 }
281
282 static int test_check_private_exponent(void)
283 {
284     int ret = 0;
285     RSA *key = NULL;
286     BN_CTX *ctx = NULL;
287     BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;
288
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           && TEST_ptr(e = BN_new())
294           && TEST_ptr(d = BN_new())
295           && TEST_ptr(n = BN_new())
296           /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
297           && TEST_true(BN_set_word(p, 15))
298           && TEST_true(BN_set_word(q, 17))
299           && TEST_true(BN_set_word(e, 5))
300           && TEST_true(BN_set_word(d, 157))
301           && TEST_true(BN_set_word(n, 15*17))
302           && TEST_true(RSA_set0_factors(key, p, q))
303           && TEST_true(RSA_set0_key(key, n, e, d))
304           /* fails since d >= lcm(p-1, q-1) */
305           && TEST_false(rsa_check_private_exponent(key, 8, ctx))
306           && TEST_true(BN_set_word(d, 45))
307           /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
308           && TEST_true(rsa_check_private_exponent(key, 8, ctx))
309           /* d is too small compared to nbits */
310           && TEST_false(rsa_check_private_exponent(key, 16, ctx))
311           /* d is too small compared to nbits */
312           && TEST_true(BN_set_word(d, 16))
313           && TEST_false(rsa_check_private_exponent(key, 8, ctx))
314           /* fail if 1 != e.d mod lcm(p-1, q-1) */
315           && TEST_true(BN_set_word(d, 46))
316           && TEST_false(rsa_check_private_exponent(key, 8, ctx));
317
318     RSA_free(key);
319     BN_CTX_free(ctx);
320     return ret;
321 }
322
323 static int test_check_crt_components(void)
324 {
325     const int P = 15;
326     const int Q = 17;
327     const int E = 5;
328     const int N = P*Q;
329     const int DP = 3;
330     const int DQ = 13;
331     const int QINV = 8;
332
333     int ret = 0;
334     RSA *key = NULL;
335     BN_CTX *ctx = NULL;
336     BIGNUM *p = NULL, *q = NULL, *e = NULL;
337
338     ret = TEST_ptr(key = RSA_new())
339           && TEST_ptr(ctx = BN_CTX_new())
340           && TEST_ptr(p = BN_new())
341           && TEST_ptr(q = BN_new())
342           && TEST_ptr(e = BN_new())
343           && TEST_true(BN_set_word(p, P))
344           && TEST_true(BN_set_word(q, Q))
345           && TEST_true(BN_set_word(e, E))
346           && TEST_true(RSA_set0_factors(key, p, q))
347           && TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
348           && TEST_BN_eq_word(key->n, N)
349           && TEST_BN_eq_word(key->dmp1, DP)
350           && TEST_BN_eq_word(key->dmq1, DQ)
351           && TEST_BN_eq_word(key->iqmp, QINV)
352           && TEST_true(rsa_check_crt_components(key, ctx))
353           /* (a) 1 < dP < (p – 1). */
354           && TEST_true(BN_set_word(key->dmp1, 1))
355           && TEST_false(rsa_check_crt_components(key, ctx))
356           && TEST_true(BN_set_word(key->dmp1, P-1))
357           && TEST_false(rsa_check_crt_components(key, ctx))
358           && TEST_true(BN_set_word(key->dmp1, DP))
359           /* (b) 1 < dQ < (q - 1). */
360           && TEST_true(BN_set_word(key->dmq1, 1))
361           && TEST_false(rsa_check_crt_components(key, ctx))
362           && TEST_true(BN_set_word(key->dmq1, Q-1))
363           && TEST_false(rsa_check_crt_components(key, ctx))
364           && TEST_true(BN_set_word(key->dmq1, DQ))
365           /* (c) 1 < qInv < p */
366           && TEST_true(BN_set_word(key->iqmp, 1))
367           && TEST_false(rsa_check_crt_components(key, ctx))
368           && TEST_true(BN_set_word(key->iqmp, P))
369           && TEST_false(rsa_check_crt_components(key, ctx))
370           && TEST_true(BN_set_word(key->iqmp, QINV))
371           /* (d) 1 = (dP . e) mod (p - 1)*/
372           && TEST_true(BN_set_word(key->dmp1, DP+1))
373           && TEST_false(rsa_check_crt_components(key, ctx))
374           && TEST_true(BN_set_word(key->dmp1, DP))
375           /* (e) 1 = (dQ . e) mod (q - 1) */
376           && TEST_true(BN_set_word(key->dmq1, DQ-1))
377           && TEST_false(rsa_check_crt_components(key, ctx))
378           && TEST_true(BN_set_word(key->dmq1, DQ))
379           /* (f) 1 = (qInv . q) mod p */
380           && TEST_true(BN_set_word(key->iqmp, QINV+1))
381           && TEST_false(rsa_check_crt_components(key, ctx))
382           && TEST_true(BN_set_word(key->iqmp, QINV))
383           /* check defaults are still valid */
384           && TEST_true(rsa_check_crt_components(key, ctx));
385
386     BN_free(e);
387     RSA_free(key);
388     BN_CTX_free(ctx);
389     return ret;
390 }
391
392 static int test_pq_diff(void)
393 {
394     int ret = 0;
395     BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
396
397     ret = TEST_ptr(tmp = BN_new())
398           && TEST_ptr(p = BN_new())
399           && TEST_ptr(q = BN_new())
400           /* |1-(2+1)| > 2^1 */
401           && TEST_true(BN_set_word(p, 1))
402           && TEST_true(BN_set_word(q, 1+2))
403           && TEST_false(rsa_check_pminusq_diff(tmp, p, q, 202))
404           /* Check |p - q| > 2^(nbits/2 - 100) */
405           && TEST_true(BN_set_word(q, 1+3))
406           && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202))
407           && TEST_true(BN_set_word(p, 1+3))
408           && TEST_true(BN_set_word(q, 1))
409           && TEST_true(rsa_check_pminusq_diff(tmp, p, q, 202));
410     BN_free(p);
411     BN_free(q);
412     BN_free(tmp);
413     return ret;
414 }
415
416 static int test_invalid_keypair(void)
417 {
418     int ret = 0;
419     RSA *key = NULL;
420     BN_CTX *ctx = NULL;
421     BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
422
423     ret = TEST_ptr(key = RSA_new())
424           && TEST_ptr(ctx = BN_CTX_new())
425           /* NULL parameters */
426           && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
427           /* load key */
428           && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
429           && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
430           && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
431           && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
432           && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
433           && TEST_true(RSA_set0_key(key, n, e, d))
434           && TEST_true(RSA_set0_factors(key, p, q))
435
436           /* bad strength/key size */
437           && 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
450           /* mismatch between bits and modulus */
451           && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
452           && TEST_true(rsa_sp800_56b_check_keypair(key, e, 112, 2048))
453           /* check n == pq failure */
454           && TEST_true(BN_add_word(n, 1))
455           && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
456           && TEST_true(BN_sub_word(n, 1))
457           /* check p  */
458           && TEST_true(BN_sub_word(p, 2))
459           && TEST_true(BN_mul(n, p, q, ctx))
460           && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
461           && TEST_true(BN_add_word(p, 2))
462           && TEST_true(BN_mul(n, p, q, ctx))
463           /* check q  */
464           && TEST_true(BN_sub_word(q, 2))
465           && TEST_true(BN_mul(n, p, q, ctx))
466           && TEST_false(rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
467           && TEST_true(BN_add_word(q, 2))
468           && TEST_true(BN_mul(n, p, q, ctx));
469
470     RSA_free(key);
471     BN_CTX_free(ctx);
472     return ret;
473 }
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
486     if (!(TEST_ptr(key = RSA_new()) && TEST_ptr(ctx = BN_CTX_new())))
487         goto err;
488     BN_CTX_start(ctx);
489
490     e = bn_load(ctx, cav_e, sizeof(cav_e));
491     Xp = bn_load(ctx, cav_Xp, sizeof(cav_Xp));
492     Xp1 = bn_load(ctx, cav_Xp1, sizeof(cav_Xp1));
493     Xp2 = bn_load(ctx, cav_Xp2, sizeof(cav_Xp2));
494     Xq = bn_load(ctx, cav_Xq, sizeof(cav_Xq));
495     Xq1 = bn_load(ctx, cav_Xq1, sizeof(cav_Xq1));
496     Xq2 = bn_load(ctx, cav_Xq2, sizeof(cav_Xq2));
497     p1_exp = bn_load(ctx, cav_p1, sizeof(cav_p1));
498     p2_exp = bn_load(ctx, cav_p2, sizeof(cav_p2));
499     q1_exp = bn_load(ctx, cav_q1, sizeof(cav_q1));
500     q2_exp = bn_load(ctx, cav_q2, sizeof(cav_q2));
501     p_exp = bn_load(ctx, cav_p, sizeof(cav_p));
502     q_exp = bn_load(ctx, cav_q, sizeof(cav_q));
503     n_exp = bn_load(ctx, cav_n, sizeof(cav_n));
504     d_exp = bn_load(ctx, cav_d, sizeof(cav_d));
505     p1 = BN_CTX_get(ctx);
506     p2 = BN_CTX_get(ctx);
507     q1 = BN_CTX_get(ctx);
508     q2 = BN_CTX_get(ctx);
509     ret = TEST_ptr(q2)
510           && TEST_true(rsa_fips186_4_gen_prob_primes(key, p1, p2, NULL, Xp, Xp1,
511                                                      Xp2, q1, q2, NULL, Xq, Xq1,
512                                                      Xq2, 2048, e, ctx, NULL))
513           && TEST_true(rsa_sp800_56b_derive_params_from_pq(key, 2048, e, ctx))
514           && TEST_BN_eq(p1_exp, p1)
515           && TEST_BN_eq(p2_exp, p2)
516           && TEST_BN_eq(q1_exp, q1)
517           && TEST_BN_eq(q2_exp, q2);
518     if (!ret)
519         goto err;
520
521     RSA_get0_key(key, &n, &e2, &d);
522     RSA_get0_factors(key, &p, &q);
523     ret = TEST_BN_eq(e, e2)
524           && TEST_BN_eq(p_exp, p)
525           && TEST_BN_eq(q_exp, q)
526           && TEST_BN_eq(n_exp, n)
527           && TEST_BN_eq(d_exp, d);
528 err:
529     RSA_free(key);
530     BN_CTX_end(ctx);
531     BN_CTX_free(ctx);
532     return ret;
533 }
534
535
536 static int keygen_size[] =
537 {
538     2048, 3072
539 };
540
541 static int test_sp80056b_keygen(int id)
542 {
543     RSA *key = NULL;
544     int ret;
545     int sz = keygen_size[id];
546
547     ret = TEST_ptr(key = RSA_new())
548           && TEST_true(rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
549           && TEST_true(rsa_sp800_56b_check_public(key))
550           && TEST_true(rsa_sp800_56b_check_private(key))
551           && TEST_true(rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
552
553     RSA_free(key);
554     return ret;
555 }
556
557 static int test_check_private_key(void)
558 {
559     int ret = 0;
560     BIGNUM *n = NULL, *d = NULL, *e = NULL;
561     RSA *key = NULL;
562
563     ret = TEST_ptr(key = RSA_new())
564           /* check NULL pointers fail */
565           && TEST_false(rsa_sp800_56b_check_private(key))
566           /* load private key */
567           && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
568           && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
569           && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
570           && TEST_true(RSA_set0_key(key, n, e, d))
571           /* check d is in range */
572           && TEST_true(rsa_sp800_56b_check_private(key))
573           /* check d is too low */
574           && TEST_true(BN_set_word(d, 0))
575           && TEST_false(rsa_sp800_56b_check_private(key))
576           /* check d is too high */
577           && TEST_ptr(BN_copy(d, n))
578           && TEST_false(rsa_sp800_56b_check_private(key));
579
580     RSA_free(key);
581     return ret;
582 }
583
584 static int test_check_public_key(void)
585 {
586     int ret = 0;
587     BIGNUM *n = NULL, *e = NULL;
588     RSA *key = NULL;
589
590     ret = TEST_ptr(key = RSA_new())
591           /* check NULL pointers fail */
592           && TEST_false(rsa_sp800_56b_check_public(key))
593           /* load public key */
594           && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
595           && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
596           && TEST_true(RSA_set0_key(key, n, e, NULL))
597           /* check public key is valid */
598           && TEST_true(rsa_sp800_56b_check_public(key))
599           /* check fail if n is even */
600           && TEST_true(BN_add_word(n, 1))
601           && TEST_false(rsa_sp800_56b_check_public(key))
602           && TEST_true(BN_sub_word(n, 1))
603           /* check fail if n is wrong number of bits */
604           && TEST_true(BN_lshift1(n, n))
605           && TEST_false(rsa_sp800_56b_check_public(key))
606           && TEST_true(BN_rshift1(n, n))
607           /* test odd exponent fails */
608           && TEST_true(BN_add_word(e, 1))
609           && TEST_false(rsa_sp800_56b_check_public(key))
610           && TEST_true(BN_sub_word(e, 1))
611           /* modulus fails composite check */
612           && TEST_true(BN_add_word(n, 2))
613           && TEST_false(rsa_sp800_56b_check_public(key));
614
615     RSA_free(key);
616     return ret;
617 }
618
619 int setup_tests(void)
620 {
621     ADD_TEST(test_check_public_exponent);
622     ADD_TEST(test_check_prime_factor_range);
623     ADD_TEST(test_check_prime_factor);
624     ADD_TEST(test_check_private_exponent);
625     ADD_TEST(test_check_crt_components);
626     ADD_TEST(test_check_private_key);
627     ADD_TEST(test_check_public_key);
628     ADD_TEST(test_invalid_keypair);
629     ADD_TEST(test_pq_diff);
630     ADD_TEST(test_fips1864_keygen_kat);
631     ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));
632     return 1;
633 }
634 #endif