2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include <openssl/bio.h>
13 #include <openssl/bn.h>
14 #include <openssl/rsa.h>
15 #include <openssl/evp.h>
16 #include <openssl/provider.h>
17 #include <openssl/core_names.h>
18 #include "internal/core.h"
19 #include "internal/nelem.h"
20 #include "crypto/evp.h" /* For the internal API */
30 static void tear_down(FIXTURE
*fixture
)
32 if (fixture
!= NULL
) {
33 OSSL_PROVIDER_unload(fixture
->prov1
);
34 OSSL_PROVIDER_unload(fixture
->prov2
);
35 OPENSSL_CTX_free(fixture
->ctx1
);
36 OPENSSL_CTX_free(fixture
->ctx2
);
37 OPENSSL_free(fixture
);
41 static FIXTURE
*set_up(const char *testcase_name
)
45 if (!TEST_ptr(fixture
= OPENSSL_zalloc(sizeof(*fixture
)))
46 || !TEST_ptr(fixture
->ctx1
= OPENSSL_CTX_new())
47 || !TEST_ptr(fixture
->prov1
= OSSL_PROVIDER_load(fixture
->ctx1
,
49 || !TEST_ptr(fixture
->ctx2
= OPENSSL_CTX_new())
50 || !TEST_ptr(fixture
->prov2
= OSSL_PROVIDER_load(fixture
->ctx2
,
64 #define F3 5 /* Extra factor */
67 #define E3 8 /* Extra exponent */
69 #define C3 10 /* Extra coefficient */
72 * We have to do this because OSSL_PARAM_get_ulong() can't handle params
73 * holding data that isn't exactly sizeof(uint32_t) or sizeof(uint64_t),
74 * and because the other end deals with BIGNUM, the resulting param might
75 * be any size. In this particular test, we know that the expected data
76 * fits within an unsigned long, and we want to get the data in that form
77 * to make testing of values easier.
79 static int get_ulong_via_BN(const OSSL_PARAM
*p
, unsigned long *goal
)
82 int ret
= 1; /* Ever so hopeful */
84 if (!TEST_true(OSSL_PARAM_get_BN(p
, &n
))
85 || !TEST_true(BN_bn2nativepad(n
, (unsigned char *)goal
, sizeof(*goal
))))
91 static int export_cb(const OSSL_PARAM
*params
, void *arg
)
93 unsigned long *keydata
= arg
;
94 const OSSL_PARAM
*p
= NULL
;
98 int ret
= 1; /* Ever so hopeful */
103 if (!TEST_ptr(p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_N
))
104 || !TEST_true(get_ulong_via_BN(p
, &keydata
[N
]))
105 || !TEST_ptr(p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_E
))
106 || !TEST_true(get_ulong_via_BN(p
, &keydata
[E
]))
107 || !TEST_ptr(p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_D
))
108 || !TEST_true(get_ulong_via_BN(p
, &keydata
[D
])))
111 for (p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_FACTOR
),
113 p
!= NULL
&& factors_idx
<= F3
;
114 p
= OSSL_PARAM_locate_const(p
+ 1, OSSL_PKEY_PARAM_RSA_FACTOR
),
116 if (!TEST_true(get_ulong_via_BN(p
, &keydata
[factors_idx
])))
118 for (p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_EXPONENT
),
120 p
!= NULL
&& exponents_idx
<= E3
;
121 p
= OSSL_PARAM_locate_const(p
+ 1, OSSL_PKEY_PARAM_RSA_EXPONENT
),
123 if (!TEST_true(get_ulong_via_BN(p
, &keydata
[exponents_idx
])))
125 for (p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_COEFFICIENT
),
126 coefficients_idx
= QINV
;
127 p
!= NULL
&& coefficients_idx
<= C3
;
128 p
= OSSL_PARAM_locate_const(p
+ 1, OSSL_PKEY_PARAM_RSA_COEFFICIENT
),
130 if (!TEST_true(get_ulong_via_BN(p
, &keydata
[coefficients_idx
])))
133 if (!TEST_int_le(factors_idx
, F3
)
134 || !TEST_int_le(exponents_idx
, E3
)
135 || !TEST_int_le(coefficients_idx
, C3
))
140 static int test_pass_rsa(FIXTURE
*fixture
)
145 BIGNUM
*bn1
= NULL
, *bn2
= NULL
, *bn3
= NULL
;
147 EVP_KEYMGMT
*km1
= NULL
, *km2
= NULL
;
148 void *provdata
= NULL
;
150 * 32-bit RSA key, extracted from this command,
151 * executed with OpenSSL 1.0.2:
153 * openssl genrsa 32 | openssl rsa -text
155 static BN_ULONG expected
[] = {
167 0 /* Extra, should remain zero */
169 static unsigned long keydata
[OSSL_NELEM(expected
)] = { 0, };
171 if (!TEST_ptr(rsa
= RSA_new()))
174 if (!TEST_ptr(bn1
= BN_new())
175 || !TEST_true(BN_set_word(bn1
, expected
[N
]))
176 || !TEST_ptr(bn2
= BN_new())
177 || !TEST_true(BN_set_word(bn2
, expected
[E
]))
178 || !TEST_ptr(bn3
= BN_new())
179 || !TEST_true(BN_set_word(bn3
, expected
[D
]))
180 || !TEST_true(RSA_set0_key(rsa
, bn1
, bn2
, bn3
)))
183 if (!TEST_ptr(bn1
= BN_new())
184 || !TEST_true(BN_set_word(bn1
, expected
[P
]))
185 || !TEST_ptr(bn2
= BN_new())
186 || !TEST_true(BN_set_word(bn2
, expected
[Q
]))
187 || !TEST_true(RSA_set0_factors(rsa
, bn1
, bn2
)))
190 if (!TEST_ptr(bn1
= BN_new())
191 || !TEST_true(BN_set_word(bn1
, expected
[DP
]))
192 || !TEST_ptr(bn2
= BN_new())
193 || !TEST_true(BN_set_word(bn2
, expected
[DQ
]))
194 || !TEST_ptr(bn3
= BN_new())
195 || !TEST_true(BN_set_word(bn3
, expected
[QINV
]))
196 || !TEST_true(RSA_set0_crt_params(rsa
, bn1
, bn2
, bn3
)))
198 bn1
= bn2
= bn3
= NULL
;
200 if (!TEST_ptr(pk
= EVP_PKEY_new())
201 || !TEST_true(EVP_PKEY_assign_RSA(pk
, rsa
)))
205 if (!TEST_ptr(km1
= EVP_KEYMGMT_fetch(fixture
->ctx1
, "RSA", NULL
))
206 || !TEST_ptr(km2
= EVP_KEYMGMT_fetch(fixture
->ctx2
, "RSA", NULL
))
207 || !TEST_ptr_ne(km1
, km2
))
210 if (!TEST_ptr(evp_keymgmt_export_to_provider(pk
, km1
, 0))
211 || !TEST_ptr(provdata
= evp_keymgmt_export_to_provider(pk
, km2
, 0)))
214 if (!TEST_true(evp_keymgmt_exportkey(km2
, provdata
, &export_cb
, keydata
)))
218 * At this point, the hope is that keydata will have all the numbers
222 for (i
= 0; i
< OSSL_NELEM(expected
); i
++) {
223 int rv
= TEST_int_eq(expected
[i
], keydata
[i
]);
226 TEST_info("i = %zu", i
);
231 ret
= (ret
== OSSL_NELEM(expected
));
239 EVP_KEYMGMT_free(km1
);
240 EVP_KEYMGMT_free(km2
);
245 static int (*tests
[])(FIXTURE
*) = {
249 static int test_pass_key(int n
)
251 SETUP_TEST_FIXTURE(FIXTURE
, set_up
);
252 EXECUTE_TEST(tests
[n
], tear_down
);
256 int setup_tests(void)
258 ADD_ALL_TESTS(test_pass_key
, 1);