X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=xtea.c;h=4605cb0ffb4ee64b055ea66351b64779f8ee391f;hb=8f855d283a31a468ea014774c4723a8b77b81644;hp=c9fe1037b2372cf0ddf92a24cb37a0fd9cece8a3;hpb=3c995d0a8faeb9d37927d48e20fc45d839e066ea;p=avr-crypto-lib.git diff --git a/xtea.c b/xtea.c index c9fe103..4605cb0 100644 --- a/xtea.c +++ b/xtea.c @@ -1,32 +1,53 @@ -/* - * XTEA implemantation - * copy'n'pasted from http://en.wikipedia.org/wiki/XTEA +/* xtea.c */ +/* + This file is part of the AVR-Crypto-Lib. + 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 . +*/ +/** + * \file xtea.c + * \brief XTEA implemantation + * copy'n'pasted from http://en.wikipedia.org/wiki/XTEA * and slightly modified - * */ + */ #include -void xtea_enc(uint32_t* dest, uint32_t* v, uint32_t* k) { - uint32_t v0=v[0], v1=v[1], i; +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]; uint32_t sum=0, delta=0x9E3779B9; for(i=0; i<32; i++) { - v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); + v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]); sum += delta; - v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); + v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]); } - dest[0]=v0; dest[1]=v1; + ((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1; } -void xtea_dec(uint32_t* dest, uint32_t* v, uint32_t* k) { - uint32_t v0=v[0], v1=v[1], i; +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]; uint32_t sum=0xC6EF3720, delta=0x9E3779B9; for(i=0; i<32; i++) { - v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]); + v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + ((uint32_t*)k)[sum>>11 & 3]); sum -= delta; - v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]); + v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + ((uint32_t*)k)[sum & 3]); } - dest[0]=v0; dest[1]=v1; + ((uint32_t*)dest)[0]=v0; ((uint32_t*)dest)[1]=v1; }