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

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

Committer:
wolfSSL
Date:
Fri Jun 05 00:11:07 2020 +0000
Revision:
17:a5f916481144
Parent:
16:8e0d178b1d1e
wolfSSL 4.4.0

Who changed what in which revision?

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