]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/macs/gmac_prov.c
Various cleanup of PROV_R_ reason codes
[thirdparty/openssl.git] / providers / implementations / macs / gmac_prov.c
CommitLineData
d33313be 1/*
fbd2ece1 2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
d33313be
RL
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 <stdlib.h>
23c48d94 11#include <openssl/core_dispatch.h>
d33313be
RL
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>
2741128e 17#include <openssl/proverr.h>
d33313be 18
af3e7e1b 19#include "prov/implementations.h"
ddd21319
RL
20#include "prov/provider_ctx.h"
21#include "prov/provider_util.h"
5b104a81 22#include "prov/providercommon.h"
d33313be
RL
23
24/*
25 * Forward declaration of everything implemented here. This is not strictly
26 * necessary for the compiler, but provides an assurance that the signatures
27 * of the functions in the dispatch table are correct.
28 */
363b1e5d
DMSP
29static OSSL_FUNC_mac_newctx_fn gmac_new;
30static OSSL_FUNC_mac_dupctx_fn gmac_dup;
31static OSSL_FUNC_mac_freectx_fn gmac_free;
32static OSSL_FUNC_mac_gettable_params_fn gmac_gettable_params;
33static OSSL_FUNC_mac_get_params_fn gmac_get_params;
34static OSSL_FUNC_mac_settable_ctx_params_fn gmac_settable_ctx_params;
35static OSSL_FUNC_mac_set_ctx_params_fn gmac_set_ctx_params;
36static OSSL_FUNC_mac_init_fn gmac_init;
37static OSSL_FUNC_mac_update_fn gmac_update;
38static OSSL_FUNC_mac_final_fn gmac_final;
d33313be
RL
39
40/* local GMAC pkey structure */
41
42struct gmac_data_st {
43 void *provctx;
44 EVP_CIPHER_CTX *ctx; /* Cipher context */
76497acf 45 PROV_CIPHER cipher;
d33313be
RL
46};
47
d33313be
RL
48static void gmac_free(void *vmacctx)
49{
50 struct gmac_data_st *macctx = vmacctx;
51
52 if (macctx != NULL) {
53 EVP_CIPHER_CTX_free(macctx->ctx);
76497acf 54 ossl_prov_cipher_reset(&macctx->cipher);
d33313be
RL
55 OPENSSL_free(macctx);
56 }
57}
58
59static void *gmac_new(void *provctx)
60{
61 struct gmac_data_st *macctx;
62
5b104a81
P
63 if (!ossl_prov_is_running())
64 return NULL;
65
d33313be
RL
66 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
67 || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
68 gmac_free(macctx);
69 return NULL;
70 }
71 macctx->provctx = provctx;
72
73 return macctx;
74}
75
76static void *gmac_dup(void *vsrc)
77{
78 struct gmac_data_st *src = vsrc;
5b104a81
P
79 struct gmac_data_st *dst;
80
81 if (!ossl_prov_is_running())
82 return NULL;
d33313be 83
5b104a81 84 dst = gmac_new(src->provctx);
d33313be
RL
85 if (dst == NULL)
86 return NULL;
87
88 if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
76497acf 89 || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
d33313be
RL
90 gmac_free(dst);
91 return NULL;
92 }
d33313be
RL
93 return dst;
94}
95
8ce04db8
RL
96static size_t gmac_size(void)
97{
98 return EVP_GCM_TLS_TAG_LEN;
99}
100
d33313be
RL
101static int gmac_init(void *vmacctx)
102{
5b104a81 103 return ossl_prov_is_running();
d33313be
RL
104}
105
106static int gmac_update(void *vmacctx, const unsigned char *data,
107 size_t datalen)
108{
109 struct gmac_data_st *macctx = vmacctx;
110 EVP_CIPHER_CTX *ctx = macctx->ctx;
111 int outlen;
112
0bc193dd
MC
113 if (datalen == 0)
114 return 1;
115
d33313be
RL
116 while (datalen > INT_MAX) {
117 if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
118 return 0;
119 data += INT_MAX;
120 datalen -= INT_MAX;
121 }
122 return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
123}
124
125static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
126 size_t outsize)
127{
128 struct gmac_data_st *macctx = vmacctx;
129 int hlen = 0;
130
5b104a81
P
131 if (!ossl_prov_is_running())
132 return 0;
133
d33313be
RL
134 if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
135 return 0;
136
137 /* TODO(3.0) Use params */
138 hlen = gmac_size();
139 if (!EVP_CIPHER_CTX_ctrl(macctx->ctx, EVP_CTRL_AEAD_GET_TAG,
140 hlen, out))
141 return 0;
142
143 *outl = hlen;
144 return 1;
145}
146
d33313be 147static const OSSL_PARAM known_gettable_params[] = {
703170d4 148 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
d33313be
RL
149 OSSL_PARAM_END
150};
af5e1e85 151static const OSSL_PARAM *gmac_gettable_params(void *provctx)
d33313be
RL
152{
153 return known_gettable_params;
154}
155
156static int gmac_get_params(OSSL_PARAM params[])
157{
158 OSSL_PARAM *p;
159
703170d4 160 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
d33313be
RL
161 return OSSL_PARAM_set_size_t(p, gmac_size());
162
163 return 1;
164}
165
166static const OSSL_PARAM known_settable_ctx_params[] = {
d33313be 167 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
d33313be
RL
168 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
169 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
170 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
171 OSSL_PARAM_END
172};
1017ab21 173static const OSSL_PARAM *gmac_settable_ctx_params(ossl_unused void *provctx)
d33313be
RL
174{
175 return known_settable_ctx_params;
176}
177
178/*
179 * ALL parameters should be set before init().
180 */
92d9d0ae 181static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
d33313be
RL
182{
183 struct gmac_data_st *macctx = vmacctx;
184 EVP_CIPHER_CTX *ctx = macctx->ctx;
a829b735 185 OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(macctx->provctx);
d33313be
RL
186 const OSSL_PARAM *p;
187
085f1d11
P
188 if (ctx == NULL
189 || !ossl_prov_cipher_load_from_params(&macctx->cipher, params, provctx))
76497acf 190 return 0;
d33313be 191
76497acf
P
192 if (EVP_CIPHER_mode(ossl_prov_cipher_cipher(&macctx->cipher))
193 != EVP_CIPH_GCM_MODE) {
f5f29796 194 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
76497acf 195 return 0;
d33313be 196 }
76497acf
P
197 if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher),
198 ossl_prov_cipher_engine(&macctx->cipher), NULL,
199 NULL))
200 return 0;
201
d33313be
RL
202 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
203 if (p->data_type != OSSL_PARAM_OCTET_STRING)
204 return 0;
205
206 if (p->data_size != (size_t)EVP_CIPHER_CTX_key_length(ctx)) {
f5f29796 207 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
d33313be
RL
208 return 0;
209 }
210 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, p->data, NULL))
211 return 0;
212 }
213 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
214 if (p->data_type != OSSL_PARAM_OCTET_STRING)
215 return 0;
216
217 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
218 p->data_size, NULL)
219 || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
220 return 0;
221 }
222 return 1;
223}
224
1be63951 225const OSSL_DISPATCH ossl_gmac_functions[] = {
d33313be
RL
226 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
227 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
228 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
229 { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
230 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
231 { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
232 { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
233 { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
234 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
235 (void (*)(void))gmac_settable_ctx_params },
92d9d0ae 236 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },
d33313be
RL
237 { 0, NULL }
238};