import lilliput_ae_1 import lilliput_ae_2 import parameters_i_128 as i_128 import parameters_i_192 as i_192 import parameters_i_256 as i_256 import parameters_ii_128 as ii_128 import parameters_ii_192 as ii_192 import parameters_ii_256 as ii_256 BLOCK_BYTES = 16 N_BYTES = 15 def GetParameters(mode = 1, length = 128) : if(mode == 1 and length == 128) : return (i_128.KEY_BITS, i_128.TWEAK_BITS, i_128.ROUNDS) if(mode == 1 and length == 192) : return (i_192.KEY_BITS, i_192.TWEAK_BITS, i_192.ROUNDS) if(mode == 1 and length == 256) : return (i_256.KEY_BITS, i_256.TWEAK_BITS, i_256.ROUNDS) if(mode == 2 and length == 128) : return (ii_128.KEY_BITS, ii_128.TWEAK_BITS, ii_128.ROUNDS) if(mode == 2 and length == 192) : return (ii_192.KEY_BITS, ii_192.TWEAK_BITS, ii_192.ROUNDS) if(mode == 2 and length == 256) : return (ii_256.KEY_BITS, ii_256.TWEAK_BITS, ii_256.ROUNDS) def ArrayToBlockbytesMatrix(array) : length = len(array) pad = 0 if(length % BLOCK_BYTES == 0) : number_blocks = int(length / BLOCK_BYTES) else : number_blocks = int((length + (BLOCK_BYTES - (length % BLOCK_BYTES))) / BLOCK_BYTES) pad = 1 matrix = [[0] * BLOCK_BYTES for block in range(0, number_blocks - pad)] if(pad == 1) : matrix.append([0] * (length % BLOCK_BYTES)) for byte in range(0, length) : matrix[int(byte / BLOCK_BYTES)][byte % BLOCK_BYTES] = array[byte] return matrix def BlockbytesMatrixToBytes(matrix): return bytes(byte for block in matrix for byte in block) ############################################ def mainEnc(plaintext, adata, mode=1, length=128): (key_bits, tweak_bits, rounds) = GetParameters(mode, length) A = adata M = plaintext N = [0 for byte in range(0, N_BYTES)] key = [byte for byte in range(0, int(key_bits/8))] A_BITS = 8 * len(A) M_BITS = 8 * len(M) A = ArrayToBlockbytesMatrix(A) M = ArrayToBlockbytesMatrix(M) if(mode == 1) : (C, tag) = lilliput_ae_1.OCB3Enc(A, M, N, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds) if(mode == 2) : (C, tag) = lilliput_ae_2.SCT2Enc(A, M, N, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds) return BlockbytesMatrixToBytes(C), bytes(tag) def mainDec(ciphertext, tag, adata, mode=1, length=128): (key_bits, tweak_bits, rounds) = GetParameters(mode, length) A = adata C = ciphertext N = [0 for byte in range(0, N_BYTES)] key = [byte for byte in range(0, int(key_bits/8))] tag = list(tag) M_BITS = 8 * len(C) A_BITS = 8 * len(A) A = ArrayToBlockbytesMatrix(A) C = ArrayToBlockbytesMatrix(C) if(mode == 1) : M = lilliput_ae_1.OCB3Dec(A, C, N, tag, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds) if(mode == 2) : M = lilliput_ae_2.SCT2Dec(A, C, N, tag, A_BITS, M_BITS, key, key_bits, tweak_bits, rounds) return BlockbytesMatrixToBytes(M)