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

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

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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