3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
23 * \brief SHABEA - a SHA Based Encryption Algorithm implementation
27 * SHABEAn-r where n is the blocksize and r the number of round used
41 void memxor(uint8_t * dest, uint8_t * src, uint8_t length){
52 #define BLOCKSIZEB (BLOCKSIZE/8)
53 #define HALFSIZEB (BLOCKSIZEB/2)
54 #define HALFSIZE (BLOCKSIZE/2)
56 #define L ((uint8_t*)block+ 0)
57 #define R ((uint8_t*)block+16)
58 void shabea256(void * block, void * key, uint16_t keysize_b, uint8_t enc, uint8_t rounds){
60 uint8_t tb[HALFSIZEB+2+(keysize_b+7)/8]; /**/
61 uint16_t kbs; /* bytes used for the key / temporary block */
64 r = (enc?0:(rounds-1));
65 kbs = (keysize_b+7)/8;
66 memcpy(tb+HALFSIZEB+2, key, kbs); /* copy key to temporary block */
67 tb[HALFSIZEB+0] = 0; /* set round counter high value to zero */
69 for(;r!=(enc?(rounds):-1);enc?r++:r--){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */
70 memcpy(tb, R, HALFSIZEB); /* copy right half into tb */
72 sha256(&hash, tb, HALFSIZE+16+keysize_b);
73 if(!(r==(enc?(rounds-1):0))){
75 memxor(hash, L, HALFSIZEB);
76 memcpy(L, R, HALFSIZEB);
77 memcpy(R, hash, HALFSIZEB);
80 memxor(L, hash, HALFSIZE);