2 * Copyright 2010-2016 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>
17 /* Cipher context to use */
20 unsigned char k1[EVP_MAX_BLOCK_LENGTH];
21 unsigned char k2[EVP_MAX_BLOCK_LENGTH];
23 unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
24 /* Last (possibly partial) block */
25 unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
26 /* Number of bytes in last block: -1 means context not initialised */
30 /* Make temporary keys K1 and K2 */
32 static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
35 unsigned char c = l[0], carry = c >> 7, cnext;
37 /* Shift block to left, including carry */
38 for (i = 0; i < bl - 1; i++, c = cnext)
39 k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
41 /* If MSB set fixup with R */
42 k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
45 CMAC_CTX *CMAC_CTX_new(void)
49 ctx = OPENSSL_malloc(sizeof(*ctx));
52 ctx->cctx = EVP_CIPHER_CTX_new();
53 if (ctx->cctx == NULL) {
57 ctx->nlast_block = -1;
61 void CMAC_CTX_cleanup(CMAC_CTX *ctx)
63 EVP_CIPHER_CTX_reset(ctx->cctx);
64 OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
65 OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
66 OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
67 OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
68 ctx->nlast_block = -1;
71 EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
76 void CMAC_CTX_free(CMAC_CTX *ctx)
80 CMAC_CTX_cleanup(ctx);
81 EVP_CIPHER_CTX_free(ctx->cctx);
85 int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
88 if (in->nlast_block == -1)
90 if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
92 bl = EVP_CIPHER_CTX_block_size(in->cctx);
93 memcpy(out->k1, in->k1, bl);
94 memcpy(out->k2, in->k2, bl);
95 memcpy(out->tbl, in->tbl, bl);
96 memcpy(out->last_block, in->last_block, bl);
97 out->nlast_block = in->nlast_block;
101 int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
102 const EVP_CIPHER *cipher, ENGINE *impl)
104 static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
105 /* All zeros means restart */
106 if (!key && !cipher && !impl && keylen == 0) {
107 /* Not initialised */
108 if (ctx->nlast_block == -1)
110 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
112 memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
113 ctx->nlast_block = 0;
116 /* Initialise context */
117 if (cipher && !EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
119 /* Non-NULL key means initialisation complete */
122 if (!EVP_CIPHER_CTX_cipher(ctx->cctx))
124 if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
126 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
128 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
129 if (!EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl))
131 make_kn(ctx->k1, ctx->tbl, bl);
132 make_kn(ctx->k2, ctx->k1, bl);
133 OPENSSL_cleanse(ctx->tbl, bl);
134 /* Reset context again ready for first data block */
135 if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
137 /* Zero tbl so resume works */
138 memset(ctx->tbl, 0, bl);
139 ctx->nlast_block = 0;
144 int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
146 const unsigned char *data = in;
148 if (ctx->nlast_block == -1)
152 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
153 /* Copy into partial block if we need to */
154 if (ctx->nlast_block > 0) {
156 nleft = bl - ctx->nlast_block;
159 memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
161 ctx->nlast_block += nleft;
162 /* If no more to process return */
166 /* Else not final block so encrypt it */
167 if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
170 /* Encrypt all but one of the complete blocks left */
172 if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
177 /* Copy any data left to last block buffer */
178 memcpy(ctx->last_block, data, dlen);
179 ctx->nlast_block = dlen;
184 int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
187 if (ctx->nlast_block == -1)
189 bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
190 *poutlen = (size_t)bl;
193 lb = ctx->nlast_block;
194 /* Is last block complete? */
196 for (i = 0; i < bl; i++)
197 out[i] = ctx->last_block[i] ^ ctx->k1[i];
199 ctx->last_block[lb] = 0x80;
201 memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
202 for (i = 0; i < bl; i++)
203 out[i] = ctx->last_block[i] ^ ctx->k2[i];
205 if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
206 OPENSSL_cleanse(out, bl);
212 int CMAC_resume(CMAC_CTX *ctx)
214 if (ctx->nlast_block == -1)
217 * The buffer "tbl" contains the last fully encrypted block which is the
218 * last IV (or all zeroes if no last encrypted block). The last block has
219 * not been modified since CMAC_final(). So reinitialising using the last
220 * decrypted block will allow CMAC to continue after calling
223 return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);