Coverage Report

Created: 2022-07-22 12:05

/libfido2/src/aes256.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021 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
 */
6
7
#include "fido.h"
8
9
static int
10
aes256_cbc(const fido_blob_t *key, const u_char *iv, const fido_blob_t *in,
11
    fido_blob_t *out, int encrypt)
12
2.02k
{
13
2.02k
        EVP_CIPHER_CTX *ctx = NULL;
14
2.02k
        const EVP_CIPHER *cipher;
15
2.02k
        int ok = -1;
16
17
2.02k
        memset(out, 0, sizeof(*out));
18
19
2.02k
        if (key->len != 32) {
20
0
                fido_log_debug("%s: invalid key len %zu", __func__, key->len);
21
0
                goto fail;
22
0
        }
23
2.02k
        if (in->len > UINT_MAX || in->len % 16 || in->len == 0) {
24
76
                fido_log_debug("%s: invalid input len %zu", __func__, in->len);
25
76
                goto fail;
26
76
        }
27
1.95k
        out->len = in->len;
28
1.95k
        if ((out->ptr = calloc(1, out->len)) == NULL) {
29
10
                fido_log_debug("%s: calloc", __func__);
30
10
                goto fail;
31
10
        }
32
1.94k
        if ((ctx = EVP_CIPHER_CTX_new()) == NULL ||
33
1.94k
            (cipher = EVP_aes_256_cbc()) == NULL) {
34
22
                fido_log_debug("%s: EVP_CIPHER_CTX_new", __func__);
35
22
                goto fail;
36
22
        }
37
1.92k
        if (EVP_CipherInit(ctx, cipher, key->ptr, iv, encrypt) == 0 ||
38
1.92k
            EVP_Cipher(ctx, out->ptr, in->ptr, (u_int)out->len) < 0) {
39
18
                fido_log_debug("%s: EVP_Cipher", __func__);
40
18
                goto fail;
41
18
        }
42
43
1.90k
        ok = 0;
44
2.02k
fail:
45
2.02k
        if (ctx != NULL)
46
1.93k
                EVP_CIPHER_CTX_free(ctx);
47
2.02k
        if (ok < 0)
48
126
                fido_blob_reset(out);
49
50
2.02k
        return ok;
51
1.90k
}
52
53
static int
54
aes256_cbc_proto1(const fido_blob_t *key, const fido_blob_t *in,
55
    fido_blob_t *out, int encrypt)
56
1.61k
{
57
1.61k
        u_char iv[16];
58
59
1.61k
        memset(&iv, 0, sizeof(iv));
60
61
1.61k
        return aes256_cbc(key, iv, in, out, encrypt);
62
1.61k
}
63
64
static int
65
aes256_cbc_fips(const fido_blob_t *secret, const fido_blob_t *in,
66
    fido_blob_t *out, int encrypt)
67
430
{
68
430
        fido_blob_t key, cin, cout;
69
430
        u_char iv[16];
70
71
430
        memset(out, 0, sizeof(*out));
72
73
430
        if (secret->len != 64) {
74
0
                fido_log_debug("%s: invalid secret len %zu", __func__,
75
0
                    secret->len);
76
0
                return -1;
77
0
        }
78
430
        if (in->len < sizeof(iv)) {
79
6
                fido_log_debug("%s: invalid input len %zu", __func__, in->len);
80
6
                return -1;
81
6
        }
82
424
        if (encrypt) {
83
353
                if (fido_get_random(iv, sizeof(iv)) < 0) {
84
6
                        fido_log_debug("%s: fido_get_random", __func__);
85
6
                        return -1;
86
6
                }
87
347
                cin = *in;
88
347
        } else {
89
71
                memcpy(iv, in->ptr, sizeof(iv));
90
71
                cin.ptr = in->ptr + sizeof(iv);
91
71
                cin.len = in->len - sizeof(iv);
92
71
        }
93
418
        key.ptr = secret->ptr + 32;
94
418
        key.len = secret->len - 32;
95
418
        if (aes256_cbc(&key, iv, &cin, &cout, encrypt) < 0)
96
71
                return -1;
97
347
        if (encrypt) {
98
326
                if (cout.len > SIZE_MAX - sizeof(iv) ||
99
326
                    (out->ptr = calloc(1, sizeof(iv) + cout.len)) == NULL) {
100
6
                        fido_blob_reset(&cout);
101
6
                        return -1;
102
6
                }
103
320
                out->len = sizeof(iv) + cout.len;
104
320
                memcpy(out->ptr, iv, sizeof(iv));
105
320
                memcpy(out->ptr + sizeof(iv), cout.ptr, cout.len);
106
320
                fido_blob_reset(&cout);
107
320
        } else
108
21
                *out = cout;
109
110
341
        return 0;
111
347
}
112
113
static int
114
aes256_gcm(const fido_blob_t *key, const fido_blob_t *nonce,
115
    const fido_blob_t *aad, const fido_blob_t *in, fido_blob_t *out,
116
    int encrypt)
