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