]> git.cryptolib.org Git - avr-crypto-lib.git/blob - shabea.c
fixed some (warning) issues
[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 Encrytion 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  * SHABEA128-16
31  */ 
32 #define L ((uint8_t*)block+0)
33 #define R ((uint8_t*)block+8)
34 void shabea128(void * block, void * key, uint16_t keysize, uint8_t enc, uint8_t rounds){
35         int8_t r;               /**/
36         uint8_t *tb;    /**/
37         uint16_t kbs;   /* bytes used for the key / temporary block */
38         sha256_hash_t hash;
39         
40         r = (enc?0:(rounds-1));
41         kbs = keysize/8 + ((keysize&7)?1:0);
42         tb = malloc(8+2+kbs);
43         memcpy(tb+8+2, key, kbs);
44         tb[8+0] = 0;
45         
46         for(;r!=(enc?(rounds):-1);enc?r++:r--){ /* enc: 0..(rounds-1) ; !enc: (rounds-1)..0 */
47                 memcpy(tb, R, 8); /* copy right half into tb */
48                 tb[8+1] = r;
49                 sha256(&hash, tb, 64+16+keysize);
50                 if(!(r==(enc?(rounds-1):0))){   
51                         /* swap */
52                         memxor(hash, L, 8);
53                         memcpy(L, R, 8);
54                         memcpy(R, hash, 8);
55                 } else {
56                         /* no swap */
57                         memxor(L, hash, 8);     
58                 }
59         }
60         free(tb);
61 }
62
63