]> git.cryptolib.org Git - avr-crypto-lib.git/blob - shabea.c
bug fixed in md5-asm.S ( wrong values for length_b%512=505..511 )
[avr-crypto-lib.git] / shabea.c
1 /* shabea.c */
2 /*
3  *   This file is part of AnonAccess, an access system which can be used
4  *    to open door or doing other things with an anonymity featured
5  *    account managment.
6  *   Copyright (C) 2006, 2007, 2008  Daniel Otte (daniel.otte@rub.de)
7  *
8  *   This program is free software: you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation, either version 3 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /**
23  * \file        shabea.c
24  * \author      Daniel Otte 
25  * \date        2007-06-07
26  * \brief       SHABEA - a SHA Based Encryption Algorithm implementation
27  * \par License 
28  * GPL
29  * 
30  * SHABEAn-r where n is the blocksize and r the number of round used
31  * 
32  * 
33  */
34 #include <stdlib.h>
35 #include <string.h>
36 #include "sha256.h"
37
38 #include "config.h"
39 #include "memxor.h"
40
41
42 /*
43  * SHABEA256-n
44  */ 
45  
46 #define SHABEA_BLOCKSIZE 256
47 #define SHABEA_BLOCKSIZEB (SHABEA_BLOCKSIZE/8)
48 #define SHABEA_HALFSIZEB  (SHABEA_BLOCKSIZEB/2)
49 #define SHABEA_HALFSIZE (SHABEA_BLOCKSIZE/2)
50
51 #define L ((uint8_t*)block+ 0)
52 #define R ((uint8_t*)block+16)
53 void shabea256(void * block, void * key, uint16_t keysize_b, uint8_t enc, uint8_t rounds){
54         uint8_t r;              /**/
55         uint8_t tb[SHABEA_HALFSIZEB+2+(keysize_b+7)/8]; /**/
56         uint16_t kbs;   /* bytes used for the key / temporary block */
57         sha256_hash_t hash;
58         uint8_t termcond; 
59         int8_t dir;
60         if(enc){
61                 r = 0;
62                 termcond = rounds-1;
63                 dir = 1;
64         } else {
65                 r = rounds-1;
66                 termcond = 0;
67                 dir = -1;
68         }
69         kbs = (keysize_b+7)/8;
70         memcpy(tb+SHABEA_HALFSIZEB+2, key, kbs); /* copy key to temporary block */
71         tb[SHABEA_HALFSIZEB+0] = 0;     /* set round counter high value to zero */
72         
73         for(;;r+=dir){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */
74                 memcpy(tb, R, SHABEA_HALFSIZEB); /* copy right half into tb */
75                 tb[SHABEA_HALFSIZEB+1] = r;
76                 sha256(&hash, tb, SHABEA_HALFSIZE+16+keysize_b);
77                 if(r!=termcond){        
78                         /* swap */
79                         memxor(hash, L, SHABEA_HALFSIZEB);
80                         memcpy(L, R, SHABEA_HALFSIZEB);
81                         memcpy(R, hash, SHABEA_HALFSIZEB);
82                 } else {
83                         /* last round */
84                         /* no swap */
85                         memxor(L, hash, SHABEA_HALFSIZEB);
86                         return; 
87                 }
88         }
89 }
90
91