-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAES128.h
126 lines (101 loc) · 3.88 KB
/
AES128.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
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
#ifndef __AES128_H__
#define __AES128_H__
#include <cstdint>
#include <cinttypes>
// IN #BYTES
#define BLOCK_128_SIZE 16
#define SMALL_KEY_128_SIZE 6
#define KEY_128_SIZE 16
#define KEY_128_ROUNDS 10
#define AES_128_COLUMNS 4
#define AES_128_ROWS 4
class AES128 {
public:
/*
Arguments:
key - An 128bit key for encryption
src - Byte Array[BLOCK_128_SIZE] containing input data
dst - Byte Array[BLOCK_128_SIZE] containing output data
This is the function which is used for encryption of data.
In order for this function to work you must specify a key.
If you are not about to change the key all the time, you can use
the setKey(key) function and then call the encrypt(src,dst) function.
*/
unsigned int encrypt(const uint8_t *temp_key,const uint8_t *src,uint8_t *output, unsigned int n_bytes);
/*
Arguments:
key - An 128bit key for encryption
src - Byte Array[BLOCK_128_SIZE] containing input data
dst - Byte Array[BLOCK_128_SIZE] containing output data
This is the function which is used for decryption of data.
In order for this function to work you must specify a key.
If you are not about to change the key all the time, you can use
the setKey(key) function and then call the decrypt(src,dst) function.
*/
unsigned int decrypt(const uint8_t *temp_key,const uint8_t *src,uint8_t *output, unsigned int n_bytes);
/*
Arguments:
src - Byte Array[BLOCK_128_SIZE] containing input data
dst - Byte Array[BLOCK_128_SIZE] containing output data
This is the function which is used for encryption of data
by using the key set with the setkey(key) function.
*/
unsigned int encrypt(const uint8_t *src,uint8_t *output, unsigned int n_bytes);
/*
Arguments:
src - Byte Array[BLOCK_128_SIZE] containing input data
dst - Byte Array[BLOCK_128_SIZE] containing output data
This is the function which is used for encryption of data
by using the key set with the setkey(key) function.
*/
unsigned int decrypt(const uint8_t *src,uint8_t *output, unsigned int n_bytes);
/*
Arguments:
key - An 128bit key for encryption
This is the function which sets up the class in such a way that
you don't need to insert the key every time you want to encrypt
or decrypt data. Simply after you set up the key correctly
you can use encrypt(src,dst) or decrypt(src,dst) function.
*/
bool setKey(uint8_t *key);
bool setUserKey(char *key);
/**/
void printData(uint8_t *key);
/**/
AES128();
private:
// PRIVATE VARIABLES
uint8_t **expanded_128_key;
uint8_t *original_128_key;
bool initialized = false;
bool key_set = false;
// LUTs
static const uint8_t rCon[10];
static const uint8_t sBox[256];
static const uint8_t invSbox[256];
// MULTIPLICATION LUTs
static const uint8_t multiply_2[256];
static const uint8_t multiply_3[256];
static const uint8_t multiply_9[256];
static const uint8_t multiply_11[256];
static const uint8_t multiply_13[256];
static const uint8_t multiply_14[256];
// GENERIC OPERATIONS
void expandKey(const uint8_t *original_key,uint8_t **expanded_key);
void convertToState(const uint8_t *src,uint8_t **dst);
void addKey(const uint8_t *key,uint8_t **dst);
void viewKey(uint8_t *key);
void view(uint8_t **dst);
// ROUND OPERATIONS
void substitute(uint8_t **dst);
void mixColumns(uint8_t **dst);
void shiftRows(uint8_t **dst);
void invSubstitute(uint8_t **dst);
void invMixColumns(uint8_t **dst);
void invShiftRows(uint8_t **dst);
void encryptBlock(uint8_t **tmp, const uint8_t *src, uint8_t *dst);
void encryptBlock(uint8_t *key, uint8_t **expandedKey, uint8_t **tmp, const uint8_t *src,uint8_t *dst);
void decryptBlock(uint8_t **tmp, const uint8_t *src, uint8_t *dst);
void decryptBlock(uint8_t *key, uint8_t **expandedKey, uint8_t **tmp, const uint8_t *src,uint8_t *dst);
};
#endif