Coverage Report

Created: 2024-11-21 07:03

/src/openssl/providers/implementations/ciphers/ciphercommon_ccm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2021 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
/* Dispatch functions for ccm mode */
11
12
#include <openssl/proverr.h>
13
#include "prov/ciphercommon.h"
14
#include "prov/ciphercommon_ccm.h"
15
#include "prov/providercommon.h"
16
17
static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
18
                               size_t *padlen, const unsigned char *in,
19
                               size_t len);
20
21
static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen)
22
0
{
23
0
    size_t len;
24
25
0
    if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
26
0
        return 0;
27
28
    /* Save the aad for later use. */
29
0
    memcpy(ctx->buf, aad, alen);
30
0
    ctx->tls_aad_len = alen;
31
32
0
    len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
33
0
    if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
34
0
        return 0;
35
36
    /* Correct length for explicit iv. */
37
0
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
38
39
0
    if (!ctx->enc) {
40
0
        if (len < ctx->m)
41
0
            return 0;
42
        /* Correct length for tag. */
43
0
        len -= ctx->m;
44
0
    }
45
0
    ctx->buf[alen - 2] = (unsigned char)(len >> 8);
46
0
    ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
47
48
    /* Extra padding: tag appended to record. */
49
0
    return ctx->m;
50
0
}
51
52
static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed,
53
                                size_t flen)
54
0
{
55
0
    if (flen != EVP_CCM_TLS_FIXED_IV_LEN)
56
0
        return 0;
57
58
    /* Copy to first part of the iv. */
59
0
    memcpy(ctx->iv, fixed, flen);
60
0
    return 1;
61
0
}
62
63
static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx)
64
1.31k
{
65
1.31k
    return 15 - ctx->l;
66
1.31k
}
67
68
int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
69
1.20k
{
70
1.20k
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
71
1.20k
    const OSSL_PARAM *p;
72
1.20k
    size_t sz;
73
74
1.20k
    if (ossl_param_is_empty(params))
75
1.13k
        return 1;
76
77
78
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
78
78
    if (p != NULL) {
79
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING) {
80
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
81
0
            return 0;
82
0
        }
83
0
        if ((p->data_size & 1) || (p->data_size < 4) || p->data_size > 16) {
84
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
85
0
            return 0;
86
0
        }
87
88
0
        if (p->data != NULL) {
89
0
            if (ctx->enc) {
90
0
                ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
91
0
                return 0;
92
0
            }
93
0
            memcpy(ctx->buf, p->data, p->data_size);
94
0
            ctx->tag_set = 1;
95
0
        }
96
0
        ctx->m = p->data_size;
97
0
    }
98
99
78
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
100
78
    if (p != NULL) {
101
78
        size_t ivlen;
102
103
78
        if (!OSSL_PARAM_get_size_t(p, &sz)) {
104
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
105
0
            return 0;
106
0
        }
107
78
        ivlen = 15 - sz;
108
78
        if (ivlen < 2 || ivlen > 8) {
109
77
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
110
77
            return 0;
111
77
        }
112
1
        if (ctx->l != ivlen) {
113
1
            ctx->l = ivlen;
114
1
            ctx->iv_set = 0;
115
1
        }
116
1
    }
117
118
1
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
119
1
    if (p != NULL) {
120
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING) {
121
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
122
0
            return 0;
123
0
        }
124
0
        sz = ccm_tls_init(ctx, p->data, p->data_size);
125
0
        if (sz == 0) {
126
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
127
0
            return 0;
128
0
        }
129
0
        ctx->tls_aad_pad_sz = sz;
130
0
    }
131
132
1
    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
133
1
    if (p != NULL) {
134
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING) {
135
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
136
0
            return 0;
137
0
        }
138
0
        if (ccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
139
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
140
0
            return 0;
141
0
        }
142
0
    }
143
144
1
    return 1;
145
1
}
146
147
int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
148
718
{
149
718
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
150
718
    OSSL_PARAM *p;
151
152
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
153
718
    if (p != NULL && !OSSL_PARAM_set_size_t(p, ccm_get_ivlen(ctx))) {
154
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
155
0
        return 0;
156
0
    }
157
158
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
159
718
    if (p != NULL) {
160
0
        size_t m = ctx->m;
161
162
0
        if (!OSSL_PARAM_set_size_t(p, m)) {
163
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
164
0
            return 0;
165
0
        }
166
0
    }
167
168
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
169
718
    if (p != NULL) {
170
0
        if (ccm_get_ivlen(ctx) > p->data_size) {
171
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
172
0
            return 0;
173
0
        }
174
0
        if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
175
0
            && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
176
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
177
0
            return 0;
178
0
        }
179
0
    }
