]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal-eax.c
78ddd519c436138661e3d765488c5afe860aa220
[avr-crypto-lib.git] / bcal-eax.c
1 /* bca-eax.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20
21 #include <stdint.h>
22 #include "bcal-basic.h"
23 #include "blockcipher_descriptor.h"
24 #include "bcal-cmac.h"
25 #include "bcal-ctr.h"
26 #include "bcal-eax.h"
27
28 uint8_t bcal_eax_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_eax_ctx_t* ctx){
29         uint8_t r;
30         ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8;
31         ctx->nonce = malloc(ctx->blocksize_B);
32         if(ctx->nonce==NULL){
33                 return 0x81;
34         }
35         r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ctag));
36         if(r){
37                 return r;
38         }
39         r = bcal_cmac_init(desc, key, keysize_b, &(ctx->htag));
40         if(r){
41                 return (r|0x10);
42         }
43         r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ntag));
44         if(r){
45                 return (r|0x20);
46         }
47         r = bcal_ctr_init(desc, key, keysize_b, NULL, &(ctx->cipher));
48         if(r){
49                 return (r|0x30);
50         }
51         ctx->header_set=0;
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);
59         return 0;
60 }
61
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));
67         free(ctx->nonce);
68 }
69
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));
74 }
75
76 void bcal_eax_addNextHeader(const void* header, bcal_eax_ctx_t* ctx){
77         bcal_cmac_nextBlock(&(ctx->htag), header);
78 }
79
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);
82         ctx->header_set = 1;
83 }
84
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);
88 }
89
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);
93 }
94
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));
98 }
99
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));
103 }
104
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);
109         }
110
111         bcal_cmac_ctx2mac(tmp, ctx->blocksize_B*8, &(ctx->htag));
112         memxor(ctx->nonce, tmp, ctx->blocksize_B);
113
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);
117 }
118