2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
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>
19 /* TODO(3.0): Needed for dummy_evp_call(). To be removed */
20 #include <openssl/sha.h>
21 #include <openssl/rand_drbg.h>
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"
31 * TODO(3.0): Should these be stored in the provider side provctx? Could they
32 * ever be different from one init to the next? Unfortunately we can't do this
33 * at the moment because c_put_error/c_add_error_vdata do not provide us with
34 * the OPENSSL_CTX as a parameter.
36 /* Functions provided by the core */
37 static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
38 static OSSL_core_get_params_fn *c_get_params = NULL;
39 extern OSSL_core_thread_start_fn *c_thread_start;
40 OSSL_core_thread_start_fn *c_thread_start = NULL;
41 static OSSL_core_put_error_fn *c_put_error = NULL;
42 static OSSL_core_add_error_vdata_fn *c_add_error_vdata = NULL;
44 typedef struct fips_global_st {
45 const OSSL_PROVIDER *prov;
48 static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
50 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
55 static void fips_prov_ossl_ctx_free(void *fgbl)
60 static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
61 fips_prov_ossl_ctx_new,
62 fips_prov_ossl_ctx_free,
66 /* Parameters we provide to the core */
67 static const OSSL_ITEM fips_param_types[] = {
68 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_NAME },
69 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_VERSION },
70 { OSSL_PARAM_UTF8_PTR, OSSL_PROV_PARAM_BUILDINFO },
74 /* TODO(3.0): To be removed */
75 static int dummy_evp_call(void *provctx)
77 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
78 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
79 EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA256", NULL);
80 char msg[] = "Hello World!";
81 const unsigned char exptd[] = {
82 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
83 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
84 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
86 unsigned int dgstlen = 0;
87 unsigned char dgst[SHA256_DIGEST_LENGTH];
90 BIGNUM *a = NULL, *b = NULL;
91 unsigned char randbuf[128];
92 RAND_DRBG *drbg = OPENSSL_CTX_get0_public_drbg(libctx);
94 if (ctx == NULL || sha256 == NULL || drbg == NULL)
97 if (!EVP_DigestInit_ex(ctx, sha256, NULL))
99 if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
101 if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
103 if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
106 bnctx = BN_CTX_new_ex(libctx);
110 a = BN_CTX_get(bnctx);
111 b = BN_CTX_get(bnctx);
117 || BN_cmp(a, b) != 0)
120 if (RAND_DRBG_bytes(drbg, randbuf, sizeof(randbuf)) <= 0)
128 EVP_MD_CTX_free(ctx);
129 EVP_MD_meth_free(sha256);
133 static const OSSL_ITEM *fips_get_param_types(const OSSL_PROVIDER *prov)
135 return fips_param_types;
138 static int fips_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
142 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
143 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
145 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
146 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
148 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
149 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
155 static const OSSL_ALGORITHM fips_digests[] = {
156 { "SHA1", "fips=yes", sha1_functions },
157 { "SHA224", "fips=yes", sha224_functions },
158 { "SHA256", "fips=yes", sha256_functions },
159 { "SHA384", "fips=yes", sha384_functions },
160 { "SHA512", "fips=yes", sha512_functions },
161 { "SHA512-224", "fips=yes", sha512_224_functions },
162 { "SHA512-256", "fips=yes", sha512_256_functions },
163 { "SHA3-224", "fips=yes", sha3_224_functions },
164 { "SHA3-256", "fips=yes", sha3_256_functions },
165 { "SHA3-384", "fips=yes", sha3_384_functions },
166 { "SHA3-512", "fips=yes", sha3_512_functions },
167 { "KMAC128", "fips=yes", keccak_kmac_128_functions },
168 { "KMAC256", "fips=yes", keccak_kmac_256_functions },
173 static const OSSL_ALGORITHM fips_ciphers[] = {
174 { "AES-256-ECB", "fips=yes", aes256ecb_functions },
175 { "AES-192-ECB", "fips=yes", aes192ecb_functions },
176 { "AES-128-ECB", "fips=yes", aes128ecb_functions },
177 { "AES-256-CBC", "fips=yes", aes256cbc_functions },
178 { "AES-192-CBC", "fips=yes", aes192cbc_functions },
179 { "AES-128-CBC", "fips=yes", aes128cbc_functions },
180 { "AES-256-CTR", "fips=yes", aes256ctr_functions },
181 { "AES-192-CTR", "fips=yes", aes192ctr_functions },
182 { "AES-128-CTR", "fips=yes", aes128ctr_functions },
186 static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
191 switch (operation_id) {
200 /* Functions we provide to the core */
201 static const OSSL_DISPATCH fips_dispatch_table[] = {
203 * To release our resources we just need to free the OPENSSL_CTX so we just
204 * use OPENSSL_CTX_free directly as our teardown function
206 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
207 { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))fips_get_param_types },
208 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
209 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
213 /* Functions we provide to ourself */
214 static const OSSL_DISPATCH intern_dispatch_table[] = {
215 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
220 int OSSL_provider_init(const OSSL_PROVIDER *provider,
221 const OSSL_DISPATCH *in,
222 const OSSL_DISPATCH **out,
228 for (; in->function_id != 0; in++) {
229 switch (in->function_id) {
230 case OSSL_FUNC_CORE_GET_PARAM_TYPES:
231 c_get_param_types = OSSL_get_core_get_param_types(in);
233 case OSSL_FUNC_CORE_GET_PARAMS:
234 c_get_params = OSSL_get_core_get_params(in);
236 case OSSL_FUNC_CORE_THREAD_START:
237 c_thread_start = OSSL_get_core_thread_start(in);
239 case OSSL_FUNC_CORE_PUT_ERROR:
240 c_put_error = OSSL_get_core_put_error(in);
242 case OSSL_FUNC_CORE_ADD_ERROR_VDATA:
243 c_add_error_vdata = OSSL_get_core_add_error_vdata(in);
245 /* Just ignore anything we don't understand */
251 ctx = OPENSSL_CTX_new();
255 fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
256 &fips_prov_ossl_ctx_method);
261 fgbl->prov = provider;
263 *out = fips_dispatch_table;
267 * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
268 * EVP calls from within the FIPS module.
270 if (!dummy_evp_call(*provctx)) {
271 OPENSSL_CTX_free(*provctx);
279 OPENSSL_CTX_free(ctx);
284 * The internal init function used when the FIPS module uses EVP to call
285 * another algorithm also in the FIPS module. This is a recursive call that has
286 * been made from within the FIPS module itself. To make this work, we populate
287 * the provider context of this inner instance with the same library context
288 * that was used in the EVP call that initiated this recursive call.
290 OSSL_provider_init_fn fips_intern_provider_init;
291 int fips_intern_provider_init(const OSSL_PROVIDER *provider,
292 const OSSL_DISPATCH *in,
293 const OSSL_DISPATCH **out,
296 OSSL_core_get_library_context_fn *c_get_libctx = NULL;
298 for (; in->function_id != 0; in++) {
299 switch (in->function_id) {
300 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
301 c_get_libctx = OSSL_get_core_get_library_context(in);
308 if (c_get_libctx == NULL)
311 *provctx = c_get_libctx(provider);
314 * Safety measure... we should get the library context that was
315 * created up in OSSL_provider_init().
317 if (*provctx == NULL)
320 *out = intern_dispatch_table;
324 void ERR_put_error(int lib, int func, int reason, const char *file, int line)
327 * TODO(3.0): This works for the FIPS module because we're going to be
328 * using lib/func/reason codes that libcrypto already knows about. This
329 * won't work for third party providers that have their own error mechanisms,
330 * so we'll need to come up with something else for them.
332 c_put_error(lib, func, reason, file, line);
333 ERR_add_error_data(1, "(in the FIPS module)");
336 void ERR_add_error_data(int num, ...)
341 ERR_add_error_vdata(num, args);
345 void ERR_add_error_vdata(int num, va_list args)
347 c_add_error_vdata(num, args);
350 const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx)
352 FIPS_GLOBAL *fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
353 &fips_prov_ossl_ctx_method);