-
Notifications
You must be signed in to change notification settings - Fork 41
/
sm4ni.c
173 lines (137 loc) · 5.03 KB
/
sm4ni.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
160
161
162
163
164
165
166
167
168
169
170
171
172
// sm4ni.c
// 2018-04-20 Markku-Juhani O. Saarinen <mjos@iki.fi>
// Vectorized implementation of SM4. Uses affine transformations and AES NI
// to implement the SM4 S-Box.
#include "sm4_ref.h"
#include <x86intrin.h>
// Encrypt 4 blocks (64 bytes) in ECB mode
void sm4_encrypt4(const uint32_t rk[32], void *src, const void *dst)
{
// nibble mask
const __m128i c0f __attribute__((aligned(0x10))) =
{ 0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F };
// flip all bytes in all 32-bit words
const __m128i flp __attribute__((aligned(0x10))) =
{ 0x0405060700010203, 0x0C0D0E0F08090A0B };
// inverse shift rows
const __m128i shr __attribute__((aligned(0x10))) =
{ 0x0B0E0104070A0D00, 0x0306090C0F020508 };
// Affine transform 1 (low and high hibbles)
const __m128i m1l __attribute__((aligned(0x10))) =
{ 0x9197E2E474720701, 0xC7C1B4B222245157 };
const __m128i m1h __attribute__((aligned(0x10))) =
{ 0xE240AB09EB49A200, 0xF052B91BF95BB012 };
// Affine transform 2 (low and high hibbles)
const __m128i m2l __attribute__((aligned(0x10))) =
{ 0x5B67F2CEA19D0834, 0xEDD14478172BBE82 };
const __m128i m2h __attribute__((aligned(0x10))) =
{ 0xAE7201DD73AFDC00, 0x11CDBE62CC1063BF };
// left rotations of 32-bit words by 8-bit increments
const __m128i r08 __attribute__((aligned(0x10))) =
{ 0x0605040702010003, 0x0E0D0C0F0A09080B };
const __m128i r16 __attribute__((aligned(0x10))) =
{ 0x0504070601000302, 0x0D0C0F0E09080B0A };
const __m128i r24 __attribute__((aligned(0x10))) =
{ 0x0407060500030201, 0x0C0F0E0D080B0A09 };
__m128i x, y, t0, t1, t2, t3;
uint32_t k, *p32, v[4] __attribute__((aligned(0x10)));
int i;
p32 = (uint32_t *) src;
t0 = _mm_set_epi32(p32[12], p32[ 8], p32[ 4], p32[ 0]);
t0 = _mm_shuffle_epi8(t0, flp);
t1 = _mm_set_epi32(p32[13], p32[ 9], p32[ 5], p32[ 1]);
t1 = _mm_shuffle_epi8(t1, flp);
t2 = _mm_set_epi32(p32[14], p32[10], p32[ 6], p32[ 2]);
t2 = _mm_shuffle_epi8(t2, flp);
t3 = _mm_set_epi32(p32[15], p32[11], p32[ 7], p32[ 3]);
t3 = _mm_shuffle_epi8(t3, flp);
#ifndef SM4NI_UNROLL
// not unrolled
for (i = 0; i < 32; i++) {
k = rk[i];
x = t1 ^ t2 ^ t3 ^ _mm_set_epi32(k, k, k, k);
y = _mm_and_si128(x, c0f); // inner affine
y = _mm_shuffle_epi8(m1l, y);
x = _mm_srli_epi64(x, 4);
x = _mm_and_si128(x, c0f);
x = _mm_shuffle_epi8(m1h, x) ^ y;
x = _mm_shuffle_epi8(x, shr); // inverse MixColumns
x = _mm_aesenclast_si128(x, c0f); // AESNI instruction
y = _mm_andnot_si128(x, c0f); // outer affine
y = _mm_shuffle_epi8(m2l, y);
x = _mm_srli_epi64(x, 4);
x = _mm_and_si128(x, c0f);
x = _mm_shuffle_epi8(m2h, x) ^ y;
// 4 parallel L1 linear transforms
y = x ^ _mm_shuffle_epi8(x, r08) ^ _mm_shuffle_epi8(x, r16);
y = _mm_slli_epi32(y, 2) ^ _mm_srli_epi32(y, 30);
x = x ^ y ^ _mm_shuffle_epi8(x, r24);
// rotate registers
x ^= t0;
t0 = t1;
t1 = t2;
t2 = t3;
t3 = x;
}
#else
// unrolled version
#define SM4_TAU_L1 { \
y = _mm_and_si128(x, c0f); \
y = _mm_shuffle_epi8(m1l, y); \
x = _mm_srli_epi64(x, 4); \
x = _mm_and_si128(x, c0f); \
x = _mm_shuffle_epi8(m1h, x) ^ y; \
x = _mm_shuffle_epi8(x, shr); \
x = _mm_aesenclast_si128(x, c0f); \
y = _mm_andnot_si128(x, c0f); \
y = _mm_shuffle_epi8(m2l, y); \
x = _mm_srli_epi64(x, 4); \
x = _mm_and_si128(x, c0f); \
x = _mm_shuffle_epi8(m2h, x) ^ y; \
y = x ^ _mm_shuffle_epi8(x, r08) ^ \
_mm_shuffle_epi8(x, r16); \
y = _mm_slli_epi32(y, 2) ^ \
_mm_srli_epi32(y, 30); \
x = x ^ y ^ _mm_shuffle_epi8(x, r24); \
}
for (i = 0; i < 32;) {
k = rk[i++];
x = t1 ^ t2 ^ t3 ^ _mm_set_epi32(k, k, k, k);
SM4_TAU_L1
t0 ^= x;
k = rk[i++];
x = t0 ^ t2 ^ t3 ^ _mm_set_epi32(k, k, k, k);
SM4_TAU_L1
t1 ^= x;
k = rk[i++];
x = t0 ^ t1 ^ t3 ^ _mm_set_epi32(k, k, k, k);
SM4_TAU_L1
t2 ^= x;
k = rk[i++];
x = t0 ^ t1 ^ t2 ^ _mm_set_epi32(k, k, k, k);
SM4_TAU_L1
t3 ^= x;
}
#endif
p32 = (uint32_t *) dst;
_mm_store_si128((__m128i *) v, _mm_shuffle_epi8(t3, flp));
p32[ 0] = v[0];
p32[ 4] = v[1];
p32[ 8] = v[2];
p32[12] = v[3];
_mm_store_si128((__m128i *) v, _mm_shuffle_epi8(t2, flp));
p32[ 1] = v[0];
p32[ 5] = v[1];
p32[ 9] = v[2];
p32[13] = v[3];
_mm_store_si128((__m128i *) v, _mm_shuffle_epi8(t1, flp));
p32[ 2] = v[0];
p32[ 6] = v[1];
p32[10] = v[2];
p32[14] = v[3];
_mm_store_si128((__m128i *) v, _mm_shuffle_epi8(t0, flp));
p32[ 3] = v[0];
p32[ 7] = v[1];
p32[11] = v[2];
p32[15] = v[3];
}