3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
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/>.
22 #include "bcal-basic.h"
23 #include "blockcipher_descriptor.h"
24 #include "bcal-cmac.h"
29 uint8_t bcal_eax_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_eax_ctx_t* ctx){
31 ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8;
32 ctx->nonce = malloc(ctx->blocksize_B);
36 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ctag));
40 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->htag));
44 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ntag));
48 r = bcal_ctr_init(desc, key, keysize_b, NULL, &(ctx->cipher));
53 uint8_t tmp[ctx->blocksize_B];
54 memset(tmp, 0, ctx->blocksize_B);
55 bcal_cmac_nextBlock(&(ctx->ntag), tmp);
56 tmp[ctx->blocksize_B-1]=1;
57 bcal_cmac_nextBlock(&(ctx->htag), tmp);
58 tmp[ctx->blocksize_B-1]=2;
59 bcal_cmac_nextBlock(&(ctx->ctag), tmp);
63 void bcal_eax_free(bcal_eax_ctx_t* ctx){
64 bcal_ctr_free(&(ctx->cipher));
65 bcal_cmac_free(&(ctx->ctag));
66 bcal_cmac_free(&(ctx->htag));
67 bcal_cmac_free(&(ctx->ntag));
71 void bcal_eax_loadNonce(const void* nonce, uint16_t length_b, bcal_eax_ctx_t* ctx){
72 bcal_cmac_lastBlock(&(ctx->ntag), nonce, length_b);
73 bcal_cmac_ctx2mac(ctx->nonce, ctx->blocksize_B*8, &(ctx->ntag));
74 bcal_ctr_loadIV(ctx->nonce, &(ctx->cipher));
77 void bcal_eax_addNextHeader(const void* header, bcal_eax_ctx_t* ctx){
78 bcal_cmac_nextBlock(&(ctx->htag), header);
81 void bcal_eax_addLastHeader(const void* header, uint16_t length_b, bcal_eax_ctx_t* ctx){
82 bcal_cmac_lastBlock(&(ctx->htag), header, length_b);
86 void bcal_eax_encNextBlock(void* block, bcal_eax_ctx_t* ctx){
87 bcal_ctr_encNext(block, &(ctx->cipher));
88 bcal_cmac_nextBlock(&(ctx->ctag), block);
91 void bcal_eax_encLastBlock(void* block, uint16_t length_b, bcal_eax_ctx_t* ctx){
92 bcal_ctr_encMsg(NULL, block, length_b, &(ctx->cipher));
93 bcal_cmac_lastBlock(&(ctx->ctag), block, length_b);
96 void bcal_eax_decNextBlock(void* block, bcal_eax_ctx_t* ctx){
97 bcal_cmac_nextBlock(&(ctx->ctag), block);
98 bcal_ctr_decNext(block, &(ctx->cipher));
101 void bcal_eax_decLastBlock(void* block, uint16_t length_b, bcal_eax_ctx_t* ctx){
102 bcal_cmac_lastBlock(&(ctx->ctag), block, length_b);
103 bcal_ctr_decMsg(NULL, block, length_b, &(ctx->cipher));
106 void bcal_eax_ctx2tag(void* dest, uint16_t length_b, bcal_eax_ctx_t* ctx){
107 uint8_t tmp[ctx->blocksize_B];
108 if(ctx->header_set==0){
109 bcal_cmac_lastBlock(&(ctx->htag), NULL, 0);
112 bcal_cmac_ctx2mac(tmp, ctx->blocksize_B*8, &(ctx->htag));
113 memxor(ctx->nonce, tmp, ctx->blocksize_B);
115 bcal_cmac_ctx2mac(tmp, ctx->blocksize_B*8, &(ctx->ctag));
116 memxor(ctx->nonce, tmp, ctx->blocksize_B);
117 memcpy(dest, ctx->nonce, (length_b+7)/8);