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
|
// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "AES tools" project.
// For details, see https://github.com/egor-tensin/aes-tools.
// Distributed under the MIT License.
#pragma once
#include <assert.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum
{
AES_ECB,
AES_CBC,
AES_CFB,
AES_OFB,
AES_CTR,
}
AES_Mode;
#define AES_ENCRYPT_BLOCK_ECB(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_ECB( \
AES_## prefix ##_Block plaintext, \
const AES_## prefix ##_RoundKeys* encryption_keys) \
{ \
assert(encryption_keys); \
\
return aes_## prefix ##_encrypt_block_(plaintext, encryption_keys); \
}
#define AES_DECRYPT_BLOCK_ECB(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_ECB( \
AES_## prefix ##_Block ciphertext, \
const AES_## prefix ##_RoundKeys* decryption_keys) \
{ \
assert(decryption_keys); \
\
return aes_## prefix ##_decrypt_block_(ciphertext, decryption_keys); \
}
#define AES_ENCRYPT_BLOCK_CBC(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CBC( \
AES_## prefix ##_Block plaintext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
return *next_init_vector = aes_## prefix ##_encrypt_block_( \
aes_## prefix ##_xor_blocks(plaintext, init_vector), encryption_keys); \
}
#define AES_DECRYPT_BLOCK_CBC(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CBC( \
AES_## prefix ##_Block ciphertext, \
const AES_## prefix ##_RoundKeys* decryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(decryption_keys); \
assert(next_init_vector); \
\
AES_## prefix ##_Block plaintext = aes_## prefix ##_xor_blocks( \
aes_## prefix ##_decrypt_block_(ciphertext, decryption_keys), init_vector); \
*next_init_vector = ciphertext; \
return plaintext; \
}
#define AES_ENCRYPT_BLOCK_CFB(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CFB( \
AES_## prefix ##_Block plaintext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
return *next_init_vector = aes_## prefix ##_xor_blocks( \
aes_## prefix ##_encrypt_block_(init_vector, encryption_keys), plaintext); \
}
#define AES_DECRYPT_BLOCK_CFB(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CFB( \
AES_## prefix ##_Block ciphertext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
AES_## prefix ##_Block plaintext = aes_## prefix ##_xor_blocks( \
aes_## prefix ##_encrypt_block_(init_vector, encryption_keys), ciphertext); \
*next_init_vector = ciphertext; \
return plaintext; \
}
#define AES_ENCRYPT_BLOCK_OFB(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_OFB( \
AES_## prefix ##_Block plaintext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
AES_## prefix ##_Block tmp = aes_## prefix ##_encrypt_block_(init_vector, encryption_keys); \
*next_init_vector = tmp; \
return aes_## prefix ##_xor_blocks(tmp, plaintext); \
}
#define AES_DECRYPT_BLOCK_OFB(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_OFB( \
AES_## prefix ##_Block ciphertext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
return aes_## prefix ##_encrypt_block_OFB( \
ciphertext, encryption_keys, init_vector, next_init_vector); \
}
#define AES_ENCRYPT_BLOCK_CTR(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_encrypt_block_CTR( \
AES_## prefix ##_Block plaintext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
AES_## prefix ##_Block ciphertext = aes_## prefix ##_xor_blocks( \
plaintext, aes_## prefix ##_encrypt_block_(init_vector, encryption_keys)); \
*next_init_vector = aes_## prefix ##_inc_block(init_vector); \
return ciphertext; \
}
#define AES_DECRYPT_BLOCK_CTR(prefix) \
static __inline AES_## prefix ##_Block __fastcall aes_## prefix ##_decrypt_block_CTR( \
AES_## prefix ##_Block ciphertext, \
const AES_## prefix ##_RoundKeys* encryption_keys, \
AES_## prefix ##_Block init_vector, \
AES_## prefix ##_Block* next_init_vector) \
{ \
assert(encryption_keys); \
assert(next_init_vector); \
\
return aes_## prefix ##_encrypt_block_CTR( \
ciphertext, encryption_keys, init_vector, next_init_vector); \
}
#ifdef __cplusplus
}
#endif
|