]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal/bcal-nessie.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / bcal / bcal-nessie.c
1 /* bcal-nessie.c */
2 /*
3  This file is part of the AVR-Crypto-Lib.
4  Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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 "nessie_bc_test.h"
21 #include "blockcipher_descriptor.h"
22 #include "keysize_descriptor.h"
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <avr/pgmspace.h>
27
28 void (*bcal_nessie_dummy_init_fpt)(const void *key, void *ctx) = NULL;
29
30 void bcal_nessie_dummy_init(const void *key, uint16_t keysize, void *ctx)
31 {
32     if (bcal_nessie_dummy_init_fpt) {
33         bcal_nessie_dummy_init_fpt(key, ctx);
34     } else {
35         memcpy(ctx, key, (keysize + 7) / 8);
36     }
37 }
38
39 void bcal_nessie(const bcdesc_t *bcd)
40 {
41     if (pgm_read_byte(&(bcd->type)) != BCDESC_TYPE_BLOCKCIPHER)
42         return;
43     char name[1 + strlen_P((void*) pgm_read_word(&(bcd->name)))];
44     strcpy_P(name, (void*) pgm_read_word(&(bcd->name)));
45     nessie_bc_init();
46
47     nessie_bc_ctx.blocksize_B = (pgm_read_word(&(bcd->blocksize_b)) + 7) / 8;
48     nessie_bc_ctx.name = name;
49     nessie_bc_ctx.ctx_size_B = pgm_read_word(&(bcd->ctxsize_B));
50     nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt) pgm_read_word(&(bcd->enc));
51     nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt) pgm_read_word(&(bcd->dec));
52     nessie_bc_ctx.cipher_free =
53             (nessie_bc_free_fpt) pgm_read_word(&(bcd->free));
54     if ((pgm_read_byte(&(bcd->flags)) & BC_INIT_TYPE) == BC_INIT_TYPE_2) {
55         nessie_bc_ctx.cipher_genctx =
56                 (nessie_bc_gen_fpt) pgm_read_word(&(bcd->init));
57     } else {
58         bcal_nessie_dummy_init_fpt =
59                 (void (*)(const void*, void*)) pgm_read_word(&(bcd->init));
60         nessie_bc_ctx.cipher_genctx =
61                 (nessie_bc_gen_fpt) bcal_nessie_dummy_init;
62     }
63
64     uint16_t *keysize_list = NULL;
65     uint16_t items, i;
66     items =
67             get_keysizes((PGM_VOID_P) pgm_read_word(&(bcd->valid_keysize_desc)), &keysize_list);
68     if (items) {
69         for (i = 0; i < items; ++i) {
70             nessie_bc_ctx.keysize_b = keysize_list[i];
71             nessie_bc_run();
72         }
73         free(keysize_list);
74     }
75
76 }
77
78 void bcal_nessie_multiple(const bcdesc_t * const *bcd_list)
79 {
80     const bcdesc_t *bcd;
81     for (;;) {
82         bcd = (void*) pgm_read_word(bcd_list);
83         if (!bcd)
84             return;
85         bcal_nessie(bcd);
86         bcd_list = (void*) ((uint8_t*) bcd_list + 2);
87     }
88 }
89