]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal-cmac.c
cmac+testvectors
[avr-crypto-lib.git] / bcal-cmac.c
1 /* bcal-omac.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 <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         r = bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
63         if(r){
64                 return r;
65         }
66         if(ctx->blocksize_B==128/8){
67                 r = const_128;
68         }else{
69                 r = const_64;
70         }
71         /* subkey computation */
72         memset(ctx->accu, 0x00, ctx->blocksize_B);
73         memset(ctx->k1, 0x00, ctx->blocksize_B);
74         bcal_cipher_enc(ctx->k1, &(ctx->cctx));
75         if(left_shift_be_block(ctx->k1, ctx->blocksize_B)){
76                 ctx->k1[ctx->blocksize_B-1] ^= r;
77         }
78         memcpy(ctx->k2, ctx->k1, ctx->blocksize_B);
79         if(left_shift_be_block(ctx->k2, ctx->blocksize_B)){
80                 ctx->k2[ctx->blocksize_B-1] ^= r;
81         }
82         return 0;
83 }
84
85 void bcal_cmac_free(bcal_cmac_ctx_t* ctx){
86         free(ctx->accu);
87         free(ctx->k1);
88         free(ctx->k2);
89         bcal_cipher_free(&(ctx->cctx));
90 }
91
92 void bcal_cmac_nextBlock (bcal_cmac_ctx_t* ctx, const void* block){
93         memxor(ctx->accu, block, ctx->blocksize_B);
94         bcal_cipher_enc(ctx->accu, &(ctx->cctx));
95 }
96
97
98 void bcal_cmac_lastBlock(bcal_cmac_ctx_t* ctx, const void* block, uint16_t length_b){
99         uint16_t blocksize_b;
100         blocksize_b = ctx->blocksize_B*8;
101         while(length_b>blocksize_b){
102                 bcal_cmac_nextBlock(ctx, block);
103                 block = (uint8_t*)block + ctx->blocksize_B;
104                 length_b -= blocksize_b;
105         }
106         memxor(ctx->accu, block, (length_b+7)/8);
107         if(length_b==blocksize_b){
108                 memxor(ctx->accu, ctx->k1, ctx->blocksize_B);
109         }else{
110                 memxor(ctx->accu, ctx->k2, ctx->blocksize_B);
111                 ctx->accu[length_b/8] ^= 0x80>>(length_b&7);
112         }
113         bcal_cipher_enc(ctx->accu, &(ctx->cctx));
114 }
115
116 void bcal_cmac_ctx2mac(void* dest, uint16_t length_b, const bcal_cmac_ctx_t* ctx){
117         memcpy(dest, ctx->accu, length_b/8);
118         if(length_b&7){
119                 ((uint8_t*)dest)[length_b/8] &= 0xff>>(length_b&7);
120                 ((uint8_t*)dest)[length_b/8] |= (0xff00>>(length_b&7))&(ctx->accu[length_b/8]);
121         }
122 }
123
124 void bcal_cmac(void* dest, uint16_t out_length_b, const void* block, uint32_t length_b, bcal_cmac_ctx_t* ctx){
125         uint16_t blocksize_b;
126         blocksize_b = ctx->blocksize_B*8;
127         while(length_b>blocksize_b){
128                 bcal_cmac_nextBlock(ctx, block);
129                 block = (uint8_t*)block + ctx->blocksize_B;
130                 length_b -= blocksize_b;
131         }
132         bcal_cmac_lastBlock(ctx, block, length_b);
133         bcal_cmac_ctx2mac(dest, out_length_b, ctx);
134 }