xref: /freebsd-src/crypto/openssl/crypto/rsa/rsa_acvp_test_params.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #include <string.h> /* memcpy */
11*b077aed3SPierre Pronchery #include <openssl/core_names.h>
12*b077aed3SPierre Pronchery #include <openssl/param_build.h>
13*b077aed3SPierre Pronchery #include "crypto/rsa.h"
14*b077aed3SPierre Pronchery #include "rsa_local.h"
15*b077aed3SPierre Pronchery 
ossl_rsa_acvp_test_gen_params_new(OSSL_PARAM ** dst,const OSSL_PARAM src[])16*b077aed3SPierre Pronchery int ossl_rsa_acvp_test_gen_params_new(OSSL_PARAM **dst, const OSSL_PARAM src[])
17*b077aed3SPierre Pronchery {
18*b077aed3SPierre Pronchery     const OSSL_PARAM *p, *s;
19*b077aed3SPierre Pronchery     OSSL_PARAM *d, *alloc = NULL;
20*b077aed3SPierre Pronchery     int ret = 1;
21*b077aed3SPierre Pronchery 
22*b077aed3SPierre Pronchery     static const OSSL_PARAM settable[] = {
23*b077aed3SPierre Pronchery         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XP, NULL, 0),
24*b077aed3SPierre Pronchery         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XP1, NULL, 0),
25*b077aed3SPierre Pronchery         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XP2, NULL, 0),
26*b077aed3SPierre Pronchery         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XQ, NULL, 0),
27*b077aed3SPierre Pronchery         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XQ1, NULL, 0),
28*b077aed3SPierre Pronchery         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_TEST_XQ2, NULL, 0),
29*b077aed3SPierre Pronchery         OSSL_PARAM_END
30*b077aed3SPierre Pronchery     };
31*b077aed3SPierre Pronchery 
32*b077aed3SPierre Pronchery     /* Assume the first element is a required field if this feature is used */
33*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(src, settable[0].key);
34*b077aed3SPierre Pronchery     if (p == NULL)
35*b077aed3SPierre Pronchery         return 1;
36*b077aed3SPierre Pronchery 
37*b077aed3SPierre Pronchery     /* Zeroing here means the terminator is always set at the end */
38*b077aed3SPierre Pronchery     alloc = OPENSSL_zalloc(sizeof(settable));
39*b077aed3SPierre Pronchery     if (alloc == NULL)
40*b077aed3SPierre Pronchery         return 0;
41*b077aed3SPierre Pronchery 
42*b077aed3SPierre Pronchery     d = alloc;
43*b077aed3SPierre Pronchery     for (s = settable; s->key != NULL; ++s) {
44*b077aed3SPierre Pronchery         /* If src contains a key from settable then copy the src to the dest */
45*b077aed3SPierre Pronchery         p = OSSL_PARAM_locate_const(src, s->key);
46*b077aed3SPierre Pronchery         if (p != NULL) {
47*b077aed3SPierre Pronchery             *d = *s; /* shallow copy from the static settable[] */
48*b077aed3SPierre Pronchery             d->data_size = p->data_size;
49*b077aed3SPierre Pronchery             d->data = OPENSSL_memdup(p->data, p->data_size);
50*b077aed3SPierre Pronchery             if (d->data == NULL)
51*b077aed3SPierre Pronchery                 ret = 0;
52*b077aed3SPierre Pronchery             ++d;
53*b077aed3SPierre Pronchery         }
54*b077aed3SPierre Pronchery     }
55*b077aed3SPierre Pronchery     if (ret == 0) {
56*b077aed3SPierre Pronchery         ossl_rsa_acvp_test_gen_params_free(alloc);
57*b077aed3SPierre Pronchery         alloc = NULL;
58*b077aed3SPierre Pronchery     }
59*b077aed3SPierre Pronchery     if (*dst != NULL)
60*b077aed3SPierre Pronchery         ossl_rsa_acvp_test_gen_params_free(*dst);
61*b077aed3SPierre Pronchery     *dst = alloc;
62*b077aed3SPierre Pronchery     return ret;
63*b077aed3SPierre Pronchery }
64*b077aed3SPierre Pronchery 
ossl_rsa_acvp_test_gen_params_free(OSSL_PARAM * dst)65*b077aed3SPierre Pronchery void ossl_rsa_acvp_test_gen_params_free(OSSL_PARAM *dst)
66*b077aed3SPierre Pronchery {
67*b077aed3SPierre Pronchery     OSSL_PARAM *p;
68*b077aed3SPierre Pronchery 
69*b077aed3SPierre Pronchery     if (dst == NULL)
70*b077aed3SPierre Pronchery         return;
71*b077aed3SPierre Pronchery 
72*b077aed3SPierre Pronchery     for (p = dst; p->key != NULL; ++p) {
73*b077aed3SPierre Pronchery         OPENSSL_free(p->data);
74*b077aed3SPierre Pronchery         p->data = NULL;
75*b077aed3SPierre Pronchery     }
76*b077aed3SPierre Pronchery     OPENSSL_free(dst);
77*b077aed3SPierre Pronchery }
78*b077aed3SPierre Pronchery 
ossl_rsa_acvp_test_set_params(RSA * r,const OSSL_PARAM params[])79*b077aed3SPierre Pronchery int ossl_rsa_acvp_test_set_params(RSA *r, const OSSL_PARAM params[])
80*b077aed3SPierre Pronchery {
81*b077aed3SPierre Pronchery     RSA_ACVP_TEST *t;
82*b077aed3SPierre Pronchery     const OSSL_PARAM *p;
83*b077aed3SPierre Pronchery 
84*b077aed3SPierre Pronchery     if (r->acvp_test != NULL) {
85*b077aed3SPierre Pronchery         ossl_rsa_acvp_test_free(r->acvp_test);
86*b077aed3SPierre Pronchery         r->acvp_test = NULL;
87*b077aed3SPierre Pronchery     }
88*b077aed3SPierre Pronchery 
89*b077aed3SPierre Pronchery     t = OPENSSL_zalloc(sizeof(*t));
90*b077aed3SPierre Pronchery     if (t == NULL)
91*b077aed3SPierre Pronchery         return 0;
92*b077aed3SPierre Pronchery 
93*b077aed3SPierre Pronchery     /* Set the input parameters */
94*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XP1)) != NULL
95*b077aed3SPierre Pronchery          && !OSSL_PARAM_get_BN(p, &t->Xp1))
96*b077aed3SPierre Pronchery         goto err;
97*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XP2)) != NULL
98*b077aed3SPierre Pronchery          && !OSSL_PARAM_get_BN(p, &t->Xp2))
99*b077aed3SPierre Pronchery         goto err;
100*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XP)) != NULL
101*b077aed3SPierre Pronchery          && !OSSL_PARAM_get_BN(p, &t->Xp))
102*b077aed3SPierre Pronchery         goto err;
103*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XQ1)) != NULL
104*b077aed3SPierre Pronchery          && !OSSL_PARAM_get_BN(p, &t->Xq1))
105*b077aed3SPierre Pronchery         goto err;
106*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XQ2)) != NULL
107*b077aed3SPierre Pronchery          && !OSSL_PARAM_get_BN(p, &t->Xq2))
108*b077aed3SPierre Pronchery         goto err;
109*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_TEST_XQ)) != NULL
110*b077aed3SPierre Pronchery          && !OSSL_PARAM_get_BN(p, &t->Xq))
111*b077aed3SPierre Pronchery         goto err;
112*b077aed3SPierre Pronchery 
113*b077aed3SPierre Pronchery     /* Setup the output parameters */
114*b077aed3SPierre Pronchery     t->p1 = BN_new();
115*b077aed3SPierre Pronchery     t->p2 = BN_new();
116*b077aed3SPierre Pronchery     t->q1 = BN_new();
117*b077aed3SPierre Pronchery     t->q2 = BN_new();
118*b077aed3SPierre Pronchery     r->acvp_test = t;
119*b077aed3SPierre Pronchery     return 1;
120*b077aed3SPierre Pronchery err:
121*b077aed3SPierre Pronchery     ossl_rsa_acvp_test_free(t);
122*b077aed3SPierre Pronchery     return 0;
123*b077aed3SPierre Pronchery }
124*b077aed3SPierre Pronchery 
ossl_rsa_acvp_test_get_params(RSA * r,OSSL_PARAM params[])125*b077aed3SPierre Pronchery int ossl_rsa_acvp_test_get_params(RSA *r, OSSL_PARAM params[])
126*b077aed3SPierre Pronchery {
127*b077aed3SPierre Pronchery     RSA_ACVP_TEST *t;
128*b077aed3SPierre Pronchery     OSSL_PARAM *p;
129*b077aed3SPierre Pronchery 
130*b077aed3SPierre Pronchery     if (r == NULL)
131*b077aed3SPierre Pronchery         return 0;
132*b077aed3SPierre Pronchery 
133*b077aed3SPierre Pronchery     t = r->acvp_test;
134*b077aed3SPierre Pronchery     if (t != NULL) {
135*b077aed3SPierre Pronchery         if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_P1)) != NULL
136*b077aed3SPierre Pronchery              && !OSSL_PARAM_set_BN(p, t->p1))
137*b077aed3SPierre Pronchery                     return 0;
138*b077aed3SPierre Pronchery         if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_P2)) != NULL
139*b077aed3SPierre Pronchery              && !OSSL_PARAM_set_BN(p, t->p2))
140*b077aed3SPierre Pronchery                     return 0;
141*b077aed3SPierre Pronchery         if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_Q1)) != NULL
142*b077aed3SPierre Pronchery              && !OSSL_PARAM_set_BN(p, t->q1))
143*b077aed3SPierre Pronchery                     return 0;
144*b077aed3SPierre Pronchery         if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_RSA_TEST_Q2)) != NULL
145*b077aed3SPierre Pronchery              && !OSSL_PARAM_set_BN(p, t->q2))
146*b077aed3SPierre Pronchery                     return 0;
147*b077aed3SPierre Pronchery     }
148*b077aed3SPierre Pronchery     return 1;
149*b077aed3SPierre Pronchery }
150*b077aed3SPierre Pronchery 
ossl_rsa_acvp_test_free(RSA_ACVP_TEST * t)151*b077aed3SPierre Pronchery void ossl_rsa_acvp_test_free(RSA_ACVP_TEST *t)
152*b077aed3SPierre Pronchery {
153*b077aed3SPierre Pronchery     if (t != NULL) {
154*b077aed3SPierre Pronchery         BN_free(t->Xp1);
155*b077aed3SPierre Pronchery         BN_free(t->Xp2);
156*b077aed3SPierre Pronchery         BN_free(t->Xp);
157*b077aed3SPierre Pronchery         BN_free(t->Xq1);
158*b077aed3SPierre Pronchery         BN_free(t->Xq2);
159*b077aed3SPierre Pronchery         BN_free(t->Xq);
160*b077aed3SPierre Pronchery         BN_free(t->p1);
161*b077aed3SPierre Pronchery         BN_free(t->p2);
162*b077aed3SPierre Pronchery         BN_free(t->q1);
163*b077aed3SPierre Pronchery         BN_free(t->q2);
164*b077aed3SPierre Pronchery         OPENSSL_free(t);
165*b077aed3SPierre Pronchery     }
166*b077aed3SPierre Pronchery }
167*b077aed3SPierre Pronchery 
168