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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
"""
SCT 2 for lilliput ae 2
"""
import lilliput_tbc as ltbc
from constants import BLOCK_BITS, BLOCK_BYTES
from helpers import (
ArrayToBlockbytesMatrix,
BlockbytesMatrixToBytes,
BuildAuth,
Padding10LSB,
XorState
)
BLOCK_BITS = 128
KEY_BITS = 128
TWEAK_BITS = 128
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANE_BITS = 64
LANES = int((TWEAKEY_BITS) / LANE_BITS)
BLOCK_BYTES = int(BLOCK_BITS / 8)
KEY_BYTES = int(KEY_BITS / 8)
TWEAK_BYTES = int(TWEAK_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
M_BITS = BLOCK_BITS
def InitParameters(key_bits) :
global KEY_BITS
global KEY_BYTES
global TWEAKEY_BITS
global TWEAKEY_BYTES
global LANES
KEY_BITS = key_bits
TWEAKEY_BITS = KEY_BITS + TWEAK_BITS
LANES = int((TWEAKEY_BITS) / LANE_BITS)
KEY_BYTES = int(KEY_BITS / 8)
TWEAKEY_BYTES = int(TWEAKEY_BITS / 8)
###############################################################################
def LowPart(array, number_bits) :
shifted = 0
for byte in range(0, len(array)) :
shifted |= (array[byte] << (8 * byte))
mask = 0
for bit in range(0, number_bits) :
mask |= (0x1 << bit)
lower_part = shifted & mask
will_padd = 0
if (number_bits % 8) != 0 :
will_padd = 1
lower_part_byte = [0 for byte in range(0, int(number_bits / 8) + will_padd)]
for byte in range(0, int(number_bits / 8) + will_padd) :
lower_part_byte[byte] = lower_part & 0xff
lower_part = lower_part >> 8
return lower_part_byte
################################################################################
def TweakTag(j, padded = 0) :
tweak = [0 for byte in range(0, TWEAK_BYTES)]
tweak[TWEAK_BYTES - 1] |= ((j >> 120) & 0xf)
for byte in range(TWEAK_BYTES - 2, -1, -1) :
tweak[byte] = (j >> (8 * byte)) & 0xff
if padded == 1 :
tweak[TWEAK_BYTES - 1] |= 0x40
return tweak
def TweakTagEnd(N) :
tweak = [0 for byte in range(0, TWEAK_BYTES)]
for byte in range(0, TWEAK_BYTES - 1) :
tweak[byte] = N[byte]
tweak[TWEAK_BYTES - 1] = 0x10
return tweak
def AddTagJ(tag, j) :
array_j = [0 for byte in range(0, TWEAK_BYTES)]
for byte in range(0, TWEAK_BYTES) :
array_j[byte] = (j >> (byte * 8))
xorr = XorState(tag, array_j)
xorr[TWEAK_BYTES - 1] |= 0x80
return xorr
def MesssageAuthTag(M, N, Auth, key) :
l = int(M_BITS / BLOCK_BITS)
if int(M_BITS % BLOCK_BITS) > 0 :
will_padd = 1
else :
will_padd = 0
tag = list(Auth)
for j in range(0, l) :
tweak = TweakTag(j, padded = 0)
encryption = ltbc.LilliputTBCEnc(tweak, key, M[j])
tag = XorState(tag, encryption)
if will_padd == 1 :
tweak = TweakTag(l, padded = 1)
m_padded = Padding10LSB(M[l], M_BITS % BLOCK_BITS)
encryption = ltbc.LilliputTBCEnc(tweak, key, m_padded)
tag = XorState(tag, encryption)
tweak = TweakTagEnd(N)
encryption = ltbc.LilliputTBCEnc(tweak, key, tag)
tag = encryption
return tag
def MessageEncryption(M, N, tag, key) :
l = int(M_BITS / BLOCK_BITS)
if int(M_BITS % BLOCK_BITS) > 0 :
will_padd = 1
else :
will_padd = 0
C = [[0 for byte in range(0, 16)] for j in range(0, l + will_padd)]
for j in range(0, l) :
tweak = AddTagJ(tag, j)
padded_nounce = list(N) + [0x00]
encryption = ltbc.LilliputTBCEnc(tweak, key, padded_nounce)
C[j] = XorState(M[j], encryption)
if will_padd :
tweak = AddTagJ(tag, l)
padded_nounce = list(N) + [0x00]
encryption = ltbc.LilliputTBCEnc(tweak, key, padded_nounce)
C[l] = XorState(M[l], encryption)
return C
################################################################################
def SCT2Enc(A, M, N, key) :
InitParameters(len(key)*8)
global M_BITS
M_BITS = len(M)*8
M = ArrayToBlockbytesMatrix(M)
K = list(key)
Auth = BuildAuth(TWEAK_BITS, A, K)
tag = MesssageAuthTag(M, N, Auth, K)
C = MessageEncryption(M, N, tag, K)
return BlockbytesMatrixToBytes(C), bytes(tag)
def SCT2Dec(A, C, N, tag, key) :
InitParameters(len(key)*8)
global M_BITS
M_BITS = len(C)*8
C = ArrayToBlockbytesMatrix(C)
K = list(key)
M = MessageEncryption(C, N, tag, K)
Auth = BuildAuth(TWEAK_BITS, A, K)
tag2 = MesssageAuthTag(M, N, Auth, K)
if(tag == tag2) :
return BlockbytesMatrixToBytes(M)
|