def test_quarter(): """ xor_a = 10010101 add_a = 01001001 add_b = 10010010 add = add_a + add_b 01001001 + 10010010 = 11011011 shift <<< 3 11011011... <<< 3 ...11011110 xor 11011110 10010101 01001011 """ prg = PRG() xor_a = '10010101' add_a = '01001001' add_b = '10010010' shift = 3 output = prg.quarter(xor_a=xor_a, add_a=add_a, add_b=add_b, shift=shift) assert output == '01001011' # inverse shift --> '01111011'
def test_columnround_function(): prg = PRG() x = make_words(n=16) y0 = prg.quarterround_function((x[0], x[4], x[8], x[12])) y1 = prg.quarterround_function((x[5], x[9], x[13], x[1])) y2 = prg.quarterround_function((x[10], x[14], x[2], x[6])) y3 = prg.quarterround_function((x[15], x[3], x[7], x[11])) y = (y0, y1, y2, y3) assert type(y) is type(y0) is type(y1) is type(y2) is type(y3) is tuple # Unpack y. y_unpacked = [] for words in y: for word in words: y_unpacked.append(word) assert len(y_unpacked) == 16 y_unpacked = tuple(y_unpacked) # Run function tests. output = prg.columnround_function(x) assert type(output) is tuple assert len(output) == 16 for element in output: assert type(element) is str # Compare output. assert output == y_unpacked
def test_to_words(): prg = PRG() bits = make_words(n=8, as_tuple=False) output = prg.to_words(bits) assert type(output) is tuple assert len(output) == len(bits) / 32 == 8 for word in output: assert len(word) == 32
def test_binary_left_rotation(): prg = PRG() w1 = make_words(n=1) output = prg.binary_left_rotation(w1, 6) assert output == '01100101011001010110010101100101' w2 = '11011011' output = prg.binary_left_rotation(w2, 3) assert output == '11011110'
def test_quarterround_function(): prg = PRG() x = make_words(n=4) output = prg.quarterround_function(x) # Values not tested. assert type(output) is tuple assert output == ('10100000001010000010100100101001', '11111011111110111111101111111011', '00000110000001100000010000000110', '01101001001010010000100101101001')
def test_init(): prg1 = PRG() assert type(prg1) is PRG assert len(prg1.a_vects) == len(prg1.b_vects) == len(PRG.A_VECTOR) == len( PRG.B_VECTOR) == 4 assert len(prg1.a_vects[0]) == 32 prg2 = PRG(test_mode=True) assert prg2.test_mode == True
def test_doubleround_function(): prg = PRG() x = make_words(n=16) verification = prg.columnround_function(x) verification = prg.rowround_function(verification) output = prg.doubleround_function(x) print(type(x)) print(type(output)) print(type(verification)) assert type(output) is type(verification) is type(x) is tuple assert len(output) == len(verification) == len(x) == 16 assert output == verification
def __init__(self): self.stat_nonce = '0' * 128 self.sal = Salsa20(mode='test', static_nonce=self.stat_nonce) self.prg = PRG(test_mode=True) self.key = None self.data = None self.QR_x = None self.QR_y = None self.gen_QR_runs = 0 self.get_QR_runs = 0
def test_QR_word_sizes_support(): prg = PRG() X_1 = ('0', '1', '1', '0') X_2 = ('10', '01', '11', '01') X_3 = ('110', '001', '101', '010') X_4 = ('1001', '0101', '1001', '0101') X_8 = ('10010101', '10010101', '10010101', '10010101') assert prg.quarterround_function(X_1) == ('1', '1', '0', '1') assert prg.quarterround_function(X_2) == ('00', '10', '11', '11') assert prg.quarterround_function(X_3) == ('000', '001', '010', '100') assert prg.quarterround_function(X_4) == ('0101', '0010', '1110', '0101') assert prg.quarterround_function(X_8) == ('01010001', '10000000', '10111111', '01110010')
def test_to_bytes(): prg = PRG() words1 = make_words(n=8) words2 = make_words(n=8, as_tuple=False) output1 = prg.to_bytes(words1) output2 = prg.to_bytes(words2) assert type(output1) is type(output2) is tuple assert len(output1) == len(output2) == len(words1) * 4 == len(words2) / 8 for byte in output1: assert len(byte) == 8 for byte in output2: assert len(byte) == 8 assert output1 == output2
def test_sum_words(): prg = PRG() w1, w2 = make_words(n=2) output1 = prg.sum_words(w1, w2) assert output1 == '00111010001110100011101000111001' w3 = '01001001' w4 = '10010010' output2 = prg.sum_words(w3, w4) assert output2 == '11011011' w5 = '11111111' w6 = '11111111' output3 = prg.sum_words(w5, w6) assert output3 == '11111110'
def test_expansion_function(): prg = PRG() key_16 = make_words(n=4, as_tuple=False) key_32 = make_words(n=8, as_tuple=False) key0_32 = key_32[0:128] key1_32 = key_32[128:256] n = make_words(n=4, as_tuple=False) output_16 = prg.expansion_function(key_16, key_16, n, False) output_32 = prg.expansion_function(key0_32, key1_32, n, True) assert len(output_16) == 512 assert len(output_32) == 512
def run_test(): #prg = PRG() #run_n_QRs(prg.quarterround_function) run_n_QRs(simple_test_1) run_n_QRs(simple_test_2) prg = PRG() run_n_QRs(prg.quarterround_function)
def test_xor(): # 10010101100101011001010110010101 <-- w1 # 10100100101001001010010010100100 <-- w2 # 00110001001100010011000100110001 <-- w1 xor w2 prg = PRG() w1, w2 = make_words(n=2) output1 = prg.xor(w1, w2) assert output1 == '00110001001100010011000100110001' # '00110001'*4 # 10010101 <-- w3 # 11011110 <-- w4 # 01001011 <-- w3 xor w4 w3 = '10010101' w4 = '11011110' output2 = prg.xor(w3, w4) assert output2 == '01001011' output3 = prg.xor(w1, w4) assert output3 == '10010101100101011001010101001011'
def test_rowround_function(): prg = PRG() # Create test variables. x = make_words(n=16) y0 = prg.quarterround_function(x[0:4]) y1 = prg.quarterround_function(x[4:8]) y2 = prg.quarterround_function(x[8:12]) y3 = prg.quarterround_function(x[12:16]) y = (y0, y1, y2, y3) assert type(y) is type(y0) is type(y1) is type(y2) is type(y3) is tuple # Unpack y. y_unpacked = [] for words in y: for word in words: y_unpacked.append(word) assert type(y_unpacked) is list y_unpacked = tuple(y_unpacked) assert type(y_unpacked) is tuple assert len(y_unpacked) == 16 # Run function tests. output = prg.rowround_function(x) assert type(output) is tuple assert len(output) == 16 for element in output: assert type(element) is str # Compare output. assert output == y_unpacked
def compare_salsa_with_similar_inputs(flips): prg = PRG() #key = '0' * 256 key = get_random_binary(256) key0 = key[:128] key1 = key[128:] nonce = get_random_binary(128) IV_0, IV_1, IV_2, IV_3 = prg.a_vects original_hash_input = IV_0 + key0 + IV_1 + nonce + IV_2 + key1 + IV_3 original_hash_output = prg.expansion_function(key0, key1, nonce, full_key=True) in_out_HDs = [] in_in_HDs = [] out_out_HDs = [] for flip in range(flips): key0 = key[:128] key1 = key[128:] hash_output = prg.expansion_function(key0, key1, nonce, full_key=True) hash_input = IV_0 + key0 + IV_1 + nonce + IV_2 + key1 + IV_3 in_out_HD = hamming_distance(hash_output, hash_input) in_out_HDs.append(in_out_HD) in_in_HD = hamming_distance(original_hash_input, hash_input) in_in_HDs.append(in_in_HD) #out_out_HD = hamming_distance(original_hash_output, hash_input) #out_out_HDs.append(out_out_HD) out_out_HDs.append(256) key = flip_random_bit(key) return in_out_HDs, in_in_HDs, out_out_HDs
def test_from_binary(): prg = PRG() output = prg.from_binary(bin(65)) assert output == 65
def test_to_binary(): prg = PRG() output = prg.to_binary(65) assert output == '01000001'
def test_from_ascii(): prg = PRG() output = prg.from_ascii(65) assert output == 'A'
def test_to_ascii(): prg = PRG() output = prg.to_ascii('A') assert output == 65
def test_to_bits(): prg = PRG() words = make_words(n=8) output = prg.to_bits(words) assert type(output) is str assert len(output) == len(words) * 32 == 256
def test_hash_function(): prg = PRG() words = make_words(n=16) output = prg.hash_function(words) assert type(output) is str assert len(words) * 32 == len(output) == 512 # == 64 bytes * 8 bites/byte
def test_littleendian_function(): prg = PRG() b = '00000100000010000001000000100000' b_ = '00100000000100000000100000000100' output = prg.littleendian_function(b) assert output == b_
class Crypto_Tools: def __init__(self): self.stat_nonce = '0' * 128 self.sal = Salsa20(mode='test', static_nonce=self.stat_nonce) self.prg = PRG(test_mode=True) self.key = None self.data = None self.QR_x = None self.QR_y = None self.gen_QR_runs = 0 self.get_QR_runs = 0 def get_QR(self, key, data, index=0): if key != self.key or data != self.data: self.gen_QR(key, data) self.get_QR_runs += 1 print(len(index)) return self.word_list_to_bits(self.QR_x[index]) def word_list_to_bits(self, word_list): bits = '' for word in word_list: bits += word return bits def gen_QR(self, key, data): self.sal = Salsa20(mode='test', static_nonce=self.stat_nonce) ciphertext = self.sal.encrypt(key=key, data=data) self.QR_x = self.sal.prg.QR_x self.QR_y = self.sal.prg.QR_y self.key = key self.data = data self.gen_QR_runs += 1 # Dette tar litt tid (kanskje 20 sek). #print('Full XOR operations:', self.sal.prg.XORs) #print('Single bit XOR operations:', self.sal.prg.single_xor) """ def QR_x(self, key, data, index=0): ciphertext = sal.encrypt(data, key) QR = sal.prg.QR_x[index] global_key = key global_data = data global_QR_x = word_list_to_bits(QR) return word_list_to_bits(QR) def QR_y(self, key, data, index=0): ciphertext = sal.encrypt(data, key) QR = sal.prg.QR_y[index] global_key = key global_data = data global_QR_y = word_list_to_bits(QR) return word_list_to_bits(QR) """ def use_QRF(self, X: tuple): """Runs X through the QRf (Quarter Round function) once. Requirements for X - type(X) is tuple - len(X) is 4 - type(X[i]) is str - len(X[i])%8 == 0. """ return self.prg.quarterround_function(X)