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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
sha3sum – SHA-3 (Keccak) checksum calculator
Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
class SHA3:
'''
SHA-3/Keccak hash algorithm implementation
@author Mattias Andrée (maandree@member.fsf.org)
'''
def __init__(self):
'''
Constructor
'''
self.RC = [0x0000000000000001, 0x0000000000008082, 0x800000000000808A, 0x8000000080008000,
0x000000000000808B, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
0x000000000000008A, 0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
0x000000008000808B, 0x800000000000008B, 0x8000000000008089, 0x8000000000008003,
0x8000000000008002, 0x8000000000000080, 0x000000000000800A, 0x800000008000000A,
0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008]
'''
:list<int> Round contants
'''
self.B = [0] * 25
'''
:list<int> Keccak-f round temporary
'''
self.C = [0] * 5
'''
:list<int> Keccak-f round temporary
'''
(self.r, self.c, self.n, self.b, self.w, self.wmod, self.l, self.nr) = (0, 0, 0, 0, 0, 0, 0, 0)
'''
r:int The bitrate
c:int The capacity
n:int The output size
b:int The state size
w:int The word size
wmod:int The word mask
l:int ℓ, the binary logarithm of the word size
nr:int 12 + 2ℓ, the number of rounds
'''
self.S = None
'''
:list<int> The current state
'''
self.M = None
'''
:bytes Left over water to fill the sponge with at next update
'''
def rotate(self, x, n):
'''
Rotate a word
@param x:int The value to rotate
@param n:int Rotation steps
@return :int The value rotated
'''
m = n % self.w
return ((x >> (self.w - m)) + (x << m)) & self.wmod
def rotate64(self, x, n):
'''
Rotate a 64-bit word
@param x:int The value to rotate
@param n:int Rotation steps
@return :int The value rotated
'''
return ((x >> (64 - n)) + (x << n)) & 0xFFFFFFFFFFFFFFFF
def lb(self, x):
'''
Binary logarithm
@param x:int The value of which to calculate the binary logarithm
@return :int The binary logarithm
'''
rc = 0
if (x & 0xFF00) != 0: rc += 8 ; x >>= 8
if (x & 0x00F0) != 0: rc += 4 ; x >>= 4
if (x & 0x000C) != 0: rc += 2 ; x >>= 2
if (x & 0x0002) != 0: rc += 1
return rc
def keccakFRound(self, A, rc):
'''
Perform one round of computation
@param A:list<int> The current state
@param rc:int Round constant
'''
if self.w == 64:
# θ step (step 1 and 2 of 3)
self.C[0] = (A[0] ^ A[1]) ^ (A[2] ^ A[3]) ^ A[4]
self.C[2] = (A[10] ^ A[11]) ^ (A[12] ^ A[13]) ^ A[14]
db = self.C[0] ^ self.rotate64(self.C[2], 1)
self.C[4] = (A[20] ^ A[21]) ^ (A[22] ^ A[23]) ^ A[24]
dd = self.C[2] ^ self.rotate64(self.C[4], 1)
self.C[1] = (A[5] ^ A[6]) ^ (A[7] ^ A[8]) ^ A[9]
da = self.C[4] ^ self.rotate64(self.C[1], 1)
self.C[3] = (A[15] ^ A[16]) ^ (A[17] ^ A[18]) ^ A[19]
dc = self.C[1] ^ self.rotate64(self.C[3], 1)
de = self.C[3] ^ self.rotate64(self.C[0], 1)
# ρ and π steps, with last part of θ
self.B[0] = self.rotate64(A[0] ^ da, 0)
self.B[1] = self.rotate64(A[15] ^ dd, 28)
self.B[2] = self.rotate64(A[5] ^ db, 1)
self.B[3] = self.rotate64(A[20] ^ de, 27)
self.B[4] = self.rotate64(A[10] ^ dc, 62)
self.B[5] = self.rotate64(A[6] ^ db, 44)
self.B[6] = self.rotate64(A[21] ^ de, 20)
self.B[7] = self.rotate64(A[11] ^ dc, 6)
self.B[8] = self.rotate64(A[1] ^ da, 36)
self.B[9] = self.rotate64(A[16] ^ dd, 55)
self.B[10] = self.rotate64(A[12] ^ dc, 43)
self.B[11] = self.rotate64(A[2] ^ da, 3)
self.B[12] = self.rotate64(A[17] ^ dd, 25)
self.B[13] = self.rotate64(A[7] ^ db, 10)
self.B[14] = self.rotate64(A[22] ^ de, 39)
self.B[15] = self.rotate64(A[18] ^ dd, 21)
self.B[16] = self.rotate64(A[8] ^ db, 45)
self.B[17] = self.rotate64(A[23] ^ de, 8)
self.B[18] = self.rotate64(A[13] ^ dc, 15)
self.B[19] = self.rotate64(A[3] ^ da, 41)
self.B[20] = self.rotate64(A[24] ^ de, 14)
self.B[21] = self.rotate64(A[14] ^ dc, 61)
self.B[22] = self.rotate64(A[4] ^ da, 18)
self.B[23] = self.rotate64(A[19] ^ dd, 56)
self.B[24] = self.rotate64(A[9] ^ db, 2)
else:
# θ step (step 1 and 2 of 3)
self.C[0] = (A[0] ^ A[1]) ^ (A[2] ^ A[3]) ^ A[4]
self.C[2] = (A[10] ^ A[11]) ^ (A[12] ^ A[13]) ^ A[14]
db = self.C[0] ^ self.rotate(self.C[2], 1)
self.C[4] = (A[20] ^ A[21]) ^ (A[22] ^ A[23]) ^ A[24]
dd = self.C[2] ^ self.rotate(self.C[4], 1)
self.C[1] = (A[5] ^ A[6]) ^ (A[7] ^ A[8]) ^ A[9]
da = self.C[4] ^ self.rotate(self.C[1], 1)
self.C[3] = (A[15] ^ A[16]) ^ (A[17] ^ A[18]) ^ A[19]
dc = self.C[1] ^ self.rotate(self.C[3], 1)
de = self.C[3] ^ self.rotate(self.C[0], 1)
# ρ and π steps, with last part of θ
self.B[0] = self.rotate(A[0] ^ da, 0)
self.B[1] = self.rotate(A[15] ^ dd, 28)
self.B[2] = self.rotate(A[5] ^ db, 1)
self.B[3] = self.rotate(A[20] ^ de, 27)
self.B[4] = self.rotate(A[10] ^ dc, 62)
self.B[5] = self.rotate(A[6] ^ db, 44)
self.B[6] = self.rotate(A[21] ^ de, 20)
self.B[7] = self.rotate(A[11] ^ dc, 6)
self.B[8] = self.rotate(A[1] ^ da, 36)
self.B[9] = self.rotate(A[16] ^ dd, 55)
self.B[10] = self.rotate(A[12] ^ dc, 43)
self.B[11] = self.rotate(A[2] ^ da, 3)
self.B[12] = self.rotate(A[17] ^ dd, 25)
self.B[13] = self.rotate(A[7] ^ db, 10)
self.B[14] = self.rotate(A[22] ^ de, 39)
self.B[15] = self.rotate(A[18] ^ dd, 21)
self.B[16] = self.rotate(A[8] ^ db, 45)
self.B[17] = self.rotate(A[23] ^ de, 8)
self.B[18] = self.rotate(A[13] ^ dc, 15)
self.B[19] = self.rotate(A[3] ^ da, 41)
self.B[20] = self.rotate(A[24] ^ de, 14)
self.B[21] = self.rotate(A[14] ^ dc, 61)
self.B[22] = self.rotate(A[4] ^ da, 18)
self.B[23] = self.rotate(A[19] ^ dd, 56)
self.B[24] = self.rotate(A[9] ^ db, 2)
# ξ step
A[0] = self.B[0] ^ ((~(self.B[5])) & self.B[10])
A[1] = self.B[1] ^ ((~(self.B[6])) & self.B[11])
A[2] = self.B[2] ^ ((~(self.B[7])) & self.B[12])
A[3] = self.B[3] ^ ((~(self.B[8])) & self.B[13])
A[4] = self.B[4] ^ ((~(self.B[9])) & self.B[14])
A[5] = self.B[5] ^ ((~(self.B[10])) & self.B[15])
A[6] = self.B[6] ^ ((~(self.B[11])) & self.B[16])
A[7] = self.B[7] ^ ((~(self.B[12])) & self.B[17])
A[8] = self.B[8] ^ ((~(self.B[13])) & self.B[18])
A[9] = self.B[9] ^ ((~(self.B[14])) & self.B[19])
A[10] = self.B[10] ^ ((~(self.B[15])) & self.B[20])
A[11] = self.B[11] ^ ((~(self.B[16])) & self.B[21])
A[12] = self.B[12] ^ ((~(self.B[17])) & self.B[22])
A[13] = self.B[13] ^ ((~(self.B[18])) & self.B[23])
A[14] = self.B[14] ^ ((~(self.B[19])) & self.B[24])
A[15] = self.B[15] ^ ((~(self.B[20])) & self.B[0])
A[16] = self.B[16] ^ ((~(self.B[21])) & self.B[1])
A[17] = self.B[17] ^ ((~(self.B[22])) & self.B[2])
A[18] = self.B[18] ^ ((~(self.B[23])) & self.B[3])
A[19] = self.B[19] ^ ((~(self.B[24])) & self.B[4])
A[20] = self.B[20] ^ ((~(self.B[0])) & self.B[5])
A[21] = self.B[21] ^ ((~(self.B[1])) & self.B[6])
A[22] = self.B[22] ^ ((~(self.B[2])) & self.B[7])
A[23] = self.B[23] ^ ((~(self.B[3])) & self.B[8])
A[24] = self.B[24] ^ ((~(self.B[4])) & self.B[9])
# ι step
A[0] ^= rc
def keccakF(self, A):
'''
Perform Keccak-f function
@param A:list<int> The current state
'''
if (self.nr == 24):
self.keccakFRound(A, 0x0000000000000001)
self.keccakFRound(A, 0x0000000000008082)
self.keccakFRound(A, 0x800000000000808A)
self.keccakFRound(A, 0x8000000080008000)
self.keccakFRound(A, 0x000000000000808B)
self.keccakFRound(A, 0x0000000080000001)
self.keccakFRound(A, 0x8000000080008081)
self.keccakFRound(A, 0x8000000000008009)
self.keccakFRound(A, 0x000000000000008A)
self.keccakFRound(A, 0x0000000000000088)
self.keccakFRound(A, 0x0000000080008009)
self.keccakFRound(A, 0x000000008000000A)
self.keccakFRound(A, 0x000000008000808B)
self.keccakFRound(A, 0x800000000000008B)
self.keccakFRound(A, 0x8000000000008089)
self.keccakFRound(A, 0x8000000000008003)
self.keccakFRound(A, 0x8000000000008002)
self.keccakFRound(A, 0x8000000000000080)
self.keccakFRound(A, 0x000000000000800A)
self.keccakFRound(A, 0x800000008000000A)
self.keccakFRound(A, 0x8000000080008081)
self.keccakFRound(A, 0x8000000000008080)
self.keccakFRound(A, 0x0000000080000001)
self.keccakFRound(A, 0x8000000080008008)
else:
for i in range(self.nr):
self.keccakFRound(A, self.RC[i] & self.wmod)
def toLane(self, message, rr, ww, off):
'''
Convert a chunk of char:s to a word
@param message:bytes The message
@param rr:int Bitrate in bytes
@param ww:int Word size in bytes
@param off:int The offset in the message
@return :int Lane
'''
rc = 0
i = off + ww - 1
n = min(len(message), rr)
while i >= off:
rc = (rc << 8) | (message[i] if (i < n) else 0)
i -= 1
return rc
def toLane64(self, message, rr, off):
'''
Convert a chunk of char:s to a 64-bit word
@param message:bytes The message
@param rr:int Bitrate in bytes
@param off:int The offset in the message
@return :int Lane
'''
n = min(len(message), rr)
return ((message[off + 7] << 56) if (off + 7 < n) else 0) | \
((message[off + 6] << 48) if (off + 6 < n) else 0) | \
((message[off + 5] << 40) if (off + 5 < n) else 0) | \
((message[off + 4] << 32) if (off + 4 < n) else 0) | \
((message[off + 3] << 24) if (off + 3 < n) else 0) | \
((message[off + 2] << 16) if (off + 2 < n) else 0) | \
((message[off + 1] << 8) if (off + 1 < n) else 0) | \
((message[off]) if (off < n) else 0)
def pad10star1(self, msg, r):
'''
pad 10*1
@param msg:bytes The message to pad
@param r:int The bitrate
@return :str The message padded
'''
nnn = len(msg) << 3
nrf = nnn >> 3
nbrf = nnn & 7
ll = nnn % r
bbbb = 1 if nbrf == 0 else ((msg[nrf] >> (8 - nbrf)) | (1 << nbrf))
message = None
if ((r - 8 <= ll) and (ll <= r - 2)):
message = [bbbb ^ 128]
else:
nnn = (nrf + 1) << 3
nnn = ((nnn - (nnn % r) + (r - 8)) >> 3) + 1
message = [0] * (nnn - nrf)
message[0] = bbbb
nnn -= nrf
message[nnn - 1] = 0x80
return msg[:nrf] + message
def initialise(self, r, c, n):
'''
Initialise Keccak sponge
@param r:int The bitrate
@param c:int The capacity
@param n:int The output size
'''
self.r = r
self.c = c
self.n = n
self.b = r + c
self.w = self.b // 25
self.l = self.lb(self.w)
self.nr = 12 + (self.l << 1)
self.wmod = (1 << self.w) - 1
self.S = [0] * 25
self.M = []
def update(self, msg, msglen = None):
'''
Absorb the more of the message message to the Keccak sponge
@param msg:bytes The partial message
@param msglen:int The length of the partial message
'''
if msglen is not None:
msg = msg[:msglen]
rr = self.r >> 3
ww = self.w >> 3
self.M += msg
nnn = len(self.M)
nnn -= nnn % ((self.r * self.b) >> 3)
message = self.M[:nnn]
self.M = self.M[nnn:]
# Absorbing phase
if ww == 8:
for i in range(0, nnn, rr):
self.S[ 0] ^= self.toLane64(message, rr, 0)
self.S[ 5] ^= self.toLane64(message, rr, 8)
self.S[10] ^= self.toLane64(message, rr, 16)
self.S[15] ^= self.toLane64(message, rr, 24)
self.S[20] ^= self.toLane64(message, rr, 32)
self.S[ 1] ^= self.toLane64(message, rr, 40)
self.S[ 6] ^= self.toLane64(message, rr, 48)
self.S[11] ^= self.toLane64(message, rr, 56)
self.S[16] ^= self.toLane64(message, rr, 64)
self.S[21] ^= self.toLane64(message, rr, 72)
self.S[ 2] ^= self.toLane64(message, rr, 80)
self.S[ 7] ^= self.toLane64(message, rr, 88)
self.S[12] ^= self.toLane64(message, rr, 96)
self.S[17] ^= self.toLane64(message, rr, 104)
self.S[22] ^= self.toLane64(message, rr, 112)
self.S[ 3] ^= self.toLane64(message, rr, 120)
self.S[ 8] ^= self.toLane64(message, rr, 128)
self.S[13] ^= self.toLane64(message, rr, 136)
self.S[18] ^= self.toLane64(message, rr, 144)
self.S[23] ^= self.toLane64(message, rr, 152)
self.S[ 4] ^= self.toLane64(message, rr, 160)
self.S[ 9] ^= self.toLane64(message, rr, 168)
self.S[14] ^= self.toLane64(message, rr, 176)
self.S[19] ^= self.toLane64(message, rr, 184)
self.S[24] ^= self.toLane64(message, rr, 192)
self.keccakF(self.S)
message = message[rr:]
else:
for i in range(0, nnn, rr):
self.S[ 0] ^= self.toLane(message, rr, ww, 0)
self.S[ 5] ^= self.toLane(message, rr, ww, ww)
self.S[10] ^= self.toLane(message, rr, ww, 2 * ww)
self.S[15] ^= self.toLane(message, rr, ww, 3 * ww)
self.S[20] ^= self.toLane(message, rr, ww, 4 * ww)
self.S[ 1] ^= self.toLane(message, rr, ww, 5 * ww)
self.S[ 6] ^= self.toLane(message, rr, ww, 6 * ww)
self.S[11] ^= self.toLane(message, rr, ww, 7 * ww)
self.S[16] ^= self.toLane(message, rr, ww, 8 * ww)
self.S[21] ^= self.toLane(message, rr, ww, 9 * ww)
self.S[ 2] ^= self.toLane(message, rr, ww, 10 * ww)
self.S[ 7] ^= self.toLane(message, rr, ww, 11 * ww)
self.S[12] ^= self.toLane(message, rr, ww, 12 * ww)
self.S[17] ^= self.toLane(message, rr, ww, 13 * ww)
self.S[22] ^= self.toLane(message, rr, ww, 14 * ww)
self.S[ 3] ^= self.toLane(message, rr, ww, 15 * ww)
self.S[ 8] ^= self.toLane(message, rr, ww, 16 * ww)
self.S[13] ^= self.toLane(message, rr, ww, 17 * ww)
self.S[18] ^= self.toLane(message, rr, ww, 18 * ww)
self.S[23] ^= self.toLane(message, rr, ww, 19 * ww)
self.S[ 4] ^= self.toLane(message, rr, ww, 20 * ww)
self.S[ 9] ^= self.toLane(message, rr, ww, 21 * ww)
self.S[14] ^= self.toLane(message, rr, ww, 22 * ww)
self.S[19] ^= self.toLane(message, rr, ww, 23 * ww)
self.S[24] ^= self.toLane(message, rr, ww, 24 * ww)
message = message[rr:]
self.keccakF(self.S)
def digest(self, msg = None, msglen = None, withReturn = None):
'''
Absorb the last part of the message and squeeze the Keccak sponge
@param msg:bytes? The rest of the message
@param msglen:int The length of the partial message
@param withReturn:bool Whether to return the hash instead of just do a quick squeeze phrase and return `None`
@return :bytes? The hash sum, or `None` if `withReturn` is `False`
'''
if (msg is not None) and isinstance(msg, bool):
(msg, withReturn) = (withReturn, msg)
elif (msglen is not None) and isinstance(msglen, bool):
(msglen, withReturn) = (withReturn, msglen)
if msg is None:
msg = []
elif msglen is not None:
msg = msg[:msglen]
message = self.pad10star1(self.M + msg, self.r)
self.M = None
nnn = len(message)
rr = self.r >> 3
nn = (self.n + 7) >> 3
ww = self.w >> 3
# Absorbing phase
if ww == 8:
for i in range(0, nnn, rr):
self.S[ 0] ^= self.toLane64(message, rr, 0)
self.S[ 5] ^= self.toLane64(message, rr, 8)
self.S[10] ^= self.toLane64(message, rr, 16)
self.S[15] ^= self.toLane64(message, rr, 24)
self.S[20] ^= self.toLane64(message, rr, 32)
self.S[ 1] ^= self.toLane64(message, rr, 40)
self.S[ 6] ^= self.toLane64(message, rr, 48)
self.S[11] ^= self.toLane64(message, rr, 56)
self.S[16] ^= self.toLane64(message, rr, 64)
self.S[21] ^= self.toLane64(message, rr, 72)
self.S[ 2] ^= self.toLane64(message, rr, 80)
self.S[ 7] ^= self.toLane64(message, rr, 88)
self.S[12] ^= self.toLane64(message, rr, 96)
self.S[17] ^= self.toLane64(message, rr, 104)
self.S[22] ^= self.toLane64(message, rr, 112)
self.S[ 3] ^= self.toLane64(message, rr, 120)
self.S[ 8] ^= self.toLane64(message, rr, 128)
self.S[13] ^= self.toLane64(message, rr, 136)
self.S[18] ^= self.toLane64(message, rr, 144)
self.S[23] ^= self.toLane64(message, rr, 152)
self.S[ 4] ^= self.toLane64(message, rr, 160)
self.S[ 9] ^= self.toLane64(message, rr, 168)
self.S[14] ^= self.toLane64(message, rr, 176)
self.S[19] ^= self.toLane64(message, rr, 184)
self.S[24] ^= self.toLane64(message, rr, 192)
self.keccakF(self.S)
message = message[rr:]
else:
for i in range(0, nnn, rr):
self.S[ 0] ^= self.toLane(message, rr, ww, 0)
self.S[ 5] ^= self.toLane(message, rr, ww, ww)
self.S[10] ^= self.toLane(message, rr, ww, 2 * ww)
self.S[15] ^= self.toLane(message, rr, ww, 3 * ww)
self.S[20] ^= self.toLane(message, rr, ww, 4 * ww)
self.S[ 1] ^= self.toLane(message, rr, ww, 5 * ww)
self.S[ 6] ^= self.toLane(message, rr, ww, 6 * ww)
self.S[11] ^= self.toLane(message, rr, ww, 7 * ww)
self.S[16] ^= self.toLane(message, rr, ww, 8 * ww)
self.S[21] ^= self.toLane(message, rr, ww, 9 * ww)
self.S[ 2] ^= self.toLane(message, rr, ww, 10 * ww)
self.S[ 7] ^= self.toLane(message, rr, ww, 11 * ww)
self.S[12] ^= self.toLane(message, rr, ww, 12 * ww)
self.S[17] ^= self.toLane(message, rr, ww, 13 * ww)
self.S[22] ^= self.toLane(message, rr, ww, 14 * ww)
self.S[ 3] ^= self.toLane(message, rr, ww, 15 * ww)
self.S[ 8] ^= self.toLane(message, rr, ww, 16 * ww)
self.S[13] ^= self.toLane(message, rr, ww, 17 * ww)
self.S[18] ^= self.toLane(message, rr, ww, 18 * ww)
self.S[23] ^= self.toLane(message, rr, ww, 19 * ww)
self.S[ 4] ^= self.toLane(message, rr, ww, 20 * ww)
self.S[ 9] ^= self.toLane(message, rr, ww, 21 * ww)
self.S[14] ^= self.toLane(message, rr, ww, 22 * ww)
self.S[19] ^= self.toLane(message, rr, ww, 23 * ww)
self.S[24] ^= self.toLane(message, rr, ww, 24 * ww)
message = message[rr:]
self.keccakF(self.S)
# Squeezing phase
if withReturn:
rc = [0] * ((self.n + 7) >> 3)
ptr = 0
olen = self.n
j = 0
ni = min(25, rr)
while olen > 0:
i = 0
while (i < ni) and (j < nn):
v = self.S[(i % 5) * 5 + i // 5]
for _ in range(ww):
if j < nn:
rc[ptr] = v & 255
ptr += 1
v >>= 8
j += 1
i += 1
olen -= self.r
if olen > 0:
self.keccakF(self.S)
if (self.n & 7) != 0:
rc[len(rc) - 1] &= (1 << (self.n & 7)) - 1
return rc
olen = self.n
while olen > self.r:
olen -= self.r
self.keccakF(self.S)
return None
def simpleSqueeze(self, times = 1):
'''
Force some rounds of Keccak-f
@param times:int The number of rounds
'''
for i in range(times):
self.keccakF(self.S)
def fastSqueeze(self, times = 1):
'''
Squeeze as much as is needed to get a digest a number of times
@param times:int The number of digests
'''
for i in range(times):
self.keccakF(self.S) # Last squeeze did not do a ending squeeze
olen = self.n
while olen > self.r:
olen -= self.r
self.keccakF(self.S)
def squeeze(self):
'''
Squeeze out another digest
@return :bytes The hash sum
'''
self.keccakF(self.S) # Last squeeze did not do a ending squeeze
nn = (self.n + 7) >> 3
ww = self.w >> 3
rc = [0] * nn
olen = self.n
j = 0
ptr = 0
ni = min(25, self.r >> 3)
while olen > 0:
i = 0
while (i < ni) and (j < nn):
v = self.S[(i % 5) * 5 + i // 5]
for _ in range(ww):
if j < nn:
rc[ptr] = v
ptr += 1
v >>= 8
j += 1
i += 1
olen -= self.r
if olen > 0:
self.keccakF(self.S)
if (self.n & 7) != 0:
rc[len(rc) - 1] &= (1 << (self.n & 7)) - 1
return rc
|