1 /*
2 * Copyright (C) 2006-2010 Vincent Hanquez <vincent@snarc.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include <string.h>
26 #include "cryptonite_skein.h"
27 #include "cryptonite_skein256.h"
28 #include "cryptonite_bitfn.h"
29 #include "cryptonite_align.h"
30
31 static const uint8_t K256_0[2] = { 14, 16, };
32 static const uint8_t K256_1[2] = { 52, 57, };
33 static const uint8_t K256_2[2] = { 23, 40, };
34 static const uint8_t K256_3[2] = { 5, 37, };
35 static const uint8_t K256_4[2] = { 25, 33, };
36 static const uint8_t K256_5[2] = { 46, 12, };
37 static const uint8_t K256_6[2] = { 58, 22, };
38 static const uint8_t K256_7[2] = { 32, 32, };
39
skein256_do_chunk(struct skein256_ctx * ctx,uint64_t * buf,uint32_t len)40 static inline void skein256_do_chunk(struct skein256_ctx *ctx, uint64_t *buf, uint32_t len)
41 {
42 uint64_t x[4];
43 uint64_t ts[3];
44 uint64_t ks[4+1];
45
46 ks[4] = 0x1bd11bdaa9fc1a22ULL;
47 ks[0] = ctx->h[0]; ks[4] ^= ctx->h[0];
48 ks[1] = ctx->h[1]; ks[4] ^= ctx->h[1];
49 ks[2] = ctx->h[2]; ks[4] ^= ctx->h[2];
50 ks[3] = ctx->h[3]; ks[4] ^= ctx->h[3];
51
52 ts[0] = ctx->t0;
53 ts[1] = ctx->t1;
54
55 ts[0] += len;
56
57 ts[2] = ts[0] ^ ts[1];
58
59 #define INJECTKEY(r) \
60 x[0] += ks[((r)+0) % (4+1)]; \
61 x[1] += ks[((r)+1) % (4+1)] + ts[((r)+0) % 3]; \
62 x[2] += ks[((r)+2) % (4+1)] + ts[((r)+1) % 3]; \
63 x[3] += ks[((r)+3) % (4+1)] + (r)
64
65 #define ROUND(a,b,c,d,k) \
66 x[a] += x[b]; x[b] = rol64(x[b],k[0]); x[b] ^= x[a]; \
67 x[c] += x[d]; x[d] = rol64(x[d],k[1]); x[d] ^= x[c];
68
69 #define PASS(i) \
70 ROUND(0,1,2,3,K256_0); \
71 ROUND(0,3,2,1,K256_1); \
72 ROUND(0,1,2,3,K256_2); \
73 ROUND(0,3,2,1,K256_3); \
74 INJECTKEY((i*2) + 1); \
75 ROUND(0,1,2,3,K256_4); \
76 ROUND(0,3,2,1,K256_5); \
77 ROUND(0,1,2,3,K256_6); \
78 ROUND(0,3,2,1,K256_7); \
79 INJECTKEY((i*2) + 2)
80
81 x[0] = le64_to_cpu(buf[0]) + ks[0];
82 x[1] = le64_to_cpu(buf[1]) + ks[1] + ts[0];
83 x[2] = le64_to_cpu(buf[2]) + ks[2] + ts[1];
84 x[3] = le64_to_cpu(buf[3]) + ks[3];
85
86 /* 9 pass of 8 rounds = 72 rounds */
87 PASS(0);
88 PASS(1);
89 PASS(2);
90 PASS(3);
91 PASS(4);
92 PASS(5);
93 PASS(6);
94 PASS(7);
95 PASS(8);
96
97 ts[1] &= ~FLAG_FIRST;
98 ctx->t0 = ts[0];
99 ctx->t1 = ts[1];
100
101 ctx->h[0] = x[0] ^ cpu_to_le64(buf[0]);
102 ctx->h[1] = x[1] ^ cpu_to_le64(buf[1]);
103 ctx->h[2] = x[2] ^ cpu_to_le64(buf[2]);
104 ctx->h[3] = x[3] ^ cpu_to_le64(buf[3]);
105 }
106
cryptonite_skein256_init(struct skein256_ctx * ctx,uint32_t hashlen)107 void cryptonite_skein256_init(struct skein256_ctx *ctx, uint32_t hashlen)
108 {
109 uint64_t buf[4];
110 memset(ctx, 0, sizeof(*ctx));
111
112 SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_CFG));
113
114 memset(buf, '\0', sizeof(buf));
115 buf[0] = cpu_to_le64((SKEIN_VERSION << 32) | SKEIN_IDSTRING);
116 buf[1] = cpu_to_le64(hashlen);
117 buf[2] = 0; /* tree info, not implemented */
118 skein256_do_chunk(ctx, buf, 4*8);
119
120 SET_TYPE(ctx, FLAG_FIRST | FLAG_TYPE(TYPE_MSG));
121 }
122
cryptonite_skein256_update(struct skein256_ctx * ctx,const uint8_t * data,uint32_t len)123 void cryptonite_skein256_update(struct skein256_ctx *ctx, const uint8_t *data, uint32_t len)
124 {
125 uint32_t to_fill;
126
127 if (!len)
128 return;
129
130 to_fill = 32 - ctx->bufindex;
131
132 if (ctx->bufindex == 32) {
133 skein256_do_chunk(ctx, (uint64_t *) ctx->buf, 32);
134 ctx->bufindex = 0;
135 }
136
137 /* process partial buffer if there's enough data to make a block
138 * and there's without doubt further blocks */
139 if (ctx->bufindex && len > to_fill) {
140 memcpy(ctx->buf + ctx->bufindex, data, to_fill);
141 skein256_do_chunk(ctx, (uint64_t *) ctx->buf, 32);
142 len -= to_fill;
143 data += to_fill;
144 ctx->bufindex = 0;
145 }
146
147 if (need_alignment(data, 8)) {
148 uint64_t tramp[4];
149 ASSERT_ALIGNMENT(tramp, 8);
150 for (; len > 32; len -= 32, data += 32) {
151 memcpy(tramp, data, 32);
152 skein256_do_chunk(ctx, tramp, 32);
153 }
154 } else {
155 /* process as much 32-block as possible except the last one in case we finalize */
156 for (; len > 32; len -= 32, data += 32)
157 skein256_do_chunk(ctx, (uint64_t *) data, 32);
158 }
159
160 /* append data into buf */
161 if (len) {
162 memcpy(ctx->buf + ctx->bufindex, data, len);
163 ctx->bufindex += len;
164 }
165 }
166
cryptonite_skein256_finalize(struct skein256_ctx * ctx,uint32_t hashlen,uint8_t * out)167 void cryptonite_skein256_finalize(struct skein256_ctx *ctx, uint32_t hashlen, uint8_t *out)
168 {
169 uint32_t outsize;
170 uint64_t x[4];
171 int i, j, n;
172
173 ctx->t1 |= FLAG_FINAL;
174 /* if buf is not complete pad with 0 bytes */
175 if (ctx->bufindex < 32)
176 memset(ctx->buf + ctx->bufindex, '\0', 32 - ctx->bufindex);
177 skein256_do_chunk(ctx, (uint64_t *) ctx->buf, ctx->bufindex);
178
179 memset(ctx->buf, '\0', 32);
180
181 /* make sure we have a 8 bit up rounded value */
182 outsize = (hashlen + 7) >> 3;
183
184 /* backup h[0--4] */
185 for (j = 0; j < 4; j++)
186 x[j] = ctx->h[j];
187 /* threefish in counter mode, 0 for 1st 64 bytes, 1 for 2nd 64 bytes, .. */
188 for (i = 0; i*32 < outsize; i++) {
189 uint64_t w[4];
190 *((uint64_t *) ctx->buf) = cpu_to_le64(i);
191 SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_OUT));
192 skein256_do_chunk(ctx, (uint64_t *) ctx->buf, sizeof(uint64_t));
193
194 n = outsize - i * 32;
195 if (n >= 32) n = 32;
196
197 cpu_to_le64_array(w, ctx->h, 4);
198 memcpy(out + i*32, w, n);
199
200 /* restore h[0--4] */
201 for (j = 0; j < 4; j++)
202 ctx->h[j] = x[j];
203 }
204 }
205