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
|
#include <string.h>
#include "aes.h"
#include "bs.h"
#include "utils.h"
void aes_ecb_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
{
word_t input_space[BLOCK_SIZE];
word_t rk[11][BLOCK_SIZE];
memset(outputb,0,size);
word_t * state = (word_t *)outputb;
bs_expand_key(rk, key);
while (size > 0)
{
if (size < BS_BLOCK_SIZE)
{
memset(input_space,0,BS_BLOCK_SIZE);
memmove(input_space, inputb, size);
bs_cipher(input_space,rk);
memmove(outputb, input_space, size);
size = 0;
state += size;
}
else
{
memmove(state,inputb,BS_BLOCK_SIZE);
bs_cipher(state,rk);
size -= BS_BLOCK_SIZE;
state += BS_BLOCK_SIZE;
}
}
}
void aes_ecb_decrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key)
{
word_t input_space[BLOCK_SIZE];
word_t rk[11][BLOCK_SIZE];
memset(outputb,0,size);
word_t * state = (word_t *)outputb;
bs_expand_key(rk, key);
while (size > 0)
{
if (size < BS_BLOCK_SIZE)
{
memset(input_space,0,BS_BLOCK_SIZE);
memmove(input_space, inputb, size);
bs_cipher_rev(input_space,rk);
memmove(outputb, input_space, size);
size = 0;
state += size;
}
else
{
memmove(state,inputb,BS_BLOCK_SIZE);
bs_cipher_rev(state,rk);
size -= BS_BLOCK_SIZE;
state += BS_BLOCK_SIZE;
}
}
}
static void INC_CTR(uint8_t * ctr, uint8_t i)
{
ctr += BLOCK_SIZE/8 - 1;
uint8_t n = *(ctr);
*ctr += i;
while(*ctr < n)
{
ctr--;
n = *ctr;
(*ctr)++;
}
}
void aes_ctr_encrypt(uint8_t * outputb, uint8_t * inputb, size_t size, uint8_t * key, uint8_t * iv)
{
word_t rk[11][BLOCK_SIZE];
word_t ctr[BLOCK_SIZE];
uint8_t iv_copy[BLOCK_SIZE/8];
memset(outputb,0,size);
memset(ctr,0,sizeof(ctr));
memmove(iv_copy,iv,BLOCK_SIZE/8);
word_t * state = (word_t *)outputb;
bs_expand_key(rk, key);
do
{
int chunk = MIN(size, BS_BLOCK_SIZE);
int blocks = chunk / (BLOCK_SIZE/8);
if (chunk % (BLOCK_SIZE/8))
{
blocks++;
}
int i;
for (i = 0; i < blocks; i++)
{
memmove(ctr + (i * WORDS_PER_BLOCK), iv_copy, BLOCK_SIZE/8);
INC_CTR(iv_copy,1);
}
bs_cipher(ctr, rk);
size -= chunk;
uint8_t * ctr_p = (uint8_t *) ctr;
while(chunk--)
{
*outputb++ = *ctr_p++ ^ *inputb++;
}
}
while(size);
}
|