示例#1
0
def test_streamcipher2():
    data = list(bytearray(16))
    key = bytearray(16)
    seed = bytearray(16)    
    size = (8, 255, 5)#(64, 0xFFFFFFFFFFFFFFFF, 40)
    data[1] = 1
    stream_cipher(data, key, seed, size)
    print words_to_bytes(data, size[0] / 8)
   # print [byte for byte in data]
    
    
    data2 = list(bytearray(16))
    data2[0] = 1
    stream_cipher(data2, key, seed, size, mode="encrypt")
    print words_to_bytes(data2, size[0] / 8)
示例#2
0
def invert_keyed_homomorphic_permutation(state, key, rounds=4):
    assert isinstance(state, bytearray), type(state)
    a, b, c, d = bytes_to_words(state, 4)
    
    key = key[:]
    for round in range(rounds + 1):
        key_schedule(key)
        
    a, b, c, d = invert_bit_permutation((a, b, c, d), bytes_to_words(key, 4))    
    invert_key_schedule(key)  
    
    for round in reversed(range(rounds)):            
        a, b, c, d = invert_mix_columns(a, b, c, d)
        b = rotate_right(b, 8, 32)
        c = rotate_right(c, 12, 32)
        d = rotate_right(d, 16, 32)
        
        a, b, c, d = invert_mix_columns(a, b, c, d)
        b = rotate_right(b, 4, 32)
        c = rotate_right(c, 8, 32)
        d = rotate_right(d, 12, 32)
        
        a, b, c, d = invert_mix_columns(a, b, c, d)
        b = rotate_right(b, 1, 32)
        c = rotate_right(c, 2, 32)
        d = rotate_right(d, 3, 32)
        
        a, b, c, d = invert_mix_columns(a, b, c, d)
                
        a, b, c, d = invert_bit_permutation((a, b, c, d), bytes_to_words(key, 4))
        invert_key_schedule(key)        
    state[:] = words_to_bytes((a, b, c, d), 4)
示例#3
0
def keyed_homomorphic_permutation(state, key, rounds=4): 
    """ Ultimately, xors together random groups of state bits,
        such that each bit is made up of bit_i XOR bit_j XOR bit_k ... for
        a large enough number of random terms. """
    a, b, c, d = bytes_to_words(state, 4)
    key = key[:]
    for i in range(rounds):        
        key_schedule(key)
        # randomize the position of bits                    
        a, b, c, d = bit_permutation((a, b, c, d), bytes_to_words(key, 4))    
        
        # stack lots of bits on top of each other
        a, b, c, d = mix_columns(a, b, c, d)
        b = rotate_left(b, 1, 32)
        c = rotate_left(c, 2, 32)
        d = rotate_left(d, 3, 32)
        
        a, b, c, d = mix_columns(a, b, c, d)
        b = rotate_left(b, 4, 32)
        c = rotate_left(c, 8, 32)
        d = rotate_left(d, 12, 32)
        
        a, b, c, d = mix_columns(a, b, c, d)
        b = rotate_left(b, 8, 32)
        c = rotate_left(c, 12, 32)
        d = rotate_left(d, 16, 32)
        
        a, b, c, d = mix_columns(a, b, c, d)      
    key_schedule(key)
    a, b, c, d = bit_permutation((a, b, c, d), bytes_to_words(key, 4))    
    state[:] = words_to_bytes((a, b, c, d), 4)
示例#4
0
 def encrypt(self, data, key, iv, size=(64, (2 ** 64) - 1, 40)):
     word64 = lambda _data: bytes_to_words(bytearray(_data), 8)
     output = word64(data)
     key += "\x00" * (128 - len(key))        
     key = word64(key)
     assert len(key) == 16, len(key)
     iv += "\x00" * (128 - len(iv))
     assert len(iv) == 128, len(iv)
     iv = word64(iv)
     assert len(iv) == 16, len(iv)
     encrypt(output, key, iv, size)
     return bytes(words_to_bytes(output, 8))
