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"
28 uint8_t bcal_eax_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_eax_ctx_t* ctx){
30 ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8;
31 ctx->nonce = malloc(ctx->blocksize_B);
35 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ctag));
39 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->htag));
43 r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ntag));
47 r = bcal_ctr_init(desc, key, keysize_b, NULL, &(ctx->cipher));
52 uint8_t tmp[ctx->blocksize_B];
53 memset(tmp, 0, ctx->blocksize_B);
54 bcal_cmac_nextBlock(&(ctx->ntag), tmp);
55 tmp[ctx->blocksize_B-1]=1;
56 bcal_cmac_nextBlock(&(ctx->htag), tmp);
57 tmp[ctx->blocksize_B-1]=2;
58 bcal_cmac_nextBlock(&(ctx->ctag), tmp);
62 void bcal_eax_free(bcal_eax_ctx_t* ctx){
63 bcal_ctr_free(&(ctx->cipher));
64 bcal_cmac_free(&(ctx->ctag));
65 bcal_cmac_free(&(ctx->htag));
66 bcal_cmac_free(&(ctx->ntag));
70 void bcal_eax_loadNonce(const void* nonce, uint16_t length_b, bcal_eax_ctx_t* ctx){
71 bcal_cmac_lastBlock(&(ctx->ntag), nonce, length_b);
72 bcal_cmac_ctx2mac(ctx->nonce, ctx->blocksize_B*8, &(ctx->ntag));
73 bcal_ctr_loadIV(ctx->nonce, &(ctx->cipher));
76 void bcal_eax_addNextHeader(const void* header, bcal_eax_ctx_t* ctx){
77 bcal_cmac_nextBlock(&(ctx->htag), header);
80 void bcal_eax_addLastHeader(const void* header, uint16_t length_b, bcal_eax_ctx_t* ctx){
81 bcal_cmac_lastBlock(&(ctx->htag), header, length_b);
85 void bcal_eax_encNextBlock(void* block, bcal_eax_ctx_t* ctx){
86 bcal_ctr_encNext(block, &(ctx->cipher));
87 bcal_cmac_nextBlock(&(ctx->ctag), block);
90 void bcal_eax_encLastBlock(void* block, uint16_t length_b, bcal_eax_ctx_t* ctx){
91 bcal_ctr_encMsg(NULL, block, length_b, &(ctx->cipher));
92 bcal_cmac_lastBlock(&(ctx->ctag), block, length_b);
95 void bcal_eax_decNextBlock(void* block, bcal_eax_ctx_t* ctx){
96 bcal_cmac_nextBlock(&(ctx->ctag), block);
97 bcal_ctr_decNext(block, &(ctx->cipher));
100 void bcal_eax_decLastBlock(void* block, uint16_t length_b, bcal_eax_ctx_t* ctx){
101 bcal_cmac_lastBlock(&(ctx->ctag), block, length_b);
102 bcal_ctr_decMsg(NULL, block, length_b, &(ctx->cipher));
105 void bcal_eax_ctx2tag(void* dest, uint16_t length_b, bcal_eax_ctx_t* ctx){
106 uint8_t tmp[ctx->blocksize_B];
107 if(ctx->header_set==0){
108 bcal_cmac_lastBlock(&(ctx->htag), NULL, 0);
111 bcal_cmac_ctx2mac(tmp, ctx->blocksize_B*8, &(ctx->htag));
112 memxor(ctx->nonce, tmp, ctx->blocksize_B);
114 bcal_cmac_ctx2mac(tmp, ctx->blocksize_B*8, &(ctx->ctag));
115 memxor(ctx->nonce, tmp, ctx->blocksize_B);
116 memcpy(dest, ctx->nonce, (length_b+7)/8);