X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=shabea.c;h=b59e138e23ca13f2569def3ac5f029649d196b3b;hb=7701e318e4e2bac7f84dbf6e368f1501814948fc;hp=f4f823c8015e20ee7d7fb564916739c9221b2367;hpb=79c9a6582ba071646a3062175715f59ebe210603;p=avr-crypto-lib.git diff --git a/shabea.c b/shabea.c index f4f823c..b59e138 100644 --- a/shabea.c +++ b/shabea.c @@ -1,8 +1,29 @@ +/* shabea.c */ +/* + * This file is part of AnonAccess, an access system which can be used + * to open door or doing other things with an anonymity featured + * account managment. + * Copyright (C) 2006, 2007, 2008 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 . + */ + /** * \file shabea.c * \author Daniel Otte * \date 2007-06-07 - * \brief SHABEA - a SHA Based Encrytion Algorithm implementation + * \brief SHABEA - a SHA Based Encryption Algorithm implementation * \par License * GPL * @@ -15,50 +36,56 @@ #include "sha256.h" #include "config.h" -#include "uart.h" -#include "debug.h" -/* - * - */ -static -void memxor(uint8_t * dest, uint8_t * src, uint8_t length){ - while(length--){ - *dest++ ^= *src++; - } -} +#include "memxor.h" + /* - * SHABEA128-16 + * SHABEA256-n */ -#define L ((uint8_t*)block+0) -#define R ((uint8_t*)block+8) -void shabea128(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds){ - int8_t r; /**/ - uint8_t *tb; /**/ + +#define SHABEA_BLOCKSIZE 256 +#define SHABEA_BLOCKSIZEB (SHABEA_BLOCKSIZE/8) +#define SHABEA_HALFSIZEB (SHABEA_BLOCKSIZEB/2) +#define SHABEA_HALFSIZE (SHABEA_BLOCKSIZE/2) + +#define L ((uint8_t*)block+ 0) +#define R ((uint8_t*)block+16) +void shabea256(void * block, void * key, uint16_t keysize_b, uint8_t enc, uint8_t rounds){ + uint8_t r; /**/ + uint8_t tb[SHABEA_HALFSIZEB+2+(keysize_b+7)/8]; /**/ uint16_t kbs; /* bytes used for the key / temporary block */ sha256_hash_t hash; + uint8_t termcond; + int8_t dir; + if(enc){ + r = 0; + termcond = rounds-1; + dir = 1; + } else { + r = rounds-1; + termcond = 0; + dir = -1; + } + kbs = (keysize_b+7)/8; + memcpy(tb+SHABEA_HALFSIZEB+2, key, kbs); /* copy key to temporary block */ + tb[SHABEA_HALFSIZEB+0] = 0; /* set round counter high value to zero */ - r = (enc?0:(rounds-1)); - kbs = keysize/8 + ((keysize&7)?1:0); - tb = malloc(8+2+kbs); - memcpy(tb+8+2, key, kbs); - tb[8+0] = 0; - - for(;r!=(enc?(rounds):-1);enc?r++:r--){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */ - memcpy(tb, R, 8); /* copy right half into tb */ - tb[8+1] = r; - sha256(hash, tb, 64+16+keysize); - if(!(r==(enc?(rounds-1):0))){ + for(;;r+=dir){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */ + memcpy(tb, R, SHABEA_HALFSIZEB); /* copy right half into tb */ + tb[SHABEA_HALFSIZEB+1] = r; + sha256(&hash, tb, SHABEA_HALFSIZE+16+keysize_b); + if(r!=termcond){ /* swap */ - memxor(hash, L, 8); - memcpy(L, R, 8); - memcpy(R, hash, 8); + memxor(hash, L, SHABEA_HALFSIZEB); + memcpy(L, R, SHABEA_HALFSIZEB); + memcpy(R, hash, SHABEA_HALFSIZEB); } else { + /* last round */ /* no swap */ - memxor(L, hash, 8); + memxor(L, hash, SHABEA_HALFSIZEB); + return; } } - free(tb); }