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"
25 #include <avr/pgmspace.h>
27 // encrypt multiple blocks of 128bit data, data_len but be mod 16
28 // key and iv are assumed to be both 128bit thus 16 uint8_t's
29 void aes128_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
30 if (data_len % 16 != 0) {
35 r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
39 bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
43 // encrypt multiple blocks of 128bit data, data_len but be mod 16
44 // key and iv are assumed to be both 192bit thus 24 uint8_t's
45 void aes192_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
46 if (data_len % 16 != 0) {
51 r = bcal_cbc_init(&aes192_desc, key, 192, &ctx);
55 bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);
59 // encrypt single 128bit block. data is assumed to be 16 uint8_t's
60 // key and iv are assumed to be both 128bit thus 16 uint8_t's
61 void aes128_enc_single(const uint8_t* key, void* data){
63 aes128_init(key, &ctx);
64 aes128_enc(data, &ctx);
67 // encrypt single 128bit block. data is assumed to be 16 uint8_t's
68 // key is assumed to be 256bit thus 32 uint8_t's
69 void aes256_enc_single(const uint8_t* key, void* data){
71 aes256_init(key, &ctx);
72 aes256_enc(data, &ctx);
76 // prepare an encrypted to use for encrypting multiple blocks lateron.
77 // key and iv are assumed to be both 128bit thus 16 uint8_t's
78 aes_context aes128_cbc_enc_start(const uint8_t* key, const void* iv){
79 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
80 uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
85 bcal_cbc_loadIV(iv, ctx);
86 return (aes_context)ctx;
89 // prepare an encrypted to use for encrypting multiple blocks lateron.
90 // key and iv are assumed to be both 192bit thus 24 uint8_t's
91 aes_context aes192_cbc_enc_start(const uint8_t* key, const void* iv){
92 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
93 uint8_t r = bcal_cbc_init(&aes192_desc, key, 192, ctx);
98 bcal_cbc_loadIV(iv, ctx);
99 return (aes_context)ctx;
102 // encrypt one or more blocks of 128bit data
103 // data_len should be mod 16
104 void aes128_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t data_len){
105 if (data_len % 16 != 0) {
108 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
109 uint16_t msg_blocks = data_len / 16;
111 bcal_cbc_encNext(data, _ctx);
112 data = (uint8_t*)data + _ctx->blocksize_B;
116 // encrypt one or more blocks of 128bit data
117 // data_len should be mod 16
118 void aes192_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t data_len){
119 if (data_len % 16 != 0) {
122 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
123 uint16_t msg_blocks = data_len / 16;
125 bcal_cbc_encNext(data, _ctx);
126 data = (uint8_t*)data + _ctx->blocksize_B;
130 // cleanup encryption context
131 void aes128_cbc_enc_finish(const aes_context ctx){
132 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
136 // cleanup encryption context
137 void aes192_cbc_enc_finish(const aes_context ctx){
138 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
142 // decrypt multiple blocks of 128bit data, data_len but be mod 16
143 // key and iv are assumed to be both 128bit thus 16 uint8_t's
144 void aes128_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
145 if (data_len % 16 != 0) {
150 r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
154 bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
158 // decrypt multiple blocks of 128bit data, data_len but be mod 16
159 // key and iv are assumed to be both 192bit thus 24 uint8_t's
160 void aes192_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const uint16_t data_len){
161 if (data_len % 16 != 0) {
166 r = bcal_cbc_init(&aes192_desc, key, 192, &ctx);
170 bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
174 // decrypt single 128bit block. data is assumed to be 16 uint8_t's
175 // key is assumed to be 128bit thus 16 uint8_t's
176 void aes128_dec_single(const uint8_t* key, void* data){
178 aes128_init(key, &ctx);
179 aes128_dec(data, &ctx);
182 // decrypt single 128bit block. data is assumed to be 16 uint8_t's
183 // key is assumed to be 256bit thus 32 uint8_t's
184 void aes256_dec_single(const uint8_t* key, void* data){
186 aes256_init(key, &ctx);
187 aes256_dec(data, &ctx);
191 // prepare an decrypted to use for decrypting multiple blocks lateron.
192 // key and iv are assumed to be both 128bit thus 16 uint8_t's
193 aes_context aes128_cbc_dec_start(const uint8_t* key, const void* iv){
194 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
195 uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
200 bcal_cbc_loadIV(iv, ctx);
201 return (aes_context)ctx;
204 // prepare an decrypted to use for decrypting multiple blocks lateron.
205 // key and iv are assumed to be both 192bit thus 24 uint8_t's
206 aes_context aes192_cbc_dec_start(const uint8_t* key, const void* iv){
207 bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
208 uint8_t r = bcal_cbc_init(&aes192_desc, key, 192, ctx);
213 bcal_cbc_loadIV(iv, ctx);
214 return (aes_context)ctx;
217 // decrypt one or more blocks of 128bit data
218 // data_len should be mod 16
219 void aes128_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t data_len){
220 if (data_len % 16 != 0) {
223 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
224 uint16_t msg_blocks = data_len / 16;
226 bcal_cbc_decNext(data, _ctx);
227 data = (uint8_t*)data + _ctx->blocksize_B;
231 // decrypt one or more blocks of 128bit data
232 // data_len should be mod 16
233 void aes192_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t data_len){
234 if (data_len % 16 != 0) {
237 bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
238 uint16_t msg_blocks = data_len / 16;
240 bcal_cbc_decNext(data, _ctx);
241 data = (uint8_t*)data + _ctx->blocksize_B;
245 // cleanup decryption context
246 void aes128_cbc_dec_finish(const aes_context ctx){
247 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
251 // cleanup decryption context
252 void aes192_cbc_dec_finish(const aes_context ctx){
253 bcal_cbc_free((bcal_cbc_ctx_t*)ctx);