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