Coverage Report

Created: 2023-09-25 06:41

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