forked from signalapp/poksho
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sho.py
141 lines (120 loc) · 5.28 KB
/
sho.py
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
#
# Copyright (C) 2020 Signal Messenger, LLC.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-3.0-only
#
import struct
import binascii
from hashlib import sha256
BLOCK_LEN = 64
HASH_LEN = 32
ZERO_PREFIX_SIZE = HASH_LEN
CHAIN_SIZE = HASH_LEN
# Encode integer as uint64 big-endian
def to_bytes8(x):
return struct.pack('>Q', x)
def to_bytes2(x):
return struct.pack('>H', x)
class SHO:
def __init__(self, label=None):
if label == None:
label = bytearray(0)
self.state = bytearray(ZERO_PREFIX_SIZE) + bytearray(CHAIN_SIZE)
self.absorb(to_bytes2(len(label)) + label)
self.pad_block()
def absorb(self, data) :
self.state += data
def squeeze(self, outlen):
inner_digest = sha256(self.state).digest()
output = bytearray(0)
counter = 0
while counter * CHAIN_SIZE < outlen:
counter += 1
output += sha256(inner_digest + to_bytes8(counter) + to_bytes8(0)).digest()
chain_key = sha256(inner_digest + to_bytes8(0) + to_bytes8(outlen)).digest()
self.state = bytearray(ZERO_PREFIX_SIZE) + chain_key
return output[:outlen]
# Adds 0...BLOCKLEN-1 bytes to pad to BLOCKLEN
def pad_block(self):
pad_len = (BLOCK_LEN - (len(self.state) % BLOCK_LEN)) % BLOCK_LEN
self.absorb(bytearray(pad_len))
if __name__ == "__main__":
block31 = bytearray(31)
block32 = bytearray(32)
block33 = bytearray(33)
block63 = bytearray(63)
block64 = bytearray(64)
block65 = bytearray(65)
for i in range(31):
block31[i] = i
for i in range(32):
block32[i] = i
for i in range(33):
block33[i] = i
for i in range(63):
block63[i] = i
for i in range(64):
block64[i] = i
for i in range(65):
block65[i] = i
# empty label with different input lengths
sho = SHO()
out = sho.squeeze(16)
assert(out == bytearray([0x5d, 0xd7, 0x7, 0x8, 0x6e, 0xed, 0x5b, 0xf, 0xf6, 0xaa, 0xb1, 0x5f, 0x5f, 0x6e, 0x27, 0x85]))
sho = SHO()
sho.absorb(block31)
out = sho.squeeze(32)
assert(out == bytearray([0xf3, 0x9a, 0x4e, 0x15, 0x19, 0x35, 0xbf, 0x73, 0x70, 0xfc, 0x35, 0xd3, 0xa2, 0xcc, 0x97, 0xa5, 0x11, 0x49, 0x5e, 0x65, 0x0, 0xdf, 0x70, 0x65, 0x1b, 0xf3, 0xc9, 0x6a, 0x2e, 0xaf, 0xb, 0x3]))
sho = SHO()
sho.absorb(block32)
out = sho.squeeze(48)
assert(out == bytearray([0x10, 0x70, 0xe2, 0x32, 0x1b, 0xbf, 0x56, 0xff, 0x5b, 0xef, 0xd2, 0xcb, 0x3, 0xd2, 0xd1, 0x5a, 0xd2, 0xff, 0x7, 0x11, 0xe3, 0x5d, 0xa0, 0x48, 0xd0, 0x14, 0x7f, 0x37, 0xb7, 0xa1, 0x0, 0xe8, 0x4a, 0x8c, 0xe7, 0xe1, 0x49, 0xf4, 0x9b, 0xdf, 0xbf, 0x71, 0xa8, 0xe6, 0xed, 0x49, 0xf6, 0xbe]))
sho = SHO()
sho.absorb(block33)
out = sho.squeeze(64)
assert(out == bytearray([0xa, 0xbd, 0x51, 0x9b, 0x17, 0x86, 0xf5, 0xa9, 0x4e, 0xc0, 0xc1, 0x97, 0x7b, 0xd, 0x34, 0xeb, 0xc7, 0xa4, 0x59, 0x1f, 0xf0, 0x40, 0x99, 0x89, 0xa4, 0xeb, 0xb2, 0xb7, 0xe2, 0x89, 0xf6, 0x9d, 0xc, 0x9a, 0x4d, 0xe6, 0x96, 0x15, 0x44, 0x55, 0x63, 0x49, 0x3e, 0xb8, 0xfc, 0x6b, 0x5a, 0x44, 0x6b, 0x74, 0x9f, 0x46, 0xce, 0x5, 0xed, 0x8e, 0x5f, 0xb, 0xb9, 0x88, 0x6, 0x74, 0x22, 0x52]))
# longer labels with different input lengths
sho = SHO(b"label")
out = sho.squeeze(16)
assert(out == bytearray([0xf7, 0xbc, 0xb8, 0xc0, 0xe0, 0xc, 0xa0, 0xb4, 0x65, 0xeb, 0xaf, 0x70, 0xeb, 0x4e, 0x98, 0x95]))
sho = SHO(block31)
sho.absorb(block63)
out = sho.squeeze(33)
assert(out == bytearray([0xde, 0xba, 0x7d, 0x15, 0x5a, 0x6a, 0x72, 0x1f, 0xf0, 0x4a, 0x8, 0x9a, 0x75, 0x2c, 0x74, 0x79, 0x55, 0xe7, 0x9c, 0x48, 0xc1, 0x37, 0xa9, 0x6d, 0x4d, 0xa, 0x62, 0x2c, 0x8, 0x57, 0x19, 0x7b, 0x2b]))
sho = SHO(block32)
sho.absorb(block64)
out = sho.squeeze(47)
assert(out == bytearray([0x31, 0x70, 0x44, 0x3b, 0x33, 0xfe, 0xae, 0xc3, 0xeb, 0xda, 0x58, 0xd4, 0xa0, 0x8c, 0xf8, 0x91, 0xee, 0x6c, 0x56, 0xf9, 0x13, 0xbe, 0xe1, 0x7b, 0xd1, 0x9e, 0xbc, 0xb0, 0x78, 0xa3, 0xf5, 0x33, 0x29, 0xf3, 0xb6, 0x32, 0xdf, 0x40, 0x53, 0x95, 0x5, 0x32, 0xe5, 0xeb, 0x5a, 0x66, 0xd3]))
sho = SHO(block33)
sho.absorb(block65)
out = sho.squeeze(80)
assert(out == bytearray([0x96, 0xe5, 0xf6, 0x1f, 0x94, 0x30, 0x1c, 0x34, 0x1, 0xfd, 0xe6, 0xdc, 0x26, 0x4b, 0x58, 0x59, 0x7f, 0x17, 0xdb, 0xe7, 0x4, 0x9f, 0x70, 0x4f, 0xf9, 0x1b, 0xb6, 0x2, 0xf6, 0xdc, 0xa3, 0x4b, 0x94, 0xfa, 0xf8, 0x2d, 0xed, 0xf5, 0x83, 0xf0, 0x1a, 0x28, 0x94, 0xe2, 0x62, 0xf2, 0x1d, 0x3c, 0x5d, 0x40, 0xab, 0x6, 0x7, 0xb2, 0x7f, 0x30, 0x1f, 0xab, 0x93, 0x6d, 0x57, 0xc, 0x94, 0x6e, 0xf7, 0x7f, 0xf9, 0xd7, 0xf7, 0x7f, 0x95, 0x44, 0x14, 0x0, 0x1d, 0x1a, 0x4b, 0xfe, 0x19, 0xe3]))
sho = SHO(block64)
sho.pad_block()
sho.squeeze(64)
sho.squeeze(17)
sho.absorb(block31)
sho.pad_block()
sho.absorb(block32)
sho.squeeze(128)
sho.absorb(block33)
sho.squeeze(32)
sho.absorb(block31)
sho.absorb(block31)
sho.pad_block()
sho.pad_block()
sho.absorb(block32)
sho.absorb(block32)
sho.squeeze(1)
sho.absorb(block63)
sho.squeeze(33)
sho.squeeze(31)
sho.absorb(block64)
sho.squeeze(1025)
sho.absorb(block65)
sho.squeeze(63)
sho.absorb(bytearray(1))
out = sho.squeeze(15)
assert(out == bytearray([0x61, 0x6d, 0x7b, 0x38, 0xcf, 0xcf, 0xf8, 0x72, 0x70, 0x9d, 0x7f, 0xf5, 0x90, 0x9b, 0xde]))
print "OK"