Coverage Report

Created: 2025-12-08 06:22

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