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 mainEnc(mode = 1, length = 128) :
(key_bits, tweak_bits, rounds) = GetParameters(mode, length)
A = [byte for byte in range(0, 16)]
M = [byte for byte in range(0, 17)]
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)
for block in range(0,len(C)) :
for byte in C[block] :
print("%02x "%(byte), end="")
for byte in tag :
print("%02x "%(byte), end="")
print()
def mainDec(mode = 1, length = 128) :
(key_bits, tweak_bits, rounds) = GetParameters(mode, length)
A = [byte for byte in range(0, 16)]
C = [byte for byte in range(0, 16)]
N = [0 for byte in range(0, N_BYTES)]
key = [byte for byte in range(0, int(key_bits/8))]
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)
for block in range(0,len(M)) :
for byte in M[block] :
print("%02x "%(byte), end="")
print()
|