]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal/bcal-basic.c
52734deec94a19ae3107264a4214cbde94760307
[avr-crypto-lib.git] / bcal / bcal-basic.c
1 /* bcal-basic.c */
2 /*
3  This file is part of the AVR-Crypto-Lib.
4  Copyright (C) 2009  Daniel Otte (daniel.otte@rub.de)
5
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <avr/pgmspace.h>
24 #include "blockcipher_descriptor.h"
25 #include "keysize_descriptor.h"
26
27 uint8_t bcal_cipher_init(const bcdesc_t *cipher_descriptor,
28         const void *key, uint16_t keysize_b, bcgen_ctx_t *ctx)
29 {
30     if (!is_valid_keysize_P((PGM_VOID_P) pgm_read_word(&(cipher_descriptor->valid_keysize_desc)),
31             keysize_b)) {
32         return 1;
33     }
34     uint8_t flags;
35     bc_init_fpt init_fpt;
36     ctx->desc_ptr = (bcdesc_t*) cipher_descriptor;
37     ctx->keysize = keysize_b;
38     flags = pgm_read_byte(cipher_descriptor->flags);
39     init_fpt.initvoid =
40             (void_fpt) (pgm_read_word(&(cipher_descriptor->init.initvoid)));
41     if (init_fpt.initvoid == NULL) {
42         if (!(ctx->ctx = malloc((keysize_b + 7) / 8)))
43             return 2;
44         memcpy(ctx->ctx, key, (keysize_b + 7) / 8);
45         return 0;
46     }
47     if (!(ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B)))))
48         return 3;
49     if ((flags & BC_INIT_TYPE) == BC_INIT_TYPE_1) {
50         init_fpt.init1((void*) key, (ctx->ctx));
51     } else {
52         init_fpt.init2((void*) key, keysize_b, (ctx->ctx));
53     }
54     return 0;
55 }
56
57 void bcal_cipher_free(bcgen_ctx_t *ctx)
58 {
59     if (!ctx)
60         return;
61     bc_free_fpt free_fpt;
62     free_fpt = (bc_free_fpt) (pgm_read_word(&(ctx->desc_ptr->free)));
63     if (free_fpt)
64         free_fpt((ctx->ctx));
65     free(ctx->ctx);
66 }
67
68 void bcal_cipher_enc(void *block, const bcgen_ctx_t *ctx)
69 {
70     bc_enc_fpt enc_fpt;
71     enc_fpt.encvoid = (void_fpt) pgm_read_word(&(ctx->desc_ptr->enc.encvoid));
72     if (!enc_fpt.encvoid) {
73         /* very bad error, no enciphering function specified */
74         return;
75     }
76     enc_fpt.enc1(block, (ctx->ctx));
77
78 }
79
80 void bcal_cipher_dec(void *block, const bcgen_ctx_t *ctx)
81 {
82     bc_dec_fpt dec_fpt;
83     dec_fpt.decvoid = (void_fpt) pgm_read_word(&(ctx->desc_ptr->dec.decvoid));
84     if (!dec_fpt.decvoid) {
85         /* very bad error, no deciphering function specified */
86         return;
87     }
88     dec_fpt.dec1(block, (ctx->ctx));
89 }
90
91 uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t *desc)
92 {
93     return pgm_read_word(&(desc->blocksize_b));
94 }
95
96 PGM_VOID_P bcal_cipher_getKeysizeDesc(const bcdesc_t *desc) {
97     return (PGM_VOID_P)pgm_read_word(&(desc->valid_keysize_desc));
98 }
99