2 This file is part of the aeslib.
3 Copyright (C) 2012 Davy Landman (davy.landman@gmail.com)
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "blockcipher_descriptor.h"
22 #include "bcal_aes128.h"
23 #include "bcal_aes192.h"
26 #include <avr\pgmspace.h>
31 // encrypt multiple blocks of 128bit data, data_len but be mod 16
32 // key and iv are assumed to be both 128bit thus 16 uint8_t's
33 void aes128_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
34 if (data_len % 16 != 0) {
39 r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
43 bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
47 // encrypt multiple blocks of 128bit data, data_len but be mod 16
48 // key and iv are assumed to be both 192bit thus 24 uint8_t's
49 void aes192_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
50 if (data_len % 16 != 0) {
55 r = bcal_cbc_init(&aes192_desc, key, 192, &ctx);
59 bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
63 // encrypt single 128bit block. data is assumed to be 16 uint8_t's
64 // key and iv are assumed to be both 128bit thus 16 uint8_t's
65 void aes128_enc_single(const uint8_t* key, void* data){
67 aes128_init(key, &ctx);
68 aes128_enc(data, &ctx);
71 // encrypt single 128bit block. data is assumed to be 16 uint8_t's
72 // key is assumed to be 256bit thus 32 uint8_t's
73 void aes256_enc_single(const uint8_t* key, void* data){
75 aes256_init(key, &ctx);
76 aes256_enc(data, &ctx);
80 // prepare an encrypted to use for encrypting multiple blocks lateron.
81 // key and iv are assumed to be both 128bit thus 16 uint8_t's
82 aes_context aes128_cbc_enc_start(const uint8_t* key, const void* iv){
83 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
84 uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
89 bcal_cbc_loadIV(iv, ctx);
90 return (aes_context)ctx;
93 // prepare an encrypted to use for encrypting multiple blocks lateron.
94 // key and iv are assumed to be both 192bit thus 24 uint8_t's
95 aes_context aes192_cbc_enc_start(const uint8_t* key, const void* iv){
96 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
97 uint8_t r = bcal_cbc_init(&aes192_desc, key, 192, ctx);
102 bcal_cbc_loadIV(iv, ctx);
103 return (aes_context)ctx;
106 // encrypt one or more blocks of 128bit data
107 // data_len should be mod 16
108 void aes128_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t data_len){
109 if (data_len % 16 != 0) {
112 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
113 uint16_t msg_blocks = data_len / 16;
115 bcal_cbc_encNext(data, _ctx);
116 data = (uint8_t*)data + _ctx->blocksize_B;
120 // encrypt one or more blocks of 128bit data
121 // data_len should be mod 16
122 void aes192_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t data_len){
123 if (data_len % 16 != 0) {
126 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
127 uint16_t msg_blocks = data_len / 16;
129 bcal_cbc_encNext(data, _ctx);
130 data = (uint8_t*)data + _ctx->blocksize_B;
134 // cleanup encryption context
135 void aes128_cbc_enc_finish(const aes_context ctx){
136 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
140 // cleanup encryption context
141 void aes192_cbc_enc_finish(const aes_context ctx){
142 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
146 // decrypt multiple blocks of 128bit data, data_len but be mod 16
147 // key and iv are assumed to be both 128bit thus 16 uint8_t's
148 void aes128_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
149 if (data_len % 16 != 0) {
154 r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
158 bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
162 // decrypt multiple blocks of 128bit data, data_len but be mod 16
163 // key and iv are assumed to be both 192bit thus 24 uint8_t's
164 void aes192_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
165 if (data_len % 16 != 0) {
170 r = bcal_cbc_init(&aes192_desc, key, 192, &ctx);
174 bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
178 // decrypt single 128bit block. data is assumed to be 16 uint8_t's
179 // key is assumed to be 128bit thus 16 uint8_t's
180 void aes128_dec_single(const uint8_t* key, void* data){
182 aes128_init(key, &ctx);
183 aes128_dec(data, &ctx);
186 // decrypt single 128bit block. data is assumed to be 16 uint8_t's
187 // key is assumed to be 256bit thus 32 uint8_t's
188 void aes256_dec_single(const uint8_t* key, void* data){
190 aes256_init(key, &ctx);
191 aes256_dec(data, &ctx);
195 // prepare an decrypted to use for decrypting multiple blocks lateron.
196 // key and iv are assumed to be both 128bit thus 16 uint8_t's
197 aes_context aes128_cbc_dec_start(const uint8_t* key, const void* iv){
198 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
199 uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
204 bcal_cbc_loadIV(iv, ctx);
205 return (aes_context)ctx;
208 // prepare an decrypted to use for decrypting multiple blocks lateron.
209 // key and iv are assumed to be both 192bit thus 24 uint8_t's
210 aes_context aes192_cbc_dec_start(const uint8_t* key, const void* iv){
211 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
212 uint8_t r = bcal_cbc_init(&aes192_desc, key, 192, ctx);
217 bcal_cbc_loadIV(iv, ctx);
218 return (aes_context)ctx;
221 // decrypt one or more blocks of 128bit data
222 // data_len should be mod 16
223 void aes128_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t data_len){
224 if (data_len % 16 != 0) {
227 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
228 uint16_t msg_blocks = data_len / 16;
230 bcal_cbc_decNext(data, _ctx);
231 data = (uint8_t*)data + _ctx->blocksize_B;
235 // decrypt one or more blocks of 128bit data
236 // data_len should be mod 16
237 void aes192_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t data_len){
238 if (data_len % 16 != 0) {
241 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
242 uint16_t msg_blocks = data_len / 16;
244 bcal_cbc_decNext(data, _ctx);
245 data = (uint8_t*)data + _ctx->blocksize_B;
249 // cleanup decryption context
250 void aes128_cbc_dec_finish(const aes_context ctx){
251 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
255 // cleanup decryption context
256 void aes192_cbc_dec_finish(const aes_context ctx){
257 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);