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