]> git.cryptolib.org Git - avr-crypto-lib.git/blob - shabea.c
7acb4d3f9b9be78a2b2d80a5657e3208a2cba3e2
[avr-crypto-lib.git] / shabea.c
1 /**
2  * \file        shabea.c
3  * \author      Daniel Otte 
4  * \date        2007-06-07
5  * \brief       SHABEA - a SHA Based Encryption Algorithm implementation
6  * \par License 
7  * GPL
8  * 
9  * SHABEAn-r where n is the blocksize and r the number of round used
10  * 
11  * 
12  */
13 #include <stdlib.h>
14 #include <string.h>
15 #include "sha256.h"
16
17 #include "config.h"
18 #include "uart.h"
19 #include "debug.h"
20 /*
21  * 
22  */
23 void memxor(uint8_t * dest, uint8_t * src, uint8_t length){
24         while(length--){
25                 *dest++ ^= *src++;
26         }
27
28
29 /*
30  * SHABEA256-n
31  */ 
32  
33 #define BLOCKSIZE 256
34 #define BLOCKSIZEB (BLOCKSIZE/8)
35 #define HALFSIZEB  (BLOCKSIZEB/2)
36 #define HALFSIZE (BLOCKSIZE/2)
37
38 #define L ((uint8_t*)block+ 0)
39 #define R ((uint8_t*)block+16)
40 void shabea256(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds){
41         int8_t r;               /**/
42         uint8_t tb[HALFSIZEB+2+(keysize+7)/8];  /**/
43         uint16_t kbs;   /* bytes used for the key / temporary block */
44         sha256_hash_t hash;
45         
46         r = (enc?0:(rounds-1));
47         kbs = (keysize+7)/8;
48         memcpy(tb+HALFSIZEB+2, key, kbs); /* copy key to temporary block */
49         tb[HALFSIZEB+0] = 0;    /* set round counter high value to zero */
50         
51         for(;r!=(enc?(rounds):-1);enc?r++:r--){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */
52                 memcpy(tb, R, HALFSIZEB); /* copy right half into tb */
53                 tb[HALFSIZEB+1] = r;
54                 sha256(&hash, tb, HALFSIZE+16+keysize);
55                 if(!(r==(enc?(rounds-1):0))){   
56                         /* swap */
57                         memxor(hash, L, HALFSIZE);
58                         memcpy(L, R, HALFSIZE);
59                         memcpy(R, hash, HALFSIZE);
60                 } else {
61                         /* no swap */
62                         memxor(L, hash, HALFSIZE);      
63                 }
64         }
65 }
66
67