]> git.cryptolib.org Git - avr-crypto-lib.git/blob - cubehash/cubehash.c
optimized cubehash a bit
[avr-crypto-lib.git] / cubehash / cubehash.c
1 /* cubehash.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  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     cubehash.c
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte
23  * \date     2010-02-20
24  * \license  GPLv3 or later
25  *
26  */
27
28
29 #include "memxor.h"
30 #include "cubehash.h"
31 #include "cubehash_rotates.h"
32 #include <string.h>
33 #include <stdint.h>
34
35 /*
36 • Add    x_0jklm into    x_1jklm modulo 232 , for each (j, k, l, m).
37 • Rotate x_0jklm upwards by 7 bits, for each (j, k, l, m).
38 • Swap   x_00klm with    x_01klm , for each (k, l, m).
39 • Xor    x_1jklm into    x_0jklm , for each (j, k, l, m).
40 • Swap   x_1jk0m with    x_1jk1m , for each (j, k, m).
41 • Add    x_0jklm into    x_1jklm modulo 232 , for each (j, k, l, m).
42 • Rotate x_0jklm upwards by 11 bits, for each (j, k, l, m).
43 • Swap   x_0j0lm with    x_0j1lm , for each (j, l, m).
44 • Xor    x_1jklm into    x_0jklm , for each (j, k, l, m).
45 • Swap   x_1jkl0 with    x_1jkl1 , for each (j, k, l).
46 */
47
48 static void cubehash_round(cubehash_ctx_t* ctx){
49         uint8_t i;
50         uint32_t t;
51         for(i=0; i<16; ++i){
52                 ctx->a[i+16] += ctx->a[i];
53                 ctx->a[i] = rotate7left(ctx->a[i]);
54         }
55         for(i=0; i<8; ++i){
56                 t = ctx->a[i];
57                 ctx->a[i] = ctx->a[i+8];
58                 ctx->a[i+8] = t;
59         }
60         for(i=0; i<16; ++i){
61                 ctx->a[i] ^= ctx->a[i+16];
62         }
63         for(i=16; i<4*4+16; i+=4){
64                 t = ctx->a[i];
65                 ctx->a[i] = ctx->a[i+2];
66                 ctx->a[i+2] = t;
67                 t = ctx->a[i+1];
68                 ctx->a[i+1] = ctx->a[i+3];
69                 ctx->a[i+3] = t;
70         }
71         for(i=0; i<16; ++i){
72                 ctx->a[i+16] += ctx->a[i];
73                 ctx->a[i] = rotate11left(ctx->a[i]);
74         }
75         for(i=0; i<4; ++i){
76                 t = ctx->a[i];
77                 ctx->a[i] = ctx->a[i+4];
78                 ctx->a[i+4] = t;
79         }
80         for(i=8; i<4+8; ++i){
81                 t = ctx->a[i];
82                 ctx->a[i] = ctx->a[i+4];
83                 ctx->a[i+4] = t;
84         }
85         for(i=0; i<16; ++i){
86                 ctx->a[i] ^= ctx->a[i+16];
87         }
88         for(i=16; i<16+16; i+=2){
89                 t = ctx->a[i];
90                 ctx->a[i] = ctx->a[i+1];
91                 ctx->a[i+1] = t;
92         }
93 }
94
95 void cubehash_init(uint8_t r, uint8_t b, uint16_t h, cubehash_ctx_t* ctx){
96         memset(ctx->a, 0, 32*4);
97         ctx->a[0] = h/8;
98         ctx->a[1] = b;
99         ctx->a[2] = r;
100         ctx->rounds = r;
101         ctx->blocksize_B = b;
102         for(b=0; b<10*r; ++b){
103                 cubehash_round(ctx);
104         }
105 }
106
107 void cubehash_nextBlock(cubehash_ctx_t* ctx, void* block){
108         uint8_t i;
109         memxor(ctx->a, block, ctx->blocksize_B);
110         for(i=0; i<ctx->rounds; ++i){
111                 cubehash_round(ctx);
112         }
113 }
114
115 void cubehash_lastBlock(cubehash_ctx_t* ctx, void* block, uint16_t length_b){
116         while(length_b>=ctx->blocksize_B*8){
117                 cubehash_nextBlock(ctx, block);
118                 block = (uint8_t*)block + ctx->blocksize_B;
119                 length_b -= ctx->blocksize_B*8;
120         }
121         uint8_t buffer[ctx->blocksize_B];
122         uint8_t i;
123         memset(buffer, 0, ctx->blocksize_B);
124         memcpy(buffer, block, (length_b+7)/8);
125         buffer[length_b/8] |= 0x80 >> (length_b&7);
126         cubehash_nextBlock(ctx, buffer);
127         ctx->a[31] ^= 1;
128         for(i=0; i<10*(ctx->rounds); ++i){
129                 cubehash_round(ctx);
130         }
131 }
132
133 void cubehash_ctx2hash(void* dest, uint16_t length_b, cubehash_ctx_t* ctx){
134         memcpy(dest, ctx->a, (length_b+7)/8);
135 }
136
137 /******************************************************************************/
138
139 void cubehash224_init(cubehash_ctx_t* ctx){
140         cubehash_init(16, 32, 224, ctx);
141 }
142
143 void cubehash224_ctx2hash(void* dest, cubehash_ctx_t* ctx){
144         cubehash_ctx2hash(dest, 224, ctx);
145 }
146
147 /******************************************************************************/
148
149 void cubehash256_init(cubehash_ctx_t* ctx){
150         cubehash_init(16, 32, 256, ctx);
151 }
152
153 void cubehash256_ctx2hash(void* dest, cubehash_ctx_t* ctx){
154         cubehash_ctx2hash(dest, 256, ctx);
155 }
156
157 /******************************************************************************/
158
159 void cubehash384_init(cubehash_ctx_t* ctx){
160         cubehash_init(16, 32, 384, ctx);
161 }
162
163 void cubehash384_ctx2hash(void* dest, cubehash_ctx_t* ctx){
164         cubehash_ctx2hash(dest, 384, ctx);
165 }
166
167 /******************************************************************************/
168
169 void cubehash512_init(cubehash_ctx_t* ctx){
170         cubehash_init(16, 32, 512, ctx);
171 }
172
173 void cubehash512_ctx2hash(void* dest, cubehash_ctx_t* ctx){
174         cubehash_ctx2hash(dest, 512, ctx);
175 }