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
|
# Implementation of the Lilliput-AE tweakable block cipher.
#
# Authors, hereby denoted as "the implementer":
# Kévin Le Gouguec,
# Léo Reynaud
# 2019.
#
# For more information, feedback or questions, refer to our website:
# https://paclido.fr/lilliput-ae
#
# To the extent possible under law, the implementer has waived all copyright
# and related or neighboring rights to the source code in this file.
# http://creativecommons.org/publicdomain/zero/1.0/
"""Multiplications for Lilliput-TBC's tweakey schedule.
This module provides a list of functions implementing lane multiplications,
from ALPHAS[0] = α₀ = I to ALPHAS[6] = α₆ = M_R³.
"""
from functools import reduce
from operator import xor
def _Sl(n):
return lambda xi: (xi<<n) & 0xff
def _Sr(n):
return lambda xi: xi>>n
def _Id(xi):
return xi
def _0(xi):
return 0
def _M1(xi):
return (xi<<3 ^ xi>>3) & 0xff
def _M2(xi):
return (xi<<6 ^ (xi&0b11111000) ^ xi>>6) & 0xff
def _M3(xi):
return xi & 0b00011111
def _M4(xi):
return ((xi<<2) & 0xff) >> 3
M = (
( _0, _Id, _0, _0, _0, _0, _0, _0),
( _0, _0, _Id, _0, _0, _0, _0, _0),
( _0, _0, _Sl(3), _Id, _0, _0, _0, _0),
( _0, _0, _0, _Sr(3), _Id, _0, _0, _0),
( _0, _0, _0, _0, _0, _Id, _0, _0),
( _0, _Sl(2), _0, _0, _0, _0, _Id, _0),
( _0, _0, _0, _0, _0, _0, _0, _Id),
(_Id, _0, _0, _0, _0, _0, _0, _0),
)
M2 = (
( _0, _0, _Id, _0, _0, _0, _0, _0),
( _0, _0, _Sl(3), _Id, _0, _0, _0, _0),
( _0, _0, _Sl(6), _M1, _Id, _0, _0, _0),
( _0, _0, _0, _Sr(6), _Sr(3), _Id, _0, _0),
( _0, _Sl(2), _0, _0, _0, _0, _Id, _0),
( _0, _0, _Sl(2), _0, _0, _0, _0, _Id),
(_Id, _0, _0, _0, _0, _0, _0, _0),
( _0, _Id, _0, _0, _0, _0, _0, _0),
)
M3 = (
( _0, _0, _Sl(3), _Id, _0, _0, _0, _0),
( _0, _0, _Sl(6), _M1, _Id, _0, _0, _0),
( _0, _0, _0, _M2, _M1, _Id, _0, _0),
( _0, _Sl(2), _0, _0, _Sr(6), _Sr(3), _Id, _0),
( _0, _0, _Sl(2), _0, _0, _0, _0, _Id),
(_Id, _0, _Sl(5), _Sl(2), _0, _0, _0, _0),
( _0, _Id, _0, _0, _0, _0, _0, _0),
( _0, _0, _Id, _0, _0, _0, _0, _0),
)
# NB: shift directions are reversed with respect to the specification
# for powers of M_R, since the specification reverses the byte order
# for those matrices.
MR = (
( _0, _Id, _0, _0, _0, _0, _0, _0),
( _0, _0, _Id, _0, _0, _0, _0, _0),
( _0, _0, _0, _Id, _Sr(3), _0, _0, _0),
( _0, _0, _0, _0, _Id, _0, _0, _0),
( _0, _0, _0, _0, _0, _Id, _Sl(3), _0),
( _0, _0, _0, _Sl(2), _0, _0, _Id, _0),
( _0, _0, _0, _0, _0, _0, _0, _Id),
(_Id, _0, _0, _0, _0, _0, _0, _0),
)
MR2 = (
( _0, _0, _Id, _0, _0, _0, _0, _0),
( _0, _0, _0, _Id, _Sr(3), _0, _0, _0),
( _0, _0, _0, _0, _Id, _Sr(3), _M3, _0),
( _0, _0, _0, _0, _0, _Id, _Sl(3), _0),
( _0, _0, _0, _Sl(2), _0, _0, _Id, _Sl(3)),
( _0, _0, _0, _0, _Sl(2), _0, _0, _Id),
(_Id, _0, _0, _0, _0, _0, _0, _0),
( _0, _Id, _0, _0, _0, _0, _0, _0),
)
MR3 = (
( _0, _0, _0, _Id, _Sr(3), _0, _0, _0),
( _0, _0, _0, _0, _Id, _Sr(3), _M3, _0),
( _0, _0, _0, _M4, _0, _Id, _M1, _M3),
( _0, _0, _0, _Sl(2), _0, _0, _Id, _Sl(3)),
(_Sl(3), _0, _0, _0, _Sl(2), _0, _0, _Id),
( _Id, _0, _0, _0, _0, _Sl(2), _Sl(5), _0),
( _0, _Id, _0, _0, _0, _0, _0, _0),
( _0, _0, _Id, _0, _0, _0, _0, _0),
)
def _multiplication(m, reverse=True):
def ordered(l):
if reverse:
return list(reversed(list(l)))
return l
def _multiply(x):
return ordered(
reduce(xor, (mj[i](xi) for i, xi in enumerate(ordered(x))))
for mj in m
)
return _multiply
ALPHAS = (
list, # Identity.
_multiplication(M),
_multiplication(M2),
_multiplication(M3),
_multiplication(MR, reverse=False),
_multiplication(MR2, reverse=False),
_multiplication(MR3, reverse=False)
)
|