krb5: rework PAC validation loop
[Samba.git] / lib / crypto / aes_cmac_128.c
blobe7bf030c92a2dbff1c7800bfed55d39fb56665eb
1 /*
2 AES-CMAC-128 (rfc 4493)
3 Copyright (C) Stefan Metzmacher 2012
4 Copyright (C) Jeremy Allison 2012
5 Copyright (C) Michael Adam 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "replace.h"
22 #include "lib/crypto/aes.h"
23 #include "lib/crypto/aes_cmac_128.h"
25 static const uint8_t const_Zero[] = {
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
30 static const uint8_t const_Rb[] = {
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87
35 #define _MSB(x) (((x)[0] & 0x80)?1:0)
37 void aes_cmac_128_init(struct aes_cmac_128_context *ctx,
38 const uint8_t K[AES_BLOCK_SIZE])
40 ZERO_STRUCTP(ctx);
42 AES_set_encrypt_key(K, 128, &ctx->aes_key);
44 /* step 1 - generate subkeys k1 and k2 */
46 AES_encrypt(const_Zero, ctx->L, &ctx->aes_key);
48 if (_MSB(ctx->L) == 0) {
49 aes_block_lshift(ctx->L, ctx->K1);
50 } else {
51 aes_block_lshift(ctx->L, ctx->tmp);
52 aes_block_xor(ctx->tmp, const_Rb, ctx->K1);
55 if (_MSB(ctx->K1) == 0) {
56 aes_block_lshift(ctx->K1, ctx->K2);
57 } else {
58 aes_block_lshift(ctx->K1, ctx->tmp);
59 aes_block_xor(ctx->tmp, const_Rb, ctx->K2);
63 void aes_cmac_128_update(struct aes_cmac_128_context *ctx,
64 const uint8_t *msg, size_t msg_len)
67 * check if we expand the block
69 if (ctx->last_len < AES_BLOCK_SIZE) {
70 size_t len = MIN(AES_BLOCK_SIZE - ctx->last_len, msg_len);
72 if (len > 0) {
73 memcpy(&ctx->last[ctx->last_len], msg, len);
74 msg += len;
75 msg_len -= len;
76 ctx->last_len += len;
80 if (msg_len == 0) {
81 /* if it is still the last block, we are done */
82 return;
86 * now checksum everything but the last block
88 aes_block_xor(ctx->X, ctx->last, ctx->Y);
89 AES_encrypt(ctx->Y, ctx->X, &ctx->aes_key);
91 while (msg_len > AES_BLOCK_SIZE) {
92 aes_block_xor(ctx->X, msg, ctx->Y);
93 AES_encrypt(ctx->Y, ctx->X, &ctx->aes_key);
94 msg += AES_BLOCK_SIZE;
95 msg_len -= AES_BLOCK_SIZE;
99 * copy the last block, it will be processed in
100 * aes_cmac_128_final().
102 ZERO_STRUCT(ctx->last);
103 memcpy(ctx->last, msg, msg_len);
104 ctx->last_len = msg_len;
107 void aes_cmac_128_final(struct aes_cmac_128_context *ctx,
108 uint8_t T[AES_BLOCK_SIZE])
110 if (ctx->last_len < AES_BLOCK_SIZE) {
111 ctx->last[ctx->last_len] = 0x80;
112 aes_block_xor(ctx->last, ctx->K2, ctx->tmp);
113 } else {
114 aes_block_xor(ctx->last, ctx->K1, ctx->tmp);
117 aes_block_xor(ctx->tmp, ctx->X, ctx->Y);
118 AES_encrypt(ctx->Y, T, &ctx->aes_key);
120 ZERO_STRUCTP(ctx);