Skip to content

Commit 1760eda

Browse files
committed
Added aes128 decryption
1 parent f4f9170 commit 1760eda

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

aeslib.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,62 @@ void aes128_cbc_enc_finish(aes_context ctx){
7979
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
8080
free(ctx);
8181
}
82+
83+
84+
85+
// decrypt multiple blocks of 128bit data, data_len but be mod 16
86+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
87+
void aes128_cbc_dec(uint8_t* key, uint8_t* iv, void* data, uint16_t data_len){
88+
if (data_len % 16 != 0) {
89+
return;
90+
}
91+
bcal_cbc_ctx_t ctx;
92+
uint8_t r;
93+
r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
94+
if (r) {
95+
return;
96+
}
97+
bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
98+
bcal_cbc_free(&ctx);
99+
}
100+
101+
// decrypt single 128bit block. data is assumed to be 16 uint8_t's
102+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
103+
void aes128_dec_single(uint8_t* key, void* data){
104+
aes128_ctx_t ctx;
105+
aes128_init(key, &ctx);
106+
aes128_dec(data, &ctx);
107+
}
108+
109+
// prepare an decrypted to use for decrypting multiple blocks lateron.
110+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
111+
aes_context aes128_cbc_dec_start(uint8_t* key, void* iv){
112+
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
113+
uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
114+
if (r) {
115+
free(ctx);
116+
return NULL;
117+
}
118+
bcal_cbc_loadIV(iv, ctx);
119+
return (aes_context)ctx;
120+
}
121+
122+
// decrypt one or more blocks of 128bit data
123+
// data_len should be mod 16
124+
void aes128_cbc_dec_continue(aes_context ctx, void* data, uint16_t data_len){
125+
if (data_len % 16 != 0) {
126+
return;
127+
}
128+
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
129+
uint16_t msg_blocks = data_len / 16;
130+
while(msg_blocks--){
131+
bcal_cbc_decNext(data, _ctx);
132+
data = (uint8_t*)data + _ctx->blocksize_B;
133+
}
134+
}
135+
136+
// cleanup decryption context
137+
void aes128_cbc_dec_finish(aes_context ctx){
138+
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
139+
free(ctx);
140+
}

aeslib.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,22 @@ void aes128_cbc_enc_continue(aes_context ctx, void* data, uint16_t data_len);
3939
// cleanup encryption context
4040
void aes128_cbc_enc_finish(aes_context ctx);
4141

42+
// decrypt multiple blocks of 128bit data, data_len but be mod 16
43+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
44+
void aes128_cbc_dec(uint8_t* key, uint8_t* iv, void* data, uint16_t data_len);
45+
46+
// decrypt single 128bit block. data is assumed to be 16 uint8_t's
47+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
48+
void aes128_dec_single(uint8_t* key, void* data);
49+
50+
// prepare an decrypter to use for decrypting multiple blocks lateron.
51+
// key and iv are assumed to be both 128bit thus 16 uint8_t's
52+
aes_context aes128_cbc_dec_start(uint8_t* key, void* iv);
53+
54+
// decrypt one or more blocks of 128bit data
55+
// data_len should be mod 16
56+
void aes128_cbc_dec_continue(aes_context ctx, void* data, uint16_t data_len);
57+
58+
// cleanup decryption context
59+
void aes128_cbc_dec_finish(aes_context ctx);
4260
#endif

0 commit comments

Comments
 (0)