xtea.c

Go to the documentation of this file.
00001 /* 
00002  * Copyright of XTEA encryption algorithm by David Wheeler and Roger Needham 
00003  * at the Computer Laboratory of Cambridge University.
00004  * 
00005  * Kindly released to Public Domain.
00006  */
00007 
00008 /*
00009  * Copyright of Implementation 2010 by Ulrich Prinz (uprinz2@netscape.net)
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions
00015  * are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  * 2. Redistributions in binary form must reproduce the above copyright
00020  *    notice, this list of conditions and the following disclaimer in the
00021  *    documentation and/or other materials provided with the distribution.
00022  * 3. Neither the name of the copyright holders nor the names of
00023  *    contributors may be used to endorse or promote products derived
00024  *    from this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00029  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00030  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00031  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00032  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00033  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00034  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00035  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00036  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00037  * SUCH DAMAGE.
00038  *
00039  * For additional information see http://www.ethernut.de/
00040  */
00041 
00042 /*
00043  * \file gorp/crypt/xtea.c
00044  * \brief Fast and effcient Extended Tiny Encryption Algorythm.
00045  *
00046  * \verbatim
00047  * $Id$
00048  * \endverbatim
00049  */
00050 
00051 #include <stdint.h>
00052 #include <string.h>
00053 
00054 #include <gorp/xtea.h>
00055 
00056 #define XTDELTA  0x9e3779b9
00057 #define XTSUM    0xC6EF3720
00058 #define ROUNDS   32
00059 
00067 void XTeaCrypt(uint32_t *w,  CONST uint32_t *v, CONST XTeaKeyBlock_t k)
00068 {
00069     register uint32_t y=v[0];
00070     register uint32_t z=v[1];
00071     register uint32_t sum=0;
00072     register uint32_t delta=XTDELTA;
00073     uint_fast8_t n=ROUNDS;
00074 
00075     while (n-- > 0) {
00076         y += (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum&3]);
00077         sum += delta;
00078         z += (((y << 4) ^ (y >> 5)) + y) ^ (sum + k[sum>>11 & 3]);
00079     }
00080 
00081     w[0]=y; w[1]=z;
00082 }
00083 
00091 void XTeaDecrypt(uint32_t *w, CONST uint32_t *v, CONST XTeaKeyBlock_t k)
00092 {
00093     register uint32_t y=v[0];
00094     register uint32_t z=v[1];
00095     register uint32_t sum=XTSUM;
00096     register uint32_t delta=XTDELTA;
00097     uint_fast8_t n=32;
00098 
00099     while (n-- > 0) {
00100         z -= (((y << 4) ^ (y >> 5)) + y) ^ (sum + k[sum>>11 & 3]);
00101         sum -= delta;
00102         y -= (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum&3]);
00103     }
00104 
00105     w[0]=y; w[1]=z;
00106 }
00107 
00120 void XTeaCryptStr( char *dst, CONST char *src, uint16_t len, CONST char *pass)
00121 {
00122     uint16_t l;
00123     uint16_t i;
00124     XTeaKeyBlock_t K = { 0,0,0,0};
00125 
00126     /* Prepare pass as XTEA Key Block */
00127     l = strlen(pass);
00128     if( l>sizeof( XTeaKeyBlock_t))
00129         l = sizeof( XTeaKeyBlock_t);
00130     memcpy( K, pass, l);
00131 
00132     i = 0; l = strlen( src);
00133 
00134     while (i<len) {
00135         XTeaCrypt( (uint32_t*)dst, (CONST uint32_t*)src, K);
00136         src+=8; dst+=8; i+=8;
00137     }
00138 }
00139 
00152 void XTeaDecryptStr( char * dst, CONST char *src, uint16_t len, CONST char *pass)
00153 {
00154     uint16_t l;
00155     uint16_t i;
00156     XTeaKeyBlock_t K = { 0,0,0,0};
00157 
00158     /* Prepare pass as XTEA Key Block */
00159     l = strlen(pass);
00160     if( l>sizeof( XTeaKeyBlock_t))
00161         l = sizeof( XTeaKeyBlock_t);
00162     memcpy( K, pass, l);
00163 
00164     i = 0; l = strlen( src);
00165 
00166     while (i<len) {
00167         XTeaDecrypt( (uint32_t*)dst, (uint32_t CONST*)src, K);
00168         src+=8; dst+=8; i+=8;
00169     }
00170 }
00171 

© 2000-2010 by contributors - visit http://www.ethernut.de/