Nut/OS  4.10.3
API Reference
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 <compiler.h>
00052 
00053 #include <stdint.h>
00054 #include <string.h>
00055 
00056 #include <gorp/xtea.h>
00057 
00058 #define XTDELTA  0x9e3779b9
00059 #define XTSUM    0xC6EF3720
00060 #define ROUNDS   32
00061 
00069 void XTeaCrypt(uint32_t *w,  CONST uint32_t *v, CONST XTeaKeyBlock_t k)
00070 {
00071     register uint32_t y=v[0];
00072     register uint32_t z=v[1];
00073     register uint32_t sum=0;
00074     register uint32_t delta=XTDELTA;
00075     uint_fast8_t n=ROUNDS;
00076 
00077     while (n-- > 0) {
00078         y += (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum&3]);
00079         sum += delta;
00080         z += (((y << 4) ^ (y >> 5)) + y) ^ (sum + k[sum>>11 & 3]);
00081     }
00082 
00083     w[0]=y; w[1]=z;
00084 }
00085 
00093 void XTeaDecrypt(uint32_t *w, CONST uint32_t *v, CONST XTeaKeyBlock_t k)
00094 {
00095     register uint32_t y=v[0];
00096     register uint32_t z=v[1];
00097     register uint32_t sum=XTSUM;
00098     register uint32_t delta=XTDELTA;
00099     uint_fast8_t n=32;
00100 
00101     while (n-- > 0) {
00102         z -= (((y << 4) ^ (y >> 5)) + y) ^ (sum + k[sum>>11 & 3]);
00103         sum -= delta;
00104         y -= (((z << 4) ^ (z >> 5)) + z) ^ (sum + k[sum&3]);
00105     }
00106 
00107     w[0]=y; w[1]=z;
00108 }
00109 
00122 void XTeaCryptStr( char *dst, CONST char *src, uint16_t len, CONST char *pass)
00123 {
00124     uint16_t l;
00125     uint16_t i;
00126     XTeaKeyBlock_t K = { 0,0,0,0};
00127 
00128     /* Prepare pass as XTEA Key Block */
00129     l = strlen(pass);
00130     if( l>sizeof( XTeaKeyBlock_t))
00131         l = sizeof( XTeaKeyBlock_t);
00132     memcpy( K, pass, l);
00133 
00134     i = 0; l = strlen( src);
00135 
00136     while (i<len) {
00137         XTeaCrypt( (uint32_t*)dst, (CONST uint32_t*)src, K);
00138         src+=8; dst+=8; i+=8;
00139     }
00140 }
00141 
00154 void XTeaDecryptStr( char * dst, CONST char *src, uint16_t len, CONST char *pass)
00155 {
00156     uint16_t l;
00157     uint16_t i;
00158     XTeaKeyBlock_t K = { 0,0,0,0};
00159 
00160     /* Prepare pass as XTEA Key Block */
00161     l = strlen(pass);
00162     if( l>sizeof( XTeaKeyBlock_t))
00163         l = sizeof( XTeaKeyBlock_t);
00164     memcpy( K, pass, l);
00165 
00166     i = 0; l = strlen( src);
00167 
00168     while (i<len) {
00169         XTeaDecrypt( (uint32_t*)dst, (uint32_t CONST*)src, K);
00170         src+=8; dst+=8; i+=8;
00171     }
00172 }
00173