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