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