1 /*
2  * Copyright 2019-2023 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 /* Dispatch functions for AES SIV mode */
11 
12 /*
13  * This file uses the low level AES functions (which are deprecated for
14  * non-internal use) in order to implement provider AES ciphers.
15  */
16 #include "internal/deprecated.h"
17 
18 #include <openssl/proverr.h>
19 #include "prov/implementations.h"
20 #include "prov/providercommon.h"
21 #include "prov/ciphercommon_aead.h"
22 #include "prov/provider_ctx.h"
23 #include "cipher_aes_gcm_siv.h"
24 
25 static int ossl_aes_gcm_siv_set_ctx_params(void *vctx, const OSSL_PARAM params[]);
26 
ossl_aes_gcm_siv_newctx(void * provctx,size_t keybits)27 static void *ossl_aes_gcm_siv_newctx(void *provctx, size_t keybits)
28 {
29     PROV_AES_GCM_SIV_CTX *ctx;
30 
31     if (!ossl_prov_is_running())
32         return NULL;
33 
34     ctx = OPENSSL_zalloc(sizeof(*ctx));
35     if (ctx != NULL) {
36         ctx->key_len = keybits / 8;
37         ctx->hw = ossl_prov_cipher_hw_aes_gcm_siv(keybits);
38         ctx->libctx = PROV_LIBCTX_OF(provctx);
39         ctx->provctx = provctx;
40     }
41     return ctx;
42 }
43 
ossl_aes_gcm_siv_freectx(void * vctx)44 static void ossl_aes_gcm_siv_freectx(void *vctx)
45 {
46     PROV_AES_GCM_SIV_CTX *ctx = (PROV_AES_GCM_SIV_CTX *)vctx;
47 
48     if (ctx == NULL)
49         return;
50 
51     OPENSSL_clear_free(ctx->aad, ctx->aad_len);
52     ctx->hw->clean_ctx(ctx);
53     OPENSSL_clear_free(ctx, sizeof(*ctx));
54 }
55 
ossl_aes_gcm_siv_dupctx(void * vctx)56 static void *ossl_aes_gcm_siv_dupctx(void *vctx)
57 {
58     PROV_AES_GCM_SIV_CTX *in = (PROV_AES_GCM_SIV_CTX *)vctx;
59     PROV_AES_GCM_SIV_CTX *ret;
60 
61     if (!ossl_prov_is_running())
62         return NULL;
63 
64     if (in->hw == NULL)
65         return NULL;
66 
67     ret = OPENSSL_memdup(in, sizeof(*in));
68     if (ret == NULL)
69         return NULL;
70     /* NULL-out these things we create later */
71     ret->aad = NULL;
72     ret->ecb_ctx = NULL;
73 
74     if (in->aad != NULL) {
75         if ((ret->aad = OPENSSL_memdup(in->aad, UP16(ret->aad_len))) == NULL)
76             goto err;
77     }
78 
79     if (!in->hw->dup_ctx(ret, in))
80         goto err;
81 
82     return ret;
83  err:
84     if (ret != NULL) {
85         OPENSSL_clear_free(ret->aad, ret->aad_len);
86         OPENSSL_free(ret);
87     }
88     return NULL;
89 }
90 
ossl_aes_gcm_siv_init(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[],int enc)91 static int ossl_aes_gcm_siv_init(void *vctx, const unsigned char *key, size_t keylen,
92                                  const unsigned char *iv, size_t ivlen,
93                                  const OSSL_PARAM params[], int enc)
94 {
95     PROV_AES_GCM_SIV_CTX *ctx = (PROV_AES_GCM_SIV_CTX *)vctx;
96 
97     if (!ossl_prov_is_running())
98         return 0;
99 
100     ctx->enc = enc;
101 
102     if (key != NULL) {
103         if (keylen != ctx->key_len) {
104             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
105             return 0;
106         }
107         memcpy(ctx->key_gen_key, key, ctx->key_len);
108     }
109     if (iv != NULL) {
110         if (ivlen != sizeof(ctx->nonce)) {
111             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
112             return 0;
113         }
114         memcpy(ctx->nonce, iv, sizeof(ctx->nonce));
115     }
116 
117     if (!ctx->hw->initkey(ctx))
118         return 0;
119 
120     return ossl_aes_gcm_siv_set_ctx_params(ctx, params);
121 }
122 
ossl_aes_gcm_siv_einit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])123 static int ossl_aes_gcm_siv_einit(void *vctx, const unsigned char *key, size_t keylen,
124                                   const unsigned char *iv, size_t ivlen,
125                                   const OSSL_PARAM params[])
126 {
127     return ossl_aes_gcm_siv_init(vctx, key, keylen, iv, ivlen, params, 1);
128 }
129 
ossl_aes_gcm_siv_dinit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])130 static int ossl_aes_gcm_siv_dinit(void *vctx, const unsigned char *key, size_t keylen,
131                                   const unsigned char *iv, size_t ivlen,
132                                   const OSSL_PARAM params[])
133 {
134     return ossl_aes_gcm_siv_init(vctx, key, keylen, iv, ivlen, params, 0);
135 }
136 
137 #define ossl_aes_gcm_siv_stream_update ossl_aes_gcm_siv_cipher
ossl_aes_gcm_siv_cipher(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)138 static int ossl_aes_gcm_siv_cipher(void *vctx, unsigned char *out, size_t *outl,
139                                    size_t outsize, const unsigned char *in, size_t inl)
140 {
141     PROV_AES_GCM_SIV_CTX *ctx = (PROV_AES_GCM_SIV_CTX *)vctx;
142     int error = 0;
143 
144     if (!ossl_prov_is_running())
145         return 0;
146 
147     /* The RFC has a test case for this, but we don't try to do anything */
148     if (inl == 0) {
149         if (outl != NULL)
150             *outl = 0;
151         return 1;
152     }
153 
154     if (outsize < inl) {
155         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
156         return 0;
157     }
158 
159     error |= !ctx->hw->cipher(ctx, out, in, inl);
160 
161     if (outl != NULL && !error)
162         *outl = inl;
163     return !error;
164 }
165 
ossl_aes_gcm_siv_stream_final(void * vctx,unsigned char * out,size_t * outl,size_t outsize)166 static int ossl_aes_gcm_siv_stream_final(void *vctx, unsigned char *out, size_t *outl,
167                                          size_t outsize)
168 {
169     PROV_AES_GCM_SIV_CTX *ctx = (PROV_AES_GCM_SIV_CTX *)vctx;
170     int error = 0;
171 
172     if (!ossl_prov_is_running())
173         return 0;
174 
175     error |= !ctx->hw->cipher(vctx, out, NULL, 0);
176 
177     if (outl != NULL && !error)
178         *outl = 0;
179     return !error;
180 }
181 
ossl_aes_gcm_siv_get_ctx_params(void * vctx,OSSL_PARAM params[])182 static int ossl_aes_gcm_siv_get_ctx_params(void *vctx, OSSL_PARAM params[])
183 {
184     PROV_AES_GCM_SIV_CTX *ctx = (PROV_AES_GCM_SIV_CTX *)vctx;
185     OSSL_PARAM *p;
186 
187     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
188     if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING) {
189         if (!ctx->enc || !ctx->generated_tag
190                 || p->data_size != sizeof(ctx->tag)
191                 || !OSSL_PARAM_set_octet_string(p, ctx->tag, sizeof(ctx->tag))) {
192             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
193             return 0;
194         }
195     }
196     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
197     if (p != NULL && !OSSL_PARAM_set_size_t(p, sizeof(ctx->tag))) {
198         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
199         return 0;
200     }
201     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
202     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->key_len)) {
203         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
204         return 0;
205     }
206     return 1;
207 }
208 
209 static const OSSL_PARAM aes_gcm_siv_known_gettable_ctx_params[] = {
210     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
211     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
212     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
213     OSSL_PARAM_END
214 };
215 
ossl_aes_gcm_siv_gettable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)216 static const OSSL_PARAM *ossl_aes_gcm_siv_gettable_ctx_params(ossl_unused void *cctx,
217                                                               ossl_unused void *provctx)
218 {
219     return aes_gcm_siv_known_gettable_ctx_params;
220 }
221 
ossl_aes_gcm_siv_set_ctx_params(void * vctx,const OSSL_PARAM params[])222 static int ossl_aes_gcm_siv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
223 {
224     PROV_AES_GCM_SIV_CTX *ctx = (PROV_AES_GCM_SIV_CTX *)vctx;
225     const OSSL_PARAM *p;
226     unsigned int speed = 0;
227 
228     if (ossl_param_is_empty(params))
229         return 1;
230 
231     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
232     if (p != NULL) {
233         if (p->data_type != OSSL_PARAM_OCTET_STRING
234                 || p->data_size != sizeof(ctx->user_tag)) {
235             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
236             return 0;
237         }
238         if (!ctx->enc) {
239             memcpy(ctx->user_tag, p->data, sizeof(ctx->tag));
240             ctx->have_user_tag = 1;
241         }
242     }
243     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_SPEED);
244     if (p != NULL) {
245         if (!OSSL_PARAM_get_uint(p, &speed)) {
246             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
247             return 0;
248         }
249         ctx->speed = !!speed;
250     }
251     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
252     if (p != NULL) {
253         size_t key_len;
254 
255         if (!OSSL_PARAM_get_size_t(p, &key_len)) {
256             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
257             return 0;
258         }
259         /* The key length can not be modified */
260         if (key_len != ctx->key_len) {
261             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
262             return 0;
263         }
264     }
265     return 1;
266 }
267 
268 static const OSSL_PARAM aes_gcm_siv_known_settable_ctx_params[] = {
269     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
270     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_SPEED, NULL),
271     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
272     OSSL_PARAM_END
273 };
ossl_aes_gcm_siv_settable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)274 static const OSSL_PARAM *ossl_aes_gcm_siv_settable_ctx_params(ossl_unused void *cctx,
275                                                               ossl_unused void *provctx)
276 {
277     return aes_gcm_siv_known_settable_ctx_params;
278 }
279 
280 #define IMPLEMENT_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits)                                \
281 static OSSL_FUNC_cipher_newctx_fn              ossl_##alg##kbits##_##lc##_newctx;                       \
282 static OSSL_FUNC_cipher_freectx_fn             ossl_##alg##_##lc##_freectx;                             \
283 static OSSL_FUNC_cipher_dupctx_fn              ossl_##alg##_##lc##_dupctx;                              \
284 static OSSL_FUNC_cipher_encrypt_init_fn        ossl_##alg##_##lc##_einit;                               \
285 static OSSL_FUNC_cipher_decrypt_init_fn        ossl_##alg##_##lc##_dinit;                               \
286 static OSSL_FUNC_cipher_update_fn              ossl_##alg##_##lc##_stream_update;                       \
287 static OSSL_FUNC_cipher_final_fn               ossl_##alg##_##lc##_stream_final;                        \
288 static OSSL_FUNC_cipher_cipher_fn              ossl_##alg##_##lc##_cipher;                              \
289 static OSSL_FUNC_cipher_get_params_fn          ossl_##alg##_##kbits##_##lc##_get_params;                \
290 static OSSL_FUNC_cipher_get_ctx_params_fn      ossl_##alg##_##lc##_get_ctx_params;                      \
291 static OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_##alg##_##lc##_gettable_ctx_params;                 \
292 static OSSL_FUNC_cipher_set_ctx_params_fn      ossl_##alg##_##lc##_set_ctx_params;                      \
293 static OSSL_FUNC_cipher_settable_ctx_params_fn ossl_##alg##_##lc##_settable_ctx_params;                 \
294 static int ossl_##alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[])                                \
295 {                                                                                                       \
296     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,                             \
297                                           flags, kbits, blkbits, ivbits);                               \
298 }                                                                                                       \
299 static void *ossl_##alg##kbits##_##lc##_newctx(void *provctx)                                          \
300 {                                                                                                       \
301     return ossl_##alg##_##lc##_newctx(provctx, kbits);                                                  \
302 }                                                                                                       \
303 const OSSL_DISPATCH ossl_##alg##kbits##lc##_functions[] = {                                             \
304     { OSSL_FUNC_CIPHER_NEWCTX,              (void (*)(void))ossl_##alg##kbits##_##lc##_newctx },        \
305     { OSSL_FUNC_CIPHER_FREECTX,             (void (*)(void))ossl_##alg##_##lc##_freectx },              \
306     { OSSL_FUNC_CIPHER_DUPCTX,              (void (*)(void))ossl_##alg##_##lc##_dupctx },               \
307     { OSSL_FUNC_CIPHER_ENCRYPT_INIT,        (void (*)(void))ossl_##alg##_##lc##_einit },                \
308     { OSSL_FUNC_CIPHER_DECRYPT_INIT,        (void (*)(void))ossl_##alg##_##lc##_dinit },                \
309     { OSSL_FUNC_CIPHER_UPDATE,              (void (*)(void))ossl_##alg##_##lc##_stream_update },        \
310     { OSSL_FUNC_CIPHER_FINAL,               (void (*)(void))ossl_##alg##_##lc##_stream_final },         \
311     { OSSL_FUNC_CIPHER_CIPHER,              (void (*)(void))ossl_##alg##_##lc##_cipher },               \
312     { OSSL_FUNC_CIPHER_GET_PARAMS,          (void (*)(void))ossl_##alg##_##kbits##_##lc##_get_params }, \
313     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,     (void (*)(void))ossl_cipher_generic_gettable_params },      \
314     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,      (void (*)(void))ossl_##alg##_##lc##_get_ctx_params },       \
315     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, (void (*)(void))ossl_##alg##_##lc##_gettable_ctx_params },  \
316     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,      (void (*)(void))ossl_##alg##_##lc##_set_ctx_params },       \
317     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, (void (*)(void))ossl_##alg##_##lc##_settable_ctx_params },  \
318     OSSL_DISPATCH_END                                                                                   \
319 }
320 
321 IMPLEMENT_cipher(aes, gcm_siv, GCM_SIV, AEAD_FLAGS, 128, 8, 96);
322 IMPLEMENT_cipher(aes, gcm_siv, GCM_SIV, AEAD_FLAGS, 192, 8, 96);
323 IMPLEMENT_cipher(aes, gcm_siv, GCM_SIV, AEAD_FLAGS, 256, 8, 96);
324