]> git.cryptolib.org Git - arm-crypto-lib.git/blob - bcal-eax.c
AES makestub added
[arm-crypto-lib.git] / bcal-eax.c
1 /* bca-eax.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-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 #include "memxor.h"
28
29 uint8_t bcal_eax_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_eax_ctx_t* ctx){
30         uint8_t r;
31         ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8;
32         ctx->nonce = malloc(ctx->blocksize_B);
33         if(ctx->nonce==NULL){
34                 return 0x81;
35         }
36         r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ctag));
37         if(r){
38                 return r;
39         }
40         r = bcal_cmac_init(desc, key, keysize_b, &(ctx->htag));
41         if(r){
42                 return (r|0x10);
43         }
44         r = bcal_cmac_init(desc, key, keysize_b, &(ctx->ntag));
45         if(r){
46                 return (r|0x20);
47         }
48         r = bcal_ctr_init(desc, key, keysize_b, NULL, &(ctx->cipher));
49         if(r){
50                 return (r|0x30);
51         }
52         ctx->header_set=0;
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);
60         return 0;
61 }
62
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));
68         free(ctx->nonce);
69 }
70
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));
75 }
76
77 void bcal_eax_addNextHeader(const void* header, bcal_eax_ctx_t* ctx){
78         bcal_cmac_nextBlock(&(ctx->htag), header);
79 }
80
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);
83         ctx->header_set = 1;
84 }
85
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);
89 }
90
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);
94 }
95
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));
99 }
100
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));
104 }
105
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);
110         }
111
112         bcal_cmac_ctx2mac(tmp, ctx->blocksize_B*8, &(ctx->htag));
113         memxor(ctx->nonce, tmp, ctx->blocksize_B);
114
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);
118 }
119