示例#5
0
def encrypt(message,
            public_key,
            ciphertext_count=16,
            prng=lambda amount: bytearray(urandom(amount))):
    """ usage: encrypt(message : bytearray, public_key : list) => ciphertext : list
    
        Public key encryption scheme, based on symmetric homomorphic encryption.
        A public key consists of encryptions of the numbers 0-255, in order.
        
        To encrypt one byte, add together (using XOR) a random subset of the integers 
        (which are actually ciphertexts) such that the sum equals the message byte.
                
        This can be done simply in practice, by adding together enough random 
        integers from the public key, then calculating the difference between the resulting integer
        and desired integer, and adding that last integer to the sum. 
        
        Encryption can send one 8-bit value per 128-bit ciphertext. This results in a 16x increase in data size. 
        Ciphertexts are partially homormophic. 
        
        Works on arbitrarily long messages, albeit one byte at a time. """
    output = []
    key_bytes = iter(prng(len(message) * ciphertext_count))
    assert not isinstance(message, int)
    for symbol in bytearray(message):
        ciphertext_byte = [0, 0, 0, 0]
        _key_byte = 0
        for count in range(ciphertext_count):
            key_byte = next(key_bytes)
            _key_byte ^= key_byte
            ciphertext_key_byte = public_key[key_byte]
            ciphertext_byte[0] ^= ciphertext_key_byte[0]
            ciphertext_byte[1] ^= ciphertext_key_byte[1]
            ciphertext_byte[2] ^= ciphertext_key_byte[2]
            ciphertext_byte[3] ^= ciphertext_key_byte[3]

        final_key_byte = _key_byte ^ symbol
        final_ciphertext = public_key[final_key_byte]
        ciphertext_byte[0] ^= final_ciphertext[0]
        ciphertext_byte[1] ^= final_ciphertext[1]
        ciphertext_byte[2] ^= final_ciphertext[2]
        ciphertext_byte[3] ^= final_ciphertext[3]
        output.append(words_to_bytes(ciphertext_byte, 4))
    return output
示例#6
0
def test_stream_cipher_diffusion():
    wordsize = 1
    size = generate_params_for_wordsize(wordsize)    
    seed = bytes_to_words(bytearray(16 * wordsize), wordsize)
    
    key = seed[:]
    seed2 = key[:]  
    seed3 = key[:]
    
    seed2[-2] = 1 
    seed3[-2] = 2
    data = seed[:]
    data2 = data[:]
    data3 = data[:]
        
    stream_cipher(data, seed, key, size)
    stream_cipher(data2, seed2, key[:15] + [1], size)
    stream_cipher(data3, seed3, key[:14] + [1, 0], size)
    
    _bytes = lambda _data: words_to_bytes(_data, wordsize)
    bits = lambda _data: ''.join(format(byte, 'b').zfill(8) for byte in _bytes(_data))
    
    print _bytes(data)
示例#7
0
def save_ciphertext(ciphertext):
    return ''.join(
        bytes(words_to_bytes(entry, 2))
        for entry in slide(ciphertext, BLOCKSIZE))
示例#8
0
def save_private_key(private_key):
    return words_to_bytes(private_key, 2)
示例#9
0
def save_public_key(public_key):
    return ''.join(
        bytes(words_to_bytes(entry, 2))
        for entry in slide(public_key, BLOCKSIZE))
示例#10
0
def save_ciphertext(ciphertext):
    return ''.join(bytes(words_to_bytes(entry, 2)) for entry in slide(ciphertext, BLOCKSIZE))
示例#11
0
def save_private_key(private_key):        
    return words_to_bytes(private_key, 2)
示例#12
0
def save_public_key(public_key):      
    return ''.join(bytes(words_to_bytes(entry, 2)) for entry in slide(public_key, BLOCKSIZE))
示例#13
0
def save_public_key(public_key):
    lines = bytearray()
    for ciphertext_words in public_key:
        lines.extend(words_to_bytes(ciphertext_words, 4))
    return lines
示例#14
0
def blockcipher32(key, m, rounds=1):
    key = bytes_to_words(bytearray(key), 4)
    m = bytes_to_words(bytearray(m), 4)[0]
    return words_to_bytes([_cic_blockcipher32_encrypt(key, m, rounds)], 4)
示例#15
0
def iv_generator(key, seed, wordsize=8, mask=(2 ** 64) - 1):
    state = bytes_to_words(seed, wordsize)    
    bit_width = wordsize * 8
    while True:
        permutation(state, key, mask, bit_width)          
        yield words_to_bytes(state, wordsize)