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