Skip to content

Commit f4f9170

Browse files
committed
Added support for aes 128 encryption
1 parent b990bd3 commit f4f9170

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

aeslib.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,64 @@
1818
#include "aeslib.h"
1919
#include <stdint.h>
2020
#include "aes.h"
21+
#include "blockcipher_descriptor.h"
2122
#include "bcal_aes128.h"
2223
#include "bcal-cbc.h"
24+
#include <avr/pgmspace.h>
2325

26+
// encrypt multiple blocks of 128bit data, data_len but be mod 16
27+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
28+
void aes128_cbc_enc(uint8_t* key, uint8_t* iv, void* data, uint16_t data_len){
29+
if (data_len % 16 != 0) {
30+
return;
31+
}
32+
bcal_cbc_ctx_t ctx;
33+
uint8_t r;
34+
r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
35+
if (r) {
36+
return;
37+
}
38+
bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
39+
bcal_cbc_free(&ctx);
40+
}
41+
42+
// encrypt single 128bit block. data is assumed to be 16 uint8_t's
43+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
44+
void aes128_enc_single(uint8_t* key, void* data){
45+
aes128_ctx_t ctx;
46+
aes128_init(key, &ctx);
47+
aes128_enc(data, &ctx);
48+
}
2449

25-
void aes128_encrypt(uint8_t* key, uint8_t* iv, uint8_t* data, uint32_t data_len) {
50+
// prepare an encrypted to use for encrypting multiple blocks lateron.
51+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
52+
aes_context aes128_cbc_enc_start(uint8_t* key, void* iv){
53+
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
54+
uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
55+
if (r) {
56+
free(ctx);
57+
return NULL;
58+
}
59+
bcal_cbc_loadIV(iv, ctx);
60+
return (aes_context)ctx;
61+
}
62+
63+
// encrypt one or more blocks of 128bit data
64+
// data_len should be mod 16
65+
void aes128_cbc_enc_continue(aes_context ctx, void* data, uint16_t data_len){
66+
if (data_len % 16 != 0) {
67+
return;
68+
}
69+
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
70+
uint16_t msg_blocks = data_len / 16;
71+
while(msg_blocks--){
72+
bcal_cbc_encNext(data, _ctx);
73+
data = (uint8_t*)data + _ctx->blocksize_B;
74+
}
75+
}
2676

77+
// cleanup encryption context
78+
void aes128_cbc_enc_finish(aes_context ctx){
79+
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
80+
free(ctx);
2781
}

aeslib.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818
#ifndef AESLIB_H
1919
#define AESLIB_H
2020
#include <stdint.h>
21-
void aes128_encrypt(uint8_t* key, uint8_t* iv, uint8_t* data, uint32_t data_len);
21+
// encrypt multiple blocks of 128bit data, data_len but be mod 16
22+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
23+
void aes128_cbc_enc(uint8_t* key, uint8_t* iv, void* data, uint16_t data_len);
24+
25+
// encrypt single 128bit block. data is assumed to be 16 uint8_t's
26+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
27+
void aes128_enc_single(uint8_t* key, void* data);
28+
29+
typedef void* aes_context;
30+
31+
// prepare an encrypted to use for encrypting multiple blocks lateron.
32+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
33+
aes_context aes128_cbc_enc_start(uint8_t* key, void* iv);
34+
35+
// encrypt one or more blocks of 128bit data
36+
// data_len should be mod 16
37+
void aes128_cbc_enc_continue(aes_context ctx, void* data, uint16_t data_len);
38+
39+
// cleanup encryption context
40+
void aes128_cbc_enc_finish(aes_context ctx);
2241

2342
#endif

0 commit comments

Comments
 (0)