wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

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?

UserRevisionLine numberNew contents of line
wolfSSL 11:cee25a834751 1 /* arc4.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_RC4
wolfSSL 11:cee25a834751 30
wolfSSL 11:cee25a834751 31 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 11:cee25a834751 32 #include <wolfssl/wolfcrypt/arc4.h>
wolfSSL 11:cee25a834751 33
wolfSSL 11:cee25a834751 34
wolfSSL 11:cee25a834751 35 int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
wolfSSL 11:cee25a834751 36 {
wolfSSL 11:cee25a834751 37 int ret = 0;
wolfSSL 11:cee25a834751 38 word32 i;
wolfSSL 11:cee25a834751 39 word32 keyIndex = 0, stateIndex = 0;
wolfSSL 11:cee25a834751 40
wolfSSL 11:cee25a834751 41 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
wolfSSL 11:cee25a834751 42 defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
wolfSSL 11:cee25a834751 43 if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
wolfSSL 11:cee25a834751 44 return NitroxArc4SetKey(arc4, key, length);
wolfSSL 11:cee25a834751 45 }
wolfSSL 11:cee25a834751 46 #endif
wolfSSL 11:cee25a834751 47
wolfSSL 11:cee25a834751 48 arc4->x = 1;
wolfSSL 11:cee25a834751 49 arc4->y = 0;
wolfSSL 11:cee25a834751 50
wolfSSL 11:cee25a834751 51 for (i = 0; i < ARC4_STATE_SIZE; i++)
wolfSSL 11:cee25a834751 52 arc4->state[i] = (byte)i;
wolfSSL 11:cee25a834751 53
wolfSSL 11:cee25a834751 54 for (i = 0; i < ARC4_STATE_SIZE; i++) {
wolfSSL 11:cee25a834751 55 word32 a = arc4->state[i];
wolfSSL 11:cee25a834751 56 stateIndex += key[keyIndex] + a;
wolfSSL 11:cee25a834751 57 stateIndex &= 0xFF;
wolfSSL 11:cee25a834751 58 arc4->state[i] = arc4->state[stateIndex];
wolfSSL 11:cee25a834751 59 arc4->state[stateIndex] = (byte)a;
wolfSSL 11:cee25a834751 60
wolfSSL 11:cee25a834751 61 if (++keyIndex >= length)
wolfSSL 11:cee25a834751 62 keyIndex = 0;
wolfSSL 11:cee25a834751 63 }
wolfSSL 11:cee25a834751 64
wolfSSL 11:cee25a834751 65 return ret;
wolfSSL 11:cee25a834751 66 }
wolfSSL 11:cee25a834751 67
wolfSSL 11:cee25a834751 68
wolfSSL 11:cee25a834751 69 static INLINE byte MakeByte(word32* x, word32* y, byte* s)
wolfSSL 11:cee25a834751 70 {
wolfSSL 11:cee25a834751 71 word32 a = s[*x], b;
wolfSSL 11:cee25a834751 72 *y = (*y+a) & 0xff;
wolfSSL 11:cee25a834751 73
wolfSSL 11:cee25a834751 74 b = s[*y];
wolfSSL 11:cee25a834751 75 s[*x] = (byte)b;
wolfSSL 11:cee25a834751 76 s[*y] = (byte)a;
wolfSSL 11:cee25a834751 77 *x = (*x+1) & 0xff;
wolfSSL 11:cee25a834751 78
wolfSSL 11:cee25a834751 79 return s[(a+b) & 0xff];
wolfSSL 11:cee25a834751 80 }
wolfSSL 11:cee25a834751 81
wolfSSL 11:cee25a834751 82
wolfSSL 11:cee25a834751 83 int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
wolfSSL 11:cee25a834751 84 {
wolfSSL 11:cee25a834751 85 int ret = 0;
wolfSSL 11:cee25a834751 86 word32 x;
wolfSSL 11:cee25a834751 87 word32 y;
wolfSSL 11:cee25a834751 88
wolfSSL 11:cee25a834751 89 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
wolfSSL 11:cee25a834751 90 defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
wolfSSL 11:cee25a834751 91 if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
wolfSSL 11:cee25a834751 92 return NitroxArc4Process(arc4, out, in, length);
wolfSSL 11:cee25a834751 93 }
wolfSSL 11:cee25a834751 94 #endif
wolfSSL 11:cee25a834751 95
wolfSSL 11:cee25a834751 96 x = arc4->x;
wolfSSL 11:cee25a834751 97 y = arc4->y;
wolfSSL 11:cee25a834751 98
wolfSSL 11:cee25a834751 99 while(length--)
wolfSSL 11:cee25a834751 100 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
wolfSSL 11:cee25a834751 101
wolfSSL 11:cee25a834751 102 arc4->x = (byte)x;
wolfSSL 11:cee25a834751 103 arc4->y = (byte)y;
wolfSSL 11:cee25a834751 104
wolfSSL 11:cee25a834751 105 return ret;
wolfSSL 11:cee25a834751 106 }
wolfSSL 11:cee25a834751 107
wolfSSL 11:cee25a834751 108 /* Initialize Arc4 for use with async device */
wolfSSL 11:cee25a834751 109 int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
wolfSSL 11:cee25a834751 110 {
wolfSSL 11:cee25a834751 111 int ret = 0;
wolfSSL 11:cee25a834751 112
wolfSSL 11:cee25a834751 113 if (arc4 == NULL)
wolfSSL 11:cee25a834751 114 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 115
wolfSSL 11:cee25a834751 116 arc4->heap = heap;
wolfSSL 11:cee25a834751 117
wolfSSL 11:cee25a834751 118 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
wolfSSL 11:cee25a834751 119 ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
wolfSSL 11:cee25a834751 120 arc4->heap, devId);
wolfSSL 11:cee25a834751 121 #else
wolfSSL 11:cee25a834751 122 (void)devId;
wolfSSL 11:cee25a834751 123 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 124
wolfSSL 11:cee25a834751 125 return ret;
wolfSSL 11:cee25a834751 126 }
wolfSSL 11:cee25a834751 127
wolfSSL 11:cee25a834751 128
wolfSSL 11:cee25a834751 129 /* Free Arc4 from use with async device */
wolfSSL 11:cee25a834751 130 void wc_Arc4Free(Arc4* arc4)
wolfSSL 11:cee25a834751 131 {
wolfSSL 11:cee25a834751 132 if (arc4 == NULL)
wolfSSL 11:cee25a834751 133 return;
wolfSSL 11:cee25a834751 134
wolfSSL 11:cee25a834751 135 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
wolfSSL 11:cee25a834751 136 wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
wolfSSL 11:cee25a834751 137 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 11:cee25a834751 138 }
wolfSSL 11:cee25a834751 139
wolfSSL 11:cee25a834751 140 #endif /* NO_RC4 */
wolfSSL 11:cee25a834751 141
wolfSSL 11:cee25a834751 142