1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery * This file uses the low level AES functions (which are deprecated for
12*b077aed3SPierre Pronchery * non-internal use) in order to implement provider AES ciphers.
13*b077aed3SPierre Pronchery */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery
16*b077aed3SPierre Pronchery #include "cipher_aes_siv.h"
17*b077aed3SPierre Pronchery
18*b077aed3SPierre Pronchery static void aes_siv_cleanup(void *vctx);
19*b077aed3SPierre Pronchery
aes_siv_initkey(void * vctx,const unsigned char * key,size_t keylen)20*b077aed3SPierre Pronchery static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
21*b077aed3SPierre Pronchery {
22*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
23*b077aed3SPierre Pronchery SIV128_CONTEXT *sctx = &ctx->siv;
24*b077aed3SPierre Pronchery size_t klen = keylen / 2;
25*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx = ctx->libctx;
26*b077aed3SPierre Pronchery const char *propq = NULL;
27*b077aed3SPierre Pronchery
28*b077aed3SPierre Pronchery EVP_CIPHER_free(ctx->cbc);
29*b077aed3SPierre Pronchery EVP_CIPHER_free(ctx->ctr);
30*b077aed3SPierre Pronchery ctx->cbc = NULL;
31*b077aed3SPierre Pronchery ctx->ctr = NULL;
32*b077aed3SPierre Pronchery
33*b077aed3SPierre Pronchery switch (klen) {
34*b077aed3SPierre Pronchery case 16:
35*b077aed3SPierre Pronchery ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", propq);
36*b077aed3SPierre Pronchery ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-128-CTR", propq);
37*b077aed3SPierre Pronchery break;
38*b077aed3SPierre Pronchery case 24:
39*b077aed3SPierre Pronchery ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-192-CBC", propq);
40*b077aed3SPierre Pronchery ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-192-CTR", propq);
41*b077aed3SPierre Pronchery break;
42*b077aed3SPierre Pronchery case 32:
43*b077aed3SPierre Pronchery ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq);
44*b077aed3SPierre Pronchery ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-256-CTR", propq);
45*b077aed3SPierre Pronchery break;
46*b077aed3SPierre Pronchery default:
47*b077aed3SPierre Pronchery break;
48*b077aed3SPierre Pronchery }
49*b077aed3SPierre Pronchery if (ctx->cbc == NULL || ctx->ctr == NULL)
50*b077aed3SPierre Pronchery return 0;
51*b077aed3SPierre Pronchery /*
52*b077aed3SPierre Pronchery * klen is the length of the underlying cipher, not the input key,
53*b077aed3SPierre Pronchery * which should be twice as long
54*b077aed3SPierre Pronchery */
55*b077aed3SPierre Pronchery return ossl_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr, libctx,
56*b077aed3SPierre Pronchery propq);
57*b077aed3SPierre Pronchery }
58*b077aed3SPierre Pronchery
aes_siv_dupctx(void * in_vctx,void * out_vctx)59*b077aed3SPierre Pronchery static int aes_siv_dupctx(void *in_vctx, void *out_vctx)
60*b077aed3SPierre Pronchery {
61*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)in_vctx;
62*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *out = (PROV_AES_SIV_CTX *)out_vctx;
63*b077aed3SPierre Pronchery
64*b077aed3SPierre Pronchery *out = *in;
65*b077aed3SPierre Pronchery out->siv.cipher_ctx = NULL;
66*b077aed3SPierre Pronchery out->siv.mac_ctx_init = NULL;
67*b077aed3SPierre Pronchery out->siv.mac = NULL;
68*b077aed3SPierre Pronchery if (!ossl_siv128_copy_ctx(&out->siv, &in->siv))
69*b077aed3SPierre Pronchery return 0;
70*b077aed3SPierre Pronchery if (out->cbc != NULL)
71*b077aed3SPierre Pronchery EVP_CIPHER_up_ref(out->cbc);
72*b077aed3SPierre Pronchery if (out->ctr != NULL)
73*b077aed3SPierre Pronchery EVP_CIPHER_up_ref(out->ctr);
74*b077aed3SPierre Pronchery return 1;
75*b077aed3SPierre Pronchery }
76*b077aed3SPierre Pronchery
aes_siv_settag(void * vctx,const unsigned char * tag,size_t tagl)77*b077aed3SPierre Pronchery static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
78*b077aed3SPierre Pronchery {
79*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
80*b077aed3SPierre Pronchery SIV128_CONTEXT *sctx = &ctx->siv;
81*b077aed3SPierre Pronchery
82*b077aed3SPierre Pronchery return ossl_siv128_set_tag(sctx, tag, tagl);
83*b077aed3SPierre Pronchery }
84*b077aed3SPierre Pronchery
aes_siv_setspeed(void * vctx,int speed)85*b077aed3SPierre Pronchery static void aes_siv_setspeed(void *vctx, int speed)
86*b077aed3SPierre Pronchery {
87*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
88*b077aed3SPierre Pronchery SIV128_CONTEXT *sctx = &ctx->siv;
89*b077aed3SPierre Pronchery
90*b077aed3SPierre Pronchery ossl_siv128_speed(sctx, (int)speed);
91*b077aed3SPierre Pronchery }
92*b077aed3SPierre Pronchery
aes_siv_cleanup(void * vctx)93*b077aed3SPierre Pronchery static void aes_siv_cleanup(void *vctx)
94*b077aed3SPierre Pronchery {
95*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
96*b077aed3SPierre Pronchery SIV128_CONTEXT *sctx = &ctx->siv;
97*b077aed3SPierre Pronchery
98*b077aed3SPierre Pronchery ossl_siv128_cleanup(sctx);
99*b077aed3SPierre Pronchery EVP_CIPHER_free(ctx->cbc);
100*b077aed3SPierre Pronchery EVP_CIPHER_free(ctx->ctr);
101*b077aed3SPierre Pronchery }
102*b077aed3SPierre Pronchery
aes_siv_cipher(void * vctx,unsigned char * out,const unsigned char * in,size_t len)103*b077aed3SPierre Pronchery static int aes_siv_cipher(void *vctx, unsigned char *out,
104*b077aed3SPierre Pronchery const unsigned char *in, size_t len)
105*b077aed3SPierre Pronchery {
106*b077aed3SPierre Pronchery PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
107*b077aed3SPierre Pronchery SIV128_CONTEXT *sctx = &ctx->siv;
108*b077aed3SPierre Pronchery
109*b077aed3SPierre Pronchery /* EncryptFinal or DecryptFinal */
110*b077aed3SPierre Pronchery if (in == NULL)
111*b077aed3SPierre Pronchery return ossl_siv128_finish(sctx) == 0;
112*b077aed3SPierre Pronchery
113*b077aed3SPierre Pronchery /* Deal with associated data */
114*b077aed3SPierre Pronchery if (out == NULL)
115*b077aed3SPierre Pronchery return (ossl_siv128_aad(sctx, in, len) == 1);
116*b077aed3SPierre Pronchery
117*b077aed3SPierre Pronchery if (ctx->enc)
118*b077aed3SPierre Pronchery return ossl_siv128_encrypt(sctx, in, out, len) > 0;
119*b077aed3SPierre Pronchery
120*b077aed3SPierre Pronchery return ossl_siv128_decrypt(sctx, in, out, len) > 0;
121*b077aed3SPierre Pronchery }
122*b077aed3SPierre Pronchery
123*b077aed3SPierre Pronchery static const PROV_CIPHER_HW_AES_SIV aes_siv_hw =
124*b077aed3SPierre Pronchery {
125*b077aed3SPierre Pronchery aes_siv_initkey,
126*b077aed3SPierre Pronchery aes_siv_cipher,
127*b077aed3SPierre Pronchery aes_siv_setspeed,
128*b077aed3SPierre Pronchery aes_siv_settag,
129*b077aed3SPierre Pronchery aes_siv_cleanup,
130*b077aed3SPierre Pronchery aes_siv_dupctx,
131*b077aed3SPierre Pronchery };
132*b077aed3SPierre Pronchery
ossl_prov_cipher_hw_aes_siv(size_t keybits)133*b077aed3SPierre Pronchery const PROV_CIPHER_HW_AES_SIV *ossl_prov_cipher_hw_aes_siv(size_t keybits)
134*b077aed3SPierre Pronchery {
135*b077aed3SPierre Pronchery return &aes_siv_hw;
136*b077aed3SPierre Pronchery }
137