]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/fips/fipsprov.c
Move CMAC to providers
[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 17#include <openssl/evp.h>
45c54042 18
319e518a
MC
19/* TODO(3.0): Needed for dummy_evp_call(). To be removed */
20#include <openssl/sha.h>
45c54042 21#include <openssl/rand_drbg.h>
04ca0027 22#include <openssl/ec.h>
45c54042 23
3593266d 24#include "internal/cryptlib.h"
319e518a
MC
25#include "internal/property.h"
26#include "internal/evp_int.h"
66ad63e8 27#include "internal/provider_algs.h"
bb751e11 28#include "internal/provider_ctx.h"
da747958 29#include "internal/providercommon.h"
9efa0ae0 30
b60cba3c
RS
31extern OSSL_core_thread_start_fn *c_thread_start;
32
da747958
MC
33/*
34 * TODO(3.0): Should these be stored in the provider side provctx? Could they
35 * ever be different from one init to the next? Unfortunately we can't do this
b60cba3c
RS
36 * at the moment because c_put_error/c_add_error_vdata do not provide
37 * us with the OPENSSL_CTX as a parameter.
da747958 38 */
9efa0ae0 39/* Functions provided by the core */
dca97d00 40static OSSL_core_gettable_params_fn *c_gettable_params;
b60cba3c
RS
41static OSSL_core_get_params_fn *c_get_params;
42OSSL_core_thread_start_fn *c_thread_start;
036913b1
RL
43static OSSL_core_new_error_fn *c_new_error;
44static OSSL_core_set_error_debug_fn *c_set_error_debug;
45static OSSL_core_vset_error_fn *c_vset_error;
b60cba3c
RS
46static OSSL_CRYPTO_malloc_fn *c_CRYPTO_malloc;
47static OSSL_CRYPTO_zalloc_fn *c_CRYPTO_zalloc;
b60cba3c
RS
48static OSSL_CRYPTO_free_fn *c_CRYPTO_free;
49static OSSL_CRYPTO_clear_free_fn *c_CRYPTO_clear_free;
50static OSSL_CRYPTO_realloc_fn *c_CRYPTO_realloc;
51static OSSL_CRYPTO_clear_realloc_fn *c_CRYPTO_clear_realloc;
52static OSSL_CRYPTO_secure_malloc_fn *c_CRYPTO_secure_malloc;
53static OSSL_CRYPTO_secure_zalloc_fn *c_CRYPTO_secure_zalloc;
54static OSSL_CRYPTO_secure_free_fn *c_CRYPTO_secure_free;
55static OSSL_CRYPTO_secure_clear_free_fn *c_CRYPTO_secure_clear_free;
56static OSSL_CRYPTO_secure_allocated_fn *c_CRYPTO_secure_allocated;
9efa0ae0 57
da747958
MC
58typedef struct fips_global_st {
59 const OSSL_PROVIDER *prov;
60} FIPS_GLOBAL;
61
62static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
63{
64 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
65
66 return fgbl;
67}
68
69static void fips_prov_ossl_ctx_free(void *fgbl)
70{
71 OPENSSL_free(fgbl);
72}
73
74static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
75 fips_prov_ossl_ctx_new,
76 fips_prov_ossl_ctx_free,
77};
78
79
9efa0ae0 80/* Parameters we provide to the core */
26175013
RL
81static const OSSL_PARAM fips_param_types[] = {
82 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0),
83 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_VERSION, OSSL_PARAM_UTF8_PTR, NULL, 0),
84 OSSL_PARAM_DEFN(OSSL_PROV_PARAM_BUILDINFO, OSSL_PARAM_UTF8_PTR, NULL, 0),
85 OSSL_PARAM_END
9efa0ae0
MC
86};
87
319e518a 88/* TODO(3.0): To be removed */
bb751e11 89static int dummy_evp_call(void *provctx)
3593266d 90{
bb751e11 91 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
319e518a
MC
92 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
93 EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
94 char msg[] = "Hello World!";
95 const unsigned char exptd[] = {
96 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
97 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
98 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
99 };
100 unsigned int dgstlen = 0;
101 unsigned char dgst[SHA256_DIGEST_LENGTH];
102 int ret = 0;
444ab3ab
MC
103 BN_CTX *bnctx = NULL;
104 BIGNUM *a = NULL, *b = NULL;
45c54042
MC
105 unsigned char randbuf[128];
106 RAND_DRBG *drbg = OPENSSL_CTX_get0_public_drbg(libctx);
f92e0815 107#ifndef OPENSSL_NO_EC
04ca0027 108 EC_KEY *key = NULL;
f92e0815 109#endif
319e518a 110
45c54042 111 if (ctx == NULL || sha256 == NULL || drbg == NULL)
319e518a
MC
112 goto err;
113
114 if (!EVP_DigestInit_ex(ctx, sha256, NULL))
115 goto err;
116 if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
117 goto err;
118 if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
119 goto err;
120 if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
121 goto err;
122
444ab3ab
MC
123 bnctx = BN_CTX_new_ex(libctx);
124 if (bnctx == NULL)
125 goto err;
126 BN_CTX_start(bnctx);
127 a = BN_CTX_get(bnctx);
128 b = BN_CTX_get(bnctx);
129 if (b == NULL)
130 goto err;
131 BN_zero(a);
132 if (!BN_one(b)
133 || !BN_add(a, a, b)
134 || BN_cmp(a, b) != 0)
135 goto err;
4bd8b240 136
45c54042
MC
137 if (RAND_DRBG_bytes(drbg, randbuf, sizeof(randbuf)) <= 0)
138 goto err;
139
eba3ebd7
MC
140 if (!BN_rand_ex(a, 256, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, bnctx))
141 goto err;
142
f92e0815 143#ifndef OPENSSL_NO_EC
04ca0027
MC
144 /* Do some dummy EC calls */
145 key = EC_KEY_new_by_curve_name_ex(libctx, NID_X9_62_prime256v1);
146 if (key == NULL)
147 goto err;
148
149 if (!EC_KEY_generate_key(key))
150 goto err;
f92e0815 151#endif
04ca0027 152
319e518a
MC
153 ret = 1;
154 err:
444ab3ab
MC
155 BN_CTX_end(bnctx);
156 BN_CTX_free(bnctx);
4bd8b240 157
319e518a
MC
158 EVP_MD_CTX_free(ctx);
159 EVP_MD_meth_free(sha256);
04ca0027 160
f92e0815 161#ifndef OPENSSL_NO_EC
04ca0027 162 EC_KEY_free(key);
f92e0815 163#endif
319e518a 164 return ret;
3593266d
MC
165}
166
dca97d00 167static const OSSL_PARAM *fips_gettable_params(const OSSL_PROVIDER *prov)
9efa0ae0
MC
168{
169 return fips_param_types;
170}
171
4e7991b4 172static int fips_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
9efa0ae0 173{
4e7991b4 174 OSSL_PARAM *p;
9efa0ae0
MC
175
176 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
177 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
178 return 0;
179 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
180 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
181 return 0;
182 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
183 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
184 return 0;
185
186 return 1;
187}
188
4cecf7a1
MC
189/* FIPS specific version of the function of the same name in provlib.c */
190const char *ossl_prov_util_nid_to_name(int nid)
191{
192 /* We don't have OBJ_nid2n() in FIPS_MODE so we have an explicit list */
193
194 switch (nid) {
195 /* Digests */
196 case NID_sha1:
197 return "SHA224";
198 case NID_sha224:
199 return "SHA224";
200 case NID_sha256:
201 return "SHA256";
202 case NID_sha384:
203 return "SHA384";
204 case NID_sha512:
205 return "SHA512";
206 case NID_sha512_224:
207 return "SHA512-224";
208 case NID_sha512_256:
209 return "SHA512-256";
210 case NID_sha3_224:
211 return "SHA3-224";
212 case NID_sha3_256:
213 return "SHA3-256";
214 case NID_sha3_384:
215 return "SHA3-384";
216 case NID_sha3_512:
217 return "SHA3-512";
218
219 /* Ciphers */
220 case NID_aes_256_ecb:
221 return "AES-256-ECB";
222 case NID_aes_192_ecb:
223 return "AES-192-ECB";
224 case NID_aes_128_ecb:
225 return "AES-128-ECB";
226 case NID_aes_256_cbc:
227 return "AES-256-CBC";
228 case NID_aes_192_cbc:
229 return "AES-192-CBC";
230 case NID_aes_128_cbc:
231 return "AES-128-CBC";
232 case NID_aes_256_ctr:
233 return "AES-256-CTR";
234 case NID_aes_192_ctr:
235 return "AES-192-CTR";
236 case NID_aes_128_ctr:
237 return "AES-128-CTR";
238 }
239
240 return NULL;
241}
242
9efa0ae0 243static const OSSL_ALGORITHM fips_digests[] = {
d5e5e2ff
SL
244 { "SHA1", "fips=yes", sha1_functions },
245 { "SHA224", "fips=yes", sha224_functions },
9efa0ae0 246 { "SHA256", "fips=yes", sha256_functions },
d5e5e2ff
SL
247 { "SHA384", "fips=yes", sha384_functions },
248 { "SHA512", "fips=yes", sha512_functions },
249 { "SHA512-224", "fips=yes", sha512_224_functions },
250 { "SHA512-256", "fips=yes", sha512_256_functions },
251 { "SHA3-224", "fips=yes", sha3_224_functions },
252 { "SHA3-256", "fips=yes", sha3_256_functions },
253 { "SHA3-384", "fips=yes", sha3_384_functions },
254 { "SHA3-512", "fips=yes", sha3_512_functions },
255 { "KMAC128", "fips=yes", keccak_kmac_128_functions },
256 { "KMAC256", "fips=yes", keccak_kmac_256_functions },
257
9efa0ae0
MC
258 { NULL, NULL, NULL }
259};
260
66ad63e8
MC
261static const OSSL_ALGORITHM fips_ciphers[] = {
262 { "AES-256-ECB", "fips=yes", aes256ecb_functions },
263 { "AES-192-ECB", "fips=yes", aes192ecb_functions },
264 { "AES-128-ECB", "fips=yes", aes128ecb_functions },
265 { "AES-256-CBC", "fips=yes", aes256cbc_functions },
266 { "AES-192-CBC", "fips=yes", aes192cbc_functions },
267 { "AES-128-CBC", "fips=yes", aes128cbc_functions },
268 { "AES-256-CTR", "fips=yes", aes256ctr_functions },
269 { "AES-192-CTR", "fips=yes", aes192ctr_functions },
270 { "AES-128-CTR", "fips=yes", aes128ctr_functions },
a672a02a
SL
271 { "id-aes256-GCM", "fips=yes", aes256gcm_functions },
272 { "id-aes192-GCM", "fips=yes", aes192gcm_functions },
273 { "id-aes128-GCM", "fips=yes", aes128gcm_functions },
66ad63e8
MC
274 { NULL, NULL, NULL }
275};
276
2e5db6ad
RL
277static const OSSL_ALGORITHM fips_macs[] = {
278 { "CMAC", "fips=yes", cmac_functions },
279 { NULL, NULL, NULL }
280};
281
9efa0ae0
MC
282static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
283 int operation_id,
284 int *no_cache)
285{
286 *no_cache = 0;
287 switch (operation_id) {
288 case OSSL_OP_DIGEST:
289 return fips_digests;
66ad63e8
MC
290 case OSSL_OP_CIPHER:
291 return fips_ciphers;
2e5db6ad
RL
292 case OSSL_OP_MAC:
293 return fips_macs;
9efa0ae0
MC
294 }
295 return NULL;
296}
297
298/* Functions we provide to the core */
299static const OSSL_DISPATCH fips_dispatch_table[] = {
319e518a
MC
300 /*
301 * To release our resources we just need to free the OPENSSL_CTX so we just
302 * use OPENSSL_CTX_free directly as our teardown function
303 */
304 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
dca97d00 305 { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))fips_gettable_params },
9efa0ae0
MC
306 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
307 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
308 { 0, NULL }
309};
310
319e518a
MC
311/* Functions we provide to ourself */
312static const OSSL_DISPATCH intern_dispatch_table[] = {
313 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
314 { 0, NULL }
315};
316
317
9efa0ae0
MC
318int OSSL_provider_init(const OSSL_PROVIDER *provider,
319 const OSSL_DISPATCH *in,
a39eb840
RL
320 const OSSL_DISPATCH **out,
321 void **provctx)
9efa0ae0 322{
da747958 323 FIPS_GLOBAL *fgbl;
03361afb 324 OPENSSL_CTX *ctx;
319e518a 325
9efa0ae0
MC
326 for (; in->function_id != 0; in++) {
327 switch (in->function_id) {
dca97d00
RL
328 case OSSL_FUNC_CORE_GETTABLE_PARAMS:
329 c_gettable_params = OSSL_get_core_gettable_params(in);
9efa0ae0
MC
330 break;
331 case OSSL_FUNC_CORE_GET_PARAMS:
332 c_get_params = OSSL_get_core_get_params(in);
333 break;
da747958
MC
334 case OSSL_FUNC_CORE_THREAD_START:
335 c_thread_start = OSSL_get_core_thread_start(in);
336 break;
036913b1
RL
337 case OSSL_FUNC_CORE_NEW_ERROR:
338 c_new_error = OSSL_get_core_new_error(in);
3593266d 339 break;
036913b1
RL
340 case OSSL_FUNC_CORE_SET_ERROR_DEBUG:
341 c_set_error_debug = OSSL_get_core_set_error_debug(in);
342 break;
343 case OSSL_FUNC_CORE_VSET_ERROR:
344 c_vset_error = OSSL_get_core_vset_error(in);
3593266d 345 break;
b60cba3c
RS
346 case OSSL_FUNC_CRYPTO_MALLOC:
347 c_CRYPTO_malloc = OSSL_get_CRYPTO_malloc(in);
348 break;
349 case OSSL_FUNC_CRYPTO_ZALLOC:
350 c_CRYPTO_zalloc = OSSL_get_CRYPTO_zalloc(in);
351 break;
b60cba3c
RS
352 case OSSL_FUNC_CRYPTO_FREE:
353 c_CRYPTO_free = OSSL_get_CRYPTO_free(in);
354 break;
355 case OSSL_FUNC_CRYPTO_CLEAR_FREE:
356 c_CRYPTO_clear_free = OSSL_get_CRYPTO_clear_free(in);
357 break;
358 case OSSL_FUNC_CRYPTO_REALLOC:
359 c_CRYPTO_realloc = OSSL_get_CRYPTO_realloc(in);
360 break;
361 case OSSL_FUNC_CRYPTO_CLEAR_REALLOC:
362 c_CRYPTO_clear_realloc = OSSL_get_CRYPTO_clear_realloc(in);
363 break;
364 case OSSL_FUNC_CRYPTO_SECURE_MALLOC:
365 c_CRYPTO_secure_malloc = OSSL_get_CRYPTO_secure_malloc(in);
366 break;
367 case OSSL_FUNC_CRYPTO_SECURE_ZALLOC:
368 c_CRYPTO_secure_zalloc = OSSL_get_CRYPTO_secure_zalloc(in);
369 break;
370 case OSSL_FUNC_CRYPTO_SECURE_FREE:
371 c_CRYPTO_secure_free = OSSL_get_CRYPTO_secure_free(in);
372 break;
373 case OSSL_FUNC_CRYPTO_SECURE_CLEAR_FREE:
374 c_CRYPTO_secure_clear_free = OSSL_get_CRYPTO_secure_clear_free(in);
375 break;
376 case OSSL_FUNC_CRYPTO_SECURE_ALLOCATED:
377 c_CRYPTO_secure_allocated = OSSL_get_CRYPTO_secure_allocated(in);
378 break;
9efa0ae0 379 default:
b60cba3c 380 /* Just ignore anything we don't understand */
9efa0ae0
MC
381 break;
382 }
383 }
384
b60cba3c
RS
385 /* Create a context. */
386 if ((ctx = OPENSSL_CTX_new()) == NULL)
319e518a 387 return 0;
b60cba3c
RS
388 if ((fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
389 &fips_prov_ossl_ctx_method)) == NULL) {
390 OPENSSL_CTX_free(ctx);
391 return 0;
392 }
03361afb 393 fgbl->prov = provider;
bb751e11
RL
394 *out = fips_dispatch_table;
395 *provctx = ctx;
396
319e518a
MC
397 /*
398 * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
399 * EVP calls from within the FIPS module.
400 */
bb751e11
RL
401 if (!dummy_evp_call(*provctx)) {
402 OPENSSL_CTX_free(*provctx);
403 *provctx = NULL;
319e518a
MC
404 return 0;
405 }
406
9efa0ae0
MC
407 return 1;
408}
3593266d 409
319e518a
MC
410/*
411 * The internal init function used when the FIPS module uses EVP to call
b1eb3fd7 412 * another algorithm also in the FIPS module. This is a recursive call that has
bb751e11
RL
413 * been made from within the FIPS module itself. To make this work, we populate
414 * the provider context of this inner instance with the same library context
415 * that was used in the EVP call that initiated this recursive call.
319e518a 416 */
3593266d
MC
417OSSL_provider_init_fn fips_intern_provider_init;
418int fips_intern_provider_init(const OSSL_PROVIDER *provider,
419 const OSSL_DISPATCH *in,
319e518a
MC
420 const OSSL_DISPATCH **out,
421 void **provctx)
3593266d 422{
bb751e11
RL
423 OSSL_core_get_library_context_fn *c_get_libctx = NULL;
424
425 for (; in->function_id != 0; in++) {
426 switch (in->function_id) {
427 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
428 c_get_libctx = OSSL_get_core_get_library_context(in);
429 break;
430 default:
431 break;
432 }
433 }
434
435 if (c_get_libctx == NULL)
436 return 0;
437
438 *provctx = c_get_libctx(provider);
439
440 /*
441 * Safety measure... we should get the library context that was
442 * created up in OSSL_provider_init().
443 */
444 if (*provctx == NULL)
445 return 0;
446
319e518a 447 *out = intern_dispatch_table;
3593266d
MC
448 return 1;
449}
450
036913b1 451void ERR_new(void)
3593266d 452{
036913b1
RL
453 c_new_error(NULL);
454}
455
456void ERR_set_debug(const char *file, int line, const char *func)
457{
458 c_set_error_debug(NULL, file, line, func);
3593266d
MC
459}
460
036913b1 461void ERR_set_error(int lib, int reason, const char *fmt, ...)
3593266d
MC
462{
463 va_list args;
8908d18c 464
036913b1
RL
465 va_start(args, fmt);
466 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
3593266d
MC
467 va_end(args);
468}
469
036913b1 470void ERR_vset_error(int lib, int reason, const char *fmt, va_list args)
3593266d 471{
036913b1 472 c_vset_error(NULL, ERR_PACK(lib, 0, reason), fmt, args);
3593266d 473}
da747958
MC
474
475const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx)
476{
477 FIPS_GLOBAL *fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
478 &fips_prov_ossl_ctx_method);
479
480 if (fgbl == NULL)
481 return NULL;
482
483 return fgbl->prov;
484}
b60cba3c
RS
485
486void *CRYPTO_malloc(size_t num, const char *file, int line)
487{
488 return c_CRYPTO_malloc(num, file, line);
489}
490
491void *CRYPTO_zalloc(size_t num, const char *file, int line)
492{
493 return c_CRYPTO_zalloc(num, file, line);
494}
495
b60cba3c
RS
496void CRYPTO_free(void *ptr, const char *file, int line)
497{
498 c_CRYPTO_free(ptr, file, line);
499}
500
501void CRYPTO_clear_free(void *ptr, size_t num, const char *file, int line)
502{
503 c_CRYPTO_clear_free(ptr, num, file, line);
504}
505
506void *CRYPTO_realloc(void *addr, size_t num, const char *file, int line)
507{
508 return c_CRYPTO_realloc(addr, num, file, line);
509}
510
511void *CRYPTO_clear_realloc(void *addr, size_t old_num, size_t num,
512 const char *file, int line)
513{
514 return c_CRYPTO_clear_realloc(addr, old_num, num, file, line);
515}
516
517void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
518{
519 return c_CRYPTO_secure_malloc(num, file, line);
520}
521
522void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
523{
524 return c_CRYPTO_secure_zalloc(num, file, line);
525}
526
527void CRYPTO_secure_free(void *ptr, const char *file, int line)
528{
529 c_CRYPTO_secure_free(ptr, file, line);
530}
531
532void CRYPTO_secure_clear_free(void *ptr, size_t num, const char *file, int line)
533{
534 c_CRYPTO_secure_clear_free(ptr, num, file, line);
535}
536
b60cba3c
RS
537int CRYPTO_secure_allocated(const void *ptr)
538{
539 return c_CRYPTO_secure_allocated(ptr);
540}