]> git.cryptolib.org Git - avr-crypto-lib.git/blob - shabea.c
insereated GPLv3 stub
[avr-crypto-lib.git] / shabea.c
1 /* shabea.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
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  * \file        shabea.c
21  * \author      Daniel Otte 
22  * \date        2007-06-07
23  * \brief       SHABEA - a SHA Based Encryption Algorithm implementation
24  * \par License 
25  * GPL
26  * 
27  * SHABEAn-r where n is the blocksize and r the number of round used
28  * 
29  * 
30  */
31 #include <stdlib.h>
32 #include <string.h>
33 #include "sha256.h"
34
35 #include "config.h"
36 #include "uart.h"
37 #include "debug.h"
38 /*
39  * 
40  */
41 void memxor(uint8_t * dest, uint8_t * src, uint8_t length){
42         while(length--){
43                 *dest++ ^= *src++;
44         }
45
46
47 /*
48  * SHABEA256-n
49  */ 
50  
51 #define BLOCKSIZE 256
52 #define BLOCKSIZEB (BLOCKSIZE/8)
53 #define HALFSIZEB  (BLOCKSIZEB/2)
54 #define HALFSIZE (BLOCKSIZE/2)
55
56 #define L ((uint8_t*)block+ 0)
57 #define R ((uint8_t*)block+16)
58 void shabea256(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds){
59         int8_t r;               /**/
60         uint8_t tb[HALFSIZEB+2+(keysize+7)/8];  /**/
61         uint16_t kbs;   /* bytes used for the key / temporary block */
62         sha256_hash_t hash;
63         
64         r = (enc?0:(rounds-1));
65         kbs = (keysize+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 */
68         
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 */
71                 tb[HALFSIZEB+1] = r;
72                 sha256(&hash, tb, HALFSIZE+16+keysize);
73                 if(!(r==(enc?(rounds-1):0))){   
74                         /* swap */
75                         memxor(hash, L, HALFSIZE);
76                         memcpy(L, R, HALFSIZE);
77                         memcpy(R, hash, HALFSIZE);
78                 } else {
79                         /* no swap */
80                         memxor(L, hash, HALFSIZE);      
81                 }
82         }
83 }
84
85