fork of cyassl-lib

Dependents:   TLS_cyassl TLS_cyassl

Committer:
feb11
Date:
Mon Sep 16 09:53:35 2013 +0000
Revision:
4:f377303c41be
Parent:
0:714293de3836
changed settings

Who changed what in which revision?

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