This is a port of cyaSSL 2.7.0.
Dependents: CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet
rabbit.c
00001 /* rabbit.c 00002 * 00003 * Copyright (C) 2006-2013 wolfSSL Inc. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * CyaSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00020 */ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <cyassl/ctaocrypt/settings.h> 00027 00028 #ifndef NO_RABBIT 00029 00030 #include <cyassl/ctaocrypt/rabbit.h> 00031 #include <cyassl/ctaocrypt/ctaoerror2.h> 00032 #include <cyassl/ctaocrypt/logging.h> 00033 #ifdef NO_INLINE 00034 #include <cyassl/ctaocrypt/misc.h> 00035 #else 00036 #include <ctaocrypt/src/misc.c> 00037 #endif 00038 00039 00040 #ifdef BIG_ENDIAN_ORDER 00041 #define LITTLE32(x) ByteReverseWord32(x) 00042 #else 00043 #define LITTLE32(x) (x) 00044 #endif 00045 00046 #define U32V(x) ((word32)(x) & 0xFFFFFFFFU) 00047 00048 00049 /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */ 00050 /* the upper 32 bits XOR the lower 32 bits */ 00051 static word32 RABBIT_g_func(word32 x) 00052 { 00053 /* Temporary variables */ 00054 word32 a, b, h, l; 00055 00056 /* Construct high and low argument for squaring */ 00057 a = x&0xFFFF; 00058 b = x>>16; 00059 00060 /* Calculate high and low result of squaring */ 00061 h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b; 00062 l = x*x; 00063 00064 /* Return high XOR low */ 00065 return U32V(h^l); 00066 } 00067 00068 00069 /* Calculate the next internal state */ 00070 static void RABBIT_next_state(RabbitCtx* ctx) 00071 { 00072 /* Temporary variables */ 00073 word32 g[8], c_old[8], i; 00074 00075 /* Save old counter values */ 00076 for (i=0; i<8; i++) 00077 c_old[i] = ctx->c[i]; 00078 00079 /* Calculate new counter values */ 00080 ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry); 00081 ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0])); 00082 ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1])); 00083 ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2])); 00084 ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3])); 00085 ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4])); 00086 ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5])); 00087 ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6])); 00088 ctx->carry = (ctx->c[7] < c_old[7]); 00089 00090 /* Calculate the g-values */ 00091 for (i=0;i<8;i++) 00092 g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i])); 00093 00094 /* Calculate new state values */ 00095 ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16)); 00096 ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]); 00097 ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16)); 00098 ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]); 00099 ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16)); 00100 ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]); 00101 ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16)); 00102 ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]); 00103 } 00104 00105 00106 /* IV setup */ 00107 static void RabbitSetIV(Rabbit* ctx, const byte* iv) 00108 { 00109 /* Temporary variables */ 00110 word32 i0, i1, i2, i3, i; 00111 00112 /* Generate four subvectors */ 00113 i0 = LITTLE32(*(word32*)(iv+0)); 00114 i2 = LITTLE32(*(word32*)(iv+4)); 00115 i1 = (i0>>16) | (i2&0xFFFF0000); 00116 i3 = (i2<<16) | (i0&0x0000FFFF); 00117 00118 /* Modify counter values */ 00119 ctx->workCtx.c[0] = ctx->masterCtx.c[0] ^ i0; 00120 ctx->workCtx.c[1] = ctx->masterCtx.c[1] ^ i1; 00121 ctx->workCtx.c[2] = ctx->masterCtx.c[2] ^ i2; 00122 ctx->workCtx.c[3] = ctx->masterCtx.c[3] ^ i3; 00123 ctx->workCtx.c[4] = ctx->masterCtx.c[4] ^ i0; 00124 ctx->workCtx.c[5] = ctx->masterCtx.c[5] ^ i1; 00125 ctx->workCtx.c[6] = ctx->masterCtx.c[6] ^ i2; 00126 ctx->workCtx.c[7] = ctx->masterCtx.c[7] ^ i3; 00127 00128 /* Copy state variables */ 00129 for (i=0; i<8; i++) 00130 ctx->workCtx.x[i] = ctx->masterCtx.x[i]; 00131 ctx->workCtx.carry = ctx->masterCtx.carry; 00132 00133 /* Iterate the system four times */ 00134 for (i=0; i<4; i++) 00135 RABBIT_next_state(&(ctx->workCtx)); 00136 } 00137 00138 00139 /* Key setup */ 00140 static INLINE int DoKey(Rabbit* ctx, const byte* key, const byte* iv) 00141 { 00142 /* Temporary variables */ 00143 word32 k0, k1, k2, k3, i; 00144 00145 /* Generate four subkeys */ 00146 k0 = LITTLE32(*(word32*)(key+ 0)); 00147 k1 = LITTLE32(*(word32*)(key+ 4)); 00148 k2 = LITTLE32(*(word32*)(key+ 8)); 00149 k3 = LITTLE32(*(word32*)(key+12)); 00150 00151 /* Generate initial state variables */ 00152 ctx->masterCtx.x[0] = k0; 00153 ctx->masterCtx.x[2] = k1; 00154 ctx->masterCtx.x[4] = k2; 00155 ctx->masterCtx.x[6] = k3; 00156 ctx->masterCtx.x[1] = U32V(k3<<16) | (k2>>16); 00157 ctx->masterCtx.x[3] = U32V(k0<<16) | (k3>>16); 00158 ctx->masterCtx.x[5] = U32V(k1<<16) | (k0>>16); 00159 ctx->masterCtx.x[7] = U32V(k2<<16) | (k1>>16); 00160 00161 /* Generate initial counter values */ 00162 ctx->masterCtx.c[0] = rotlFixed(k2, 16); 00163 ctx->masterCtx.c[2] = rotlFixed(k3, 16); 00164 ctx->masterCtx.c[4] = rotlFixed(k0, 16); 00165 ctx->masterCtx.c[6] = rotlFixed(k1, 16); 00166 ctx->masterCtx.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF); 00167 ctx->masterCtx.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF); 00168 ctx->masterCtx.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF); 00169 ctx->masterCtx.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF); 00170 00171 /* Clear carry bit */ 00172 ctx->masterCtx.carry = 0; 00173 00174 /* Iterate the system four times */ 00175 for (i=0; i<4; i++) 00176 RABBIT_next_state(&(ctx->masterCtx)); 00177 00178 /* Modify the counters */ 00179 for (i=0; i<8; i++) 00180 ctx->masterCtx.c[i] ^= ctx->masterCtx.x[(i+4)&0x7]; 00181 00182 /* Copy master instance to work instance */ 00183 for (i=0; i<8; i++) { 00184 ctx->workCtx.x[i] = ctx->masterCtx.x[i]; 00185 ctx->workCtx.c[i] = ctx->masterCtx.c[i]; 00186 } 00187 ctx->workCtx.carry = ctx->masterCtx.carry; 00188 00189 if (iv) RabbitSetIV(ctx, iv); 00190 00191 return 0; 00192 } 00193 00194 00195 /* Key setup */ 00196 int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv) 00197 { 00198 #ifdef XSTREAM_ALIGN 00199 if ((word)key % 4 || (iv && (word)iv % 4)) { 00200 int alignKey[4]; 00201 int alignIv[2]; 00202 00203 CYASSL_MSG("RabbitSetKey unaligned key/iv"); 00204 00205 XMEMCPY(alignKey, key, sizeof(alignKey)); 00206 if (iv) { 00207 XMEMCPY(alignIv, iv, sizeof(alignIv)); 00208 iv = (const byte*)alignIv; 00209 } 00210 00211 return DoKey(ctx, (const byte*)alignKey, iv); 00212 } 00213 #endif /* XSTREAM_ALIGN */ 00214 00215 return DoKey(ctx, key, iv); 00216 } 00217 00218 00219 /* Encrypt/decrypt a message of any size */ 00220 static INLINE int DoProcess(Rabbit* ctx, byte* output, const byte* input, 00221 word32 msglen) 00222 { 00223 /* Encrypt/decrypt all full blocks */ 00224 while (msglen >= 16) { 00225 /* Iterate the system */ 00226 RABBIT_next_state(&(ctx->workCtx)); 00227 00228 /* Encrypt/decrypt 16 bytes of data */ 00229 *(word32*)(output+ 0) = *(word32*)(input+ 0) ^ 00230 LITTLE32(ctx->workCtx.x[0] ^ (ctx->workCtx.x[5]>>16) ^ 00231 U32V(ctx->workCtx.x[3]<<16)); 00232 *(word32*)(output+ 4) = *(word32*)(input+ 4) ^ 00233 LITTLE32(ctx->workCtx.x[2] ^ (ctx->workCtx.x[7]>>16) ^ 00234 U32V(ctx->workCtx.x[5]<<16)); 00235 *(word32*)(output+ 8) = *(word32*)(input+ 8) ^ 00236 LITTLE32(ctx->workCtx.x[4] ^ (ctx->workCtx.x[1]>>16) ^ 00237 U32V(ctx->workCtx.x[7]<<16)); 00238 *(word32*)(output+12) = *(word32*)(input+12) ^ 00239 LITTLE32(ctx->workCtx.x[6] ^ (ctx->workCtx.x[3]>>16) ^ 00240 U32V(ctx->workCtx.x[1]<<16)); 00241 00242 /* Increment pointers and decrement length */ 00243 input += 16; 00244 output += 16; 00245 msglen -= 16; 00246 } 00247 00248 /* Encrypt/decrypt remaining data */ 00249 if (msglen) { 00250 00251 word32 i; 00252 word32 tmp[4]; 00253 byte* buffer = (byte*)tmp; 00254 00255 XMEMSET(tmp, 0, sizeof(tmp)); /* help static analysis */ 00256 00257 /* Iterate the system */ 00258 RABBIT_next_state(&(ctx->workCtx)); 00259 00260 /* Generate 16 bytes of pseudo-random data */ 00261 tmp[0] = LITTLE32(ctx->workCtx.x[0] ^ 00262 (ctx->workCtx.x[5]>>16) ^ U32V(ctx->workCtx.x[3]<<16)); 00263 tmp[1] = LITTLE32(ctx->workCtx.x[2] ^ 00264 (ctx->workCtx.x[7]>>16) ^ U32V(ctx->workCtx.x[5]<<16)); 00265 tmp[2] = LITTLE32(ctx->workCtx.x[4] ^ 00266 (ctx->workCtx.x[1]>>16) ^ U32V(ctx->workCtx.x[7]<<16)); 00267 tmp[3] = LITTLE32(ctx->workCtx.x[6] ^ 00268 (ctx->workCtx.x[3]>>16) ^ U32V(ctx->workCtx.x[1]<<16)); 00269 00270 /* Encrypt/decrypt the data */ 00271 for (i=0; i<msglen; i++) 00272 output[i] = input[i] ^ buffer[i]; 00273 } 00274 00275 return 0; 00276 } 00277 00278 00279 /* Encrypt/decrypt a message of any size */ 00280 int RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen) 00281 { 00282 #ifdef XSTREAM_ALIGN 00283 if ((word)input % 4 || (word)output % 4) { 00284 #ifndef NO_CYASSL_ALLOC_ALIGN 00285 byte* tmp; 00286 CYASSL_MSG("RabbitProcess unaligned"); 00287 00288 tmp = (byte*)XMALLOC(msglen, NULL, DYNAMIC_TYPE_TMP_BUFFER); 00289 if (tmp == NULL) return MEMORY_E; 00290 00291 XMEMCPY(tmp, input, msglen); 00292 DoProcess(ctx, tmp, tmp, msglen); 00293 XMEMCPY(output, tmp, msglen); 00294 00295 XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER); 00296 00297 return 0; 00298 #else 00299 return BAD_ALIGN_E; 00300 #endif 00301 } 00302 #endif /* XSTREAM_ALIGN */ 00303 00304 return DoProcess(ctx, output, input, msglen); 00305 } 00306 00307 00308 #endif /* NO_RABBIT */
Generated on Tue Jul 12 2022 20:44:51 by 1.7.2