xtea.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
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
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
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