-
Notifications
You must be signed in to change notification settings - Fork 25
/
generate_input.py
executable file
·167 lines (125 loc) · 3 KB
/
generate_input.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
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
#! /usr/bin/env python3
import json
import secrets
from secrets import randbits, randbelow
import sys
# CAUTION: `random` isn't cryptographically secure. In general, prefer to use
# `secrets`. But `secrets` currently lacks a shuffle() function, so we just
# import that specific function from `random`.
from random import shuffle
import hashlib
animals = [
"aardvark",
"butterfly",
"cat",
"dog",
"elephant",
"fox",
"giraffe",
"hippopotamus",
"iguana",
"jaguar",
"kangaroo",
"llama",
"manatee",
"narwhal",
"octopus",
"pig",
"quail",
"rhinoceros",
"sheep",
"turkey",
"unicorn",
"vulture",
"wombat",
"xenoceratops",
"yak",
"zebra",
]
def random_animals(n):
return " ".join(secrets.choice(animals) for _ in range(n))
def random_string(n):
return random_animals(n)[:n]
inputs = {}
### Building Blocks
# Problem 1
inputs["problem1"] = [
[1, 2],
[(2**32) - 1, 1],
# Something random that will overflow.
[(2**31) + randbits(31), (2**31) + randbits(31)],
]
# Problem 2
inputs["problem2"] = [
[2, 1],
[1, 1],
# Avoid generating pathological rotations (0 or 32). Bitshifting by those
# amounts is weird in C/C++/Rust.
[randbits(32), randbelow(31) + 1],
]
### The Message Schedule
# Problem 3
inputs["problem3"] = randbits(32)
# Problem 4
inputs["problem4"] = randbits(32)
# Problem 5
inputs["problem5"] = random_string(64)
### The Round Function
# Problem 6
inputs["problem6"] = randbits(32)
# Problem 7
inputs["problem7"] = randbits(32)
# Problem 8
inputs["problem8"] = [randbits(32), randbits(32), randbits(32)]
# Problem 9
inputs["problem9"] = [randbits(32), randbits(32), randbits(32)]
# Problem 10
state = [randbits(32) for _ in range(8)]
inputs["problem10"] = {
"state": [randbits(32) for _ in range(8)],
"round_constant": 0x428A2F98,
"schedule_word": randbits(32),
}
### The Compression Function
# Problem 11
inputs["problem11"] = {
"state": [randbits(32) for _ in range(8)],
"block": random_string(64),
}
### Padding
# Problem 12
inputs["problem12"] = [
0,
1,
55,
56,
64,
secrets.randbits(61), # not 64, because of the x8 step
]
### The Hash Function
# Problem 13
lyrics = "John Jacob Jingleheimer Schmidt! His name is my name too. Whenever we go out the people always shout there goes John Jacob Jingleheimer Schmidt! Nanananananana..."
inputs["problem13"] = [
"",
"hello world",
random_string(55),
random_string(56),
lyrics,
]
### The Length Extension Attack
# Problem 14
inputs["problem14"] = {
"original_input": random_animals(3),
"chosen_suffix": random_animals(3),
}
# Problem 15
inputs["problem15"] = secrets.token_bytes(32).hex()
# Problem 16
hidden_input = random_animals(10)
inputs["problem16"] = {
"original_hash": hashlib.sha256(hidden_input.encode()).hexdigest(),
"original_len": len(hidden_input),
"chosen_suffix": random_animals(5),
}
json.dump(inputs, sys.stdout, indent=" ")
print()