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>
18 /* TODO(3.0): Needed for dummy_evp_call(). To be removed */
19 #include <openssl/sha.h>
20 #include "internal/cryptlib.h"
21 #include "internal/property.h"
22 #include "internal/evp_int.h"
23 #include "internal/provider_algs.h"
24 #include "internal/provider_ctx.h"
25 #include "internal/providercommon.h"
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.
33 /* Functions provided by the core */
34 static OSSL_core_get_param_types_fn *c_get_param_types = NULL;
35 static OSSL_core_get_params_fn *c_get_params = NULL;
36 extern OSSL_core_thread_start_fn *c_thread_start;
37 OSSL_core_thread_start_fn *c_thread_start = NULL;
38 static OSSL_core_put_error_fn *c_put_error = NULL;
39 static OSSL_core_add_error_vdata_fn *c_add_error_vdata = NULL;
41 typedef struct fips_global_st {
42 const OSSL_PROVIDER *prov;
45 static void *fips_prov_ossl_ctx_new(OPENSSL_CTX *libctx)
47 FIPS_GLOBAL *fgbl = OPENSSL_zalloc(sizeof(*fgbl));
52 static void fips_prov_ossl_ctx_free(void *fgbl)
57 static const OPENSSL_CTX_METHOD fips_prov_ossl_ctx_method = {
58 fips_prov_ossl_ctx_new,
59 fips_prov_ossl_ctx_free,
63 /* Parameters we provide to the core */
64 static 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 },
71 /* TODO(3.0): To be removed */
72 static int dummy_evp_call(void *provctx)
74 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
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
83 unsigned int dgstlen = 0;
84 unsigned char dgst[SHA256_DIGEST_LENGTH];
87 BIGNUM *a = NULL, *b = NULL;
89 if (ctx == NULL || sha256 == NULL)
92 if (!EVP_DigestInit_ex(ctx, sha256, NULL))
94 if (!EVP_DigestUpdate(ctx, msg, sizeof(msg) - 1))
96 if (!EVP_DigestFinal(ctx, dgst, &dgstlen))
98 if (dgstlen != sizeof(exptd) || memcmp(dgst, exptd, sizeof(exptd)) != 0)
101 bnctx = BN_CTX_new_ex(libctx);
105 a = BN_CTX_get(bnctx);
106 b = BN_CTX_get(bnctx);
112 || BN_cmp(a, b) != 0)
120 EVP_MD_CTX_free(ctx);
121 EVP_MD_meth_free(sha256);
125 static const OSSL_ITEM *fips_get_param_types(const OSSL_PROVIDER *prov)
127 return fips_param_types;
130 static int fips_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
134 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_NAME);
135 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "OpenSSL FIPS Provider"))
137 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_VERSION);
138 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR))
140 p = OSSL_PARAM_locate(params, OSSL_PROV_PARAM_BUILDINFO);
141 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, OPENSSL_FULL_VERSION_STR))
147 static const OSSL_ALGORITHM fips_digests[] = {
148 { "SHA1", "fips=yes", sha1_functions },
149 { "SHA224", "fips=yes", sha224_functions },
150 { "SHA256", "fips=yes", sha256_functions },
151 { "SHA384", "fips=yes", sha384_functions },
152 { "SHA512", "fips=yes", sha512_functions },
153 { "SHA512-224", "fips=yes", sha512_224_functions },
154 { "SHA512-256", "fips=yes", sha512_256_functions },
155 { "SHA3-224", "fips=yes", sha3_224_functions },
156 { "SHA3-256", "fips=yes", sha3_256_functions },
157 { "SHA3-384", "fips=yes", sha3_384_functions },
158 { "SHA3-512", "fips=yes", sha3_512_functions },
159 { "KMAC128", "fips=yes", keccak_kmac_128_functions },
160 { "KMAC256", "fips=yes", keccak_kmac_256_functions },
165 static const OSSL_ALGORITHM fips_ciphers[] = {
166 { "AES-256-ECB", "fips=yes", aes256ecb_functions },
167 { "AES-192-ECB", "fips=yes", aes192ecb_functions },
168 { "AES-128-ECB", "fips=yes", aes128ecb_functions },
169 { "AES-256-CBC", "fips=yes", aes256cbc_functions },
170 { "AES-192-CBC", "fips=yes", aes192cbc_functions },
171 { "AES-128-CBC", "fips=yes", aes128cbc_functions },
172 { "AES-256-CTR", "fips=yes", aes256ctr_functions },
173 { "AES-192-CTR", "fips=yes", aes192ctr_functions },
174 { "AES-128-CTR", "fips=yes", aes128ctr_functions },
178 static const OSSL_ALGORITHM *fips_query(OSSL_PROVIDER *prov,
183 switch (operation_id) {
192 /* Functions we provide to the core */
193 static const OSSL_DISPATCH fips_dispatch_table[] = {
195 * To release our resources we just need to free the OPENSSL_CTX so we just
196 * use OPENSSL_CTX_free directly as our teardown function
198 { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OPENSSL_CTX_free },
199 { OSSL_FUNC_PROVIDER_GET_PARAM_TYPES, (void (*)(void))fips_get_param_types },
200 { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))fips_get_params },
201 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
205 /* Functions we provide to ourself */
206 static const OSSL_DISPATCH intern_dispatch_table[] = {
207 { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fips_query },
212 int OSSL_provider_init(const OSSL_PROVIDER *provider,
213 const OSSL_DISPATCH *in,
214 const OSSL_DISPATCH **out,
220 for (; in->function_id != 0; in++) {
221 switch (in->function_id) {
222 case OSSL_FUNC_CORE_GET_PARAM_TYPES:
223 c_get_param_types = OSSL_get_core_get_param_types(in);
225 case OSSL_FUNC_CORE_GET_PARAMS:
226 c_get_params = OSSL_get_core_get_params(in);
228 case OSSL_FUNC_CORE_THREAD_START:
229 c_thread_start = OSSL_get_core_thread_start(in);
231 case OSSL_FUNC_CORE_PUT_ERROR:
232 c_put_error = OSSL_get_core_put_error(in);
234 case OSSL_FUNC_CORE_ADD_ERROR_VDATA:
235 c_add_error_vdata = OSSL_get_core_add_error_vdata(in);
237 /* Just ignore anything we don't understand */
243 ctx = OPENSSL_CTX_new();
247 fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
248 &fips_prov_ossl_ctx_method);
253 fgbl->prov = provider;
255 *out = fips_dispatch_table;
259 * TODO(3.0): Remove me. This is just a dummy call to demonstrate making
260 * EVP calls from within the FIPS module.
262 if (!dummy_evp_call(*provctx)) {
263 OPENSSL_CTX_free(*provctx);
271 OPENSSL_CTX_free(ctx);
276 * The internal init function used when the FIPS module uses EVP to call
277 * another algorithm also in the FIPS module. This is a recursive call that has
278 * been made from within the FIPS module itself. To make this work, we populate
279 * the provider context of this inner instance with the same library context
280 * that was used in the EVP call that initiated this recursive call.
282 OSSL_provider_init_fn fips_intern_provider_init;
283 int fips_intern_provider_init(const OSSL_PROVIDER *provider,
284 const OSSL_DISPATCH *in,
285 const OSSL_DISPATCH **out,
288 OSSL_core_get_library_context_fn *c_get_libctx = NULL;
290 for (; in->function_id != 0; in++) {
291 switch (in->function_id) {
292 case OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT:
293 c_get_libctx = OSSL_get_core_get_library_context(in);
300 if (c_get_libctx == NULL)
303 *provctx = c_get_libctx(provider);
306 * Safety measure... we should get the library context that was
307 * created up in OSSL_provider_init().
309 if (*provctx == NULL)
312 *out = intern_dispatch_table;
316 void ERR_put_error(int lib, int func, int reason, const char *file, int line)
319 * TODO(3.0): This works for the FIPS module because we're going to be
320 * using lib/func/reason codes that libcrypto already knows about. This
321 * won't work for third party providers that have their own error mechanisms,
322 * so we'll need to come up with something else for them.
324 c_put_error(lib, func, reason, file, line);
325 ERR_add_error_data(1, "(in the FIPS module)");
328 void ERR_add_error_data(int num, ...)
333 ERR_add_error_vdata(num, args);
337 void ERR_add_error_vdata(int num, va_list args)
339 c_add_error_vdata(num, args);
342 const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx)
344 FIPS_GLOBAL *fgbl = openssl_ctx_get_data(ctx, OPENSSL_CTX_FIPS_PROV_INDEX,
345 &fips_prov_ossl_ctx_method);