]> git.cryptolib.org Git - avr-crypto-lib.git/blob - xtea.c
insereated GPLv3 stub
[avr-crypto-lib.git] / xtea.c
1 /* xtea.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        xtea.c
21  * \brief       XTEA implemantation 
22  *   copy'n'pasted from http://en.wikipedia.org/wiki/XTEA
23  *   and slightly modified
24  */
25  
26 #include <stdint.h> 
27  
28
29 void xtea_enc(void* dest, const void* v, const void* k) {
30     uint8_t i;
31     uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
32     uint32_t sum=0, delta=0x9E3779B9;
33     for(i=0; i<32; i++) {
34         v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
35         sum += delta;
36         v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
37     }
38     ((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
39 }
40
41 void xtea_dec(void* dest, const void* v, const void* k) {
42     uint8_t i;
43     uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
44     uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
45     for(i=0; i<32; i++) {
46         v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
47         sum -= delta;
48         v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
49     }
50     ((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
51 }
52
53