-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem64.py
559 lines (437 loc) · 16.7 KB
/
problem64.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
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
from functools import lru_cache
from math import log2
import galois
from galois import GF2
import numpy as np
import os
import pickle
import random
import string
from typing import Callable, List, TypeVar
from problem63 import (
GCM_MODULUS,
FieldElement,
aes_encrypt,
element_add,
element_exp,
element_mult,
gcm_decrypt,
gcm_encrypt,
gcm_mac,
gcm_mac_compute_g,
get_nth_block,
int_from_bytes,
)
# Length = 128
Scalar = int
SV = TypeVar('SV', bound=Scalar)
ScalarFunc = Callable[[SV, SV], SV]
# Length of both of these = 128
Vec = List[Scalar]
Matrix = List[Vec]
MatrixSize = 128
ir_poly = [0] * 129
ir_poly[128] = ir_poly[127] = ir_poly[126] = ir_poly[121] = ir_poly[0] = 1
field = galois.GF(2**128, galois.Poly(ir_poly, field=GF2))
def test_field_and_element_mult_consistency():
for _ in range(200):
x, y = field.Random(), field.Random()
x_, y_ = int(x), int(y)
assert element_mult(x_, y_, GCM_MODULUS) == x * y
def matrix_null_space(a: galois.FieldArray):
# https://math.stackexchange.com/a/1612735/585559
rows, cols = a.shape
aug_array = a.Zeros((cols, rows + cols))
aug_array[:, 0:rows] = a.transpose()
aug_array[:, rows:] = a.Identity(cols)
reduced = aug_array.row_reduce(ncols=rows)
zero_rows, = np.where(~reduced[0:cols, 0:rows].any(axis=1))
results = []
for row in zero_rows:
results.append(reduced[row, rows:])
return results
def test_matrix_null_space():
GF13 = galois.GF(13)
knuth_example = GF13([
[0, 0, 0, 0, 0, 0, 0, 0],
[2, 0, 7, 11, 10, 12, 5, 11],
[3, 6, 3, 3, 0, 4, 7, 2],
[4, 3, 6, 4, 1, 6, 2, 3],
[2, 11, 8, 8, 2, 1, 3, 11],
[6, 11, 8, 6, 2, 6, 10, 9],
[5, 11, 7, 10, 0, 11, 6, 12],
[3, 3, 12, 5, 0, 11, 9, 11],
])
results = matrix_null_space(knuth_example.copy())
assert len(results) == 3
for result in results:
assert np.matmul(knuth_example, result).all() == GF13.Zeros((8)).all()
linearly_independent = GF13([
[1, 0, 5],
[0, 1, 2],
[0, 0, 1],
])
results = matrix_null_space(linearly_independent)
assert len(results) == 0
GF6199 = galois.GF(6199)
so_example = GF6199([
[1, 0, 2, 6199-3],
[0, 1, 6199-1, 2],
[0, 0, 0, 0],
])
results = matrix_null_space(so_example)
assert len(results) == 2
for result in results:
assert np.matmul(so_example, result).all() == GF6199.Zeros((4)).all()
def element_to_gf2_element(c: FieldElement) -> Vec:
v = [0] * 128
i = 0
while c > 0:
if c % 2 == 1:
v[i] = 1
c = c // 2
i += 1
return field.Vector(list(reversed(v)))
def vec_trunc(v: Vec):
last_non_zero = None
for i in range(0, 128):
if v[i] != 0:
last_non_zero = i + 1
return v[0:last_non_zero]
def test_element_to_gf2_element():
assert element_to_gf2_element(9) == field(9)
def matrix_transpose(a: Matrix) -> Matrix:
rows = len(a)
cols = len(a[0])
b: Matrix = [[]] * cols
for i in range(0, rows):
b[i] = [0] * rows
for i in range(0, len(a)):
for j in range(0, len(a[i])):
b[j][i] = a[i][j]
return b
@lru_cache
def get_basis_elems() -> galois.FieldArray:
elems: List[galois.FieldArray] = []
a = field.primitive_element
x = 1
for _ in range(0, 128):
elems.append(x)
x = x * a
return field(elems)
def gf2_scalar_matrix(c: FieldElement) -> galois.FieldArray:
c_elem = field(c)
# It ends up being much faster to create this trasnform in numpy and then
# bless it into GF2 afterwards
transform = np.zeros((128, 128), dtype=int)
basis_els = c_elem * get_basis_elems()
for i in range(0, 128):
result = int(basis_els[127 - i])
mask = 1
j = 0
for j in range(0, 128):
if result & mask:
transform[127 - j][i] = 1
mask <<= 1
return GF2(transform)
def vec_to_matrix(v: Vec) -> Matrix:
return [[x] for x in v]
def matrix_to_vec(m: Matrix) -> Vec:
v = [0] * len(m)
for i in range(0, len(m)):
assert len(m[i]) == 1, \
'Assertion error: matrix_to_vec run on have multi-column matrix'
v[i] = m[i][0]
return v
def vec_to_element(v: Vec) -> FieldElement:
e = 0
for i in range(0, 128):
if v[i] == 1:
e += 2**i
return e
def test_scalar_multiplication_vector():
# Let's experiment with finding a matrix
# Using test from problem63:
# (x^2 + x + 1) * (x + 1) == x^3 + 1
x_plus_one_matrix = gf2_scalar_matrix(3)
e2 = field(7)
result = np.matmul(x_plus_one_matrix, e2.vector())
assert field.Vector(result) == field(9)
e3 = field(3)
result = np.matmul(x_plus_one_matrix, e3.vector())
assert field.Vector(result) == field(5)
for _ in range(10):
x, y = field.Random(), field.Random()
result = np.matmul(gf2_scalar_matrix(x), y.vector())
assert field.Vector(result) == x * y
def gf2_square_matrix() -> GF2:
rows: List[np.ndarray] = []
a = field.primitive_element
for i in range(0, 128):
basis_elem = a ** i
rows.insert(0, (basis_elem * basis_elem).vector())
return GF2(np.vstack(rows).transpose())
def test_squaring_as_matrix():
sq_m = gf2_square_matrix()
# (a + 1)^2 = a^2 + 1
result = np.matmul(sq_m, field(3).vector())
assert field.Vector(result) == field(5)
# (a^2 + a + 1)^2 = a^4 + a^2 + 1
result = np.matmul(sq_m, field(7).vector())
assert field.Vector(result) == field(21)
# (a^5 + a + 1)^2 = a^10 + a^2 + 1
result = np.matmul(sq_m, field(35).vector())
assert field.Vector(result) == field(1029)
def generate_plaintext(num_blocks):
block_size = 128 // 8
plaintext = ''
for _ in range(num_blocks):
block = ''.join(random.choice(string.ascii_letters)
for _ in range(block_size))
assert len(block.encode()) == block_size
plaintext += block
return plaintext
@lru_cache
def get_matrix_pows(n):
matrix_pows = [[[None]]] * (n + 1)
sq_matrix = m = gf2_square_matrix() # Msq (y) = y * y
matrix_pows[0] = GF2.Identity(128)
for i in range(1, n + 1):
matrix_pows[i] = m
m = np.matmul(sq_matrix, m)
return matrix_pows
def test_matrix_pows():
matrix_pows = get_matrix_pows(17)
for _ in range(0, 10):
x = field.Random()
x_vec = x.vector()
for i in range(0, len(matrix_pows)):
matrix_result = field.Vector(np.matmul(matrix_pows[i], x_vec))
assert matrix_result == (x ** (2 ** i))
@lru_cache
def get_gf2_scalar_matrices():
filename = './problem64_gf2_scalar_matrices.p'
if os.path.exists(filename):
data = pickle.load(open(filename, 'rb'))
return data['gf2_scalar_matrices']
matrices = []
basis_elems = get_basis_elems()
for i in range(0, 128):
matrices.insert(0, gf2_scalar_matrix(basis_elems[i]))
pickle.dump({'gf2_scalar_matrices': matrices}, open(filename, 'wb'))
return matrices
@lru_cache(maxsize=(128 * (2 ** 17)))
def calculate_ad(n: int,
block_idx: int,
bit_flip_position: int,
) -> galois.FieldArray:
matrix_pows = get_matrix_pows(n)
scalar_matrices = get_gf2_scalar_matrices()
ad_matrix = GF2.Zeros((128, 128))
for i in range(1, n + 1):
if i != block_idx:
continue
ad_factor = np.matmul(scalar_matrices[bit_flip_position],
matrix_pows[i])
ad_matrix += ad_factor
return ad_matrix
def calculate_ad_from_flip_vector(n, flip_vector) -> galois.FieldArray:
matrix_pows = get_matrix_pows(n)
ad_matrix = GF2.Zeros((128, 128))
for i in range(1, n + 1):
start = (i - 1) * 128
flip_int = field.Vector(flip_vector[start: start + 128])
ad_factor = np.matmul(gf2_scalar_matrix(flip_int),
matrix_pows[i])
ad_matrix += ad_factor
return ad_matrix
def test_squaring_as_matrix_with_precomputed_powers():
n = 17
matrix_pows = get_matrix_pows(n)
e1 = field(3)
expected = field(3)
for i in range(1, 17):
result = np.matmul(matrix_pows[i], e1.vector())
expected = expected * expected
assert field.Vector(result) == expected
def apply_bitflips(ciphertext, flip_vector):
forged_text = bytearray(ciphertext)
for i, should_flip in enumerate(flip_vector):
if should_flip:
block_idx, bit_pos = divmod(i, 128)
modify_block_num = 2 ** (block_idx + 1)
byte_pos, bit_shift_num = divmod(bit_pos, 8)
bytes_per_block = 128 // 8
start = (modify_block_num - 1) * bytes_per_block
forged_text[start + byte_pos] ^= (1 << (7 - bit_shift_num))
return bytes(forged_text)
def reverse_blocks(ciphertext: bytes):
"""
Our bit-flipping operates on the original text but in reverse
"""
reversed_ciphertext = bytearray()
bytes_per_block = 128 // 8
num_blocks = len(ciphertext) // (bytes_per_block)
assert ~(num_blocks & (num_blocks - 1)), \
f'{num_blocks} must be a power of 2'
for i in range(num_blocks, 0, -1):
reversed_ciphertext += get_nth_block(ciphertext, i)
return bytes(reversed_ciphertext)
def field_from_bytes(b: bytes) -> galois.FieldArray:
return field(int_from_bytes(b))
def test_validate_ad():
random.seed(0)
aes_key = ''.join(random.choice(string.ascii_letters) for _ in range(32))
nonce = ''.join(random.choice(string.ascii_letters) for _ in range(12))
num_blocks = 2 ** 4
n = int(log2(num_blocks))
plaintext = generate_plaintext(num_blocks - 1).encode()
ciphertext, t = gcm_encrypt(plaintext, b'', aes_key, nonce.encode())
# Let's validate that AD works the way we think it does
bytes_per_block = 128 // 8
h = int_from_bytes(aes_encrypt(bytes(bytes_per_block), aes_key))
num_columns = n * 128
associated_bitlen = (0).to_bytes(8, byteorder='big')
cipher_bitlen = (len(plaintext) * 8).to_bytes(8, byteorder='big')
length_block = associated_bitlen + cipher_bitlen
total_bytes = ciphertext + length_block
reversed_total_bytes = reverse_blocks(total_bytes)
coeffs: List = [0] * (n + 1)
for i in range(0, n):
block_num = 2**i
block = get_nth_block(reversed_total_bytes, block_num)
coeffs[i] = field(int_from_bytes(block))
for i in range(0, num_columns):
flip_vector = GF2.Zeros(num_columns)
flip_vector[i] = 1
flipped_ciphertext = apply_bitflips(reversed_total_bytes, flip_vector)
block_idx, bit_flip_position = divmod(i, 128)
block_idx += 1
ad = calculate_ad(n, block_idx, bit_flip_position)
matrix_result = np.matmul(ad, field(h).vector())
original_result = gcm_mac_compute_g(total_bytes, aes_key)
flipped_result = gcm_mac_compute_g(reverse_blocks(flipped_ciphertext),
aes_key)
# Another way to get this result
block_num = 2 ** block_idx
x = field_from_bytes(get_nth_block(reversed_total_bytes, block_num))
y = field_from_bytes(get_nth_block(flipped_ciphertext, block_num))
direct_computation_result = (x - y) * (field(h) ** block_num)
expected_result = field(flipped_result ^ original_result)
assert direct_computation_result == expected_result, \
'Direct computation did match expected'
assert direct_computation_result == field.Vector(matrix_result), \
'Matrix computation did not match as expected'
assert field.Vector(matrix_result) == \
field(flipped_result ^ original_result), \
'AD calculation did not match as expected'
def calc_dependency_matrix(num_columns, tag_bits, x, n):
# This is the number of rows we want to zero out multiplied by the
# degree of freedom that we have.
_, x_columns = x.shape
num_rows = min(tag_bits - 1, (((n - 1) * 128) // x_columns)) * x_columns
# This is the number of rows we want to zero out multiplied by the
# degree of freedom that we have.
_, x_columns = x.shape
# this is the dependency matrix in the problem description
t_matrix = GF2.Zeros((num_rows, num_columns))
for j in range(num_columns):
# Create the matrix AD that results from flipping the jth bit of
# the ciphertext.
block_num = (j // 128) + 1
ad_matrix = calculate_ad(n, block_num, j % 128)
mtz = np.matmul(ad_matrix, x) # matrix to zero
for i in range(num_rows // x_columns):
# Copy row i of ad_matrix into column j of t_matrix
t_matrix[(i * x_columns):(i + 1) * x_columns, j] = mtz[i, :]
return t_matrix
def error_polynomial_oracle(h, flip_vector):
"""
Get the error polynomial from the given bitflips.
This is similar to calculating the MAC for the ciphertext + bitflips,
but happens to be much faster.
"""
num_blocks = len(flip_vector) // 128
e = 0
for i in range(1, num_blocks + 1):
block = flip_vector[(i - 1) * 128:i * 128]
ci = int(field.Vector(block))
e = element_add(e, element_mult(ci, element_exp(int(h), 2 ** i)))
return e
def test_gcm_encrypt_truncated_mac_attack():
random.seed(0)
tag_bits = 32
num_blocks = 2 ** (tag_bits // 2 + 1)
n = int(log2(num_blocks))
aes_key = ''.join(random.choice(string.ascii_letters) for _ in range(32))
nonce = ''.join(random.choice(string.ascii_letters) for _ in range(12))
assert len(nonce.encode()) * 8 == 96
print('-----> generate plaintext')
plaintext1 = generate_plaintext(num_blocks).encode()
print('-----> run gcm_encryption')
ciphertext, t = gcm_encrypt(plaintext1, b'', aes_key, nonce.encode(),
tag_bits=tag_bits)
reversed_total_bytes = reverse_blocks(ciphertext)
coeffs: List = [0] * (n + 1)
for i in range(1, n + 1):
block_num = 2**i
block = get_nth_block(reversed_total_bytes, block_num)
coeffs[i] = field(int_from_bytes(block))
x = GF2.Identity(128)
k_vecs = []
count = 0
while True:
# Degrees of freedom (# of columns) is always n * 128 since we can
# fiddle with any 128 bits in the n blocks.
num_columns = n * 128
print(f'-----> calculating dependency matrix')
t_matrix = calc_dependency_matrix(num_columns, tag_bits, x, n)
print('-----> matrix null space')
results = matrix_null_space(t_matrix)
# assert len(results) == 128
# all 2^i combinations
# found_forgery = False
h = field_from_bytes(aes_encrypt(bytes(128 // 8), aes_key))
print('-----> testing vectors in null space to find forgery')
found_forgery = False
# not certain this is working as described
while not found_forgery:
count += 1
v = GF2.Zeros(num_columns)
for vector in results:
if bool(random.getrandbits(1)):
v += vector
err_polynomial = error_polynomial_oracle(h, v)
if err_polynomial >> (128 - tag_bits):
if count % 25 == 0:
print(f'Attempt {count} - no forgery '
f'{err_polynomial:#0{34}x}')
continue
forged_ciphertext = apply_bitflips(reversed_total_bytes, v.vector())
forged_blocks = reverse_blocks(forged_ciphertext)
forged_t = gcm_mac(forged_blocks,
b'',
aes_key, nonce.encode(),
tag_bits=tag_bits)
assert forged_t == t, 'Error polynomial was not accurate'
print(f'Attempt {count} - found forgery '
f'{err_polynomial:#0{34}x}')
ad = calculate_ad_from_flip_vector(n, v)
ad_relevant_part = ad[:tag_bits]
non_zero_rows, = np.where(ad_relevant_part.any(axis=1))
k_vecs += [ad_relevant_part[i] for i in non_zero_rows]
k = GF2(np.vstack(k_vecs))
assert not np.matmul(k, h.vector()).any(), \
'h should have been in null space of K'
k_rank = np.linalg.matrix_rank(k)
null_vectors = matrix_null_space(k)
if k_rank == 127:
assert len(null_vectors) == 1
recovered_h = null_vectors[0]
assert field.Vector(recovered_h) == h, \
'Did not recover correct key'
print(f'Success! Recovered key {h}')
return
x = GF2(np.vstack(null_vectors).transpose())
print(f'Rank of K: {k_rank}, X: {x.shape=}')
found_forgery = True