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/nelem.h"
19 #include "crypto/evp.h" /* For the internal API */
29 static void tear_down(FIXTURE *fixture)
31 if (fixture != NULL) {
32 OSSL_PROVIDER_unload(fixture->prov1);
33 OSSL_PROVIDER_unload(fixture->prov2);
34 OPENSSL_CTX_free(fixture->ctx1);
35 OPENSSL_CTX_free(fixture->ctx2);
36 OPENSSL_free(fixture);
40 static FIXTURE *set_up(const char *testcase_name)
44 if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))
45 || !TEST_ptr(fixture->ctx1 = OPENSSL_CTX_new())
46 || !TEST_ptr(fixture->prov1 = OSSL_PROVIDER_load(fixture->ctx1,
48 || !TEST_ptr(fixture->ctx2 = OPENSSL_CTX_new())
49 || !TEST_ptr(fixture->prov2 = OSSL_PROVIDER_load(fixture->ctx2,
57 static int test_pass_rsa(FIXTURE *fixture)
65 #define F3 5 /* Extra factor */
68 #define E3 8 /* Extra exponent */
70 #define C3 10 /* Extra coefficient */
75 BIGNUM *bn1 = NULL, *bn2 = NULL, *bn3 = NULL;
77 EVP_KEYMGMT *km1 = NULL, *km2 = NULL;
78 void *provdata = NULL;
80 * 32-bit RSA key, extracted from this command,
81 * executed with OpenSSL 1.0.2:
83 * openssl genrsa 32 | openssl rsa -text
85 static BN_ULONG expected[] = {
97 0 /* Extra, should remain zero */
99 static unsigned long keydata[OSSL_NELEM(expected)] = { 0, };
100 OSSL_PARAM params[] = {
101 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &keydata[N]),
102 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &keydata[E]),
103 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &keydata[D]),
104 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &keydata[P]),
105 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &keydata[Q]),
106 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR, &keydata[F3]),
107 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &keydata[DP]),
108 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &keydata[DQ]),
109 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT, &keydata[E3]),
110 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT, &keydata[QINV]),
111 OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT, &keydata[C3]),
115 if (!TEST_ptr(rsa = RSA_new()))
118 if (!TEST_ptr(bn1 = BN_new())
119 || !TEST_true(BN_set_word(bn1, expected[N]))
120 || !TEST_ptr(bn2 = BN_new())
121 || !TEST_true(BN_set_word(bn2, expected[E]))
122 || !TEST_ptr(bn3 = BN_new())
123 || !TEST_true(BN_set_word(bn3, expected[D]))
124 || !TEST_true(RSA_set0_key(rsa, bn1, bn2, bn3)))
127 if (!TEST_ptr(bn1 = BN_new())
128 || !TEST_true(BN_set_word(bn1, expected[P]))
129 || !TEST_ptr(bn2 = BN_new())
130 || !TEST_true(BN_set_word(bn2, expected[Q]))
131 || !TEST_true(RSA_set0_factors(rsa, bn1, bn2)))
134 if (!TEST_ptr(bn1 = BN_new())
135 || !TEST_true(BN_set_word(bn1, expected[DP]))
136 || !TEST_ptr(bn2 = BN_new())
137 || !TEST_true(BN_set_word(bn2, expected[DQ]))
138 || !TEST_ptr(bn3 = BN_new())
139 || !TEST_true(BN_set_word(bn3, expected[QINV]))
140 || !TEST_true(RSA_set0_crt_params(rsa, bn1, bn2, bn3)))
142 bn1 = bn2 = bn3 = NULL;
144 if (!TEST_ptr(pk = EVP_PKEY_new())
145 || !TEST_true(EVP_PKEY_assign_RSA(pk, rsa)))
149 if (!TEST_ptr(km1 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA", NULL))
150 || !TEST_ptr(km2 = EVP_KEYMGMT_fetch(fixture->ctx2, "RSA", NULL))
151 || !TEST_ptr_ne(km1, km2))
154 if (!TEST_ptr(evp_keymgmt_export_to_provider(pk, km1, 0))
155 || !TEST_ptr(provdata = evp_keymgmt_export_to_provider(pk, km2, 0)))
158 if (!TEST_true(evp_keymgmt_exportkey(km2, provdata, params)))
162 * At this point, the hope is that keydata will have all the numbers
166 for (i = 0; i < OSSL_NELEM(expected); i++)
167 ret += !! TEST_int_eq(expected[i], keydata[i]);
169 ret = (ret == OSSL_NELEM(expected));
177 EVP_KEYMGMT_free(km1);
178 EVP_KEYMGMT_free(km2);
183 static int (*tests[])(FIXTURE *) = {
187 static int test_pass_key(int n)
189 SETUP_TEST_FIXTURE(FIXTURE, set_up);
190 EXECUTE_TEST(tests[n], tear_down);
194 int setup_tests(void)
196 ADD_ALL_TESTS(test_pass_key, 1);