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/>.
23 #include "bcal-basic.h"
24 #include "bcal-cmac.h"
28 static uint8_t left_shift_be_block(void* block, uint8_t blocksize_B){
32 c2 = (((uint8_t*)block)[blocksize_B])>>7;
33 (((uint8_t*)block)[blocksize_B]) <<= 1;
34 (((uint8_t*)block)[blocksize_B]) |= c1;
40 static const uint8_t const_128 = 0x87;
41 static const uint8_t const_64 = 0x1b;
43 uint8_t bcal_cmac_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_cmac_ctx_t* ctx){
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){
50 ctx->accu = malloc(ctx->blocksize_B);
54 ctx->k1 = malloc(ctx->blocksize_B);
58 ctx->k2 = malloc(ctx->blocksize_B);
62 r = bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
66 if(ctx->blocksize_B==128/8){
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;
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;
85 void bcal_cmac_free(bcal_cmac_ctx_t* ctx){
89 bcal_cipher_free(&(ctx->cctx));
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));
98 void bcal_cmac_lastBlock(bcal_cmac_ctx_t* ctx, const void* block, uint16_t length_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;
106 memxor(ctx->accu, block, (length_b+7)/8);
107 if(length_b==blocksize_b){
108 memxor(ctx->accu, ctx->k1, ctx->blocksize_B);
110 memxor(ctx->accu, ctx->k2, ctx->blocksize_B);
111 ctx->accu[length_b/8] ^= 0x80>>(length_b&7);
113 bcal_cipher_enc(ctx->accu, &(ctx->cctx));
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);
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]);
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;
132 bcal_cmac_lastBlock(ctx, block, length_b);
133 bcal_cmac_ctx2mac(dest, out_length_b, ctx);