-
Notifications
You must be signed in to change notification settings - Fork 8
/
bb98_graphic.sage
210 lines (190 loc) · 6.04 KB
/
bb98_graphic.sage
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
# setup
def generate_keypair(size_N_in_bits):
size_prime = 1 << (size_N_in_bits / 2)
while True:
p = random_prime(size_prime)
q = random_prime(size_prime)
N = p * q
phi = (p-1)*(q-1)
e = 17
if gcd(e, phi) != 1:
continue
d = inverse_mod(e, phi) # will sometimes not work, generate another setup?
break
return e, d, N
# helper
def get_byte_length(message):
res = 0
if (len(bin(message)) - 2) % 8 != 0:
res += 1
res += (len(bin(message)) - 2) // 8
return res
# pad plaintext [00, 02, randoms, 00, messsage] of len target_length
def padding(message, target_length):
# 02
res = 0x02 << 8 * (target_length - 2)
# random
random_pad = os.urandom(target_length - 3 - get_byte_length(message))
for idx, val in enumerate(random_pad):
res += ord(val) << (len(random_pad) - idx + get_byte_length(message)) * 8
# 00
# message
res += message
return res
# a length oracle
def oracle_length(c, d, N):
p = power_mod(c, d, N)
return get_byte_length(p)
# a padding oracle
def oracle_padding(c, d, N):
p = power_mod(c, d, N)
if get_byte_length(p) != get_byte_length(N) - 1:
return False
if p >> ((get_byte_length(p) -1)) * 8 != 0x02: # this is not correct
return False
return True
import sys
def print_meta(text):
sys.stdout.write("=============== ")
sys.stdout.write(text)
sys.stdout.write(" ================")
sys.stdout.write('\n')
sys.stdout.flush()
def print_line(min, max, m, ranges):
# print min, max, m, ranges
# init
to_draw = {}
# - min -> 0
# - max -> 100
dist = max - min
# calculate where ranges are
l = ord('a')
for r in ranges:
down = round((r[0]-min)*100/dist)
up = round((r[1]-min)*100/dist)
for i in range(down, up+1):
if i == down:
to_draw[i] = "["
elif i == up:
to_draw[i] = "]"
else:
to_draw[i] = chr(l)
l += 1
# m
pos_m = round((m-min)*100/dist)
to_draw[pos_m-1] = "["
to_draw[pos_m] = "m"
to_draw[pos_m+1] = "]"
# print
for i in range(100):
if i in to_draw:
sys.stdout.write(to_draw[i])
else:
sys.stdout.write('-')
sys.stdout.write('\n')
sys.stdout.flush()
def bleichenbacher_padding():
# time
import time
start_time = time.time()
# setup
e, d, N = generate_keypair(512)
N_size = get_byte_length(N)
plaintext = 0x6c6f6c # "lol"
padded = padding(plaintext, N_size)
print "to find:", padded
ciphertext = power_mod(padded, e, N)
# setup attack
N_bit_length = (get_byte_length(N) - 2) * 8
B = 1 << N_bit_length
# debug
print_meta("start")
min_n = 0
max_n = N
print_line(min_n, max_n, padded, [])
# attack
previous_steps = [(2*B, 3*B-1)]
mult = ceil(N / (3 * B)) - 1
i = 1
number_msg = 0
while True:
# debug
print_meta("message #" + str(i))
print_meta("zooming in")
min_n = previous_steps[0][0]
max_n = previous_steps[0][1]
for r in previous_steps: # min_n, max_n based on previous step
if r[0] < min_n:
min_n = r[0]
if r[1] > max_n:
max_n = r[1]
print_line(min_n, max_n, padded, previous_steps)
# find a valid padding
c2 = 0
if i > 1 and len(previous_steps) == 1:
previous_mult = mult
ri = floor(2 * (previous_steps[0][1]*previous_mult - 2 * B) / N)
found = False
while True:
mult = ceil((2*B+ri*N) / previous_steps[0][1]) - 1
mult_max = ceil((3*B+ri*N)/previous_steps[0][0])
while mult < mult_max:
mult += 1
c2 = (ciphertext * power_mod(mult, e, N)) % N
number_msg += 1
if oracle_padding(c2, d, N):
found = True
break
if found:
break
ri += 1
else:
while not oracle_padding(c2, d, N):
number_msg += 1
mult += 1
c2 = (ciphertext * power_mod(mult, e, N)) % N
# debug
# print "found a valid padding", c2
# raw_input("press a key to enter next step...\n")
# compute the new set of intervals
new_interval = []
for interval in previous_steps:
min_range = (interval[0] * mult - 3 * B + 1) // N
max_range = (interval[1] * mult - 2 * B) // N
# print max_range + 1 - min_range, "possible r's"
# print interval[0]
# print interval[1]
possible_r = min_range
# print max_range + 1
while possible_r < max_range + 1:
new_min = max(interval[0], ceil((2*B+possible_r*N)/mult))
new_max = min(interval[1], floor((3*B-1+possible_r*N)/mult))
if new_min > interval[1] or new_max < interval[0]:
possible_r += 1
continue
# found?
if new_max == new_min:
# print "found!"
# print new_min
# print "did we find that?"
# print padded
# print "took", time.time() - start_time, "seconds"
return
# nope
new_interval.append((new_min, new_max))
# print ""
possible_r += 1
# debug
print_meta("reducing range")
print_line(min_n, max_n, padded, new_interval)
#
previous_steps = new_interval
i += 1
# debug
# print "\n"
# print len(previous_steps), "potential intervals left:"
# for interval in previous_steps:
# print " - [", interval[0], ",", interval[1], "]"
# print "\n"
if __name__ == "__main__":
bleichenbacher_padding()