3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2008 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/>.
21 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
30 #include "cubehash_rotates.h"
36 • Add x_0jklm into x_1jklm modulo 2**32 , 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 2**32 , 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).
48 static void cubehash_round(cubehash_ctx_t *ctx)
52 for (i = 0; i < 16; ++i) {
53 ctx->a[i + 16] += t = ctx->a[i];
54 ctx->a[i] = rotate7left(t);
56 xchg32_array(&(ctx->a[0]), &(ctx->a[8]), 8);
57 for (i = 0; i < 16; i += 4) {
60 ctx->a[i + 16] = ctx->a[i + 18] + t2;
61 ctx->a[i] = rotate11left(t2);
62 t2 = ctx->a[i + 2] ^= ctx->a[i + 18];
63 ctx->a[i + 18] = t + t2;
64 ctx->a[i + 2] = rotate11left(t2);
66 t2 = ctx->a[i + 1] ^= t;
67 ctx->a[i + 17] = ctx->a[i + 19] + t2;
68 ctx->a[i + 1] = rotate11left(t2);
69 t2 = ctx->a[i + 3] ^= ctx->a[i + 19];
70 ctx->a[i + 19] = t + t2;
71 ctx->a[i + 3] = rotate11left(t2);
73 xchg32_array(&(ctx->a[0]), &(ctx->a[4]), 4);
74 xchg32_array(&(ctx->a[8]), &(ctx->a[12]), 4);
75 for (i = 0; i < 16; i += 2) {
76 ctx->a[i] ^= t = ctx->a[i + 16];
77 ctx->a[i + 1] ^= ctx->a[i + 16] = ctx->a[i + 17];
82 void cubehash_init(uint8_t r, uint8_t b, uint16_t h, cubehash_ctx_t *ctx)
84 memset(ctx->a, 0, 32 * 4);
90 for (b = 0; b < 10 * r; ++b) {
95 void cubehash_nextBlock(cubehash_ctx_t *ctx, void *block)
98 memxor(ctx->a, block, ctx->blocksize_B);
99 for (i = 0; i < ctx->rounds; ++i) {
104 void cubehash_lastBlock(cubehash_ctx_t *ctx, void *block, uint16_t length_b)
106 while (length_b >= ctx->blocksize_B * 8) {
107 cubehash_nextBlock(ctx, block);
108 block = (uint8_t*) block + ctx->blocksize_B;
109 length_b -= ctx->blocksize_B * 8;
111 uint8_t buffer[ctx->blocksize_B];
113 memset(buffer, 0, ctx->blocksize_B);
114 memcpy(buffer, block, (length_b + 7) / 8);
115 buffer[length_b / 8] |= 0x80 >> (length_b & 7);
116 cubehash_nextBlock(ctx, buffer);
118 for (i = 0; i < 10 * (ctx->rounds); ++i) {
123 void cubehash_ctx2hash(void *dest, uint16_t length_b, cubehash_ctx_t *ctx)
125 memcpy(dest, ctx->a, (length_b + 7) / 8);
128 /******************************************************************************/
130 void cubehash224_init(cubehash_ctx_t *ctx)
132 cubehash_init(16, 32, 224, ctx);
135 void cubehash224_ctx2hash(void *dest, cubehash_ctx_t *ctx)
137 cubehash_ctx2hash(dest, 224, ctx);
140 /******************************************************************************/
142 void cubehash256_init(cubehash_ctx_t *ctx)
144 cubehash_init(16, 32, 256, ctx);
147 void cubehash256_ctx2hash(void *dest, cubehash_ctx_t *ctx)
149 cubehash_ctx2hash(dest, 256, ctx);
152 /******************************************************************************/
154 void cubehash384_init(cubehash_ctx_t *ctx)
156 cubehash_init(16, 32, 384, ctx);
159 void cubehash384_ctx2hash(void *dest, cubehash_ctx_t *ctx)
161 cubehash_ctx2hash(dest, 384, ctx);
164 /******************************************************************************/
166 void cubehash512_init(cubehash_ctx_t *ctx)
168 cubehash_init(16, 32, 512, ctx);
171 void cubehash512_ctx2hash(void *dest, cubehash_ctx_t *ctx)
173 cubehash_ctx2hash(dest, 512, ctx);