2 * Copyright 2018 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
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/err.h>
18 #include "internal/providercommonerr.h"
19 #include "internal/provider_algs.h"
20 #include "internal/provider_ctx.h"
23 * Forward declaration of everything implemented here. This is not strictly
24 * necessary for the compiler, but provides an assurance that the signatures
25 * of the functions in the dispatch table are correct.
27 static OSSL_OP_mac_newctx_fn gmac_new;
28 static OSSL_OP_mac_dupctx_fn gmac_dup;
29 static OSSL_OP_mac_freectx_fn gmac_free;
30 static OSSL_OP_mac_gettable_params_fn gmac_gettable_params;
31 static OSSL_OP_mac_get_params_fn gmac_get_params;
32 static OSSL_OP_mac_settable_ctx_params_fn gmac_settable_ctx_params;
33 static OSSL_OP_mac_set_ctx_params_fn gmac_set_ctx_params;
34 static OSSL_OP_mac_init_fn gmac_init;
35 static OSSL_OP_mac_update_fn gmac_update;
36 static OSSL_OP_mac_final_fn gmac_final;
38 /* local GMAC pkey structure */
42 EVP_CIPHER_CTX *ctx; /* Cipher context */
45 * References to the underlying cipher implementation. |cipher| caches
46 * the cipher, always. |alloc_cipher| only holds a reference to an
47 * explicitly fetched cipher.
48 * |cipher| is cleared after a CMAC_Init call.
50 const EVP_CIPHER *cipher; /* Cache GCM cipher */
51 EVP_CIPHER *alloc_cipher; /* Fetched cipher */
54 * Conditions for legacy EVP_CIPHER uses.
56 ENGINE *engine; /* Engine implementing the cipher */
59 static size_t gmac_size(void);
61 static void gmac_free(void *vmacctx)
63 struct gmac_data_st *macctx = vmacctx;
66 EVP_CIPHER_CTX_free(macctx->ctx);
67 EVP_CIPHER_free(macctx->alloc_cipher);
72 static void *gmac_new(void *provctx)
74 struct gmac_data_st *macctx;
76 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
77 || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
81 macctx->provctx = provctx;
86 static void *gmac_dup(void *vsrc)
88 struct gmac_data_st *src = vsrc;
89 struct gmac_data_st *dst = gmac_new(src->provctx);
94 if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
95 || (src->alloc_cipher != NULL
96 && !EVP_CIPHER_up_ref(src->alloc_cipher))) {
101 dst->cipher = src->cipher;
102 dst->alloc_cipher = src->alloc_cipher;
103 dst->engine = src->engine;
107 static int gmac_init(void *vmacctx)
112 static int gmac_update(void *vmacctx, const unsigned char *data,
115 struct gmac_data_st *macctx = vmacctx;
116 EVP_CIPHER_CTX *ctx = macctx->ctx;
119 while (datalen > INT_MAX) {
120 if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
125 return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
128 static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
131 struct gmac_data_st *macctx = vmacctx;
134 if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
137 /* TODO(3.0) Use params */
139 if (!EVP_CIPHER_CTX_ctrl(macctx->ctx, EVP_CTRL_AEAD_GET_TAG,
147 static size_t gmac_size(void)
149 return EVP_GCM_TLS_TAG_LEN;
152 static const OSSL_PARAM known_gettable_params[] = {
153 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
156 static const OSSL_PARAM *gmac_gettable_params(void)
158 return known_gettable_params;
161 static int gmac_get_params(OSSL_PARAM params[])
165 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
166 return OSSL_PARAM_set_size_t(p, gmac_size());
171 static const OSSL_PARAM known_settable_ctx_params[] = {
172 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
173 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
174 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
175 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
176 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
179 static const OSSL_PARAM *gmac_settable_ctx_params(void)
181 return known_settable_ctx_params;
185 * ALL parameters should be set before init().
187 static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
189 struct gmac_data_st *macctx = vmacctx;
190 EVP_CIPHER_CTX *ctx = macctx->ctx;
193 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
194 if (p->data_type != OSSL_PARAM_UTF8_STRING)
198 const char *algoname = p->data;
199 const char *propquery = NULL;
201 /* Inside the FIPS module, we don't support engines */
202 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
203 ENGINE_finish(macctx->engine);
204 macctx->engine = NULL;
206 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
208 if (p->data_type != OSSL_PARAM_UTF8_STRING)
211 macctx->engine = ENGINE_by_id(p->data);
212 if (macctx->engine == NULL)
216 if ((p = OSSL_PARAM_locate_const(params,
217 OSSL_MAC_PARAM_PROPERTIES))
219 if (p->data_type != OSSL_PARAM_UTF8_STRING)
225 EVP_CIPHER_free(macctx->alloc_cipher);
226 macctx->cipher = macctx->alloc_cipher = NULL;
228 macctx->cipher = macctx->alloc_cipher =
229 EVP_CIPHER_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
230 algoname, propquery);
231 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy ciphers */
232 /* TODO(3.0) BEGIN legacy stuff, to be removed */
233 if (macctx->cipher == NULL)
234 macctx->cipher = EVP_get_cipherbyname(algoname);
235 /* TODO(3.0) END of legacy stuff */
238 if (macctx->cipher == NULL)
241 if (EVP_CIPHER_mode(macctx->cipher) != EVP_CIPH_GCM_MODE) {
242 ERR_raise(ERR_LIB_PROV, EVP_R_CIPHER_NOT_GCM_MODE);
246 if (!EVP_EncryptInit_ex(ctx, macctx->cipher, macctx->engine,
250 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
251 if (p->data_type != OSSL_PARAM_OCTET_STRING)
254 if (p->data_size != (size_t)EVP_CIPHER_CTX_key_length(ctx)) {
255 ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY_LENGTH);
258 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, p->data, NULL))
261 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
262 if (p->data_type != OSSL_PARAM_OCTET_STRING)
265 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
267 || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
273 const OSSL_DISPATCH gmac_functions[] = {
274 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
275 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
276 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
277 { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
278 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
279 { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
280 { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
281 { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
282 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
283 (void (*)(void))gmac_settable_ctx_params },
284 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },