first lines of a blockcipher abstraction layer

This commit is contained in:
bg 2009-01-08 12:15:12 +00:00
parent 3caccd2034
commit 017345097a
7 changed files with 376 additions and 0 deletions

98
bcal-basic.c Normal file
View File

@ -0,0 +1,98 @@
/* bcal-basic.c */
/*
This file is part of the Crypto-avr-lib/microcrypt-lib.
Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "blockcipher_descriptor.h"
#include "keysize_descriptor.h"
uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
const void* key, uint16_t keysize, bcgen_ctx_t* ctx){
if(!is_valid_keysize_P((PGM_VOID_P)(pgm_read_word(cipher_descriptor->valid_keysize_desc)),
keysize))
return 1;
uint8_t flags;
bc_init_fpt init_fpt;
ctx->desc_ptr = (bcdesc_t*)cipher_descriptor;
ctx->keysize = keysize;
flags = pgm_read_byte(cipher_descriptor->flags);
init_fpt.initvoid = (void_fpt)(pgm_read_word(cipher_descriptor->init.initvoid));
if(init_fpt.initvoid == NULL){
if(!(ctx->ctx = malloc(keysize/8)))
return 2;
memcpy(ctx->ctx, key, keysize/8);
return 0;
}
if(!(ctx->ctx = malloc(pgm_read_word(cipher_descriptor->ctxsize_B))))
return 3;
if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
init_fpt.init1((void*)key, ctx->ctx);
}else{
init_fpt.init2((void*)key, keysize, ctx->ctx);
}
return 0;
}
void bcal_cipher_free(bcgen_ctx_t* ctx){
if(!ctx)
return;
bc_free_fpt free_fpt;
free_fpt = (bc_free_fpt)(pgm_read_word(ctx->desc_ptr->free));
if(free_fpt)
free_fpt(ctx->ctx);
free(ctx->ctx);
}
void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){
uint8_t flags;
bc_enc_fpt enc_fpt;
flags = pgm_read_byte(ctx->desc_ptr->flags);
enc_fpt.encvoid = (void_fpt)pgm_read_word(ctx->desc_ptr->enc.encvoid);
if(!enc_fpt.encvoid){
/* very bad error, no enciphering function specified */
return;
}
if((flags&BC_ENC_TYPE)==BC_ENC_TYPE_1){
enc_fpt.enc1(block, ctx->ctx);
}else{
enc_fpt.enc2(block, block, ctx->ctx);
}
}
void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){
uint8_t flags;
bc_dec_fpt dec_fpt;
flags = pgm_read_byte(ctx->desc_ptr->flags);
dec_fpt.decvoid = (void_fpt)pgm_read_word(ctx->desc_ptr->dec.decvoid);
if(!dec_fpt.decvoid){
/* very bad error, no deciphering function specified */
return;
}
if((flags&BC_DEC_TYPE)==BC_DEC_TYPE_1){
dec_fpt.dec1(block, ctx->ctx);
}else{
dec_fpt.dec2(block, block, ctx->ctx);
}
}

25
bcal-basic.h Normal file
View File

@ -0,0 +1,25 @@
/* bcal-basic.h */
/*
This file is part of the Crypto-avr-lib/microcrypt-lib.
Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdint.h>
#include "blockciper_descriptor.h"
#include "keysize_descriptor.h"

41
bcal_noekeon.c Normal file
View File

@ -0,0 +1,41 @@
/* bcal_noekeon.c */
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "blockcipher_descriptor.h"
#include "noekeon.h"
#include "keysize_descriptor.h"
const char noekeon_direct_str[] PROGMEM = "Noekeon-Direct";
const char noekeon_indirect_str[] PROGMEM = "Noekeon-Indirect";
const uint8_t noekeon_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, 128,
KS_TYPE_TERMINATOR };
const bcdesc_t noekeon_direct_desc PROGMEM = {
BCDESC_TYPE_BLOCKCIPHER,
BC_ENC_TYPE_1,
noekeon_direct_str,
16,
128,
(void_fpt)NULL,
(void_fpt)noekeon_enc,
(void_fpt)noekeon_dec,
(void_fpt)NULL,
noekeon_keysize_desc
};
const bcdesc_t noekeon_indirect_desc PROGMEM = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_1 | BC_ENC_TYPE_1,
noekeon_indirect_str,
16,
128,
(void_fpt)noekeon_init,
(void_fpt)noekeon_enc,
(void_fpt)noekeon_dec,
(void_fpt)NULL,
noekeon_keysize_desc
};

10
bcal_noekeon.h Normal file
View File

@ -0,0 +1,10 @@
/* bcal_noekeon.h */
#include <avr/pgmspace.h>
#include "blopckcipher_descriptor.h"
#include "noekeon.h"
#include "keysize_descriptor.h"
extern const bcdesc_t noekeon_direct_desc;
extern const bcdesc_t noekeon_indirect_desc;

69
blockcipher_descriptor.h Normal file
View File

@ -0,0 +1,69 @@
/* blockcipher_descriptor.h */
#ifndef BLOCKCIPHER_DESCRIPTOR_H_
#define BLOCKCIPHER_DESCRIPTOR_H_
#include <stdint.h>
#include <avr/pgmspace.h>
typedef void(*void_fpt)(void);
typedef void(*bc_init1_fpt)(void*, void*);
typedef void(*bc_init2_fpt)(void*, uint16_t,void*);
typedef void(*bc_enc1_fpt)(void*, void*);
typedef void(*bc_enc2_fpt)(void*, void*, void*);
typedef void(*bc_dec1_fpt)(void*, void*);
typedef void(*bc_dec2_fpt)(void*, void*, void*);
typedef void(*bc_free_fpt)(void*);
typedef union{
void_fpt initvoid;
bc_init1_fpt init1;
bc_init2_fpt init2;
} bc_init_fpt;
typedef union{
void_fpt encvoid;
bc_enc1_fpt enc1;
bc_enc2_fpt enc2;
} bc_enc_fpt;
typedef union{
void_fpt decvoid;
bc_dec1_fpt dec1;
bc_dec2_fpt dec2;
} bc_dec_fpt;
#define BC_INIT_TYPE 0x01
#define BC_INIT_TYPE_1 0x00
#define BC_INIT_TYPE_2 0x01
#define BC_ENC_TYPE 0x02
#define BC_ENC_TYPE_1 0x00
#define BC_ENC_TYPE_2 0x02
#
#define BC_DEC_TYPE 0x04
#define BC_DEC_TYPE_1 0x00
#define BC_DEC_TYPE_2 0x04
#define BCDESC_TYPE_BLOCKCIPHER 0x01
typedef struct {
uint8_t type; /* 1==blockcipher */
uint8_t flags;
PGM_P name;
uint16_t ctxsize_B;
uint16_t blocksize_b;
bc_init_fpt init;
bc_enc_fpt enc;
bc_dec_fpt dec;
bc_free_fpt free;
PGM_VOID_P valid_keysize_desc;
} bcdesc_t; /* blockcipher descriptor type */
typedef struct{
bcdesc_t* desc_ptr;
uint16_t keysize;
void* ctx;
} bcgen_ctx_t;
#endif /* BLOCKCIPHER_DESCRIPTOR_H_ */

75
keysize_descriptor.c Normal file
View File

@ -0,0 +1,75 @@
/* keysize_descriptor.c */
/*
This file is part of the Crypto-avr-lib/microcrypt-lib.
Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file keysize_descriptor.c
* \author Daniel Otte
* \email daniel.otte@rub.de
* \date 2009-01-07
* \license GPLv3 or later
*/
#include <stdint.h>
#include <avr/pgmspace.h>
#include "keysize_descriptor.h"
uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){
uint8_t type;
type = pgm_read_byte(ks_desc++);
if(type==KS_TYPE_TERMINATOR)
return 0;
if(type==KS_TYPE_LIST){
uint8_t items;
uint16_t item;
items = pgm_read_byte(ks_desc++);
while(items--){
item = pgm_read_word(ks_desc);
ks_desc+=2;
if(item==keysize)
return 1;
}
ks_desc -= 2;
}
if(type==KS_TYPE_RANGE){
uint16_t max, min;
min = pgm_read_word(ks_desc);
ks_desc+=2;
max = pgm_read_word(ks_desc);
if(min<=keysize && keysize<=max)
return 1;
}
if(type==KS_TYPE_ARG_RANGE){
uint16_t max, min, dist, offset;
min = pgm_read_word(ks_desc);
ks_desc+=2;
max = pgm_read_word(ks_desc);
ks_desc+=2;
dist = pgm_read_word(ks_desc);
ks_desc+=2;
offset = pgm_read_word(ks_desc);
if(min<=keysize && keysize<=max && (keysize%dist==offset))
return 1;
}
if(type>KS_TYPE_ARG_RANGE){
/* bad error, you may insert a big warning message here */
return 0;
}
return is_valid_keysize(ks_desc+1, keysize) /* search the next record */
}

58
keysize_descriptor.h Normal file
View File

@ -0,0 +1,58 @@
/* keysize_descriptor.h */
/*
This file is part of the Crypto-avr-lib/microcrypt-lib.
Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file keysize_descriptor.h
* \author Daniel Otte
* \email daniel.otte@rub.de
* \date 2009-01-07
* \license GPLv3 or later
*/
#ifndef KEYSIZE_DESCRIPTOR_H_
#define KEYSIZE_DESCRIPTOR_H_
#include <stdint.h>
#include <avr/pgmspace.h>
#define KS_TYPE_TERMINATOR 0x00
#define KS_TYPE_LIST 0x01
#define KS_TYPE_RANGE 0x02
#define KS_TYPE_ARG_RANGE 0x03
typedef struct{ /* keysize is valid if listed in items */
uint8_t n_items; /* number of items (value 0 is reserved) */
uint16_t items[]; /* list of valid lengths */
}keysize_desc_list_t;
typedef struct{ /* keysize is valid if min<=keysize<=max */
uint16_t min;
uint16_t max;
}keysize_desc_range_t;
typedef struct{ /* keysize is valid if min<=keysize<=max and if keysize mod distance == offset */
uint16_t min;
uint16_t max;
uint16_t distance;
uint16_t offset;
}keysize_desc_arg_range_t;
uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize);
#endif /* KEYSIZE_DESCRIPTOR_H_ */