Coverage Report

Created: 2025-12-04 06:33

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