wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* rabbit.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 16:8e0d178b1d1e 3 * Copyright (C) 2006-2020 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 24 #include <config.h>
wolfSSL 15:117db924cf7c 25 #endif
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 28
wolfSSL 15:117db924cf7c 29 #ifndef NO_RABBIT
wolfSSL 15:117db924cf7c 30
wolfSSL 15:117db924cf7c 31 #include <wolfssl/wolfcrypt/rabbit.h>
wolfSSL 15:117db924cf7c 32 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 33 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 34 #ifdef NO_INLINE
wolfSSL 15:117db924cf7c 35 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 15:117db924cf7c 36 #else
wolfSSL 15:117db924cf7c 37 #define WOLFSSL_MISC_INCLUDED
wolfSSL 15:117db924cf7c 38 #include <wolfcrypt/src/misc.c>
wolfSSL 15:117db924cf7c 39 #endif
wolfSSL 15:117db924cf7c 40
wolfSSL 15:117db924cf7c 41
wolfSSL 15:117db924cf7c 42 #ifdef BIG_ENDIAN_ORDER
wolfSSL 15:117db924cf7c 43 #define LITTLE32(x) ByteReverseWord32(x)
wolfSSL 15:117db924cf7c 44 #else
wolfSSL 15:117db924cf7c 45 #define LITTLE32(x) (x)
wolfSSL 15:117db924cf7c 46 #endif
wolfSSL 15:117db924cf7c 47
wolfSSL 15:117db924cf7c 48 #define U32V(x) ((word32)(x) & 0xFFFFFFFFU)
wolfSSL 15:117db924cf7c 49
wolfSSL 15:117db924cf7c 50
wolfSSL 15:117db924cf7c 51 /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */
wolfSSL 15:117db924cf7c 52 /* the upper 32 bits XOR the lower 32 bits */
wolfSSL 15:117db924cf7c 53 static word32 RABBIT_g_func(word32 x)
wolfSSL 15:117db924cf7c 54 {
wolfSSL 15:117db924cf7c 55 /* Temporary variables */
wolfSSL 15:117db924cf7c 56 word32 a, b, h, l;
wolfSSL 15:117db924cf7c 57
wolfSSL 15:117db924cf7c 58 /* Construct high and low argument for squaring */
wolfSSL 15:117db924cf7c 59 a = x&0xFFFF;
wolfSSL 15:117db924cf7c 60 b = x>>16;
wolfSSL 15:117db924cf7c 61
wolfSSL 15:117db924cf7c 62 /* Calculate high and low result of squaring */
wolfSSL 15:117db924cf7c 63 h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
wolfSSL 15:117db924cf7c 64 l = x*x;
wolfSSL 15:117db924cf7c 65
wolfSSL 15:117db924cf7c 66 /* Return high XOR low */
wolfSSL 15:117db924cf7c 67 return U32V(h^l);
wolfSSL 15:117db924cf7c 68 }
wolfSSL 15:117db924cf7c 69
wolfSSL 15:117db924cf7c 70
wolfSSL 15:117db924cf7c 71 /* Calculate the next internal state */
wolfSSL 15:117db924cf7c 72 static void RABBIT_next_state(RabbitCtx* ctx)
wolfSSL 15:117db924cf7c 73 {
wolfSSL 15:117db924cf7c 74 /* Temporary variables */
wolfSSL 15:117db924cf7c 75 word32 g[8], c_old[8], i;
wolfSSL 15:117db924cf7c 76
wolfSSL 15:117db924cf7c 77 /* Save old counter values */
wolfSSL 15:117db924cf7c 78 for (i=0; i<8; i++)
wolfSSL 15:117db924cf7c 79 c_old[i] = ctx->c[i];
wolfSSL 15:117db924cf7c 80
wolfSSL 15:117db924cf7c 81 /* Calculate new counter values */
wolfSSL 15:117db924cf7c 82 ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry);
wolfSSL 15:117db924cf7c 83 ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0]));
wolfSSL 15:117db924cf7c 84 ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1]));
wolfSSL 15:117db924cf7c 85 ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2]));
wolfSSL 15:117db924cf7c 86 ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3]));
wolfSSL 15:117db924cf7c 87 ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4]));
wolfSSL 15:117db924cf7c 88 ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5]));
wolfSSL 15:117db924cf7c 89 ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6]));
wolfSSL 15:117db924cf7c 90 ctx->carry = (ctx->c[7] < c_old[7]);
wolfSSL 15:117db924cf7c 91
wolfSSL 15:117db924cf7c 92 /* Calculate the g-values */
wolfSSL 15:117db924cf7c 93 for (i=0;i<8;i++)
wolfSSL 15:117db924cf7c 94 g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
wolfSSL 15:117db924cf7c 95
wolfSSL 15:117db924cf7c 96 /* Calculate new state values */
wolfSSL 15:117db924cf7c 97 ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16));
wolfSSL 15:117db924cf7c 98 ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]);
wolfSSL 15:117db924cf7c 99 ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16));
wolfSSL 15:117db924cf7c 100 ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]);
wolfSSL 15:117db924cf7c 101 ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16));
wolfSSL 15:117db924cf7c 102 ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]);
wolfSSL 15:117db924cf7c 103 ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16));
wolfSSL 15:117db924cf7c 104 ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]);
wolfSSL 15:117db924cf7c 105 }
wolfSSL 15:117db924cf7c 106
wolfSSL 15:117db924cf7c 107
wolfSSL 15:117db924cf7c 108 /* IV setup */
wolfSSL 15:117db924cf7c 109 static void wc_RabbitSetIV(Rabbit* ctx, const byte* inIv)
wolfSSL 15:117db924cf7c 110 {
wolfSSL 15:117db924cf7c 111 /* Temporary variables */
wolfSSL 15:117db924cf7c 112 word32 i0, i1, i2, i3, i;
wolfSSL 15:117db924cf7c 113 word32 iv[2];
wolfSSL 15:117db924cf7c 114
wolfSSL 15:117db924cf7c 115 if (inIv)
wolfSSL 15:117db924cf7c 116 XMEMCPY(iv, inIv, sizeof(iv));
wolfSSL 15:117db924cf7c 117 else
wolfSSL 15:117db924cf7c 118 XMEMSET(iv, 0, sizeof(iv));
wolfSSL 15:117db924cf7c 119
wolfSSL 15:117db924cf7c 120 /* Generate four subvectors */
wolfSSL 15:117db924cf7c 121 i0 = LITTLE32(iv[0]);
wolfSSL 15:117db924cf7c 122 i2 = LITTLE32(iv[1]);
wolfSSL 15:117db924cf7c 123 i1 = (i0>>16) | (i2&0xFFFF0000);
wolfSSL 15:117db924cf7c 124 i3 = (i2<<16) | (i0&0x0000FFFF);
wolfSSL 15:117db924cf7c 125
wolfSSL 15:117db924cf7c 126 /* Modify counter values */
wolfSSL 15:117db924cf7c 127 ctx->workCtx.c[0] = ctx->masterCtx.c[0] ^ i0;
wolfSSL 15:117db924cf7c 128 ctx->workCtx.c[1] = ctx->masterCtx.c[1] ^ i1;
wolfSSL 15:117db924cf7c 129 ctx->workCtx.c[2] = ctx->masterCtx.c[2] ^ i2;
wolfSSL 15:117db924cf7c 130 ctx->workCtx.c[3] = ctx->masterCtx.c[3] ^ i3;
wolfSSL 15:117db924cf7c 131 ctx->workCtx.c[4] = ctx->masterCtx.c[4] ^ i0;
wolfSSL 15:117db924cf7c 132 ctx->workCtx.c[5] = ctx->masterCtx.c[5] ^ i1;
wolfSSL 15:117db924cf7c 133 ctx->workCtx.c[6] = ctx->masterCtx.c[6] ^ i2;
wolfSSL 15:117db924cf7c 134 ctx->workCtx.c[7] = ctx->masterCtx.c[7] ^ i3;
wolfSSL 15:117db924cf7c 135
wolfSSL 15:117db924cf7c 136 /* Copy state variables */
wolfSSL 15:117db924cf7c 137 for (i=0; i<8; i++)
wolfSSL 15:117db924cf7c 138 ctx->workCtx.x[i] = ctx->masterCtx.x[i];
wolfSSL 15:117db924cf7c 139 ctx->workCtx.carry = ctx->masterCtx.carry;
wolfSSL 15:117db924cf7c 140
wolfSSL 15:117db924cf7c 141 /* Iterate the system four times */
wolfSSL 15:117db924cf7c 142 for (i=0; i<4; i++)
wolfSSL 15:117db924cf7c 143 RABBIT_next_state(&(ctx->workCtx));
wolfSSL 15:117db924cf7c 144 }
wolfSSL 15:117db924cf7c 145
wolfSSL 15:117db924cf7c 146
wolfSSL 15:117db924cf7c 147 /* Key setup */
wolfSSL 15:117db924cf7c 148 static WC_INLINE int DoKey(Rabbit* ctx, const byte* key, const byte* iv)
wolfSSL 15:117db924cf7c 149 {
wolfSSL 15:117db924cf7c 150 /* Temporary variables */
wolfSSL 15:117db924cf7c 151 word32 k0, k1, k2, k3, i;
wolfSSL 15:117db924cf7c 152
wolfSSL 15:117db924cf7c 153 /* Generate four subkeys */
wolfSSL 15:117db924cf7c 154 k0 = LITTLE32(*(word32*)(key+ 0));
wolfSSL 15:117db924cf7c 155 k1 = LITTLE32(*(word32*)(key+ 4));
wolfSSL 15:117db924cf7c 156 k2 = LITTLE32(*(word32*)(key+ 8));
wolfSSL 15:117db924cf7c 157 k3 = LITTLE32(*(word32*)(key+12));
wolfSSL 15:117db924cf7c 158
wolfSSL 15:117db924cf7c 159 /* Generate initial state variables */
wolfSSL 15:117db924cf7c 160 ctx->masterCtx.x[0] = k0;
wolfSSL 15:117db924cf7c 161 ctx->masterCtx.x[2] = k1;
wolfSSL 15:117db924cf7c 162 ctx->masterCtx.x[4] = k2;
wolfSSL 15:117db924cf7c 163 ctx->masterCtx.x[6] = k3;
wolfSSL 15:117db924cf7c 164 ctx->masterCtx.x[1] = U32V(k3<<16) | (k2>>16);
wolfSSL 15:117db924cf7c 165 ctx->masterCtx.x[3] = U32V(k0<<16) | (k3>>16);
wolfSSL 15:117db924cf7c 166 ctx->masterCtx.x[5] = U32V(k1<<16) | (k0>>16);
wolfSSL 15:117db924cf7c 167 ctx->masterCtx.x[7] = U32V(k2<<16) | (k1>>16);
wolfSSL 15:117db924cf7c 168
wolfSSL 15:117db924cf7c 169 /* Generate initial counter values */
wolfSSL 15:117db924cf7c 170 ctx->masterCtx.c[0] = rotlFixed(k2, 16);
wolfSSL 15:117db924cf7c 171 ctx->masterCtx.c[2] = rotlFixed(k3, 16);
wolfSSL 15:117db924cf7c 172 ctx->masterCtx.c[4] = rotlFixed(k0, 16);
wolfSSL 15:117db924cf7c 173 ctx->masterCtx.c[6] = rotlFixed(k1, 16);
wolfSSL 15:117db924cf7c 174 ctx->masterCtx.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF);
wolfSSL 15:117db924cf7c 175 ctx->masterCtx.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF);
wolfSSL 15:117db924cf7c 176 ctx->masterCtx.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF);
wolfSSL 15:117db924cf7c 177 ctx->masterCtx.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF);
wolfSSL 15:117db924cf7c 178
wolfSSL 15:117db924cf7c 179 /* Clear carry bit */
wolfSSL 15:117db924cf7c 180 ctx->masterCtx.carry = 0;
wolfSSL 15:117db924cf7c 181
wolfSSL 15:117db924cf7c 182 /* Iterate the system four times */
wolfSSL 15:117db924cf7c 183 for (i=0; i<4; i++)
wolfSSL 15:117db924cf7c 184 RABBIT_next_state(&(ctx->masterCtx));
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186 /* Modify the counters */
wolfSSL 15:117db924cf7c 187 for (i=0; i<8; i++)
wolfSSL 15:117db924cf7c 188 ctx->masterCtx.c[i] ^= ctx->masterCtx.x[(i+4)&0x7];
wolfSSL 15:117db924cf7c 189
wolfSSL 15:117db924cf7c 190 /* Copy master instance to work instance */
wolfSSL 15:117db924cf7c 191 for (i=0; i<8; i++) {
wolfSSL 15:117db924cf7c 192 ctx->workCtx.x[i] = ctx->masterCtx.x[i];
wolfSSL 15:117db924cf7c 193 ctx->workCtx.c[i] = ctx->masterCtx.c[i];
wolfSSL 15:117db924cf7c 194 }
wolfSSL 15:117db924cf7c 195 ctx->workCtx.carry = ctx->masterCtx.carry;
wolfSSL 15:117db924cf7c 196
wolfSSL 15:117db924cf7c 197 wc_RabbitSetIV(ctx, iv);
wolfSSL 15:117db924cf7c 198
wolfSSL 15:117db924cf7c 199 return 0;
wolfSSL 15:117db924cf7c 200 }
wolfSSL 15:117db924cf7c 201
wolfSSL 15:117db924cf7c 202
wolfSSL 15:117db924cf7c 203 int wc_Rabbit_SetHeap(Rabbit* ctx, void* heap)
wolfSSL 15:117db924cf7c 204 {
wolfSSL 15:117db924cf7c 205 if (ctx == NULL) {
wolfSSL 15:117db924cf7c 206 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 207 }
wolfSSL 15:117db924cf7c 208
wolfSSL 15:117db924cf7c 209 #ifdef XSTREAM_ALIGN
wolfSSL 15:117db924cf7c 210 ctx->heap = heap;
wolfSSL 15:117db924cf7c 211 #endif
wolfSSL 15:117db924cf7c 212
wolfSSL 15:117db924cf7c 213 (void)heap;
wolfSSL 15:117db924cf7c 214 return 0;
wolfSSL 15:117db924cf7c 215 }
wolfSSL 15:117db924cf7c 216
wolfSSL 15:117db924cf7c 217
wolfSSL 15:117db924cf7c 218 /* Key setup */
wolfSSL 15:117db924cf7c 219 int wc_RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
wolfSSL 15:117db924cf7c 220 {
wolfSSL 15:117db924cf7c 221 if (ctx == NULL || key == NULL) {
wolfSSL 15:117db924cf7c 222 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 223 }
wolfSSL 15:117db924cf7c 224
wolfSSL 15:117db924cf7c 225 #ifdef XSTREAM_ALIGN
wolfSSL 15:117db924cf7c 226 /* default heap to NULL or heap test value */
wolfSSL 15:117db924cf7c 227 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 15:117db924cf7c 228 ctx->heap = (void*)WOLFSSL_HEAP_TEST;
wolfSSL 15:117db924cf7c 229 #else
wolfSSL 15:117db924cf7c 230 ctx->heap = NULL;
wolfSSL 15:117db924cf7c 231 #endif /* WOLFSSL_HEAP_TEST */
wolfSSL 15:117db924cf7c 232
wolfSSL 15:117db924cf7c 233 if ((wolfssl_word)key % 4) {
wolfSSL 15:117db924cf7c 234 int alignKey[4];
wolfSSL 15:117db924cf7c 235
wolfSSL 15:117db924cf7c 236 /* iv aligned in SetIV */
wolfSSL 15:117db924cf7c 237 WOLFSSL_MSG("wc_RabbitSetKey unaligned key");
wolfSSL 15:117db924cf7c 238
wolfSSL 15:117db924cf7c 239 XMEMCPY(alignKey, key, sizeof(alignKey));
wolfSSL 15:117db924cf7c 240
wolfSSL 15:117db924cf7c 241 return DoKey(ctx, (const byte*)alignKey, iv);
wolfSSL 15:117db924cf7c 242 }
wolfSSL 15:117db924cf7c 243 #endif /* XSTREAM_ALIGN */
wolfSSL 15:117db924cf7c 244
wolfSSL 15:117db924cf7c 245 return DoKey(ctx, key, iv);
wolfSSL 15:117db924cf7c 246 }
wolfSSL 15:117db924cf7c 247
wolfSSL 15:117db924cf7c 248
wolfSSL 15:117db924cf7c 249 /* Encrypt/decrypt a message of any size */
wolfSSL 15:117db924cf7c 250 static WC_INLINE int DoProcess(Rabbit* ctx, byte* output, const byte* input,
wolfSSL 15:117db924cf7c 251 word32 msglen)
wolfSSL 15:117db924cf7c 252 {
wolfSSL 15:117db924cf7c 253 /* Encrypt/decrypt all full blocks */
wolfSSL 15:117db924cf7c 254 while (msglen >= 16) {
wolfSSL 15:117db924cf7c 255 /* Iterate the system */
wolfSSL 15:117db924cf7c 256 RABBIT_next_state(&(ctx->workCtx));
wolfSSL 15:117db924cf7c 257
wolfSSL 15:117db924cf7c 258 /* Encrypt/decrypt 16 bytes of data */
wolfSSL 15:117db924cf7c 259 *(word32*)(output+ 0) = *(word32*)(input+ 0) ^
wolfSSL 15:117db924cf7c 260 LITTLE32(ctx->workCtx.x[0] ^ (ctx->workCtx.x[5]>>16) ^
wolfSSL 15:117db924cf7c 261 U32V(ctx->workCtx.x[3]<<16));
wolfSSL 15:117db924cf7c 262 *(word32*)(output+ 4) = *(word32*)(input+ 4) ^
wolfSSL 15:117db924cf7c 263 LITTLE32(ctx->workCtx.x[2] ^ (ctx->workCtx.x[7]>>16) ^
wolfSSL 15:117db924cf7c 264 U32V(ctx->workCtx.x[5]<<16));
wolfSSL 15:117db924cf7c 265 *(word32*)(output+ 8) = *(word32*)(input+ 8) ^
wolfSSL 15:117db924cf7c 266 LITTLE32(ctx->workCtx.x[4] ^ (ctx->workCtx.x[1]>>16) ^
wolfSSL 15:117db924cf7c 267 U32V(ctx->workCtx.x[7]<<16));
wolfSSL 15:117db924cf7c 268 *(word32*)(output+12) = *(word32*)(input+12) ^
wolfSSL 15:117db924cf7c 269 LITTLE32(ctx->workCtx.x[6] ^ (ctx->workCtx.x[3]>>16) ^
wolfSSL 15:117db924cf7c 270 U32V(ctx->workCtx.x[1]<<16));
wolfSSL 15:117db924cf7c 271
wolfSSL 15:117db924cf7c 272 /* Increment pointers and decrement length */
wolfSSL 15:117db924cf7c 273 input += 16;
wolfSSL 15:117db924cf7c 274 output += 16;
wolfSSL 15:117db924cf7c 275 msglen -= 16;
wolfSSL 15:117db924cf7c 276 }
wolfSSL 15:117db924cf7c 277
wolfSSL 15:117db924cf7c 278 /* Encrypt/decrypt remaining data */
wolfSSL 15:117db924cf7c 279 if (msglen) {
wolfSSL 15:117db924cf7c 280
wolfSSL 15:117db924cf7c 281 word32 i;
wolfSSL 15:117db924cf7c 282 word32 tmp[4];
wolfSSL 15:117db924cf7c 283 byte* buffer = (byte*)tmp;
wolfSSL 15:117db924cf7c 284
wolfSSL 15:117db924cf7c 285 XMEMSET(tmp, 0, sizeof(tmp)); /* help static analysis */
wolfSSL 15:117db924cf7c 286
wolfSSL 15:117db924cf7c 287 /* Iterate the system */
wolfSSL 15:117db924cf7c 288 RABBIT_next_state(&(ctx->workCtx));
wolfSSL 15:117db924cf7c 289
wolfSSL 15:117db924cf7c 290 /* Generate 16 bytes of pseudo-random data */
wolfSSL 15:117db924cf7c 291 tmp[0] = LITTLE32(ctx->workCtx.x[0] ^
wolfSSL 15:117db924cf7c 292 (ctx->workCtx.x[5]>>16) ^ U32V(ctx->workCtx.x[3]<<16));
wolfSSL 15:117db924cf7c 293 tmp[1] = LITTLE32(ctx->workCtx.x[2] ^
wolfSSL 15:117db924cf7c 294 (ctx->workCtx.x[7]>>16) ^ U32V(ctx->workCtx.x[5]<<16));
wolfSSL 15:117db924cf7c 295 tmp[2] = LITTLE32(ctx->workCtx.x[4] ^
wolfSSL 15:117db924cf7c 296 (ctx->workCtx.x[1]>>16) ^ U32V(ctx->workCtx.x[7]<<16));
wolfSSL 15:117db924cf7c 297 tmp[3] = LITTLE32(ctx->workCtx.x[6] ^
wolfSSL 15:117db924cf7c 298 (ctx->workCtx.x[3]>>16) ^ U32V(ctx->workCtx.x[1]<<16));
wolfSSL 15:117db924cf7c 299
wolfSSL 15:117db924cf7c 300 /* Encrypt/decrypt the data */
wolfSSL 15:117db924cf7c 301 for (i=0; i<msglen; i++)
wolfSSL 15:117db924cf7c 302 output[i] = input[i] ^ buffer[i];
wolfSSL 15:117db924cf7c 303 }
wolfSSL 15:117db924cf7c 304
wolfSSL 15:117db924cf7c 305 return 0;
wolfSSL 15:117db924cf7c 306 }
wolfSSL 15:117db924cf7c 307
wolfSSL 15:117db924cf7c 308
wolfSSL 15:117db924cf7c 309 /* Encrypt/decrypt a message of any size */
wolfSSL 15:117db924cf7c 310 int wc_RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
wolfSSL 15:117db924cf7c 311 {
wolfSSL 15:117db924cf7c 312 if (ctx == NULL || output == NULL || input == NULL) {
wolfSSL 15:117db924cf7c 313 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 314 }
wolfSSL 15:117db924cf7c 315
wolfSSL 15:117db924cf7c 316 #ifdef XSTREAM_ALIGN
wolfSSL 15:117db924cf7c 317 if ((wolfssl_word)input % 4 || (wolfssl_word)output % 4) {
wolfSSL 15:117db924cf7c 318 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 15:117db924cf7c 319 byte* tmp;
wolfSSL 15:117db924cf7c 320 WOLFSSL_MSG("wc_RabbitProcess unaligned");
wolfSSL 15:117db924cf7c 321
wolfSSL 15:117db924cf7c 322 tmp = (byte*)XMALLOC(msglen, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 323 if (tmp == NULL) return MEMORY_E;
wolfSSL 15:117db924cf7c 324
wolfSSL 15:117db924cf7c 325 XMEMCPY(tmp, input, msglen);
wolfSSL 15:117db924cf7c 326 DoProcess(ctx, tmp, tmp, msglen);
wolfSSL 15:117db924cf7c 327 XMEMCPY(output, tmp, msglen);
wolfSSL 15:117db924cf7c 328
wolfSSL 15:117db924cf7c 329 XFREE(tmp, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 330
wolfSSL 15:117db924cf7c 331 return 0;
wolfSSL 15:117db924cf7c 332 #else
wolfSSL 15:117db924cf7c 333 return BAD_ALIGN_E;
wolfSSL 15:117db924cf7c 334 #endif
wolfSSL 15:117db924cf7c 335 }
wolfSSL 15:117db924cf7c 336 #endif /* XSTREAM_ALIGN */
wolfSSL 15:117db924cf7c 337
wolfSSL 15:117db924cf7c 338 return DoProcess(ctx, output, input, msglen);
wolfSSL 15:117db924cf7c 339 }
wolfSSL 15:117db924cf7c 340
wolfSSL 15:117db924cf7c 341
wolfSSL 15:117db924cf7c 342 #endif /* NO_RABBIT */
wolfSSL 15:117db924cf7c 343