Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/providers/implementations/ciphers/cipher_aes_siv_hw.c
48383 views
1
/*
2
* Copyright 2019-2025 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
/*
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
16
#include "cipher_aes_siv.h"
17
18
static void aes_siv_cleanup(void *vctx);
19
20
static 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;
25
OSSL_LIB_CTX *libctx = ctx->libctx;
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;
32
33
switch (klen) {
34
case 16:
35
ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", propq);
36
ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-128-CTR", propq);
37
break;
38
case 24:
39
ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-192-CBC", propq);
40
ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-192-CTR", propq);
41
break;
42
case 32:
43
ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq);
44
ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-256-CTR", propq);
45
break;
46
default:
47
break;
48
}
49
if (ctx->cbc == NULL || ctx->ctr == NULL)
50
return 0;
51
/*
52
* klen is the length of the underlying cipher, not the input key,
53
* which should be twice as long
54
*/
55
return ossl_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr, libctx,
56
propq);
57
}
58
59
static 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
if (in->cbc != NULL && !EVP_CIPHER_up_ref(in->cbc))
65
return 0;
66
if (in->ctr != NULL && !EVP_CIPHER_up_ref(in->ctr)) {
67
EVP_CIPHER_free(in->cbc);
68
return 0;
69
}
70
71
*out = *in;
72
out->siv.cipher_ctx = NULL;
73
out->siv.mac_ctx_init = NULL;
74
out->siv.mac = NULL;
75
if (!ossl_siv128_copy_ctx(&out->siv, &in->siv))
76
return 0;
77
78
return 1;
79
}
80
81
static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
82
{
83
PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
84
SIV128_CONTEXT *sctx = &ctx->siv;
85
86
return ossl_siv128_set_tag(sctx, tag, tagl);
87
}
88
89
static void aes_siv_setspeed(void *vctx, int speed)
90
{
91
PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
92
SIV128_CONTEXT *sctx = &ctx->siv;
93
94
ossl_siv128_speed(sctx, (int)speed);
95
}
96
97
static void aes_siv_cleanup(void *vctx)
98
{
99
PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
100
SIV128_CONTEXT *sctx = &ctx->siv;
101
102
ossl_siv128_cleanup(sctx);
103
EVP_CIPHER_free(ctx->cbc);
104
EVP_CIPHER_free(ctx->ctr);
105
}
106
107
static int aes_siv_cipher(void *vctx, unsigned char *out,
108
const unsigned char *in, size_t len)
109
{
110
PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
111
SIV128_CONTEXT *sctx = &ctx->siv;
112
113
/* EncryptFinal or DecryptFinal */
114
if (in == NULL)
115
return ossl_siv128_finish(sctx) == 0;
116
117
/* Deal with associated data */
118
if (out == NULL)
119
return (ossl_siv128_aad(sctx, in, len) == 1);
120
121
if (ctx->enc)
122
return ossl_siv128_encrypt(sctx, in, out, len) > 0;
123
124
return ossl_siv128_decrypt(sctx, in, out, len) > 0;
125
}
126
127
static const PROV_CIPHER_HW_AES_SIV aes_siv_hw = {
128
aes_siv_initkey,
129
aes_siv_cipher,
130
aes_siv_setspeed,
131
aes_siv_settag,
132
aes_siv_cleanup,
133
aes_siv_dupctx,
134
};
135
136
const PROV_CIPHER_HW_AES_SIV *ossl_prov_cipher_hw_aes_siv(size_t keybits)
137
{
138
return &aes_siv_hw;
139
}
140
141