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