117
857
{
118
857
        EVP_CIPHER_CTX *ctx = NULL;
119
857
        const EVP_CIPHER *cipher;
120
857
        size_t textlen;
121
857
        int ok = -1;
122
123
857
        memset(out, 0, sizeof(*out));
124
125
857
        if (nonce->len != 12 || key->len != 32 || aad->len > UINT_MAX) {
126
0
                fido_log_debug("%s: invalid params %zu, %zu, %zu", __func__,
127
0
                    nonce->len, key->len, aad->len);
128
0
                goto fail;
129
0
        }
130
857
        if (in->len > UINT_MAX || in->len > SIZE_MAX - 16 || in->len < 16) {
131
69
                fido_log_debug("%s: invalid input len %zu", __func__, in->len);
132
69
                goto fail;
133
69
        }
134
        /* add tag to (on encrypt) or trim tag from the output (on decrypt) */
135
788
        out->len = encrypt ? in->len + 16 : in->len - 16;
136
788
        if ((out->ptr = calloc(1, out->len)) == NULL) {
137
6
                fido_log_debug("%s: calloc", __func__);
138
6
                goto fail;
139
6
        }
140
782
        if ((ctx = EVP_CIPHER_CTX_new()) == NULL ||
141
782
            (cipher = EVP_aes_256_gcm()) == NULL) {
142
10
                fido_log_debug("%s: EVP_CIPHER_CTX_new", __func__);
143
10
                goto fail;
144
10
        }
145
772
        if (EVP_CipherInit(ctx, cipher, key->ptr, nonce->ptr, encrypt) == 0) {
146
5
                fido_log_debug("%s: EVP_CipherInit", __func__);
147
5
                goto fail;
148
5
        }
149
150
767
        if (encrypt)
151
390
                textlen = in->len;
152
377
        else {
153
377
                textlen = in->len - 16;
154
                /* point openssl at the mac tag */
155
377
                if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16,
156
377
                    in->ptr + in->len - 16) == 0) {
157
5
                        fido_log_debug("%s: EVP_CIPHER_CTX_ctrl", __func__);
158
5
                        goto fail;
159
5
                }
160
377
        }
161
        /* the last EVP_Cipher() will either compute or verify the mac tag */
162
762
        if (EVP_Cipher(ctx, NULL, aad->ptr, (u_int)aad->len) < 0 ||
163
762
            EVP_Cipher(ctx, out->ptr, in->ptr, (u_int)textlen) < 0 ||
164
762
            EVP_Cipher(ctx, NULL, NULL, 0) < 0) {
165
174
                fido_log_debug("%s: EVP_Cipher", __func__);
166
174
                goto fail;
167
174
        }
168
588
        if (encrypt) {
169
                /* append the mac tag */
170
385
                if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16,
171
385
                    out->ptr + out->len - 16) == 0) {
172
1
                        fido_log_debug("%s: EVP_CIPHER_CTX_ctrl", __func__);
173
1
                        goto fail;
174
1
                }
175
385
        }
176
177
587
        ok = 0;
178
857
fail:
179
857
        if (ctx != NULL)
180
776
                EVP_CIPHER_CTX_free(ctx);
181
857
        if (ok < 0)
182
270
                fido_blob_reset(out);
183
184
857
        return ok;
185
587
}
186
187
int
188
aes256_cbc_enc(const fido_dev_t *dev, const fido_blob_t *secret,
189
    const fido_blob_t *in, fido_blob_t *out)
190
1.28k
{
191
1.28k
        return fido_dev_get_pin_protocol(dev) == 2 ? aes256_cbc_fips(secret,
192
936
            in, out, 1) : aes256_cbc_proto1(secret, in, out, 1);
193
1.28k
}
194
195
int
196
aes256_cbc_dec(const fido_dev_t *dev, const fido_blob_t *secret,
197
    const fido_blob_t *in, fido_blob_t *out)
198
752
{
199
752
        return fido_dev_get_pin_protocol(dev) == 2 ? aes256_cbc_fips(secret,
200
675
            in, out, 0) : aes256_cbc_proto1(secret, in, out, 0);
201
752
}
202
203
int
204
aes256_gcm_enc(const fido_blob_t *key, const fido_blob_t *nonce,
205
    const fido_blob_t *aad, const fido_blob_t *in, fido_blob_t *out)
206
468
{
207
468
        return aes256_gcm(key, nonce, aad, in, out, 1);
208
468
}
209
210
int
211
aes256_gcm_dec(const fido_blob_t *key, const fido_blob_t *nonce,
212
    const fido_blob_t *aad, const fido_blob_t *in, fido_blob_t *out)
213
389
{
214
389
        return aes256_gcm(key, nonce, aad, in, out, 0);
215
389
}