17
17
#include <string.h>
18
18
#include <pthread.h>
19
19
20
-
21
20
#define AES128_ROUNDS 10
22
21
#define BITS_PER_BYTE 8
23
22
24
-
25
23
#ifdef AES_HW
26
24
27
25
#if defined(__x86_64__ )
@@ -40,10 +38,10 @@ aes128_hw_supported(void)
40
38
}
41
39
42
40
#if defined(__clang__ )
43
- # pragma clang attribute push (__attribute__((target("sse2,aes"))), apply_to= function)
41
+ #pragma clang attribute push(__attribute__((target("sse2,aes"))), apply_to = function)
44
42
#elif defined(__GNUC__ )
45
- # pragma GCC push_options
46
- # pragma GCC target ("sse2,aes")
43
+ #pragma GCC push_options
44
+ #pragma GCC target("sse2,aes")
47
45
#endif
48
46
49
47
struct aes128_hw_ctx {
@@ -53,7 +51,7 @@ struct aes128_hw_ctx {
53
51
static __m128i
54
52
aes128_hw_round_key (__m128i rk , __m128i rc )
55
53
{
56
- rc = _mm_shuffle_epi32 (rc , _MM_SHUFFLE (3 ,3 , 3 , 3 ));
54
+ rc = _mm_shuffle_epi32 (rc , _MM_SHUFFLE (3 , 3 , 3 , 3 ));
57
55
rk = _mm_xor_si128 (rk , _mm_slli_si128 (rk , 4 ));
58
56
rk = _mm_xor_si128 (rk , _mm_slli_si128 (rk , 4 ));
59
57
rk = _mm_xor_si128 (rk , _mm_slli_si128 (rk , 4 ));
@@ -97,42 +95,42 @@ aes128_hw_enc_block(struct aes128_hw_ctx const *ctx, uint8_t const *pt, uint8_t
97
95
}
98
96
99
97
#if defined(__clang__ )
100
- # pragma clang attribute pop
98
+ #pragma clang attribute pop
101
99
#elif defined(__GNUC__ )
102
- # pragma GCC pop_options
100
+ #pragma GCC pop_options
103
101
#endif
104
102
105
103
#elif defined(__aarch64__ )
106
104
#define AES_HW_NAME "ARMv8 CE"
107
105
108
106
#ifdef __ARM_ACLE
109
- # include <arm_acle.h>
107
+ #include <arm_acle.h>
110
108
#endif
111
109
#ifdef __ARM_NEON
112
- # include <arm_neon.h>
110
+ #include <arm_neon.h>
113
111
#endif
114
112
115
113
#if defined(__APPLE__ )
116
- # include <sys/types.h>
117
- # include <sys/sysctl.h>
114
+ #include <sys/types.h>
115
+ #include <sys/sysctl.h>
118
116
#elif defined(__FreeBSD__ )
119
- # include <sys/auxv.h>
120
- # ifndef HWCAP_NEON
121
- # define HWCAP_NEON 0x00001000
122
- # endif
123
- # ifndef HWCAP2_AES
124
- # define HWCAP2_AES 0x00000001
125
- # endif
117
+ #include <sys/auxv.h>
118
+ #ifndef HWCAP_NEON
119
+ #define HWCAP_NEON 0x00001000
120
+ #endif
121
+ #ifndef HWCAP2_AES
122
+ #define HWCAP2_AES 0x00000001
123
+ #endif
126
124
#elif defined(__linux__ )
127
- # include <sys/auxv.h>
128
- # ifndef HWCAP_NEON
129
- # define HWCAP_NEON 0x00000010
130
- # endif
131
- # ifndef HWCAP_AES
132
- # define HWCAP_AES 0x00001000
133
- # endif
125
+ #include <sys/auxv.h>
126
+ #ifndef HWCAP_NEON
127
+ #define HWCAP_NEON 0x00000010
128
+ #endif
129
+ #ifndef HWCAP_AES
130
+ #define HWCAP_AES 0x00001000
131
+ #endif
134
132
#else
135
- # warning "Runtime detection of AES hardware acceleration not implemented for platform"
133
+ #warning "Runtime detection of AES hardware acceleration not implemented for platform"
136
134
#endif
137
135
138
136
static bool
@@ -160,16 +158,17 @@ aes128_hw_supported(void)
160
158
}
161
159
162
160
#if defined(__clang__ )
163
- # pragma clang attribute push (__attribute__((target("aes"))), apply_to= function)
161
+ #pragma clang attribute push(__attribute__((target("aes"))), apply_to = function)
164
162
#elif defined(__GNUC__ )
165
- # pragma GCC push_options
166
- # pragma GCC target ("+aes")
163
+ #pragma GCC push_options
164
+ #pragma GCC target("+aes")
167
165
#endif
168
166
169
167
struct aes128_hw_ctx {
170
168
uint8_t rk [AES128_ROUNDS + 1 ][AES128_KEY_BYTES ];
171
169
};
172
170
171
+ // clang-format off
173
172
static uint8_t const sbox [256 ] = {
174
173
0x63 , 0x7c , 0x77 , 0x7b , 0xf2 , 0x6b , 0x6f , 0xc5 , 0x30 , 0x01 , 0x67 , 0x2b , 0xfe , 0xd7 , 0xab , 0x76 ,
175
174
0xca , 0x82 , 0xc9 , 0x7d , 0xfa , 0x59 , 0x47 , 0xf0 , 0xad , 0xd4 , 0xa2 , 0xaf , 0x9c , 0xa4 , 0x72 , 0xc0 ,
@@ -188,8 +187,9 @@ static uint8_t const sbox[256] = {
188
187
0xe1 , 0xf8 , 0x98 , 0x11 , 0x69 , 0xd9 , 0x8e , 0x94 , 0x9b , 0x1e , 0x87 , 0xe9 , 0xce , 0x55 , 0x28 , 0xdf ,
189
188
0x8c , 0xa1 , 0x89 , 0x0d , 0xbf , 0xe6 , 0x42 , 0x68 , 0x41 , 0x99 , 0x2d , 0x0f , 0xb0 , 0x54 , 0xbb , 0x16 ,
190
189
};
190
+ // clang-format on
191
191
192
- static uint8_t const rcon [AES128_ROUNDS + 1 ] = { 0 , 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 , 0x1b , 0x36 };
192
+ static uint8_t const rcon [AES128_ROUNDS + 1 ] = {0 , 0x01 , 0x02 , 0x04 , 0x08 , 0x10 , 0x20 , 0x40 , 0x80 , 0x1b , 0x36 };
193
193
194
194
static void
195
195
aes128_hw_key_sched (uint8_t const * key , struct aes128_hw_ctx * ctx )
@@ -234,9 +234,9 @@ aes128_hw_enc_block(struct aes128_hw_ctx const *ctx, uint8_t const *pt, uint8_t
234
234
}
235
235
236
236
#if defined(__clang__ )
237
- # pragma clang attribute pop
237
+ #pragma clang attribute pop
238
238
#elif defined(__GNUC__ )
239
- # pragma GCC pop_options
239
+ #pragma GCC pop_options
240
240
#endif
241
241
242
242
#else
@@ -245,7 +245,6 @@ aes128_hw_enc_block(struct aes128_hw_ctx const *ctx, uint8_t const *pt, uint8_t
245
245
246
246
#endif // AES_HW
247
247
248
-
249
248
struct aes128_ctx {
250
249
union {
251
250
struct {
@@ -298,8 +297,7 @@ aes128_init(uint8_t const *key)
298
297
return ctx ;
299
298
}
300
299
301
- void
302
- aes128_encrypt_block (aes128_ctx_t * ctx , uint8_t const * pt , uint8_t * ct )
300
+ void aes128_encrypt_block (aes128_ctx_t * ctx , uint8_t const * pt , uint8_t * ct )
303
301
{
304
302
#ifdef AES_HW
305
303
if (use_hw ) {
@@ -311,19 +309,17 @@ aes128_encrypt_block(aes128_ctx_t *ctx, uint8_t const *pt, uint8_t *ct)
311
309
rijndaelEncrypt (ctx -> u .sw .rk , AES128_ROUNDS , pt , ct );
312
310
}
313
311
314
- void
315
- aes128_fini (aes128_ctx_t * ctx )
312
+ void aes128_fini (aes128_ctx_t * ctx )
316
313
{
317
314
free (ctx );
318
315
}
319
316
320
- void
321
- aes128_selftest (void )
317
+ void aes128_selftest (void )
322
318
{
323
319
// Test vector from appendix C of NIST FIPS-197.
324
- uint8_t const pt [AES128_BLOCK_BYTES ] = { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 , 0x88 , 0x99 , 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
325
- uint8_t const key [AES128_KEY_BYTES ] = { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f };
326
- uint8_t const expected_ct [AES128_BLOCK_BYTES ] = { 0x69 , 0xc4 , 0xe0 , 0xd8 , 0x6a , 0x7b , 0x04 , 0x30 , 0xd8 , 0xcd , 0xb7 , 0x80 , 0x70 , 0xb4 , 0xc5 , 0x5a };
320
+ uint8_t const pt [AES128_BLOCK_BYTES ] = {0x00 , 0x11 , 0x22 , 0x33 , 0x44 , 0x55 , 0x66 , 0x77 , 0x88 , 0x99 , 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
321
+ uint8_t const key [AES128_KEY_BYTES ] = {0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f };
322
+ uint8_t const expected_ct [AES128_BLOCK_BYTES ] = {0x69 , 0xc4 , 0xe0 , 0xd8 , 0x6a , 0x7b , 0x04 , 0x30 , 0xd8 , 0xcd , 0xb7 , 0x80 , 0x70 , 0xb4 , 0xc5 , 0x5a };
327
323
328
324
uint8_t actual_ct [AES128_BLOCK_BYTES ];
329
325
memset (actual_ct , 0 , sizeof (actual_ct ));
0 commit comments