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