-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitives.c
159 lines (140 loc) · 4.26 KB
/
primitives.c
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
/*
*
* University of Luxembourg
* Laboratory of Algorithmics, Cryptology and Security (LACS)
*
* FELICS - Fair Evaluation of Lightweight Cryptographic Systems
*
* Copyright (C) 2015 University of Luxembourg
*
* Written in 2015 by Daniel Dinu <[email protected]>
*
* This file is part of FELICS.
*
* FELICS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* FELICS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h>
#include <string.h>
#include "constants.h"
#include "primitives.h"
/*
*
* Cipher Primitives
*
*/
void inline ByteXor(uint8_t *dst, const uint8_t *a, const uint8_t *b, int8_t bytelen)
{
while(bytelen-- > 0){
*dst++ = *a++ ^ *b++;
}
}
uint8_t ClefiaMul2(uint8_t x)
{
/* multiplication over GF(2^8) (p(x) = '11d') */
if(x & 0x80U){
x ^= 0x0eU;
}
return ((x << 1) | (x >> 7));
}
void ClefiaF0Xor(uint8_t *dst, const uint8_t *src, const uint8_t *rk)
{
uint8_t x[4], y[4], z[4];
/* F0 */
/* Key addition */
ByteXor(x, src, rk, 4);
/* Substitution layer */
z[0] = clefia_s0[x[0]];
z[1] = clefia_s1[x[1]];
z[2] = clefia_s0[x[2]];
z[3] = clefia_s1[x[3]];
/* Diffusion layer (M0) */
y[0] = z[0] ^ ClefiaMul2(z[1]) ^ ClefiaMul4(z[2]) ^ ClefiaMul6(z[3]);
y[1] = ClefiaMul2(z[0]) ^ z[1] ^ ClefiaMul6(z[2]) ^ ClefiaMul4(z[3]);
y[2] = ClefiaMul4(z[0]) ^ ClefiaMul6(z[1]) ^ z[2] ^ ClefiaMul2(z[3]);
y[3] = ClefiaMul6(z[0]) ^ ClefiaMul4(z[1]) ^ ClefiaMul2(z[2]) ^ z[3] ;
/* Xoring after F0 */
memcpy(dst + 0, src + 0, 4);
ByteXor(dst + 4, src + 4, y, 4);
}
void ClefiaF1Xor(uint8_t *dst, const uint8_t *src, const uint8_t *rk)
{
uint8_t x[4], y[4], z[4];
/* F1 */
/* Key addition */
ByteXor(x, src, rk, 4);
/* Substitution layer */
z[0] = clefia_s1[x[0]];
z[1] = clefia_s0[x[1]];
z[2] = clefia_s1[x[2]];
z[3] = clefia_s0[x[3]];
/* Diffusion layer (M1) */
y[0] = z[0] ^ ClefiaMul8(z[1]) ^ ClefiaMul2(z[2]) ^ ClefiaMulA(z[3]);
y[1] = ClefiaMul8(z[0]) ^ z[1] ^ ClefiaMulA(z[2]) ^ ClefiaMul2(z[3]);
y[2] = ClefiaMul2(z[0]) ^ ClefiaMulA(z[1]) ^ z[2] ^ ClefiaMul8(z[3]);
y[3] = ClefiaMulA(z[0]) ^ ClefiaMul2(z[1]) ^ ClefiaMul8(z[2]) ^ z[3] ;
/* Xoring after F1 */
memcpy(dst + 0, src + 0, 4);
ByteXor(dst + 4, src + 4, y, 4);
}
void ClefiaGfn4(uint8_t *block, const uint8_t *rk, int8_t r)
{
uint8_t fin[16];
memcpy(fin, block, 16);
while(r-- > 0){
ClefiaF0Xor(block + 0, fin + 0, rk + 0);
ClefiaF1Xor(block + 8, fin + 8, rk + 4);
rk += 8;
if(r){ /* swapping for encryption */
memcpy(fin + 0, block + 4, 12);
memcpy(fin + 12, block + 0, 4);
}
}
}
void ClefiaGfn4Inv(uint8_t *block, const uint8_t *rk, int8_t r)
{
uint8_t fin[16];
rk += (r - 1) * 8;
memcpy(fin, block, 16);
while(r-- > 0){
ClefiaF0Xor(block + 0, fin + 0, rk + 0);
ClefiaF1Xor(block + 8, fin + 8, rk + 4);
rk -= 8;
if(r){ /* swapping for decryption */
memcpy(fin + 0, block + 12, 4);
memcpy(fin + 4, block + 0, 12);
}
}
}
void ClefiaDoubleSwap(uint8_t *lk)
{
uint8_t t[16];
t[0] = (lk[0] << 7) | (lk[1] >> 1);
t[1] = (lk[1] << 7) | (lk[2] >> 1);
t[2] = (lk[2] << 7) | (lk[3] >> 1);
t[3] = (lk[3] << 7) | (lk[4] >> 1);
t[4] = (lk[4] << 7) | (lk[5] >> 1);
t[5] = (lk[5] << 7) | (lk[6] >> 1);
t[6] = (lk[6] << 7) | (lk[7] >> 1);
t[7] = (lk[7] << 7) | (lk[15] & 0x7fU);
t[8] = (lk[8] >> 7) | (lk[0] & 0xfeU);
t[9] = (lk[9] >> 7) | (lk[8] << 1);
t[10] = (lk[10] >> 7) | (lk[9] << 1);
t[11] = (lk[11] >> 7) | (lk[10] << 1);
t[12] = (lk[12] >> 7) | (lk[11] << 1);
t[13] = (lk[13] >> 7) | (lk[12] << 1);
t[14] = (lk[14] >> 7) | (lk[13] << 1);
t[15] = (lk[15] >> 7) | (lk[14] << 1);
memcpy(lk, t, 16);
}