3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "bcal-basic.h"
22 #include "blockcipher_descriptor.h"
23 #include "bcal-cmac.h"
28 uint8_t bcal_eax_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b,
32 ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc) + 7) / 8;
33 ctx->nonce = malloc(ctx->blocksize_B);
34 if (ctx->nonce == NULL) {
37 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ctag));
41 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->htag));
45 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ntag));
49 r = bcal_ctr_init(desc, key, keysize_b, NULL, &(ctx->cipher));
54 uint8_t tmp[ctx->blocksize_B];
55 memset(tmp, 0, ctx->blocksize_B);
56 bcal_cmac_nextBlock(&(ctx->ntag), tmp);
57 tmp[ctx->blocksize_B - 1] = 1;
58 bcal_cmac_nextBlock(&(ctx->htag), tmp);
59 tmp[ctx->blocksize_B - 1] = 2;
60 bcal_cmac_nextBlock(&(ctx->ctag), tmp);
64 void bcal_eax_free(bcal_eax_ctx_t *ctx)
66 bcal_ctr_free(&(ctx->cipher));
67 bcal_cmac_free(&(ctx->ctag));
68 bcal_cmac_free(&(ctx->htag));
69 bcal_cmac_free(&(ctx->ntag));
73 void bcal_eax_loadNonce(const void *nonce, uint16_t length_b,
76 bcal_cmac_lastBlock(&(ctx->ntag), nonce, length_b);
77 bcal_cmac_ctx2mac(ctx->nonce, ctx->blocksize_B * 8, &(ctx->ntag));
78 bcal_ctr_loadIV(ctx->nonce, &(ctx->cipher));
81 void bcal_eax_addNextHeader(const void *header, bcal_eax_ctx_t *ctx)
83 bcal_cmac_nextBlock(&(ctx->htag), header);
86 void bcal_eax_addLastHeader(const void *header, uint16_t length_b,
89 bcal_cmac_lastBlock(&(ctx->htag), header, length_b);
93 void bcal_eax_encNextBlock(void *block, bcal_eax_ctx_t *ctx)
95 bcal_ctr_encNext(block, &(ctx->cipher));
96 bcal_cmac_nextBlock(&(ctx->ctag), block);
99 void bcal_eax_encLastBlock(void *block, uint16_t length_b, bcal_eax_ctx_t *ctx)
101 bcal_ctr_encMsg(NULL, block, length_b, &(ctx->cipher));
102 bcal_cmac_lastBlock(&(ctx->ctag), block, length_b);
105 void bcal_eax_decNextBlock(void *block, bcal_eax_ctx_t *ctx)
107 bcal_cmac_nextBlock(&(ctx->ctag), block);
108 bcal_ctr_decNext(block, &(ctx->cipher));
111 void bcal_eax_decLastBlock(void *block, uint16_t length_b, bcal_eax_ctx_t *ctx)
113 bcal_cmac_lastBlock(&(ctx->ctag), block, length_b);
114 bcal_ctr_decMsg(NULL, block, length_b, &(ctx->cipher));
117 void bcal_eax_ctx2tag(void *dest, uint16_t length_b, bcal_eax_ctx_t *ctx)
119 uint8_t tmp[ctx->blocksize_B];
120 if (ctx->header_set == 0) {
121 bcal_cmac_lastBlock(&(ctx->htag), NULL, 0);
124 bcal_cmac_ctx2mac(tmp, ctx->blocksize_B * 8, &(ctx->htag));
125 memxor(ctx->nonce, tmp, ctx->blocksize_B);
127 bcal_cmac_ctx2mac(tmp, ctx->blocksize_B * 8, &(ctx->ctag));
128 memxor(ctx->nonce, tmp, ctx->blocksize_B);
129 memcpy(dest, ctx->nonce, (length_b + 7) / 8);