180
181
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
182
718
    if (p != NULL) {
183
0
        if (ccm_get_ivlen(ctx) > p->data_size) {
184
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
185
0
            return 0;
186
0
        }
187
0
        if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
188
0
            && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
189
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
190
0
            return 0;
191
0
        }
192
0
    }
193
194
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
195
718
    if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
196
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
197
0
        return 0;
198
0
    }
199
200
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
201
718
    if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
202
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
203
0
        return 0;
204
0
    }
205
206
718
    p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
207
718
    if (p != NULL) {
208
0
        if (!ctx->enc || !ctx->tag_set) {
209
0
            ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
210
0
            return 0;
211
0
        }
212
0
        if (p->data_type != OSSL_PARAM_OCTET_STRING) {
213
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
214
0
            return 0;
215
0
        }
216
0
        if (!ctx->hw->gettag(ctx, p->data, p->data_size))
217
0
            return 0;
218
0
        ctx->tag_set = 0;
219
0
        ctx->iv_set = 0;
220
0
        ctx->len_set = 0;
221
0
    }
222
718
    return 1;
223
718
}
224
225
static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
226
                    const unsigned char *iv, size_t ivlen,
227
                    const OSSL_PARAM params[], int enc)
228
1.13k
{
229
1.13k
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
230
231
1.13k
    if (!ossl_prov_is_running())
232
0
        return 0;
233
234
1.13k
    ctx->enc = enc;
235
236
1.13k
    if (iv != NULL) {
237
658
        if (ivlen != ccm_get_ivlen(ctx)) {
238
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
239
0
            return 0;
240
0
        }
241
658
        memcpy(ctx->iv, iv, ivlen);
242
658
        ctx->iv_set = 1;
243
658
    }
244
1.13k
    if (key != NULL) {
245
329
        if (keylen != ctx->keylen) {
246
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
247
0
            return 0;
248
0
        }
249
329
        if (!ctx->hw->setkey(ctx, key, keylen))
250
0
            return 0;
251
329
    }
252
1.13k
    return ossl_ccm_set_ctx_params(ctx, params);
253
1.13k
}
254
255
int ossl_ccm_einit(void *vctx, const unsigned char *key, size_t keylen,
256
                   const unsigned char *iv, size_t ivlen,
257
                   const OSSL_PARAM params[])
258
1.12k
{
259
1.12k
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 1);
260
1.12k
}
261
262
int ossl_ccm_dinit(void *vctx, const unsigned char *key, size_t keylen,
263
                   const unsigned char *iv, size_t ivlen,
264
                   const OSSL_PARAM params[])
265
8
{
266
8
    return ccm_init(vctx, key, keylen, iv, ivlen, params, 0);
267
8
}
268
269
int ossl_ccm_stream_update(void *vctx, unsigned char *out, size_t *outl,
270
                           size_t outsize, const unsigned char *in,
271
                           size_t inl)
272
0
{
273
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
274
275
0
    if (outsize < inl) {
276
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
277
0
        return 0;
278
0
    }
279
280
0
    if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
281
0
        ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
282
0
        return 0;
283
0
    }
284
0
    return 1;
285
0
}
286
287
int ossl_ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,
288
                          size_t outsize)
289
0
{
290
0
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
291
0
    int i;
292
293
0
    if (!ossl_prov_is_running())
294
0
        return 0;
295
296
0
    i = ccm_cipher_internal(ctx, out, outl, NULL, 0);
297
0
    if (i <= 0)
298
0
        return 0;
299
300
0
    *outl = 0;
301
0
    return 1;
302
0
}
303
304
int ossl_ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize,
305
                    const unsigned char *in, size_t inl)
