]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/fips/fipsprov.c
crypto/modes/build.conf: Fix MODES asm mistakes
[thirdparty/openssl.git] / providers / fips / fipsprov.c
CommitLineData
9efa0ae0
MC
1/*
2 * Copyright 2019 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>
11#include <stdio.h>
12#include <openssl/core.h>
13#include <openssl/core_numbers.h>
14#include <openssl/core_names.h>
15#include <openssl/params.h>
3593266d 16#include <openssl/err.h>
319e518a
MC
17#include <openssl/evp.h>
18/* TODO(3.0): Needed for dummy_evp_call(). To be removed */
19#include <openssl/sha.h>
3593266d 20#include "internal/cryptlib.h"
319e518a
MC
21#include "internal/property.h"
22#include "internal/evp_int.h"
66ad63e8 23#include "internal/provider_algs.h"
bb751e11 24#include "internal/provider_ctx.h"
da747958 25#include "internal/providercommon.h"
9efa0ae0 26
da747958
MC
27/*
28 * TODO(3.0): Should these be stored in the provider side provctx? Could they
29 * ever be different from one init to the next? Unfortunately we can't do this
30 * at the moment because c_put_error/c_add_error_vdata do not provide us with
31 * the OPENSSL_CTX as a parameter.
32 */
9efa0ae0
MC
33/* Functions provided by the core */
34static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
35static OSSL_core_get_params_fn *c_get_params = NULL;
da747958
MC
36extern OSSL_core_thread_start_fn *c_thread_start;
37OSSL_core_thread_start_fn *c_thread_start = NULL;
3593266d
MC
38static OSSL_core_put_error_fn *c_put_error = NULL;
39static OSSL_core_add_error_vdata_fn *c_add_error_vdata = NULL;
9efa0ae0 40
da747958
MC
41typedef struct fips_global_st {
42 const OSSL_PROVIDER *prov;
43} FIPS_GLOBAL;
44
45static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
46{
47 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
48
49 return fgbl;
50}
51
52static void fips_prov_ossl_ctx_free(void *fgbl)
53{
54 OPENSSL_free(fgbl);
55}
56
57static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
58 fips_prov_ossl_ctx_new,
59 fips_prov_ossl_ctx_free,
60};
61
62
9efa0ae0
MC
63/* Parameters we provide to the core */
64static const OSSL_ITEM fips_param_types[] = {
65 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
66 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
67 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
68 { 0, NULL }
69};
70
319e518a 71/* TODO(3.0): To be removed */
bb751e11 72static int dummy_evp_call(void *provctx)
3593266d 73{
bb751e11 74 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
319e518a
MC
75 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
76 EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
77 char msg[] = "Hello World!";
78 const unsigned char exptd[] = {
79 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
80 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
81 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
82 };
83 unsigned int dgstlen = 0;
84 unsigned char dgst[SHA256_DIGEST_LENGTH];
85 int ret = 0;
444ab3ab
MC
86 BN_CTX *bnctx = NULL;
87 BIGNUM *a = NULL, *b = NULL;
319e518a
MC
88
89 if (ctx == NULL || sha256 == NULL)
90 goto err;
91
92 if (!EVP_DigestInit_ex(ctx, sha256, NULL))
93 goto err;
94 if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
95 goto err;
96 if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
97 goto err;
98 if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
99 goto err;
100
444ab3ab
MC
101 bnctx = BN_CTX_new_ex(libctx);
102 if (bnctx == NULL)
103 goto err;
104 BN_CTX_start(bnctx);
105 a = BN_CTX_get(bnctx);
106 b = BN_CTX_get(bnctx);
107 if (b == NULL)
108 goto err;
109 BN_zero(a);
110 if (!BN_one(b)
111 || !BN_add(a, a, b)
112 || BN_cmp(a, b) != 0)
113 goto err;
114
319e518a
MC
115 ret = 1;
116 err:
444ab3ab
MC
117 BN_CTX_end(bnctx);
118 BN_CTX_free(bnctx);
119
319e518a
MC
120 EVP_MD_CTX_free(ctx);
121 EVP_MD_meth_free(sha256);
122 return ret;
3593266d
MC
123}
124
9efa0ae0
MC
125static const OSSL_ITEM *fips_get_param_types(const OSSL_PROVIDER *prov)
126{
127 return fips_param_types;
128}
129
130static int fips_get_params(const OSSL_PROVIDER *prov,
131 const OSSL_PARAM params[])
132{
133 const OSSL_PARAM *p;
134
135 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
136 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
137 return 0;
138 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
139 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
140 return 0;
141 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
142 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
143 return 0;
144
145 return 1;
146}
147
9efa0ae0 148static const OSSL_ALGORITHM fips_digests[] = {
d5e5e2ff
SL
149 { "SHA1", "fips=yes", sha1_functions },
150 { "SHA224", "fips=yes", sha224_functions },
9efa0ae0 151 { "SHA256", "fips=yes", sha256_functions },
d5e5e2ff
SL
152 { "SHA384", "fips=yes", sha384_functions },
153 { "SHA512", "fips=yes", sha512_functions },
154 { "SHA512-224", "fips=yes", sha512_224_functions },
155 { "SHA512-256", "fips=yes", sha512_256_functions },
156 { "SHA3-224", "fips=yes", sha3_224_functions },
157 { "SHA3-256", "fips=yes", sha3_256_functions },
158 { "SHA3-384", "fips=yes", sha3_384_functions },
159 { "SHA3-512", "fips=yes", sha3_512_functions },
160 { "KMAC128", "fips=yes", keccak_kmac_128_functions },
161 { "KMAC256", "fips=yes", keccak_kmac_256_functions },
162
9efa0ae0
MC
163 { NULL, NULL, NULL }
164};
165
66ad63e8
MC
166static const OSSL_ALGORITHM fips_ciphers[] = {
167 { "AES-256-ECB", "fips=yes", aes256ecb_functions },
168 { "AES-192-ECB", "fips=yes", aes192ecb_functions },
169 { "AES-128-ECB", "fips=yes", aes128ecb_functions },
170 { "AES-256-CBC", "fips=yes", aes256cbc_functions },
171 { "AES-192-CBC", "fips=yes", aes192cbc_functions },
172 { "AES-128-CBC", "fips=yes", aes128cbc_functions },
173 { "AES-256-CTR", "fips=yes", aes256ctr_functions },
174 { "AES-192-CTR", "fips=yes", aes192ctr_functions },
175 { "AES-128-CTR", "fips=yes", aes128ctr_functions },
176 { NULL, NULL, NULL }
177};
178
9efa0ae0
MC
179static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
180 int operation_id,
181 int *no_cache)
182{
183 *no_cache = 0;
184 switch (operation_id) {
185 case OSSL_OP_DIGEST:
186 return fips_digests;
66ad63e8
MC
187 case OSSL_OP_CIPHER:
188 return fips_ciphers;
9efa0ae0
MC
189 }
190 return NULL;
191}
192
193/* Functions we provide to the core */
194static const OSSL_DISPATCH fips_dispatch_table[] = {
319e518a
MC
195 /*
196 * To release our resources we just need to free the OPENSSL_CTX so we just
197 * use OPENSSL_CTX_free directly as our teardown function
198 */
199 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
9efa0ae0
MC
200 { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))fips_get_param_types },
201 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
202 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
203 { 0, NULL }
204};
205
319e518a
MC
206/* Functions we provide to ourself */
207static const OSSL_DISPATCH intern_dispatch_table[] = {
208 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
209 { 0, NULL }
210};
211
212
9efa0ae0
MC
213int OSSL_provider_init(const OSSL_PROVIDER *provider,
214 const OSSL_DISPATCH *in,
a39eb840
RL
215 const OSSL_DISPATCH **out,
216 void **provctx)
9efa0ae0 217{
da747958
MC
218 FIPS_GLOBAL *fgbl;
219 OPENSSL_CTX *ctx = OPENSSL_CTX_new();
220
221 if (ctx == NULL)
222 return 0;
223
224 fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
225 &fips_prov_ossl_ctx_method);
226
227 if (fgbl == NULL)
228 goto err;
229
230 fgbl->prov = provider;
319e518a 231
9efa0ae0
MC
232 for (; in->function_id != 0; in++) {
233 switch (in->function_id) {
234 case OSSL_FUNC_CORE_GET_PARAM_TYPES:
235 c_get_param_types = OSSL_get_core_get_param_types(in);
236 break;
237 case OSSL_FUNC_CORE_GET_PARAMS:
238 c_get_params = OSSL_get_core_get_params(in);
239 break;
da747958
MC
240 case OSSL_FUNC_CORE_THREAD_START:
241 c_thread_start = OSSL_get_core_thread_start(in);
242 break;
3593266d
MC
243 case OSSL_FUNC_CORE_PUT_ERROR:
244 c_put_error = OSSL_get_core_put_error(in);
245 break;
246 case OSSL_FUNC_CORE_ADD_ERROR_VDATA:
247 c_add_error_vdata = OSSL_get_core_add_error_vdata(in);
248 break;
9efa0ae0
MC
249 /* Just ignore anything we don't understand */
250 default:
251 break;
252 }
253 }
254
319e518a
MC
255 ctx = OPENSSL_CTX_new();
256 if (ctx == NULL)
257 return 0;
258
bb751e11
RL
259 *out = fips_dispatch_table;
260 *provctx = ctx;
261
319e518a
MC
262 /*
263 * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
264 * EVP calls from within the FIPS module.
265 */
bb751e11
RL
266 if (!dummy_evp_call(*provctx)) {
267 OPENSSL_CTX_free(*provctx);
268 *provctx = NULL;
319e518a
MC
269 return 0;
270 }
271
9efa0ae0 272 return 1;
da747958
MC
273
274 err:
275 OPENSSL_CTX_free(ctx);
276 return 0;
9efa0ae0 277}
3593266d 278
319e518a
MC
279/*
280 * The internal init function used when the FIPS module uses EVP to call
b1eb3fd7 281 * another algorithm also in the FIPS module. This is a recursive call that has
bb751e11
RL
282 * been made from within the FIPS module itself. To make this work, we populate
283 * the provider context of this inner instance with the same library context
284 * that was used in the EVP call that initiated this recursive call.
319e518a 285 */
3593266d
MC
286OSSL_provider_init_fn fips_intern_provider_init;
287int fips_intern_provider_init(const OSSL_PROVIDER *provider,
288 const OSSL_DISPATCH *in,
319e518a
MC
289 const OSSL_DISPATCH **out,
290 void **provctx)
3593266d 291{
bb751e11
RL
292 OSSL_core_get_library_context_fn *c_get_libctx = NULL;
293
294 for (; in->function_id != 0; in++) {
295 switch (in->function_id) {
296 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
297 c_get_libctx = OSSL_get_core_get_library_context(in);
298 break;
299 default:
300 break;
301 }
302 }
303
304 if (c_get_libctx == NULL)
305 return 0;
306
307 *provctx = c_get_libctx(provider);
308
309 /*
310 * Safety measure... we should get the library context that was
311 * created up in OSSL_provider_init().
312 */
313 if (*provctx == NULL)
314 return 0;
315
319e518a 316 *out = intern_dispatch_table;
3593266d
MC
317 return 1;
318}
319
320void ERR_put_error(int lib, int func, int reason, const char *file, int line)
321{
322 /*
323 * TODO(3.0): This works for the FIPS module because we're going to be
324 * using lib/func/reason codes that libcrypto already knows about. This
325 * won't work for third party providers that have their own error mechanisms,
326 * so we'll need to come up with something else for them.
327 */
328 c_put_error(lib, func, reason, file, line);
329}
330
331void ERR_add_error_data(int num, ...)
332{
333 va_list args;
334 va_start(args, num);
335 ERR_add_error_vdata(num, args);
336 va_end(args);
337}
338
339void ERR_add_error_vdata(int num, va_list args)
340{
341 c_add_error_vdata(num, args);
342}
da747958
MC
343
344const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx)
345{
346 FIPS_GLOBAL *fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
347 &fips_prov_ossl_ctx_method);
348
349 if (fgbl == NULL)
350 return NULL;
351
352 return fgbl->prov;
353}