]> git.cryptolib.org Git - arm-crypto-lib.git/blob - bcal-cmac.c
AES added
[arm-crypto-lib.git] / bcal-cmac.c
1 /* bcal-omac.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 <string.h>
23 #include "bcal-basic.h"
24 #include "bcal-cmac.h"
25 #include "memxor.h"
26
27
28 static uint8_t left_shift_be_block(void* block, uint8_t blocksize_B){
29         uint8_t c1=0, c2;
30         do{
31                 --blocksize_B;
32                 c2 = (((uint8_t*)block)[blocksize_B])>>7;
33                 (((uint8_t*)block)[blocksize_B]) <<= 1;
34                 (((uint8_t*)block)[blocksize_B]) |= c1;
35                 c1 = c2;
36         }while(blocksize_B);
37         return c1;
38 }
39
40 static const uint8_t const_128 = 0x87;
41 static const uint8_t const_64  = 0x1b;
42
43 uint8_t bcal_cmac_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_cmac_ctx_t* ctx){
44         uint8_t r;
45         ctx->desc = (bcdesc_t*)desc;
46         ctx->blocksize_B = bcal_cipher_getBlocksize_b(desc)/8;
47         if (ctx->blocksize_B!=128/8 && ctx->blocksize_B!=64/8){
48                 return 0x13;
49         }
50         ctx->accu = malloc(ctx->blocksize_B);
51         if(ctx->accu==NULL){
52                 return 0x14;
53         }
54         ctx->k1 = malloc(ctx->blocksize_B);
55         if(ctx->k1==NULL){
56                 return 0x15;
57         }
58         ctx->k2 = malloc(ctx->blocksize_B);
59         if(ctx->k2==NULL){
60                 return 0x16;
61         }
62         ctx->lastblock = malloc(ctx->blocksize_B);
63         if(ctx->lastblock==NULL){
64                 return 0x17;
65         }
66         r = bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
67         if(r){
68                 return r;
69         }
70         if(ctx->blocksize_B==128/8){
71                 r = const_128;
72         }else{
73                 r = const_64;
74         }
75         /* subkey computation */
76         memset(ctx->accu, 0x00, ctx->blocksize_B);
77         memset(ctx->k1, 0x00, ctx->blocksize_B);
78         bcal_cipher_enc(ctx->k1, &(ctx->cctx));
79         if(left_shift_be_block(ctx->k1, ctx->blocksize_B)){
80                 ctx->k1[ctx->blocksize_B-1] ^= r;
81         }
82         memcpy(ctx->k2, ctx->k1, ctx->blocksize_B);
83         if(left_shift_be_block(ctx->k2, ctx->blocksize_B)){
84                 ctx->k2[ctx->blocksize_B-1] ^= r;
85         }
86         ctx->last_set=0;
87         return 0;
88 }
89
90 void bcal_cmac_free(bcal_cmac_ctx_t* ctx){
91         free(ctx->accu);
92         free(ctx->k1);
93         free(ctx->k2);
94         bcal_cipher_free(&(ctx->cctx));
95 }
96
97 void bcal_cmac_nextBlock (bcal_cmac_ctx_t* ctx, const void* block){
98         if(ctx->last_set){
99                 memxor(ctx->accu, ctx->lastblock, ctx->blocksize_B);
100                 bcal_cipher_enc(ctx->accu, &(ctx->cctx));
101         }
102         memcpy(ctx->lastblock, block, ctx->blocksize_B);
103         ctx->last_set=1;
104 }
105
106
107 void bcal_cmac_lastBlock(bcal_cmac_ctx_t* ctx, const void* block, uint16_t length_b){
108         uint16_t blocksize_b;
109         blocksize_b = ctx->blocksize_B*8;
110         while(length_b>=blocksize_b){
111                 bcal_cmac_nextBlock(ctx, block);
112                 block = (uint8_t*)block + ctx->blocksize_B;
113                 length_b -= blocksize_b;
114         }
115         if(ctx->last_set==0){
116                 memxor(ctx->accu, block, (length_b+7)/8);
117                 memxor(ctx->accu, ctx->k2, ctx->blocksize_B);
118                 ctx->accu[length_b/8] ^= 0x80>>(length_b&7);
119         }else{
120                 if(length_b==0){
121                         memxor(ctx->accu, ctx->lastblock, ctx->blocksize_B);
122                         memxor(ctx->accu, ctx->k1, ctx->blocksize_B);
123                 }else{
124                         memxor(ctx->accu, ctx->lastblock, ctx->blocksize_B);
125                         bcal_cipher_enc(ctx->accu, &(ctx->cctx));
126                         memxor(ctx->accu, block, (length_b+7)/8);
127                         memxor(ctx->accu, ctx->k2, ctx->blocksize_B);
128                         ctx->accu[length_b/8] ^= 0x80>>(length_b&7);
129                 }
130         }
131         bcal_cipher_enc(ctx->accu, &(ctx->cctx));
132 }
133
134 void bcal_cmac_ctx2mac(void* dest, uint16_t length_b, const bcal_cmac_ctx_t* ctx){
135         memcpy(dest, ctx->accu, length_b/8);
136         if(length_b&7){
137                 ((uint8_t*)dest)[length_b/8] &= 0xff>>(length_b&7);
138                 ((uint8_t*)dest)[length_b/8] |= (0xff00>>(length_b&7))&(ctx->accu[length_b/8]);
139         }
140 }
141
142 void bcal_cmac(void* dest, uint16_t out_length_b, const void* block, uint32_t length_b, bcal_cmac_ctx_t* ctx){
143         uint16_t blocksize_b;
144         blocksize_b = ctx->blocksize_B*8;
145         while(length_b>blocksize_b){
146                 bcal_cmac_nextBlock(ctx, block);
147                 block = (uint8_t*)block + ctx->blocksize_B;
148                 length_b -= blocksize_b;
149         }
150         bcal_cmac_lastBlock(ctx, block, length_b);
151         bcal_cmac_ctx2mac(dest, out_length_b, ctx);
152 }