-
Notifications
You must be signed in to change notification settings - Fork 106
/
AESLib.c
324 lines (292 loc) · 9.13 KB
/
AESLib.c
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
/*
This file is part of the aeslib.
Copyright (C) 2012 Davy Landman ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AESLib.h"
#include <stdint.h>
#include "aes.h"
#include "blockcipher_descriptor.h"
#include "bcal_aes128.h"
#include "bcal_aes192.h"
#include "bcal-cbc.h"
#include <avr/pgmspace.h>
// encrypt multiple blocks of 128bit data, data_len but be mod 16
// key and iv are assumed to be both 128bit thus 16 uint8_t's
void aes128_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t ctx;
uint8_t r;
r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
if (r) {
bcal_cbc_free(&ctx);
return;
}
bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
bcal_cbc_free(&ctx);
}
// encrypt multiple blocks of 128bit data, data_len but be mod 16
// key and iv are assumed to be both 192bit thus 24 uint8_t's
void aes192_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t ctx;
uint8_t r;
r = bcal_cbc_init(&aes192_desc, key, 192, &ctx);
if (r) {
bcal_cbc_free(&ctx);
return;
}
bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
bcal_cbc_free(&ctx);
}
// encrypt single 128bit block. data is assumed to be 16 uint8_t's
// key and iv are assumed to be both 128bit thus 16 uint8_t's
void aes128_enc_single(const uint8_t* key, void* data){
aes128_ctx_t ctx;
aes128_init(key, &ctx);
aes128_enc(data, &ctx);
}
// encrypt multiple blocks of 128bit data, data_len must be mod 16
// key is assumed to be 128bit thus 16 uint8_t's
void aes128_enc_multiple(const uint8_t* key, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
aes128_ctx_t ctx;
aes128_init(key, &ctx);
uint8_t* current = (uint8_t*)data;
uint8_t* stop = current + data_len;
while(current != stop){
aes128_enc(current, &ctx);
current += 16;
}
}
// encrypt single 128bit block. data is assumed to be 16 uint8_t's
// key is assumed to be 256bit thus 32 uint8_t's
void aes256_enc_single(const uint8_t* key, void* data){
aes256_ctx_t ctx;
aes256_init(key, &ctx);
aes256_enc(data, &ctx);
}
// encrypt multiple blocks of 128bit data, data_len but be mod 16
// key is assumed to be 256bit thus 32 uint8_t's
void aes256_enc_multiple(const uint8_t* key, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
aes256_ctx_t ctx;
aes256_init(key, &ctx);
uint8_t* current = (uint8_t*)data;
uint8_t* stop = current + data_len;
while(current != stop){
aes256_enc(current, &ctx);
current += 16;
}
}
// prepare an encrypted to use for encrypting multiple blocks lateron.
// key and iv are assumed to be both 128bit thus 16 uint8_t's
aes_context aes128_cbc_enc_start(const uint8_t* key, const void* iv){
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
if (r) {
free(ctx);
return NULL;
}
bcal_cbc_loadIV(iv, ctx);
return (aes_context)ctx;
}
// prepare an encrypted to use for encrypting multiple blocks lateron.
// key and iv are assumed to be both 192bit thus 24 uint8_t's
aes_context aes192_cbc_enc_start(const uint8_t* key, const void* iv){
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
uint8_t r = bcal_cbc_init(&aes192_desc, key, 192, ctx);
if (r) {
free(ctx);
return NULL;
}
bcal_cbc_loadIV(iv, ctx);
return (aes_context)ctx;
}
// encrypt one or more blocks of 128bit data
// data_len should be mod 16
void aes128_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
uint16_t msg_blocks = data_len / 16;
while(msg_blocks--){
bcal_cbc_encNext(data, _ctx);
data = (uint8_t*)data + _ctx->blocksize_B;
}
}
// encrypt one or more blocks of 128bit data
// data_len should be mod 16
void aes192_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
uint16_t msg_blocks = data_len / 16;
while(msg_blocks--){
bcal_cbc_encNext(data, _ctx);
data = (uint8_t*)data + _ctx->blocksize_B;
}
}
// cleanup encryption context
void aes128_cbc_enc_finish(const aes_context ctx){
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
free(ctx);
}
// cleanup encryption context
void aes192_cbc_enc_finish(const aes_context ctx){
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
free(ctx);
}
// decrypt multiple blocks of 128bit data, data_len but be mod 16
// key and iv are assumed to be both 128bit thus 16 uint8_t's
void aes128_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t ctx;
uint8_t r;
r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
if (r) {
return;
}
bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
bcal_cbc_free(&ctx);
}
// decrypt multiple blocks of 128bit data, data_len but be mod 16
// key and iv are assumed to be both 192bit thus 24 uint8_t's
void aes192_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t ctx;
uint8_t r;
r = bcal_cbc_init(&aes192_desc, key, 192, &ctx);
if (r) {
return;
}
bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
bcal_cbc_free(&ctx);
}
// decrypt single 128bit block. data is assumed to be 16 uint8_t's
// key is assumed to be 128bit thus 16 uint8_t's
void aes128_dec_single(const uint8_t* key, void* data){
aes128_ctx_t ctx;
aes128_init(key, &ctx);
aes128_dec(data, &ctx);
}
// decrypt multiple blocks of 128bit data, data_len must be mod 16
// key is assumed to be 128bit thus 16 uint8_t's
void aes128_dec_multiple(const uint8_t* key, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
aes128_ctx_t ctx;
aes128_init(key, &ctx);
uint8_t* current = (uint8_t*)data;
uint8_t* stop = current + data_len;
while(current != stop) {
aes128_dec(current, &ctx);
current += 16;
}
}
// decrypt single 128bit block. data is assumed to be 16 uint8_t's
// key is assumed to be 256bit thus 32 uint8_t's
void aes256_dec_single(const uint8_t* key, void* data){
aes256_ctx_t ctx;
aes256_init(key, &ctx);
aes256_dec(data, &ctx);
}
// decrypt multiple blocks of 128bit data, data_len but be mod 16
// key is assumed to be 256bit thus 32 uint8_t's
void aes256_dec_multiple(const uint8_t* key, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
aes256_ctx_t ctx;
aes256_init(key, &ctx);
uint8_t* current = (uint8_t*)data;
uint8_t* stop = current + data_len;
while(current != stop) {
aes256_dec(current, &ctx);
current += 16;
}
}
// prepare an decrypted to use for decrypting multiple blocks lateron.
// key and iv are assumed to be both 128bit thus 16 uint8_t's
aes_context aes128_cbc_dec_start(const uint8_t* key, const void* iv){
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
if (r) {
free(ctx);
return NULL;
}
bcal_cbc_loadIV(iv, ctx);
return (aes_context)ctx;
}
// prepare an decrypted to use for decrypting multiple blocks lateron.
// key and iv are assumed to be both 192bit thus 24 uint8_t's
aes_context aes192_cbc_dec_start(const uint8_t* key, const void* iv){
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
uint8_t r = bcal_cbc_init(&aes192_desc, key, 192, ctx);
if (r) {
free(ctx);
return NULL;
}
bcal_cbc_loadIV(iv, ctx);
return (aes_context)ctx;
}
// decrypt one or more blocks of 128bit data
// data_len should be mod 16
void aes128_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
uint16_t msg_blocks = data_len / 16;
while(msg_blocks--){
bcal_cbc_decNext(data, _ctx);
data = (uint8_t*)data + _ctx->blocksize_B;
}
}
// decrypt one or more blocks of 128bit data
// data_len should be mod 16
void aes192_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
uint16_t msg_blocks = data_len / 16;
while(msg_blocks--){
bcal_cbc_decNext(data, _ctx);
data = (uint8_t*)data + _ctx->blocksize_B;
}
}
// cleanup decryption context
void aes128_cbc_dec_finish(const aes_context ctx){
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
free(ctx);
}
// cleanup decryption context
void aes192_cbc_dec_finish(const aes_context ctx){
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
free(ctx);
}