2008-05-26 19:13:21 +00:00
|
|
|
/* xtea.c */
|
|
|
|
/*
|
2009-02-04 13:50:15 +00:00
|
|
|
This file is part of the AVR-Crypto-Lib.
|
2008-05-26 19:13:21 +00:00
|
|
|
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2006-10-08 14:02:29 +00:00
|
|
|
/**
|
|
|
|
* \file xtea.c
|
|
|
|
* \brief XTEA implemantation
|
|
|
|
* copy'n'pasted from http://en.wikipedia.org/wiki/XTEA
|
2006-07-31 16:04:26 +00:00
|
|
|
* and slightly modified
|
2006-10-08 14:02:29 +00:00
|
|
|
*/
|
2006-07-31 16:04:26 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
2008-04-20 02:36:13 +00:00
|
|
|
void xtea_enc(void* dest, const void* v, const void* k) {
|
|
|
|
uint8_t i;
|
|
|
|
uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
|
2006-07-31 16:04:26 +00:00
|
|
|
uint32_t sum=0, delta=0x9E3779B9;
|
|
|
|
for(i=0; i<32; i++) {
|
2008-04-20 02:36:13 +00:00
|
|
|
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
|
2006-07-31 16:04:26 +00:00
|
|
|
sum += delta;
|
2008-04-20 02:36:13 +00:00
|
|
|
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
|
2006-07-31 16:04:26 +00:00
|
|
|
}
|
2008-04-20 02:36:13 +00:00
|
|
|
((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
|
2006-07-31 16:04:26 +00:00
|
|
|
}
|
|
|
|
|
2008-04-20 02:36:13 +00:00
|
|
|
void xtea_dec(void* dest, const void* v, const void* k) {
|
|
|
|
uint8_t i;
|
|
|
|
uint32_t v0=((uint32_t*)v)[0], v1=((uint32_t*)v)[1];
|
2006-07-31 16:04:26 +00:00
|
|
|
uint32_t sum=0xC6EF3720, delta=0x9E3779B9;
|
|
|
|
for(i=0; i<32; i++) {
|
2008-04-20 02:36:13 +00:00
|
|
|
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]);
|
2006-07-31 16:04:26 +00:00
|
|
|
sum -= delta;
|
2008-04-20 02:36:13 +00:00
|
|
|
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]);
|
2006-07-31 16:04:26 +00:00
|
|
|
}
|
2008-04-20 02:36:13 +00:00
|
|
|
((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1;
|
2006-07-31 16:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|