wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 13:f67a6c6013ca 1 /* arc4.c
wolfSSL 13:f67a6c6013ca 2 *
wolfSSL 13:f67a6c6013ca 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 13:f67a6c6013ca 4 *
wolfSSL 13:f67a6c6013ca 5 * This file is part of wolfSSL.
wolfSSL 13:f67a6c6013ca 6 *
wolfSSL 13:f67a6c6013ca 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 13:f67a6c6013ca 8 * it under the terms of the GNU General Public License as published by
wolfSSL 13:f67a6c6013ca 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 13:f67a6c6013ca 10 * (at your option) any later version.
wolfSSL 13:f67a6c6013ca 11 *
wolfSSL 13:f67a6c6013ca 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 13:f67a6c6013ca 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 13:f67a6c6013ca 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 13:f67a6c6013ca 15 * GNU General Public License for more details.
wolfSSL 13:f67a6c6013ca 16 *
wolfSSL 13:f67a6c6013ca 17 * You should have received a copy of the GNU General Public License
wolfSSL 13:f67a6c6013ca 18 * along with this program; if not, write to the Free Software
wolfSSL 13:f67a6c6013ca 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 13:f67a6c6013ca 20 */
wolfSSL 13:f67a6c6013ca 21
wolfSSL 13:f67a6c6013ca 22
wolfSSL 13:f67a6c6013ca 23 #ifdef HAVE_CONFIG_H
wolfSSL 13:f67a6c6013ca 24 #include <config.h>
wolfSSL 13:f67a6c6013ca 25 #endif
wolfSSL 13:f67a6c6013ca 26
wolfSSL 13:f67a6c6013ca 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 13:f67a6c6013ca 28
wolfSSL 13:f67a6c6013ca 29 #ifndef NO_RC4
wolfSSL 13:f67a6c6013ca 30
wolfSSL 13:f67a6c6013ca 31 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 13:f67a6c6013ca 32 #include <wolfssl/wolfcrypt/arc4.h>
wolfSSL 13:f67a6c6013ca 33
wolfSSL 13:f67a6c6013ca 34
wolfSSL 13:f67a6c6013ca 35 int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
wolfSSL 13:f67a6c6013ca 36 {
wolfSSL 13:f67a6c6013ca 37 int ret = 0;
wolfSSL 13:f67a6c6013ca 38 word32 i;
wolfSSL 13:f67a6c6013ca 39 word32 keyIndex = 0, stateIndex = 0;
wolfSSL 13:f67a6c6013ca 40
wolfSSL 13:f67a6c6013ca 41 if (arc4 == NULL || key == NULL) {
wolfSSL 13:f67a6c6013ca 42 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 43 }
wolfSSL 13:f67a6c6013ca 44
wolfSSL 13:f67a6c6013ca 45 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
wolfSSL 13:f67a6c6013ca 46 defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
wolfSSL 13:f67a6c6013ca 47 if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
wolfSSL 13:f67a6c6013ca 48 return NitroxArc4SetKey(arc4, key, length);
wolfSSL 13:f67a6c6013ca 49 }
wolfSSL 13:f67a6c6013ca 50 #endif
wolfSSL 13:f67a6c6013ca 51
wolfSSL 13:f67a6c6013ca 52 arc4->x = 1;
wolfSSL 13:f67a6c6013ca 53 arc4->y = 0;
wolfSSL 13:f67a6c6013ca 54
wolfSSL 13:f67a6c6013ca 55 for (i = 0; i < ARC4_STATE_SIZE; i++)
wolfSSL 13:f67a6c6013ca 56 arc4->state[i] = (byte)i;
wolfSSL 13:f67a6c6013ca 57
wolfSSL 13:f67a6c6013ca 58 for (i = 0; i < ARC4_STATE_SIZE; i++) {
wolfSSL 13:f67a6c6013ca 59 word32 a = arc4->state[i];
wolfSSL 13:f67a6c6013ca 60 stateIndex += key[keyIndex] + a;
wolfSSL 13:f67a6c6013ca 61 stateIndex &= 0xFF;
wolfSSL 13:f67a6c6013ca 62 arc4->state[i] = arc4->state[stateIndex];
wolfSSL 13:f67a6c6013ca 63 arc4->state[stateIndex] = (byte)a;
wolfSSL 13:f67a6c6013ca 64
wolfSSL 13:f67a6c6013ca 65 if (++keyIndex >= length)
wolfSSL 13:f67a6c6013ca 66 keyIndex = 0;
wolfSSL 13:f67a6c6013ca 67 }
wolfSSL 13:f67a6c6013ca 68
wolfSSL 13:f67a6c6013ca 69 return ret;
wolfSSL 13:f67a6c6013ca 70 }
wolfSSL 13:f67a6c6013ca 71
wolfSSL 13:f67a6c6013ca 72
wolfSSL 13:f67a6c6013ca 73 static INLINE byte MakeByte(word32* x, word32* y, byte* s)
wolfSSL 13:f67a6c6013ca 74 {
wolfSSL 13:f67a6c6013ca 75 word32 a = s[*x], b;
wolfSSL 13:f67a6c6013ca 76 *y = (*y+a) & 0xff;
wolfSSL 13:f67a6c6013ca 77
wolfSSL 13:f67a6c6013ca 78 b = s[*y];
wolfSSL 13:f67a6c6013ca 79 s[*x] = (byte)b;
wolfSSL 13:f67a6c6013ca 80 s[*y] = (byte)a;
wolfSSL 13:f67a6c6013ca 81 *x = (*x+1) & 0xff;
wolfSSL 13:f67a6c6013ca 82
wolfSSL 13:f67a6c6013ca 83 return s[(a+b) & 0xff];
wolfSSL 13:f67a6c6013ca 84 }
wolfSSL 13:f67a6c6013ca 85
wolfSSL 13:f67a6c6013ca 86
wolfSSL 13:f67a6c6013ca 87 int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
wolfSSL 13:f67a6c6013ca 88 {
wolfSSL 13:f67a6c6013ca 89 int ret = 0;
wolfSSL 13:f67a6c6013ca 90 word32 x;
wolfSSL 13:f67a6c6013ca 91 word32 y;
wolfSSL 13:f67a6c6013ca 92
wolfSSL 13:f67a6c6013ca 93 if (arc4 == NULL || out == NULL || in == NULL) {
wolfSSL 13:f67a6c6013ca 94 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 95 }
wolfSSL 13:f67a6c6013ca 96
wolfSSL 13:f67a6c6013ca 97 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
wolfSSL 13:f67a6c6013ca 98 defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
wolfSSL 13:f67a6c6013ca 99 if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
wolfSSL 13:f67a6c6013ca 100 return NitroxArc4Process(arc4, out, in, length);
wolfSSL 13:f67a6c6013ca 101 }
wolfSSL 13:f67a6c6013ca 102 #endif
wolfSSL 13:f67a6c6013ca 103
wolfSSL 13:f67a6c6013ca 104 x = arc4->x;
wolfSSL 13:f67a6c6013ca 105 y = arc4->y;
wolfSSL 13:f67a6c6013ca 106
wolfSSL 13:f67a6c6013ca 107 while(length--)
wolfSSL 13:f67a6c6013ca 108 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
wolfSSL 13:f67a6c6013ca 109
wolfSSL 13:f67a6c6013ca 110 arc4->x = (byte)x;
wolfSSL 13:f67a6c6013ca 111 arc4->y = (byte)y;
wolfSSL 13:f67a6c6013ca 112
wolfSSL 13:f67a6c6013ca 113 return ret;
wolfSSL 13:f67a6c6013ca 114 }
wolfSSL 13:f67a6c6013ca 115
wolfSSL 13:f67a6c6013ca 116 /* Initialize Arc4 for use with async device */
wolfSSL 13:f67a6c6013ca 117 int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
wolfSSL 13:f67a6c6013ca 118 {
wolfSSL 13:f67a6c6013ca 119 int ret = 0;
wolfSSL 13:f67a6c6013ca 120
wolfSSL 13:f67a6c6013ca 121 if (arc4 == NULL)
wolfSSL 13:f67a6c6013ca 122 return BAD_FUNC_ARG;
wolfSSL 13:f67a6c6013ca 123
wolfSSL 13:f67a6c6013ca 124 arc4->heap = heap;
wolfSSL 13:f67a6c6013ca 125
wolfSSL 13:f67a6c6013ca 126 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
wolfSSL 13:f67a6c6013ca 127 ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
wolfSSL 13:f67a6c6013ca 128 arc4->heap, devId);
wolfSSL 13:f67a6c6013ca 129 #else
wolfSSL 13:f67a6c6013ca 130 (void)devId;
wolfSSL 13:f67a6c6013ca 131 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 13:f67a6c6013ca 132
wolfSSL 13:f67a6c6013ca 133 return ret;
wolfSSL 13:f67a6c6013ca 134 }
wolfSSL 13:f67a6c6013ca 135
wolfSSL 13:f67a6c6013ca 136
wolfSSL 13:f67a6c6013ca 137 /* Free Arc4 from use with async device */
wolfSSL 13:f67a6c6013ca 138 void wc_Arc4Free(Arc4* arc4)
wolfSSL 13:f67a6c6013ca 139 {
wolfSSL 13:f67a6c6013ca 140 if (arc4 == NULL)
wolfSSL 13:f67a6c6013ca 141 return;
wolfSSL 13:f67a6c6013ca 142
wolfSSL 13:f67a6c6013ca 143 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
wolfSSL 13:f67a6c6013ca 144 wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
wolfSSL 13:f67a6c6013ca 145 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 13:f67a6c6013ca 146 }
wolfSSL 13:f67a6c6013ca 147
wolfSSL 13:f67a6c6013ca 148 #endif /* NO_RC4 */
wolfSSL 13:f67a6c6013ca 149
wolfSSL 13:f67a6c6013ca 150