|
18 | 18 | #include "aeslib.h"
|
19 | 19 | #include <stdint.h>
|
20 | 20 | #include "aes.h"
|
| 21 | +#include "blockcipher_descriptor.h" |
21 | 22 | #include "bcal_aes128.h"
|
22 | 23 | #include "bcal-cbc.h"
|
| 24 | +#include <avr/pgmspace.h> |
23 | 25 |
|
| 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 | +} |
24 | 49 |
|
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 | +} |
26 | 76 |
|
| 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); |
27 | 81 | }
|
0 commit comments