]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bmw/bmw_small-cstub.c
even more asm fun for BMW
[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
78 void bmw224(void* dest, const void* msg, uint32_t length_b){
79         bmw_small_ctx_t ctx;
80         bmw224_init(&ctx);
81         while(length_b>=BMW_SMALL_BLOCKSIZE){
82                 bmw_small_nextBlock(&ctx, msg);
83                 length_b -= BMW_SMALL_BLOCKSIZE;
84                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
85         }
86         bmw_small_lastBlock(&ctx, msg, length_b);
87         bmw224_ctx2hash(dest, &ctx);
88 }
89
90 void bmw256(void* dest, const void* msg, uint32_t length_b){
91         bmw_small_ctx_t ctx;
92         bmw256_init(&ctx);
93         while(length_b>=BMW_SMALL_BLOCKSIZE){
94                 bmw_small_nextBlock(&ctx, msg);
95                 length_b -= BMW_SMALL_BLOCKSIZE;
96                 msg = (uint8_t*)msg + BMW_SMALL_BLOCKSIZE_B;
97         }
98         bmw_small_lastBlock(&ctx, msg, length_b);
99         bmw256_ctx2hash(dest, &ctx);
100 }
101
102