summaryrefslogtreecommitdiff
path: root/lib/crypto/sha3.myr
blob: 9d44ae32f27f2176e57bc19e88ca1a0bbe8c24d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use std

pkg crypto =
	type keccak224 = struct
		x	: uint64[25]
		tail	: byte[144]
		msglen	: std.size
	;;

	type keccak256 = struct
		x	: uint64[25]
		tail	: byte[136]
		msglen	: std.size
	;;
	
	type keccak384 = struct
		x	: uint64[25]
		tail	: byte[104]
		msglen	: std.size
	;;
	
	type keccak512 = struct
		x	: uint64[25]
		tail	: byte[72]
		msglen	: std.size
	;;

	const keccak224	: (data : byte[:] -> byte[28])
	const keccak224init	: (st : keccak224# -> void)
	const keccak224add	: (st : keccak224#, data : byte[:] -> void)
	const keccak224fin	: (st : keccak224# -> byte[28])
	
	const keccak256	: (data : byte[:] -> byte[32])
	const keccak256init	: (st : keccak256# -> void)
	const keccak256add	: (st : keccak256#, data : byte[:] -> void)
	const keccak256fin	: (st : keccak256# -> byte[32])

	const keccak384	: (data : byte[:] -> byte[48])
	const keccak384init	: (st : keccak384# -> void)
	const keccak384add	: (st : keccak384#, data : byte[:] -> void)
	const keccak384fin	: (st : keccak384# -> byte[48])

	const keccak512	: (data : byte[:] -> byte[64])
	const keccak512init	: (st : keccak512# -> void)
	const keccak512add	: (st : keccak512#, data : byte[:] -> void)
	const keccak512fin	: (st : keccak512# -> byte[64])
;;

const Nrounds = 24

const keccak224 = {data
	var st

	keccak224init(&st)
	keccak224add(&st, data)
	-> keccak224fin(&st)
}

const keccak224init = {st; 
	std.slfill(st.x[:], 0)
	st.msglen = 0
}

const keccak224add = {st, data
	st.msglen += keccak(st.x[:], data, 144, st.tail[:], st.msglen)
}

const keccak224fin = {st
	var ret : byte[28]

	fin(st.x[:], 144, st.tail[:], st.msglen)
	std.putle64(ret[ 0: 8], st.x[0])
	std.putle64(ret[ 8:16], st.x[1])
	std.putle64(ret[16:24], st.x[2])
	std.putle32(ret[24:28], st.x[3])
	-> ret
}

const keccak256 = {data
	var st

	keccak256init(&st)
	keccak256add(&st, data)
	-> keccak256fin(&st)
}

const keccak256init = {st
	std.slfill(st.x[:], 0)
	st.msglen = 0
}

const keccak256add = {st, data
	st.msglen += keccak(st.x[:], data, 136, st.tail[:], st.msglen)
}

const keccak256fin = {st
	var ret : byte[32]

	fin(st.x[:], 136, st.tail[:], st.msglen) 
	std.putle64(ret[ 0: 8], st.x[0])
	std.putle64(ret[ 8:16], st.x[1])
	std.putle64(ret[16:24], st.x[2])
	std.putle64(ret[24:32], st.x[3])
	-> ret
}

const keccak384 = {data
	var st

	keccak384init(&st)
	keccak384add(&st, data)
	-> keccak384fin(&st)
}

const keccak384init = {st
	std.slfill(st.x[:], 0)
	st.msglen = 0
}

const keccak384add = {st, data
	st.msglen += keccak(st.x[:], data, 104, st.tail[:], st.msglen)
}

const keccak384fin = {st
	var ret : byte[48]

	fin(st.x[:], 104, st.tail[:], st.msglen) 
	std.putle64(ret[ 0: 8], st.x[0])
	std.putle64(ret[ 8:16], st.x[1])
	std.putle64(ret[16:24], st.x[2])
	std.putle64(ret[24:32], st.x[3])
	std.putle64(ret[32:40], st.x[4])
	std.putle64(ret[40:48], st.x[5])
	-> ret
}

const keccak512init = {st
	std.slfill(st.x[:], 0)
	st.msglen = 0
}
const keccak512 = {data
	var st

	keccak512init(&st)
	keccak512add(&st, data)
	-> keccak512fin(&st)
}

const keccak512add = {st, data
	st.msglen += keccak(st.x[:], data, 72, st.tail[:], st.msglen)
}

const keccak512fin = {st
	var ret : byte[64]

	fin(st.x[:], 72, st.tail[:], st.msglen) 
	std.putle64(ret[ 0: 8], st.x[0])
	std.putle64(ret[ 8:16], st.x[1])
	std.putle64(ret[16:24], st.x[2])
	std.putle64(ret[24:32], st.x[3])
	std.putle64(ret[32:40], st.x[4])
	std.putle64(ret[40:48], st.x[5])
	std.putle64(ret[48:56], st.x[6])
	std.putle64(ret[56:64], st.x[7])
	-> ret
}

const fin = {x, blocksz, tail, msgsz
	var ntail

	ntail = msgsz % blocksz
	std.slfill(tail[ntail:], 0)
	tail[ntail] |= 0x01 /* wat */
	tail[tail.len - 1] |= 0x80
	addblock(x, tail)
	keccakf(x)
}

const keccak = {x, data, blocksz, tail, msglen
	var n, ntail, len

	len = data.len
	/* handle tail of last block */
	ntail = msglen % blocksz
	if ntail > 0
		n = std.min(blocksz - ntail, data.len)
		std.slcp(tail[ntail:ntail + n], data[:n])
		data = data[n:]
		if n + ntail < blocksz
			-> n + ntail
		;;
		addblock(x, tail)
		keccakf(x)
	;;
	while data.len >= blocksz
		addblock(x, data)
		keccakf(x)
		data = data[blocksz:]
	;;
	std.slcp(tail[:data.len], data)
	-> len
}

const keccakf = {x : uint64[:]
	var bc0, bc1, bc2, bc3, bc4
	var t0, t1, t2, t3, t4

	for var round = 0; round < Nrounds; round++
		/* theta(x) */
		bc0 = x[0] ^ x[5] ^ x[10] ^ x[15] ^ x[20]
		bc1 = x[1] ^ x[6] ^ x[11] ^ x[16] ^ x[21]
		bc2 = x[2] ^ x[7] ^ x[12] ^ x[17] ^ x[22]
		bc3 = x[3] ^ x[8] ^ x[13] ^ x[18] ^ x[23]
		bc4 = x[4] ^ x[9] ^ x[14] ^ x[19] ^ x[24]

		t0 = bc4 ^ ((bc1 << 1) | (bc1 >> 63))
		t1 = bc0 ^ ((bc2 << 1) | (bc2 >> 63))
		t2 = bc1 ^ ((bc3 << 1) | (bc3 >> 63))
		t3 = bc2 ^ ((bc4 << 1) | (bc4 >> 63))
		t4 = bc3 ^ ((bc0 << 1) | (bc0 >> 63))

		x[ 0] ^= t0
		x[ 5] ^= t0
		x[10] ^= t0
		x[15] ^= t0
		x[20] ^= t0

		x[ 1] ^= t1
		x[ 6] ^= t1
		x[11] ^= t1
		x[16] ^= t1
		x[21] ^= t1

		x[ 2] ^= t2
		x[ 7] ^= t2
		x[12] ^= t2
		x[17] ^= t2
		x[22] ^= t2

		x[ 3] ^= t3
		x[ 8] ^= t3
		x[13] ^= t3
		x[18] ^= t3
		x[23] ^= t3

		x[ 4] ^= t4
		x[ 9] ^= t4
		x[14] ^= t4
		x[19] ^= t4
		x[24] ^= t4

		/* rho(x) */

		t0 = x[1]
		t1 = x[10]; x[10] = ((t0 <<  1) | (t0 >> 63)); t0 = t1
		t1 = x[ 7]; x[ 7] = ((t0 <<  3) | (t0 >> 61)); t0 = t1
		t1 = x[11]; x[11] = ((t0 <<  6) | (t0 >> 58)); t0 = t1
		t1 = x[17]; x[17] = ((t0 << 10) | (t0 >> 54)); t0 = t1
		t1 = x[18]; x[18] = ((t0 << 15) | (t0 >> 49)); t0 = t1
		t1 = x[ 3]; x[ 3] = ((t0 << 21) | (t0 >> 43)); t0 = t1
		t1 = x[ 5]; x[ 5] = ((t0 << 28) | (t0 >> 36)); t0 = t1
		t1 = x[16]; x[16] = ((t0 << 36) | (t0 >> 28)); t0 = t1
		t1 = x[ 8]; x[ 8] = ((t0 << 45) | (t0 >> 19)); t0 = t1
		t1 = x[21]; x[21] = ((t0 << 55) | (t0 >>  9)); t0 = t1
		t1 = x[24]; x[24] = ((t0 <<  2) | (t0 >> 62)); t0 = t1
		t1 = x[ 4]; x[ 4] = ((t0 << 14) | (t0 >> 50)); t0 = t1
		t1 = x[15]; x[15] = ((t0 << 27) | (t0 >> 37)); t0 = t1
		t1 = x[23]; x[23] = ((t0 << 41) | (t0 >> 23)); t0 = t1
		t1 = x[19]; x[19] = ((t0 << 56) | (t0 >>  8)); t0 = t1
		t1 = x[13]; x[13] = ((t0 <<  8) | (t0 >> 56)); t0 = t1
		t1 = x[12]; x[12] = ((t0 << 25) | (t0 >> 39)); t0 = t1
		t1 = x[ 2]; x[ 2] = ((t0 << 43) | (t0 >> 21)); t0 = t1
		t1 = x[20]; x[20] = ((t0 << 62) | (t0 >>  2)); t0 = t1
		t1 = x[14]; x[14] = ((t0 << 18) | (t0 >> 46)); t0 = t1
		t1 = x[22]; x[22] = ((t0 << 39) | (t0 >> 25)); t0 = t1
		t1 = x[ 9]; x[ 9] = ((t0 << 61) | (t0 >>  3)); t0 = t1
		t1 = x[ 6]; x[ 6] = ((t0 << 20) | (t0 >> 44)); t0 = t1
		t1 = x[ 1]; x[ 1] = ((t0 << 44) | (t0 >> 20)); t0 = t1

		/* chi */
		for var i = 0; i < 25; i += 5
			bc0 = x[i+0]
			bc1 = x[i+1]
			bc2 = x[i+2]
			bc3 = x[i+3]
			bc4 = x[i+4]
			x[i+0] ^= ~bc1 & bc2
			x[i+1] ^= ~bc2 & bc3
			x[i+2] ^= ~bc3 & bc4
			x[i+3] ^= ~bc4 & bc0
			x[i+4] ^= ~bc0 & bc1
		;;


		/* iota */
		x[0] ^= rconst[round]
	;;
}

const addblock = {x, buf
	x[0] ^= std.getle64(buf[ 0: 8])
	x[1] ^= std.getle64(buf[ 8:16])
	x[2] ^= std.getle64(buf[16:24])
	x[3] ^= std.getle64(buf[24:32])
	x[4] ^= std.getle64(buf[32:40])
	x[5] ^= std.getle64(buf[40:48])
	x[6] ^= std.getle64(buf[48:56])
	x[7] ^= std.getle64(buf[56:64])
	x[8] ^= std.getle64(buf[64:72])
	if buf.len <= 72
		-> void
	;;
	x[ 9] ^= std.getle64(buf[72:80]);
	x[10] ^= std.getle64(buf[80:88]);
	x[11] ^= std.getle64(buf[88:96]);
	x[12] ^= std.getle64(buf[96:104]);
	if buf.len <= 104
		-> void
	;;
	x[13] ^= std.getle64(buf[104:112]);
	x[14] ^= std.getle64(buf[112:120]);
	x[15] ^= std.getle64(buf[120:128]);
	x[16] ^= std.getle64(buf[128:136]);
	if buf.len <= 136
		-> void
	;;
	x[17] ^= std.getle64(buf[136:144]);
}

const rconst = [
	0x0000000000000001ul, 0x0000000000008082ul, 0x800000000000808aul,
	0x8000000080008000ul, 0x000000000000808bul, 0x0000000080000001ul,
	0x8000000080008081ul, 0x8000000000008009ul, 0x000000000000008aul,
	0x0000000000000088ul, 0x0000000080008009ul, 0x000000008000000aul,
	0x000000008000808bul, 0x800000000000008bul, 0x8000000000008089ul,
	0x8000000000008003ul, 0x8000000000008002ul, 0x8000000000000080ul, 
	0x000000000000800aul, 0x800000008000000aul, 0x8000000080008081ul,
	0x8000000000008080ul, 0x0000000080000001ul, 0x8000000080008008ul
]