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