-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes.h
68 lines (52 loc) · 1.72 KB
/
aes.h
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
#ifndef _AES_H_
#define _AES_H_
#include <stdint.h>
#include <stddef.h>
// #define the macros below to 1/0 to enable/disable the mode of operation.
//
// CBC enables AES encryption in CBC-mode of operation.
// CTR enables encryption in counter-mode.
// ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
#ifndef CBC
#define CBC 1
#endif
#ifndef ECB
#define ECB 1
#endif
#ifndef CTR
#define CTR 1
#endif
#define AES128 1
//#define AES192 1
//#define AES256 1
#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
#if defined(AES256) && (AES256 == 1)
#define AES_KEYLEN 32
#define AES_keyExpSize 240
#elif defined(AES192) && (AES192 == 1)
#define AES_KEYLEN 24
#define AES_keyExpSize 208
#else
#define AES_KEYLEN 16 // Key length in bytes
#define AES_keyExpSize 176
#endif
typedef uint8_t state_t[4][4];
struct AES_ctx
{
uint8_t RoundKey[AES_keyExpSize];
};
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key, uint8_t r);
void AES_encrypt(const struct AES_ctx* ctx, uint8_t* buf, uint8_t r);
void AES_decrypt(const struct AES_ctx* ctx, uint8_t* buf, uint8_t r);
void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key, uint8_t round);
void AddRoundKey(uint8_t round, state_t* state, const uint8_t* RoundKey);
void SubBytes(state_t* state);
void InvSubBytes(state_t* state);
void ShiftRows(state_t* state);
void InvShiftRows(state_t* state);
void MixColumns(state_t* state);
void InvMixColumns(state_t* state);
void Cipher(state_t* state, const uint8_t* RoundKey, uint8_t nr);
void InvCipher(state_t* state, const uint8_t* RoundKey, uint8_t nr);
#endif // _AES_H_