コード例 #1
0
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
コード例 #2
0
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
コード例 #3
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')
コード例 #4
0
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')
コード例 #5
0
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)