2 * Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include "internal/cryptlib.h"
14 #include <openssl/cmac.h>
15 #include <openssl/err.h>
18 /* Cipher context to use */
21 unsigned char k1[EVP_MAX_BLOCK_LENGTH];
22 unsigned char k2[EVP_MAX_BLOCK_LENGTH];
24 unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
25 /* Last (possibly partial) block */
26 unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
27 /* Number of bytes in last block: -1 means context not initialised */
31 /* Make temporary keys K1 and K2 */
33 static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
36 unsigned char c = l[0], carry = c >> 7, cnext;
38 /* Shift block to left, including carry */
39 for (i = 0; i < bl - 1; i++, c = cnext)
40 k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
42 /* If MSB set fixup with R */
43 k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
46 CMAC_CTX *CMAC_CTX_new(void)
50 if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
51 CRYPTOerr(CRYPTO_F_CMAC_CTX_NEW, ERR_R_MALLOC_FAILURE);
54 ctx->cctx = EVP_CIPHER_CTX_new();
55 if (ctx->cctx == NULL) {
59 ctx->nlast_block = -1;
63 void CMAC_CTX_cleanup(CMAC_CTX *ctx)
65 EVP_CIPHER_CTX_reset(ctx->cctx);
66 OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
67 OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
68 OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
69 OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
70 ctx->nlast_block = -1;
73 EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
78 void CMAC_CTX_free(CMAC_CTX *ctx)
82 CMAC_CTX_cleanup(ctx);
83 EVP_CIPHER_CTX_free(ctx->cctx);
87 int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
90 if (in->nlast_block == -1)
92 if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
94 bl = EVP_CIPHER_CTX_block_size(in->cctx);
95 memcpy(out->k1, in->k1, bl);
96 memcpy(out->k2, in->k2, bl);
97 memcpy(out->tbl, in->tbl, bl);
98 memcpy(out->last_block, in->last_block, bl);
99 out->nlast_block = in->nlast_block;
103 int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
104 const EVP_CIPHER *cipher, ENGINE *impl)
106 static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
107 /* All zeros means restart */
108 if (!key && !cipher && !impl && keylen == 0) {
109 /* Not initialised */
110 if (ctx->nlast_block == -1)
112 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
114 memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
115 ctx->nlast_block = 0;
118 /* Initialise context */
119 if (cipher != NULL) {
120 /* Ensure we can't use this ctx until we also have a key */
121 ctx->nlast_block = -1;
122 if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
125 /* Non-NULL key means initialisation complete */
129 /* If anything fails then ensure we can't use this ctx */
130 ctx->nlast_block = -1;
131 if (!EVP_CIPHER_CTX_cipher(ctx->cctx))
133 if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
135 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
137 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
138 if (EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl) <= 0)
140 make_kn(ctx->k1, ctx->tbl, bl);
141 make_kn(ctx->k2, ctx->k1, bl);
142 OPENSSL_cleanse(ctx->tbl, bl);
143 /* Reset context again ready for first data block */
144 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
146 /* Zero tbl so resume works */
147 memset(ctx->tbl, 0, bl);
148 ctx->nlast_block = 0;
153 int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
155 const unsigned char *data = in;
157 if (ctx->nlast_block == -1)
161 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
162 /* Copy into partial block if we need to */
163 if (ctx->nlast_block > 0) {
165 nleft = bl - ctx->nlast_block;
168 memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
170 ctx->nlast_block += nleft;
171 /* If no more to process return */
175 /* Else not final block so encrypt it */
176 if (EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl) <= 0)
179 /* Encrypt all but one of the complete blocks left */
181 if (EVP_Cipher(ctx->cctx, ctx->tbl, data, bl) <= 0)
186 /* Copy any data left to last block buffer */
187 memcpy(ctx->last_block, data, dlen);
188 ctx->nlast_block = dlen;
193 int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
196 if (ctx->nlast_block == -1)
198 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
199 *poutlen = (size_t)bl;
202 lb = ctx->nlast_block;
203 /* Is last block complete? */
205 for (i = 0; i < bl; i++)
206 out[i] = ctx->last_block[i] ^ ctx->k1[i];
208 ctx->last_block[lb] = 0x80;
210 memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
211 for (i = 0; i < bl; i++)
212 out[i] = ctx->last_block[i] ^ ctx->k2[i];
214 if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
215 OPENSSL_cleanse(out, bl);
221 int CMAC_resume(CMAC_CTX *ctx)
223 if (ctx->nlast_block == -1)
226 * The buffer "tbl" contains the last fully encrypted block which is the
227 * last IV (or all zeroes if no last encrypted block). The last block has
228 * not been modified since CMAC_final(). So reinitialising using the last
229 * decrypted block will allow CMAC to continue after calling
232 return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);