306
658
{
307
658
    PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
308
309
658
    if (!ossl_prov_is_running())
310
0
        return 0;
311
312
658
    if (outsize < inl) {
313
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
314
0
        return 0;
315
0
    }
316
317
658
    if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
318
329
        return 0;
319
320
329
    *outl = inl;
321
329
    return 1;
322
658
}
323
324
/* Copy the buffered iv */
325
static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
326
329
{
327
329
    const PROV_CCM_HW *hw = ctx->hw;
328
329
329
    if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
330
0
        return 0;
331
329
    ctx->len_set = 1;
332
329
    return 1;
333
329
}
334
335
static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
336
                          unsigned char *out, size_t *padlen,
337
                          const unsigned char *in, size_t len)
338
0
{
339
0
    int rv = 0;
340
0
    size_t olen = 0;
341
342
0
    if (!ossl_prov_is_running())
343
0
        goto err;
344
345
    /* Encrypt/decrypt must be performed in place */
346
0
    if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
347
0
        goto err;
348
349
    /* If encrypting set explicit IV from sequence number (start of AAD) */
350
0
    if (ctx->enc)
351
0
        memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
352
    /* Get rest of IV from explicit IV */
353
0
    memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
354
    /* Correct length value */
355
0
    len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
356
0
    if (!ccm_set_iv(ctx, len))
357
0
        goto err;
358
359
    /* Use saved AAD */
360
0
    if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
361
0
        goto err;
362
363
    /* Fix buffer to point to payload */
364
0
    in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
365
0
    out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
366
0
    if (ctx->enc) {
367
0
        if (!ctx->hw->auth_encrypt(ctx, in, out, len,  out + len, ctx->m))
368
0
            goto err;
369
0
        olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
370
0
    } else {
371
0
        if (!ctx->hw->auth_decrypt(ctx, in, out, len,
372
0
                                   (unsigned char *)in + len, ctx->m))
373
0
            goto err;
374
0
        olen = len;
375
0
    }
376
0
    rv = 1;
377
0
err:
378
0
    *padlen = olen;
379
0
    return rv;
380
0
}
381
382
static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
383
                               size_t *padlen, const unsigned char *in,
384
                               size_t len)
385
658
{
386
658
    int rv = 0;
387
658
    size_t olen = 0;
388
658
    const PROV_CCM_HW *hw = ctx->hw;
389
390
    /* If no key set, return error */
391
658
    if (!ctx->key_set)
392
0
        return 0;
393
394
658
    if (ctx->tls_aad_len != UNINITIALISED_SIZET)
395
0
        return ccm_tls_cipher(ctx, out, padlen, in, len);
396
397
    /* EVP_*Final() doesn't return any data */
398
658
    if (in == NULL && out != NULL)
399
0
        goto finish;
400
401
658
    if (!ctx->iv_set)
402
0
        goto err;
403
404
658
    if (out == NULL) {
405
0
        if (in == NULL) {
406
0
            if (!ccm_set_iv(ctx, len))
407
0
                goto err;
408
0
        } else {
409
            /* If we have AAD, we need a message length */
410
0
            if (!ctx->len_set && len)
411
0
                goto err;
412
0
            if (!hw->setaad(ctx, in, len))
413
0
                goto err;
414
0
        }
415
658
    } else {
416
        /* If not set length yet do it */
417
658
        if (!ctx->len_set && !ccm_set_iv(ctx, len))
418
0
            goto err;
419
420
658
        if (ctx->enc) {
421
658
            if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
422
329
                goto err;
423
329
            ctx->tag_set = 1;
424
329
        } else {
425
            /* The tag must be set before actually decrypting data */
426
0
            if (!ctx->tag_set)
427
0
                goto err;
428
429
0
            if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
430
0
                goto err;
431
            /* Finished - reset flags so calling this method again will fail */
432
0
            ctx->iv_set = 0;
433
0
            ctx->tag_set = 0;
434
0
            ctx->len_set = 0;
435
0
        }
436
658
    }
437
329
    olen = len;
438
329
finish:
439
329
    rv = 1;
440
658
err:
441
658
    *padlen = olen;
442
658
    return rv;
443
329
}
444
445
void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
446
472
{
447
472
    ctx->keylen = keybits / 8;
448
472
    ctx->key_set = 0;
449
472
    ctx->iv_set = 0;
450
472
    ctx->tag_set = 0;
451
472
    ctx->len_set = 0;
452
472
    ctx->l = 8;
453
472
    ctx->m = 12;
454
472
    ctx->tls_aad_len = UNINITIALISED_SIZET;
455
472
    ctx->hw = hw;
456
472
}