]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw/bmw_small-cstub.c
5b3414587ecd75b22b254a8df44b9ab184043323
[avr-crypto-lib.git] / bmw / bmw_small-cstub.c
1 /* bmw_small.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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  * \file    bmw_small.c
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-04-27
24  * \license GPLv3 or later
25  *
26  */
27
28 #include <stdint.h>
29 #include <string.h>
30 #include <avr/pgmspace.h>
31 #include "bmw_small.h"
32
33
34 #define SHL32(a,n) ((a)<<(n))
35 #define SHR32(a,n) ((a)>>(n))
36 #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
37 #define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
38
39 #define DEBUG 0
40
41
42 #if DEBUG
43  #include "cli.h"
44
45  void ctx_dump(const bmw_small_ctx_t* ctx){
46         uint8_t i;
47         cli_putstr_P(PSTR("\r\n==== ctx dump ===="));
48         for(i=0; i<16;++i){
49                 cli_putstr_P(PSTR("\r\n h["));
50                 cli_hexdump(&i, 1);
51                 cli_putstr_P(PSTR("] = "));
52                 cli_hexdump_rev(&(ctx->h[i]), 4);
53         }
54         cli_putstr_P(PSTR("\r\n counter = "));
55         cli_hexdump(&(ctx->counter), 4);
56  }
57
58  void dump_x(const uint32_t* q, uint8_t elements, char x){
59         uint8_t i;
60         cli_putstr_P(PSTR("\r\n==== "));
61         cli_putc(x);
62         cli_putstr_P(PSTR(" dump ===="));
63         for(i=0; i<elements;++i){
64                 cli_putstr_P(PSTR("\r\n "));
65                 cli_putc(x);
66                 cli_putstr_P(PSTR("["));
67                 cli_hexdump(&i, 1);
68                 cli_putstr_P(PSTR("] = "));
69                 cli_hexdump_rev(&(q[i]), 4);
70         }
71  }
72 #else
73  #define ctx_dump(x)
74  #define dump_x(a,b,c)
75 #endif
76
77 void bmw_small_f1(uint32_t* q, const void* m, const void* h);
78 void bmw_small_f0(uint32_t* h, const void* m, uint32_t* q);
79 void bmw_small_f2(uint32_t* h, uint32_t* q, const void* m);
80 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block);
81
82 /*
83 void bmw_small_nextBlock(bmw_small_ctx_t* ctx, const void* block){
84         uint32_t q[32];
85         dump_x(block, 16, 'M');
86         bmw_small_f0(ctx->h, block, q);
87         dump_x(q, 16, 'Q');
88         bmw_small_f1(q, block, ctx->h);
89         dump_x(q, 32, 'Q');
90         bmw_small_f2(ctx->h, q, block);
91         ctx->counter += 1;
92         ctx_dump(ctx);
93 }
94 */
95
96 void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
97         struct {
98                 uint8_t  buffer[64];
99                 uint32_t ctr;
100         } pctx;
101         while(length_b >= BMW_SMALL_BLOCKSIZE){
102                 bmw_small_nextBlock(ctx, block);
103                 length_b -= BMW_SMALL_BLOCKSIZE;
104                 block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
105         }
106         memset(pctx.buffer, 0, 64);
107         memcpy(pctx.buffer, block, (length_b+7)/8);
108         pctx.buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
109         if(length_b+1>64*8-64){
110                 bmw_small_nextBlock(ctx, pctx.buffer);
111                 memset(pctx.buffer, 0, 64-8);
112                 ctx->counter -= 1;
113         }
114         *((uint64_t*)&(pctx.buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
115         bmw_small_nextBlock(ctx, pctx.buffer);
116         uint8_t i;
117         memset(pctx.buffer, 0xaa, 64);
118         for(i=0; i<16;++i){
119                 pctx.buffer[i*4] = i+0xa0;
120         }
121         bmw_small_nextBlock((bmw_small_ctx_t*)&pctx, ctx->h);
122         memcpy(ctx->h, pctx.buffer, 64);
123 }
124
125 void bmw224_init(bmw224_ctx_t* ctx){
126         uint8_t i;
127         ctx->h[0] = 0x00010203;
128         for(i=1; i<16; ++i){
129                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
130         }
131         ctx->counter=0;
132 //      ctx_dump(ctx);
133 }
134
135 void bmw256_init(bmw256_ctx_t* ctx){
136         uint8_t i;
137         ctx->h[0] = 0x40414243;
138         for(i=1; i<16; ++i){
139                 ctx->h[i] = ctx->h[i-1]+ 0x04040404;
140         }
141         ctx->counter=0;
142 //      ctx_dump(ctx);
143 }
144
145 void bmw224_nextBlock(bmw224_ctx_t* ctx, const void* block){
146         bmw_small_nextBlock(ctx, block);
147 }
148
149 void bmw256_nextBlock(bmw256_ctx_t* ctx, const void* block){
150         bmw_small_nextBlock(ctx, block);
151 }
152
153 void bmw224_lastBlock(bmw224_ctx_t* ctx, const void* block, uint16_t length_b){
154         bmw_small_lastBlock(ctx, block, length_b);
155 }
156
157 void bmw256_lastBlock(bmw256_ctx_t* ctx, const void* block, uint16_t length_b){
158         bmw_small_lastBlock(ctx, block, length_b);
159 }
160
161 void bmw224_ctx2hash(void* dest, const bmw224_ctx_t* ctx){
162         memcpy(dest, &(ctx->h[9]), 224/8);
163 }
164
165 void bmw256_ctx2hash(void* dest, const bmw256_ctx_t* ctx){
166         memcpy(dest, &(ctx->h[8]), 256/8);
167 }
168
169 void bmw224(void* dest, const void* msg, uint32_t length_b){
170         bmw_small_ctx_t ctx;
171         bmw224_init(&ctx);
172         while(length_b>=BMW_SMALL_BLOCKSIZE){
173                 bmw_small_nextBlock(&ctx, msg);
174                 length_b -= BMW_SMALL_BLOCKSIZE;
175                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
176         }
177         bmw_small_lastBlock(&ctx, msg, length_b);
178         bmw224_ctx2hash(dest, &ctx);
179 }
180
181 void bmw256(void* dest, const void* msg, uint32_t length_b){
182         bmw_small_ctx_t ctx;
183         bmw256_init(&ctx);
184         while(length_b>=BMW_SMALL_BLOCKSIZE){
185                 bmw_small_nextBlock(&ctx, msg);
186                 length_b -= BMW_SMALL_BLOCKSIZE;
187                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
188         }
189         bmw_small_lastBlock(&ctx, msg, length_b);
190         bmw256_ctx2hash(dest, &ctx);
191 }
192
193