Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/cmac/cmac.c
Line
Count
Source
1
/*
2
 * Copyright 2010-2024 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
 * CMAC low level APIs are deprecated for public use, but still ok for internal
12
 * use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include "internal/cryptlib.h"
20
#include <openssl/cmac.h>
21
#include <openssl/err.h>
22
#include "crypto/cmac.h"
23
24
1.29k
#define LOCAL_BUF_SIZE 2048
25
struct CMAC_CTX_st {
26
    /* Cipher context to use */
27
    EVP_CIPHER_CTX *cctx;
28
    /* Keys k1 and k2 */
29
    unsigned char k1[EVP_MAX_BLOCK_LENGTH];
30
    unsigned char k2[EVP_MAX_BLOCK_LENGTH];
31
    /* Temporary block */
32
    unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
33
    /* Last (possibly partial) block */
34
    unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
35
    /* Number of bytes in last block: -1 means context not initialised */
36
    int nlast_block;
37
};
38
39
/* Make temporary keys K1 and K2 */
40
41
static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
42
746
{
43
746
    int i;
44
746
    unsigned char c = l[0], carry = c >> 7, cnext;
45
46
    /* Shift block to left, including carry */
47
10.4k
    for (i = 0; i < bl - 1; i++, c = cnext)
48
9.68k
        k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
49
50
    /* If MSB set fixup with R */
51
746
    k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
52
746
}
53
54
CMAC_CTX *CMAC_CTX_new(void)
55
996
{
56
996
    CMAC_CTX *ctx;
57
58
996
    if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL)
59
0
        return NULL;
60
996
    ctx->cctx = EVP_CIPHER_CTX_new();
61
996
    if (ctx->cctx == NULL) {
62
0
        OPENSSL_free(ctx);
63
0
        return NULL;
64
0
    }
65
996
    ctx->nlast_block = -1;
66
996
    return ctx;
67
996
}
68
69
void CMAC_CTX_cleanup(CMAC_CTX *ctx)
70
996
{
71
996
    EVP_CIPHER_CTX_reset(ctx->cctx);
72
996
    OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
73
996
    OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
74
996
    OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
75
996
    OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
76
996
    ctx->nlast_block = -1;
77
996
}
78
79
EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
80
669
{
81
669
    return ctx->cctx;
82
669
}
83
84
void CMAC_CTX_free(CMAC_CTX *ctx)
85
996
{
86
996
    if (!ctx)
87
0
        return;
88
996
    CMAC_CTX_cleanup(ctx);
89
996
    EVP_CIPHER_CTX_free(ctx->cctx);
90
996
    OPENSSL_free(ctx);
91
996
}
92
93
int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
94
468
{
95
468
    int bl;
96
97
468
    if (in->nlast_block == -1)
98
0
        return 0;
99
468
    if ((bl = EVP_CIPHER_CTX_get_block_size(in->cctx)) == 0)
100
0
        return 0;
101
468
    if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
102
0
        return 0;
103
468
    memcpy(out->k1, in->k1, bl);
104
468
    memcpy(out->k2, in->k2, bl);
105
468
    memcpy(out->tbl, in->tbl, bl);
106
468
    memcpy(out->last_block, in->last_block, bl);
107
468
    out->nlast_block = in->nlast_block;
108
468
    return 1;
109
468
}
110
111
int ossl_cmac_init(CMAC_CTX *ctx, const void *key, size_t keylen,
112
    const EVP_CIPHER *cipher, ENGINE *impl,
113
    const OSSL_PARAM param[])
114
191
{
115
191
    static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
116
191
    int block_len;
117
118
    /* All zeros means restart */
119
191
    if (!key && !cipher && !impl && keylen == 0) {
120
        /* Not initialised */
121
0
        if (ctx->nlast_block == -1)
122
0
            return 0;
123
0
        if (!EVP_EncryptInit_ex2(ctx->cctx, NULL, NULL, zero_iv, param))
124
0
            return 0;
125
0
        block_len = EVP_CIPHER_CTX_get_block_size(ctx->cctx);
126
0
        if (block_len == 0)
127
0
            return 0;
128
0
        memset(ctx->tbl, 0, block_len);
129
0
        ctx->nlast_block = 0;
130
0
        return 1;
131
0
    }
132
    /* Initialise context */
133
191
    if (cipher != NULL) {
134
        /* Ensure we can't use this ctx until we also have a key */
135
159
        ctx->nlast_block = -1;
136
159
        if (impl != NULL) {
137
0
            if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
138
0
                return 0;
139
159
        } else {
140
159
            if (!EVP_EncryptInit_ex2(ctx->cctx, cipher, NULL, NULL, param))
141
0
                return 0;
142
159
        }
143
159
    }
144
    /* Non-NULL key means initialisation complete */
145
191
    if (key != NULL) {
146
191
        int bl;
147
148
        /* If anything fails then ensure we can't use this ctx */
149
191
        ctx->nlast_block = -1;
150
191
        if (EVP_CIPHER_CTX_get0_cipher(ctx->cctx) == NULL)
151
3
            return 0;
152
188
        if (EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen) <= 0)
153
93
            return 0;
154
95
        if (!EVP_EncryptInit_ex2(ctx->cctx, NULL, key, zero_iv, param))
155
0
            return 0;
156
95
        if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) < 0)
157
0
            return 0;
158
95
        if (EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl) <= 0)
159
0
            return 0;
160
95
        make_kn(ctx->k1, ctx->tbl, bl);
