2 * Copyright 2019-2023 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
10 /* Dispatch functions for AES SIV mode */
13 * This file uses the low level AES functions (which are deprecated for
14 * non-internal use) in order to implement provider AES ciphers.
16 #include "internal/deprecated.h"
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"
25 static int ossl_aes_gcm_siv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[]);
27 static void *ossl_aes_gcm_siv_newctx(void *provctx
, size_t keybits
)
29 PROV_AES_GCM_SIV_CTX
*ctx
;
31 if (!ossl_prov_is_running())
34 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
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
;
44 static void ossl_aes_gcm_siv_freectx(void *vctx
)
46 PROV_AES_GCM_SIV_CTX
*ctx
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
51 OPENSSL_clear_free(ctx
->aad
, ctx
->aad_len
);
52 ctx
->hw
->clean_ctx(ctx
);
53 OPENSSL_clear_free(ctx
, sizeof(*ctx
));
56 static void *ossl_aes_gcm_siv_dupctx(void *vctx
)
58 PROV_AES_GCM_SIV_CTX
*in
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
59 PROV_AES_GCM_SIV_CTX
*ret
;
61 if (!ossl_prov_is_running())
67 ret
= OPENSSL_memdup(in
, sizeof(*in
));
70 /* NULL-out these things we create later */
74 if (in
->aad
!= NULL
) {
75 if ((ret
->aad
= OPENSSL_memdup(in
->aad
, UP16(ret
->aad_len
))) == NULL
)
79 if (!in
->hw
->dup_ctx(ret
, in
))
85 OPENSSL_clear_free(ret
->aad
, ret
->aad_len
);
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
)
95 PROV_AES_GCM_SIV_CTX
*ctx
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
97 if (!ossl_prov_is_running())
103 if (keylen
!= ctx
->key_len
) {
104 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
107 memcpy(ctx
->key_gen_key
, key
, ctx
->key_len
);
110 if (ivlen
!= sizeof(ctx
->nonce
)) {
111 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_IV_LENGTH
);
114 memcpy(ctx
->nonce
, iv
, sizeof(ctx
->nonce
));
117 if (!ctx
->hw
->initkey(ctx
))
120 return ossl_aes_gcm_siv_set_ctx_params(ctx
, 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
[])
127 return ossl_aes_gcm_siv_init(vctx
, key
, keylen
, iv
, ivlen
, params
, 1);
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
[])
134 return ossl_aes_gcm_siv_init(vctx
, key
, keylen
, iv
, ivlen
, params
, 0);
137 #define ossl_aes_gcm_siv_stream_update ossl_aes_gcm_siv_cipher
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
)
141 PROV_AES_GCM_SIV_CTX
*ctx
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
144 if (!ossl_prov_is_running())
147 /* The RFC has a test case for this, but we don't try to do anything */
155 ERR_raise(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
);
159 error
|= !ctx
->hw
->cipher(ctx
, out
, in
, inl
);
161 if (outl
!= NULL
&& !error
)
166 static int ossl_aes_gcm_siv_stream_final(void *vctx
, unsigned char *out
, size_t *outl
,
169 PROV_AES_GCM_SIV_CTX
*ctx
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
172 if (!ossl_prov_is_running())
175 error
|= !ctx
->hw
->cipher(vctx
, out
, NULL
, 0);
177 if (outl
!= NULL
&& !error
)
182 static int ossl_aes_gcm_siv_get_ctx_params(void *vctx
, OSSL_PARAM params
[])
184 PROV_AES_GCM_SIV_CTX
*ctx
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
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
);
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
);
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
);
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),
216 static const OSSL_PARAM
*ossl_aes_gcm_siv_gettable_ctx_params(ossl_unused
void *cctx
,
217 ossl_unused
void *provctx
)
219 return aes_gcm_siv_known_gettable_ctx_params
;
222 static int ossl_aes_gcm_siv_set_ctx_params(void *vctx
, const OSSL_PARAM params
[])
224 PROV_AES_GCM_SIV_CTX
*ctx
= (PROV_AES_GCM_SIV_CTX
*)vctx
;
226 unsigned int speed
= 0;
231 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_AEAD_TAG
);
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
);
239 memcpy(ctx
->user_tag
, p
->data
, sizeof(ctx
->tag
));
240 ctx
->have_user_tag
= 1;
243 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_SPEED
);
245 if (!OSSL_PARAM_get_uint(p
, &speed
)) {
246 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_TO_GET_PARAMETER
);
249 ctx
->speed
= !!speed
;
251 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_KEYLEN
);
255 if (!OSSL_PARAM_get_size_t(p
, &key_len
)) {
256 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_TO_GET_PARAMETER
);
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
);
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),
274 static const OSSL_PARAM
*ossl_aes_gcm_siv_settable_ctx_params(ossl_unused
void *cctx
,
275 ossl_unused
void *provctx
)
277 return aes_gcm_siv_known_settable_ctx_params
;
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[]) \
296 return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
297 flags, kbits, blkbits, ivbits); \
299 static void *ossl_##alg##kbits##_##lc##_newctx(void *provctx) \
301 return ossl_##alg##_##lc##_newctx(provctx, kbits); \
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 }, \
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);