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