161
95
        make_kn(ctx->k2, ctx->k1, bl);
162
95
        OPENSSL_cleanse(ctx->tbl, bl);
163
        /* Reset context again ready for first data block */
164
95
        if (!EVP_EncryptInit_ex2(ctx->cctx, NULL, NULL, zero_iv, param))
165
0
            return 0;
166
        /* Zero tbl so resume works */
167
95
        memset(ctx->tbl, 0, bl);
168
95
        ctx->nlast_block = 0;
169
95
    }
170
95
    return 1;
171
191
}
172
173
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
174
    const EVP_CIPHER *cipher, ENGINE *impl)
175
0
{
176
0
    return ossl_cmac_init(ctx, key, keylen, cipher, impl, NULL);
177
0
}
178
179
int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
180
1.81k
{
181
1.81k
    const unsigned char *data = in;
182
1.81k
    int bl;
183
1.81k
    size_t max_burst_blocks, cipher_blocks;
184
1.81k
    unsigned char buf[LOCAL_BUF_SIZE];
185
186
1.81k
    if (ctx->nlast_block == -1)
187
0
        return 0;
188
1.81k
    if (dlen == 0)
189
140
        return 1;
190
1.67k
    if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) == 0)
191
0
        return 0;
192
    /* Copy into partial block if we need to */
193
1.67k
    if (ctx->nlast_block > 0) {
194
1.20k
        size_t nleft;
195
196
1.20k
        nleft = bl - ctx->nlast_block;
197
1.20k
        if (dlen < nleft)
198
326
            nleft = dlen;
199
1.20k
        memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
200
1.20k
        dlen -= nleft;
201
1.20k
        ctx->nlast_block += nleft;
202
        /* If no more to process return */
203
1.20k
        if (dlen == 0)
204
382
            return 1;
205
826
        data += nleft;
206
        /* Else not final block so encrypt it */
207
826
        if (EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl) <= 0)
208
0
            return 0;
209
826
    }
210
    /* Encrypt all but one of the complete blocks left */
211
212
1.29k
    max_burst_blocks = LOCAL_BUF_SIZE / bl;
213
1.29k
    cipher_blocks = (dlen - 1) / bl;
214
1.29k
    if (max_burst_blocks == 0) {
215
        /*
216
         * When block length is greater than local buffer size,
217
         * use ctx->tbl as cipher output.
218
         */
219
0
        while (dlen > (size_t)bl) {
220
0
            if (EVP_Cipher(ctx->cctx, ctx->tbl, data, bl) <= 0)
221
0
                return 0;
222
0
            dlen -= bl;
223
0
            data += bl;
224
0
        }
225
1.29k
    } else {
226
13.2k
        while (cipher_blocks > max_burst_blocks) {
227
11.9k
            if (EVP_Cipher(ctx->cctx, buf, data, max_burst_blocks * bl) <= 0)
228
0
                return 0;
229
11.9k
            dlen -= max_burst_blocks * bl;
230
11.9k
            data += max_burst_blocks * bl;
231
11.9k
            cipher_blocks -= max_burst_blocks;
232
11.9k
        }
233
1.29k
        if (cipher_blocks > 0) {
234
608
            if (EVP_Cipher(ctx->cctx, buf, data, cipher_blocks * bl) <= 0)
235
0
                return 0;
236
608
            dlen -= cipher_blocks * bl;
237
608
            data += cipher_blocks * bl;
238
608
            memcpy(ctx->tbl, &buf[(cipher_blocks - 1) * bl], bl);
239
608
        }
240
1.29k
    }
241
    /* Copy any data left to last block buffer */
242
1.29k
    memcpy(ctx->last_block, data, dlen);
243
1.29k
    ctx->nlast_block = dlen;
244
1.29k
    return 1;
245
1.29k
}
246
247
int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
248
468
{
249
468
    int i, bl, lb;
250
251
468
    if (ctx->nlast_block == -1)
252
0
        return 0;
253
468
    if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) == 0)
254
0
        return 0;
255
468
    if (poutlen != NULL)
256
468
        *poutlen = (size_t)bl;
257
468
    if (!out)
258
0
        return 1;
259
468
    lb = ctx->nlast_block;
260
    /* Is last block complete? */
261
468
    if (lb == bl) {
262
756
        for (i = 0; i < bl; i++)
263
704
            out[i] = ctx->last_block[i] ^ ctx->k1[i];
264
416
    } else {
265
416
        ctx->last_block[lb] = 0x80;
266
416
        if (bl - lb > 1)
267
378
            memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
268
5.85k
        for (i = 0; i < bl; i++)
269
5.44k
            out[i] = ctx->last_block[i] ^ ctx->k2[i];
270
416
    }
271
468
    if (EVP_Cipher(ctx->cctx, out, out, bl) <= 0) {
272
0
        OPENSSL_cleanse(out, bl);
273
0
        return 0;
274
0
    }
275
468
    return 1;
276
468
}
277
278
int CMAC_resume(CMAC_CTX *ctx)
279
0
{
280
0
    if (ctx->nlast_block == -1)
281
0
        return 0;
282
    /*
283
     * The buffer "tbl" contains the last fully encrypted block which is the
284
     * last IV (or all zeroes if no last encrypted block). The last block has
285
     * not been modified since CMAC_final(). So reinitialising using the last
286
     * decrypted block will allow CMAC to continue after calling
287
     * CMAC_Final().
288
     */
289
0
    return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
290
0
}