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
|
/**
* \file
* \author Egor Tensin <Egor.Tensin@gmail.com>
* \date 2015
* \copyright This file is licensed under the terms of the MIT License.
* See LICENSE.txt for details.
*/
#pragma once
#include <assert.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
AESNI_ECB,
AESNI_CBC,
AESNI_CFB,
AESNI_OFB,
AESNI_CTR,
}
AesNI_Mode;
#define AESNI_ENCRYPT_BLOCK_ECB(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ecb( \
BlockT plaintext, \
const KeyT* key) \
{ \
assert(key); \
\
return aesni_## prefix ##_encrypt_block_(plaintext, key); \
}
#define AESNI_DECRYPT_BLOCK_ECB(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ecb( \
BlockT ciphertext, \
const KeyT* key) \
{ \
assert(key); \
\
return aesni_## prefix ##_decrypt_block_(ciphertext, key); \
}
#define AESNI_ENCRYPT_BLOCK_CBC(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_cbc( \
BlockT plaintext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
assert(key); \
assert(next_init_vector); \
\
return *next_init_vector = aesni_## prefix ##_encrypt_block_ecb( \
aesni_## prefix ##_xor_blocks(plaintext, init_vector), key); \
}
#define AESNI_DECRYPT_BLOCK_CBC(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_cbc( \
BlockT ciphertext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
assert(key); \
assert(next_init_vector); \
\
BlockT plaintext = aesni_## prefix ##_xor_blocks( \
aesni_## prefix ##_decrypt_block_ecb(ciphertext, key), init_vector); \
*next_init_vector = ciphertext; \
return plaintext; \
}
#define AESNI_ENCRYPT_BLOCK_CFB(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_cfb( \
BlockT plaintext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
assert(key); \
assert(next_init_vector); \
\
return *next_init_vector = aesni_## prefix ##_xor_blocks( \
aesni_## prefix ##_encrypt_block_ecb(init_vector, key), plaintext); \
}
#define AESNI_DECRYPT_BLOCK_CFB(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_cfb( \
BlockT ciphertext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
assert(key); \
assert(next_init_vector); \
\
BlockT plaintext = aesni_## prefix ##_xor_blocks( \
aesni_## prefix ##_encrypt_block_ecb(init_vector, key), ciphertext); \
*next_init_vector = ciphertext; \
return plaintext; \
}
#define AESNI_ENCRYPT_BLOCK_OFB(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ofb( \
BlockT plaintext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
assert(key); \
assert(next_init_vector); \
\
BlockT tmp = aesni_## prefix ##_encrypt_block_ecb(init_vector, key); \
*next_init_vector = tmp; \
return aesni_## prefix ##_xor_blocks(tmp, plaintext); \
}
#define AESNI_DECRYPT_BLOCK_OFB(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ofb( \
BlockT ciphertext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
return aesni_## prefix ##_encrypt_block_ofb( \
ciphertext, key, init_vector, next_init_vector); \
}
#define AESNI_ENCRYPT_BLOCK_CTR(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_encrypt_block_ctr( \
BlockT plaintext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
assert(key); \
assert(next_init_vector); \
\
BlockT ciphertext = aesni_## prefix ##_xor_blocks( \
plaintext, aesni_## prefix ##_encrypt_block_ecb(init_vector, key)); \
*next_init_vector = aesni_## prefix ##_inc_block(init_vector); \
return ciphertext; \
}
#define AESNI_DECRYPT_BLOCK_CTR(prefix, BlockT, KeyT) \
static __inline BlockT __fastcall aesni_## prefix ##_decrypt_block_ctr( \
BlockT ciphertext, \
const KeyT* key, \
BlockT init_vector, \
BlockT* next_init_vector) \
{ \
return aesni_## prefix ##_encrypt_block_ctr( \
ciphertext, key, init_vector, next_init_vector); \
}
#ifdef __cplusplus
}
#endif
|