1/*
2 * Copyright 2018 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 <stdlib.h>
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>
17
18#include "prov/providercommonerr.h"
19#include "prov/implementations.h"
20#include "prov/provider_ctx.h"
21#include "prov/provider_util.h"
22
23/*
24 * Forward declaration of everything implemented here. This is not strictly
25 * necessary for the compiler, but provides an assurance that the signatures
26 * of the functions in the dispatch table are correct.
27 */
28static OSSL_OP_mac_newctx_fn gmac_new;
29static OSSL_OP_mac_dupctx_fn gmac_dup;
30static OSSL_OP_mac_freectx_fn gmac_free;
31static OSSL_OP_mac_gettable_params_fn gmac_gettable_params;
32static OSSL_OP_mac_get_params_fn gmac_get_params;
33static OSSL_OP_mac_settable_ctx_params_fn gmac_settable_ctx_params;
34static OSSL_OP_mac_set_ctx_params_fn gmac_set_ctx_params;
35static OSSL_OP_mac_init_fn gmac_init;
36static OSSL_OP_mac_update_fn gmac_update;
37static OSSL_OP_mac_final_fn gmac_final;
38
39/* local GMAC pkey structure */
40
41struct gmac_data_st {
42 void *provctx;
43 EVP_CIPHER_CTX *ctx; /* Cipher context */
44 PROV_CIPHER cipher;
45};
46
47static size_t gmac_size(void);
48
49static void gmac_free(void *vmacctx)
50{
51 struct gmac_data_st *macctx = vmacctx;
52
53 if (macctx != NULL) {
54 EVP_CIPHER_CTX_free(macctx->ctx);
55 ossl_prov_cipher_reset(&macctx->cipher);
56 OPENSSL_free(macctx);
57 }
58}
59
60static void *gmac_new(void *provctx)
61{
62 struct gmac_data_st *macctx;
63
64 if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
65 || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
66 gmac_free(macctx);
67 return NULL;
68 }
69 macctx->provctx = provctx;
70
71 return macctx;
72}
73
74static void *gmac_dup(void *vsrc)
75{
76 struct gmac_data_st *src = vsrc;
77 struct gmac_data_st *dst = gmac_new(src->provctx);
78
79 if (dst == NULL)
80 return NULL;
81
82 if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
83 || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
84 gmac_free(dst);
85 return NULL;
86 }
87 return dst;
88}
89
90static int gmac_init(void *vmacctx)
91{
92 return 1;
93}
94
95static int gmac_update(void *vmacctx, const unsigned char *data,
96 size_t datalen)
97{
98 struct gmac_data_st *macctx = vmacctx;
99 EVP_CIPHER_CTX *ctx = macctx->ctx;
100 int outlen;
101
102 while (datalen > INT_MAX) {
103 if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
104 return 0;
105 data += INT_MAX;
106 datalen -= INT_MAX;
107 }
108 return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
109}
110
111static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
112 size_t outsize)
113{
114 struct gmac_data_st *macctx = vmacctx;
115 int hlen = 0;
116
117 if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
118 return 0;
119
120 /* TODO(3.0) Use params */
121 hlen = gmac_size();
122 if (!EVP_CIPHER_CTX_ctrl(macctx->ctx, EVP_CTRL_AEAD_GET_TAG,
123 hlen, out))
124 return 0;
125
126 *outl = hlen;
127 return 1;
128}
129
130static size_t gmac_size(void)
131{
132 return EVP_GCM_TLS_TAG_LEN;
133}
134
135static const OSSL_PARAM known_gettable_params[] = {
136 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
137 OSSL_PARAM_END
138};
139static const OSSL_PARAM *gmac_gettable_params(void)
140{
141 return known_gettable_params;
142}
143
144static int gmac_get_params(OSSL_PARAM params[])
145{
146 OSSL_PARAM *p;
147
148 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
149 return OSSL_PARAM_set_size_t(p, gmac_size());
150
151 return 1;
152}
153
154static const OSSL_PARAM known_settable_ctx_params[] = {
155 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
156 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
157 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
158 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
159 OSSL_PARAM_END
160};
161static const OSSL_PARAM *gmac_settable_ctx_params(void)
162{
163 return known_settable_ctx_params;
164}
165
166/*
167 * ALL parameters should be set before init().
168 */
169static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
170{
171 struct gmac_data_st *macctx = vmacctx;
172 EVP_CIPHER_CTX *ctx = macctx->ctx;
173 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
174 const OSSL_PARAM *p;
175
176 if (ctx == NULL
177 || !ossl_prov_cipher_load_from_params(&macctx->cipher, params, provctx))
178 return 0;
179
180 if (EVP_CIPHER_mode(ossl_prov_cipher_cipher(&macctx->cipher))
181 != EVP_CIPH_GCM_MODE) {
182 ERR_raise(ERR_LIB_PROV, EVP_R_CIPHER_NOT_GCM_MODE);
183 return 0;
184 }
185 if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher),
186 ossl_prov_cipher_engine(&macctx->cipher), NULL,
187 NULL))
188 return 0;
189
190 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
191 if (p->data_type != OSSL_PARAM_OCTET_STRING)
192 return 0;
193
194 if (p->data_size != (size_t)EVP_CIPHER_CTX_key_length(ctx)) {
195 ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY_LENGTH);
196 return 0;
197 }
198 if (!EVP_EncryptInit_ex(ctx, NULL, NULL, p->data, NULL))
199 return 0;
200 }
201 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
202 if (p->data_type != OSSL_PARAM_OCTET_STRING)
203 return 0;
204
205 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
206 p->data_size, NULL)
207 || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
208 return 0;
209 }
210 return 1;
211}
212
213const OSSL_DISPATCH gmac_functions[] = {
214 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
215 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
216 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
217 { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
218 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
219 { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
220 { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
221 { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
222 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
223 (void (*)(void))gmac_settable_ctx_params },
224 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },
225 { 0, NULL }
226};
227