Debian Codegraph @

src/aes256.c

raw
1 /*
2 * Copyright (c) 2021-2026 Yubico AB. All rights reserved.
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "fido.h"
9
10 static const EVP_CIPHER *
11 aes_cbc_cipher(size_t keysize)
12 {
13 switch (keysize) {
14 case 16:
15 return EVP_aes_128_cbc();
16 case 32:
17 return EVP_aes_256_cbc();
18 default:
19 return NULL;
20 }
21 }
22
23 static int
24 aes_cbc(const fido_blob_t *key, const u_char *iv, const fido_blob_t *in,
25 fido_blob_t *out, int encrypt)
26 {
27 EVP_CIPHER_CTX *ctx = NULL;
28 const EVP_CIPHER *cipher;
29 int ok = -1;
30
31 memset(out, 0, sizeof(*out));
32
33 if (in->len > UINT_MAX || in->len % 16 || in->len == 0) {
34 fido_log_debug("%s: invalid input len %zu", __func__, in->len);
35 goto fail;
36 }
37 out->len = in->len;
38 if ((out->ptr = calloc(1, out->len)) == NULL) {
39 fido_log_debug("%s: calloc", __func__);
40 goto fail;
41 }
42 if ((ctx = EVP_CIPHER_CTX_new()) == NULL ||
43 (cipher = aes_cbc_cipher(key->len)) == NULL) {
44 fido_log_debug("%s: EVP_CIPHER_CTX_new", __func__);
45 goto fail;
46 }
47 if (EVP_CipherInit(ctx, cipher, key->ptr, iv, encrypt) == 0 ||
48 EVP_Cipher(ctx, out->ptr, in->ptr, (u_int)out->len) < 0) {
49 fido_log_debug("%s: EVP_Cipher", __func__);
50 goto fail;
51 }
52
53 ok = 0;
54 fail:
55 if (ctx != NULL)
56 EVP_CIPHER_CTX_free(ctx);
57 if (ok < 0)
58 fido_blob_reset(out);
59
60 return ok;
61 }
62
63 int
64 aes128_cbc_dec(const fido_blob_t *key, const fido_blob_t *in, fido_blob_t *out)
65 {
66 const uint8_t *iv;
67 fido_blob_t cin;
68
69 if (key->len != 16) {
70 fido_log_debug("%s: invalid key len %zu", __func__, key->len);
71 return -1;
72 }
73 if (in->len < 16) {
74 fido_log_debug("%s: invalid input len %zu", __func__, in->len);
75 return -1;
76 }
77 cin.ptr = in->ptr + 16;
78 cin.len = in->len - 16;
79 iv = in->ptr;
80 return aes_cbc(key, iv, &cin, out, 0);
81 }
82
83 static int
84 aes256_cbc(const fido_blob_t *key, const u_char *iv, const fido_blob_t *in,
85 fido_blob_t *out, int encrypt)
86 {
87 if (key->len != 32) {
88 fido_log_debug("%s: invalid key len %zu", __func__, key->len);
89 return -1;
90 }
91 return aes_cbc(key, iv, in, out, encrypt);
92 }
93
94 static int
95 aes256_cbc_proto1(const fido_blob_t *key, const fido_blob_t *in,
96 fido_blob_t *out, int encrypt)
97 {
98 u_char iv[16];
99
100 memset(&iv, 0, sizeof(iv));
101
102 return aes256_cbc(key, iv, in, out, encrypt);
103 }
104
105 static int
106 aes256_cbc_fips(const fido_blob_t *secret, const fido_blob_t *in,
107 fido_blob_t *out, int encrypt)
108 {
109 fido_blob_t key, cin, cout;
110 u_char iv[16];
111
112 memset(out, 0, sizeof(*out));
113
114 if (secret->len != 64) {
115 fido_log_debug("%s: invalid secret len %zu", __func__,
116 secret->len);
117 return -1;
118 }
119 if (in->len < sizeof(iv)) {
120 fido_log_debug("%s: invalid input len %zu", __func__, in->len);
121 return -1;
122 }
123 if (encrypt) {
124 if (fido_get_random(iv, sizeof(iv)) < 0) {
125 fido_log_debug("%s: fido_get_random", __func__);
126 return -1;
127 }
128 cin = *in;
129 } else {
130 memcpy(iv, in->ptr, sizeof(iv));
131 cin.ptr = in->ptr + sizeof(iv);
132 cin.len = in->len - sizeof(iv);
133 }
134 key.ptr = secret->ptr + 32;
135 key.len = secret->len - 32;
136 if (aes256_cbc(&key, iv, &cin, &cout, encrypt) < 0)
137 return -1;
138 if (encrypt) {
139 if (cout.len > SIZE_MAX - sizeof(iv) ||
140 (out->ptr = calloc(1, sizeof(iv) + cout.len)) == NULL) {
141 fido_blob_reset(&cout);
142 return -1;
143 }
144 out->len = sizeof(iv) + cout.len;
145 memcpy(out->ptr, iv, sizeof(iv));
146 memcpy(out->ptr + sizeof(iv), cout.ptr, cout.len);
147 fido_blob_reset(&cout);
148 } else
149 *out = cout;
150
151 return 0;
152 }
153
154 static int
155 aes256_gcm(const fido_blob_t *key, const fido_blob_t *nonce,
156 const fido_blob_t *aad, const fido_blob_t *in, fido_blob_t *out,
157 int encrypt)
158 {
159 EVP_CIPHER_CTX *ctx = NULL;
160 const EVP_CIPHER *cipher;
161 size_t textlen;
162 int ok = -1;
163
164 memset(out, 0, sizeof(*out));
165
166 if (nonce->len != 12 || key->len != 32 || aad->len > UINT_MAX) {
167 fido_log_debug("%s: invalid params %zu, %zu, %zu", __func__,
168 nonce->len, key->len, aad->len);
169 goto fail;
170 }
171 if (in->len > UINT_MAX || in->len > SIZE_MAX - 16) {
172 fido_log_debug("%s: invalid input len %zu", __func__, in->len);
173 goto fail;
174 }
175 if (!encrypt && in->len < 16) {
176 fido_log_debug("%s: invalid input len %zu", __func__, in->len);
177 goto fail;
178 }
179 /* add tag to (on encrypt) or trim tag from the output (on decrypt) */
180 out->len = encrypt ? in->len + 16 : in->len - 16;
181 if ((out->ptr = calloc(1, out->len)) == NULL) {
182 fido_log_debug("%s: calloc", __func__);
183 goto fail;
184 }
185 if ((ctx = EVP_CIPHER_CTX_new()) == NULL ||
186 (cipher = EVP_aes_256_gcm()) == NULL) {
187 fido_log_debug("%s: EVP_CIPHER_CTX_new", __func__);
188 goto fail;
189 }
190 if (EVP_CipherInit(ctx, cipher, key->ptr, nonce->ptr, encrypt) == 0) {
191 fido_log_debug("%s: EVP_CipherInit", __func__);
192 goto fail;
193 }
194
195 if (encrypt)
196 textlen = in->len;
197 else {
198 textlen = in->len - 16;
199 /* point openssl at the mac tag */
200 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16,
201 in->ptr + in->len - 16) == 0) {
202 fido_log_debug("%s: EVP_CIPHER_CTX_ctrl", __func__);
203 goto fail;
204 }
205 }
206 /* the last EVP_Cipher() will either compute or verify the mac tag */
207 if (EVP_Cipher(ctx, NULL, aad->ptr, (u_int)aad->len) < 0 ||
208 EVP_Cipher(ctx, out->ptr, in->ptr, (u_int)textlen) < 0 ||
209 EVP_Cipher(ctx, NULL, NULL, 0) < 0) {
210 fido_log_debug("%s: EVP_Cipher", __func__);
211 goto fail;
212 }
213 if (encrypt) {
214 /* append the mac tag */
215 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16,
216 out->ptr + out->len - 16) == 0) {
217 fido_log_debug("%s: EVP_CIPHER_CTX_ctrl", __func__);
218 goto fail;
219 }
220 }
221
222 ok = 0;
223 fail:
224 if (ctx != NULL)
225 EVP_CIPHER_CTX_free(ctx);
226 if (ok < 0)
227 fido_blob_reset(out);
228
229 return ok;
230 }
231
232 int
233 aes256_cbc_enc(const fido_dev_t *dev, const fido_blob_t *secret,
234 const fido_blob_t *in, fido_blob_t *out)
235 {
236 return fido_dev_get_pin_protocol(dev) == 2 ? aes256_cbc_fips(secret,
237 in, out, 1) : aes256_cbc_proto1(secret, in, out, 1);
238 }
239
240 int
241 aes256_cbc_dec(const fido_dev_t *dev, const fido_blob_t *secret,
242 const fido_blob_t *in, fido_blob_t *out)
243 {
244 return fido_dev_get_pin_protocol(dev) == 2 ? aes256_cbc_fips(secret,
245 in, out, 0) : aes256_cbc_proto1(secret, in, out, 0);
246 }
247
248 int
249 aes256_gcm_enc(const fido_blob_t *key, const fido_blob_t *nonce,
250 const fido_blob_t *aad, const fido_blob_t *in, fido_blob_t *out)
251 {
252 return aes256_gcm(key, nonce, aad, in, out, 1);
253 }
254
255 int
256 aes256_gcm_dec(const fido_blob_t *key, const fido_blob_t *nonce,
257 const fido_blob_t *aad, const fido_blob_t *in, fido_blob_t *out)
258 {
259 return aes256_gcm(key, nonce, aad, in, out, 0);
260 }