2 AES-CMAC-128 (rfc 4493)
3 Copyright (C) Stefan Metzmacher 2012
4 Copyright (C) Jeremy Allison 2012
5 Copyright (C) Michael Adam 2012
6 Copyright (C) 2017, Red Hat Inc.
8 This file is part of GNU Nettle.
10 GNU Nettle is free software: you can redistribute it and/or
11 modify it under the terms of either:
13 * the GNU Lesser General Public License as published by the Free
14 Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
19 * the GNU General Public License as published by the Free
20 Software Foundation; either version 2 of the License, or (at your
21 option) any later version.
23 or both in parallel, as here.
25 GNU Nettle is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
30 You should have received copies of the GNU General Public License and
31 the GNU Lesser General Public License along with this program. If
32 not, see http://www.gnu.org/licenses/.
46 #include "nettle-internal.h"
47 #include "block-internal.h"
51 cmac128_set_key(struct cmac128_key *key, const void *cipher,
52 nettle_cipher_func *encrypt)
54 static const union nettle_block16 zero_block;
55 union nettle_block16 L;
57 /* step 1 - generate subkeys k1 and k2 */
58 encrypt(cipher, 16, L.b, zero_block.b);
60 block16_mulx_be(&key->K1, &L);
61 block16_mulx_be(&key->K2, &key->K1);
65 cmac128_init(struct cmac128_ctx *ctx)
67 memset(&ctx->X, 0, sizeof(ctx->X));
71 #define MIN(x,y) ((x)<(y)?(x):(y))
74 cmac128_update(struct cmac128_ctx *ctx, const void *cipher,
75 nettle_cipher_func *encrypt,
76 size_t msg_len, const uint8_t *msg)
78 union nettle_block16 Y;
80 * check if we expand the block
84 size_t len = MIN(16 - ctx->index, msg_len);
85 memcpy(&ctx->block.b[ctx->index], msg, len);
92 /* if it is still the last block, we are done */
97 * now checksum everything but the last block
99 block16_xor3(&Y, &ctx->X, &ctx->block);
100 encrypt(cipher, 16, ctx->X.b, Y.b);
104 block16_xor_bytes (&Y, &ctx->X, msg);
105 encrypt(cipher, 16, ctx->X.b, Y.b);
111 * copy the last block, it will be processed in
114 memcpy(ctx->block.b, msg, msg_len);
115 ctx->index = msg_len;
119 cmac128_digest(struct cmac128_ctx *ctx, const struct cmac128_key *key,
120 const void *cipher, nettle_cipher_func *encrypt,
121 unsigned length, uint8_t *dst)
123 union nettle_block16 Y;
125 /* re-use ctx->block for memxor output */
128 ctx->block.b[ctx->index] = 0x80;
129 memset(ctx->block.b + ctx->index + 1, 0, 16 - 1 - ctx->index);
131 block16_xor (&ctx->block, &key->K2);
135 block16_xor (&ctx->block, &key->K1);
138 block16_xor3 (&Y, &ctx->block, &ctx->X);
140 assert(length <= 16);
143 encrypt(cipher, 16, dst, Y.b);
147 encrypt(cipher, 16, ctx->block.b, Y.b);
148 memcpy(dst, ctx->block.b, length);
151 /* reset state for re-use */