aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/include/aesni/aes.h
blob: 427ca8515e48d59a950690efa3c36a93cb97dbce (plain) (blame)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
 * \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 "data.h"
#include "error.h"
#include "mode.h"

#include <assert.h>

#ifdef __cplusplus
extern "C"
{
#endif

typedef AesNI_Block128 AesNI_Aes_Block;

typedef struct
{
    AesNI_Aes_Block key;
}
AesNI_Aes128_Key;

typedef struct
{
    AesNI_Aes_Block hi;
    AesNI_Aes_Block lo;
}
AesNI_Aes192_Key;

typedef struct
{
    AesNI_Aes_Block hi;
    AesNI_Aes_Block lo;
}
AesNI_Aes256_Key;

static __inline void aesni_aes_make_block(AesNI_Aes_Block* dest, int hi3, int hi2, int lo1, int lo0)
{
    *dest = aesni_make_block128(hi3, hi2, lo1, lo0);
}

static __inline void aesni_aes128_make_key(AesNI_Aes128_Key* dest, int hi3, int hi2, int lo1, int lo0)
{
    dest->key = aesni_make_block128(hi3, hi2, lo1, lo0);
}

static __inline void aesni_aes192_make_key(AesNI_Aes192_Key* dest, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0)
{
    dest->hi = aesni_make_block128(0, 0, hi5, hi4);
    dest->lo = aesni_make_block128(lo3, lo2, lo1, lo0);
}

static __inline void aesni_aes256_make_key(AesNI_Aes256_Key* dest, int hi7, int hi6, int hi5, int hi4, int lo3, int lo2, int lo1, int lo0)
{
    dest->hi = aesni_make_block128(hi7, hi6, hi5, hi4);
    dest->lo = aesni_make_block128(lo3, lo2, lo1, lo0);
}

typedef struct { char str[33]; } AesNI_Aes_BlockString;
typedef struct { char str[49]; } AesNI_Aes_BlockMatrixString;

AesNI_StatusCode aesni_aes_format_block(
    AesNI_Aes_BlockString*,
    const AesNI_Aes_Block*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes_format_block_as_matrix(
    AesNI_Aes_BlockMatrixString*,
    const AesNI_Aes_Block*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes_print_block(
    const AesNI_Aes_Block*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes_print_block_as_matrix(
    const AesNI_Aes_Block*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes_parse_block(
    AesNI_Aes_Block* dest,
    const char* src,
    AesNI_ErrorDetails* err_details);

typedef AesNI_Aes_BlockString AesNI_Aes128_KeyString;
typedef struct { char str[49]; } AesNI_Aes192_KeyString;
typedef struct { char str[65]; } AesNI_Aes256_KeyString;

AesNI_StatusCode aesni_aes128_format_key(
    AesNI_Aes128_KeyString*,
    const AesNI_Aes128_Key*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes192_format_key(
    AesNI_Aes192_KeyString*,
    const AesNI_Aes192_Key*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes256_format_key(
    AesNI_Aes256_KeyString*,
    const AesNI_Aes256_Key*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes128_print_key(
    const AesNI_Aes128_Key*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes192_print_key(
    const AesNI_Aes192_Key*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes256_print_key(
    const AesNI_Aes256_Key*,
    AesNI_ErrorDetails*);

AesNI_StatusCode aesni_aes128_parse_key(
    AesNI_Aes128_Key* dest,
    const char* src,
    AesNI_ErrorDetails* err_details);

AesNI_StatusCode aesni_aes192_parse_key(
    AesNI_Aes192_Key* dest,
    const char* src,
    AesNI_ErrorDetails* err_details);

AesNI_StatusCode aesni_aes256_parse_key(
    AesNI_Aes256_Key* dest,
    const char* src,
    AesNI_ErrorDetails* err_details);

typedef struct
{
    AesNI_Aes_Block keys[11];
}
AesNI_Aes128_RoundKeys;

typedef struct
{
    AesNI_Aes_Block keys[13];
}
AesNI_Aes192_RoundKeys;

typedef struct
{
    AesNI_Aes_Block keys[15];
}
AesNI_Aes256_RoundKeys;

void __fastcall aesni_aes128_expand_key_(
    AesNI_Aes_Block key,
    AesNI_Aes128_RoundKeys* encryption_keys);

void __fastcall aesni_aes192_expand_key_(
    AesNI_Aes_Block key_lo,
    AesNI_Aes_Block key_hi,
    AesNI_Aes192_RoundKeys* encryption_keys);

void __fastcall aesni_aes256_expand_key_(
    AesNI_Aes_Block key_lo,
    AesNI_Aes_Block key_hi,
    AesNI_Aes256_RoundKeys* encryption_keys);

void __fastcall aesni_aes128_derive_decryption_keys_(
    const AesNI_Aes128_RoundKeys* encryption_keys,
    AesNI_Aes128_RoundKeys* decryption_keys);

void __fastcall aesni_aes192_derive_decryption_keys_(
    const AesNI_Aes192_RoundKeys* encryption_keys,
    AesNI_Aes192_RoundKeys* decryption_keys);

void __fastcall aesni_aes256_derive_decryption_keys_(
    const AesNI_Aes256_RoundKeys* encryption_keys,
    AesNI_Aes256_RoundKeys* decryption_keys);

AesNI_Aes_Block __fastcall aesni_aes128_encrypt_block_(
    AesNI_Aes_Block plaintext,
    const AesNI_Aes128_RoundKeys*);

AesNI_Aes_Block __fastcall aesni_aes192_encrypt_block_(
    AesNI_Aes_Block plaintext,
    const AesNI_Aes192_RoundKeys*);

AesNI_Aes_Block __fastcall aesni_aes256_encrypt_block_(
    AesNI_Aes_Block plaintext,
    const AesNI_Aes256_RoundKeys*);

AesNI_Aes_Block __fastcall aesni_aes128_decrypt_block_(
    AesNI_Aes_Block ciphertext,
    const AesNI_Aes128_RoundKeys*);

AesNI_Aes_Block __fastcall aesni_aes192_decrypt_block_(
    AesNI_Aes_Block ciphertext,
    const AesNI_Aes192_RoundKeys*);

AesNI_Aes_Block __fastcall aesni_aes256_decrypt_block_(
    AesNI_Aes_Block ciphertext,
    const AesNI_Aes256_RoundKeys*);

static __inline AesNI_Aes_Block __fastcall aesni_aes_xor_blocks(
    AesNI_Aes_Block a,
    AesNI_Aes_Block b)
{
    return aesni_xor_block128(a, b);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes128_xor_blocks(
    AesNI_Aes_Block a,
    AesNI_Aes_Block b)
{
    return aesni_aes_xor_blocks(a, b);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes192_xor_blocks(
    AesNI_Aes_Block a,
    AesNI_Aes_Block b)
{
    return aesni_aes_xor_blocks(a, b);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes256_xor_blocks(
    AesNI_Aes_Block a,
    AesNI_Aes_Block b)
{
    return aesni_aes_xor_blocks(a, b);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes_inc_block(
    AesNI_Aes_Block block)
{
    block = aesni_reverse_byte_order_block128(block);
    block = aesni_inc_block128(block);
    return aesni_reverse_byte_order_block128(block);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes128_inc_block(
    AesNI_Aes_Block block)
{
    return aesni_aes_inc_block(block);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes192_inc_block(
    AesNI_Aes_Block block)
{
    return aesni_aes_inc_block(block);
}

static __inline AesNI_Aes_Block __fastcall aesni_aes256_inc_block(
    AesNI_Aes_Block block)
{
    return aesni_aes_inc_block(block);
}

AESNI_ENCRYPT_BLOCK_ECB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_DECRYPT_BLOCK_ECB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_ENCRYPT_BLOCK_CBC(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_DECRYPT_BLOCK_CBC(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_ENCRYPT_BLOCK_CFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_DECRYPT_BLOCK_CFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_ENCRYPT_BLOCK_OFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_DECRYPT_BLOCK_OFB(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_ENCRYPT_BLOCK_CTR(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);
AESNI_DECRYPT_BLOCK_CTR(aes128, AesNI_Aes_Block, AesNI_Aes128_RoundKeys);

AESNI_ENCRYPT_BLOCK_ECB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_DECRYPT_BLOCK_ECB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_ENCRYPT_BLOCK_CBC(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_DECRYPT_BLOCK_CBC(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_ENCRYPT_BLOCK_CFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_DECRYPT_BLOCK_CFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_ENCRYPT_BLOCK_OFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_DECRYPT_BLOCK_OFB(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_ENCRYPT_BLOCK_CTR(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);
AESNI_DECRYPT_BLOCK_CTR(aes192, AesNI_Aes_Block, AesNI_Aes192_RoundKeys);

AESNI_ENCRYPT_BLOCK_ECB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_DECRYPT_BLOCK_ECB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_ENCRYPT_BLOCK_CBC(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_DECRYPT_BLOCK_CBC(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_ENCRYPT_BLOCK_CFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_DECRYPT_BLOCK_CFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_ENCRYPT_BLOCK_OFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_DECRYPT_BLOCK_OFB(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_ENCRYPT_BLOCK_CTR(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);
AESNI_DECRYPT_BLOCK_CTR(aes256, AesNI_Aes_Block, AesNI_Aes256_RoundKeys);

/**
 * \brief Expands an AES-128 key into 10 encryption round keys.
 *
 * \param[in] key The AES-128 key.
 * \param[out] encryption_keys The AES-128 encryption round keys. Must not be `NULL`.
 */
static __inline void __fastcall aesni_aes128_expand_key(
    const AesNI_Aes128_Key* key,
    AesNI_Aes128_RoundKeys* encryption_keys)
{
    assert(encryption_keys);

    aesni_aes128_expand_key_(key->key, encryption_keys);
}

/**
 * \brief Derives AES-128 decryption round keys from AES-128 encryption round keys.
 *
 * \param[in] encryption_keys The AES-128 encryption round keys. Must not be `NULL`.
 * \param[out] decryption_keys The AES-128 decryption round keys. Must not be `NULL`.
 */
static __inline void __fastcall aesni_aes128_derive_decryption_keys(
    const AesNI_Aes128_RoundKeys* encryption_keys,
    AesNI_Aes128_RoundKeys* decryption_keys)
{
    assert(encryption_keys);
    assert(decryption_keys);

    aesni_aes128_derive_decryption_keys_(encryption_keys, decryption_keys);
}

/**
 * \brief Expands an AES-192 key into 12 encryption round keys.
 *
 * \param[in] key The AES-192 key.
 * \param[out] encryption_keys The AES-192 encryption round keys. Must not be `NULL`.
 */
static __inline void __fastcall aesni_aes192_expand_key(
    const AesNI_Aes192_Key* key,
    AesNI_Aes192_RoundKeys* encryption_keys)
{
    assert(key);
    assert(encryption_keys);

    aesni_aes192_expand_key_(key->lo, key->hi, encryption_keys);
}

/**
 * \brief Derives AES-192 decryption round keys from AES-192 encryption round keys.
 *
 * \param[in] encryption_keys The AES-192 encryption round keys. Must not be `NULL`.
 * \param[out] decryption_keys The AES-192 decryption round keys. Must not be `NULL`.
 */
static __inline void __fastcall aesni_aes192_derive_decryption_keys(
    const AesNI_Aes192_RoundKeys* encryption_keys,
    AesNI_Aes192_RoundKeys* decryption_keys)
{
    assert(encryption_keys);
    assert(decryption_keys);

    aesni_aes192_derive_decryption_keys_(encryption_keys, decryption_keys);
}

/**
 * \brief Expands an AES-256 key into 14 encryption round keys.
 *
 * \param[in] key The AES-256 key.
 * \param[out] encryption_keys The AES-256 encryption round keys. Must not be `NULL`.
 */
static __inline void __fastcall aesni_aes256_expand_key(
    const AesNI_Aes256_Key* key,
    AesNI_Aes256_RoundKeys* encryption_keys)
{
    assert(key);
    assert(encryption_keys);

    aesni_aes256_expand_key_(key->lo, key->hi, encryption_keys);
}

/**
 * \brief Derives AES-256 decryption round keys from AES-256 encryption round keys.
 *
 * \param[in] encryption_keys The AES-256 encryption round keys. Must not be `NULL`.
 * \param[out] decryption_keys The AES-256 decryption round keys. Must not be `NULL`.
 */
static __inline void __fastcall aesni_aes256_derive_decryption_keys(
    const AesNI_Aes256_RoundKeys* encryption_keys,
    AesNI_Aes256_RoundKeys* decryption_keys)
{
    assert(encryption_keys);
    assert(decryption_keys);

    aesni_aes256_derive_decryption_keys_(encryption_keys, decryption_keys);
}

#ifdef __cplusplus
}
#endif