Permalink
Browse files

fixed 192 bit cbc functions

1 parent 7089d95 commit b00f1f83984171e1ce6ac28c50175e0a65e58232 yids committed Dec 7, 2015
Showing with 129 additions and 1 deletion.
  1. +98 −1 AESLib.c
  2. +31 −0 AESLib.h
View
@@ -20,6 +20,7 @@
#include "aes.h"
#include "blockcipher_descriptor.h"
#include "bcal_aes128.h"
+#include "bcal_aes192.h"
#include "bcal-cbc.h"
#include <avr/pgmspace.h>
@@ -39,6 +40,22 @@ void aes128_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uin
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) {
+ 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){
@@ -69,6 +86,19 @@ aes_context aes128_cbc_enc_start(const uint8_t* key, const void* iv){
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){
@@ -83,13 +113,31 @@ void aes128_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t d
}
}
+// 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
@@ -107,6 +155,22 @@ void aes128_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uin
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){
@@ -137,6 +201,19 @@ aes_context aes128_cbc_dec_start(const uint8_t* key, const void* iv){
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){
@@ -151,8 +228,28 @@ void aes128_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t d
}
}
+// 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);
+}
View
@@ -25,6 +25,10 @@ extern "C"{
// 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);
+// 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);
+
// encrypt 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_enc_single(const uint8_t* key, void* data);
@@ -39,17 +43,32 @@ typedef void* aes_context;
// 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);
+// 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);
+
// 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);
+// 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);
+
// cleanup encryption context
void aes128_cbc_enc_finish(const aes_context ctx);
+// cleanup encryption context
+void aes192_cbc_enc_finish(const aes_context 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);
+// 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);
+
// 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);
@@ -62,12 +81,24 @@ void aes256_dec_single(const uint8_t* key, void* data);
// 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);
+// prepare an decrypter 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);
+
// 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);
+// 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);
+
// cleanup decryption context
void aes128_cbc_dec_finish(const aes_context ctx);
+
+// cleanup decryption context
+void aes192_cbc_dec_finish(const aes_context ctx);
+
#ifdef __cplusplus
}
#endif

0 comments on commit b00f1f8

Please sign in to comment.