Coverage Report

Created: 2025-06-13 06:58

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