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
|
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 _checkInputs(key, length, nonce):
if len(key) != length//8:
raise ValueError('invalid key size: {} != {}'.format(len(key), length//8))
if len(nonce) != N_BYTES:
raise ValueError('nonce must be {}-byte long'.format(N_BYTES))
def mainEnc(plaintext, adata, key, nonce, mode=1, length=128):
_checkInputs(key, length, nonce)
(key_bits, tweak_bits, rounds) = GetParameters(mode, length)
A = adata
M = plaintext
N = nonce
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, key, nonce, mode=1, length=128):
_checkInputs(key, length, nonce)
(key_bits, tweak_bits, rounds) = GetParameters(mode, length)
A = adata
C = ciphertext
N = nonce
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)
|