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.
Fork of wolfSSL by
arc4.c
00001 /* arc4.c 00002 * 00003 * Copyright (C) 2006-2016 wolfSSL Inc. 00004 * 00005 * This file is part of wolfSSL. 00006 * 00007 * wolfSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * wolfSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 00020 */ 00021 00022 00023 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <wolfssl/wolfcrypt/settings.h> 00028 00029 #ifndef NO_RC4 00030 00031 #include <wolfssl/wolfcrypt/error-crypt.h> 00032 #include <wolfssl/wolfcrypt/arc4.h> 00033 00034 00035 int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length) 00036 { 00037 int ret = 0; 00038 word32 i; 00039 word32 keyIndex = 0, stateIndex = 0; 00040 00041 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \ 00042 defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V) 00043 if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) { 00044 return NitroxArc4SetKey(arc4, key, length); 00045 } 00046 #endif 00047 00048 arc4->x = 1; 00049 arc4->y = 0; 00050 00051 for (i = 0; i < ARC4_STATE_SIZE; i++) 00052 arc4->state[i] = (byte)i; 00053 00054 for (i = 0; i < ARC4_STATE_SIZE; i++) { 00055 word32 a = arc4->state[i]; 00056 stateIndex += key[keyIndex] + a; 00057 stateIndex &= 0xFF; 00058 arc4->state[i] = arc4->state[stateIndex]; 00059 arc4->state[stateIndex] = (byte)a; 00060 00061 if (++keyIndex >= length) 00062 keyIndex = 0; 00063 } 00064 00065 return ret; 00066 } 00067 00068 00069 static INLINE byte MakeByte(word32* x, word32* y, byte* s) 00070 { 00071 word32 a = s[*x], b; 00072 *y = (*y+a) & 0xff; 00073 00074 b = s[*y]; 00075 s[*x] = (byte)b; 00076 s[*y] = (byte)a; 00077 *x = (*x+1) & 0xff; 00078 00079 return s[(a+b) & 0xff]; 00080 } 00081 00082 00083 int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length) 00084 { 00085 int ret = 0; 00086 word32 x; 00087 word32 y; 00088 00089 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \ 00090 defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V) 00091 if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) { 00092 return NitroxArc4Process(arc4, out, in, length); 00093 } 00094 #endif 00095 00096 x = arc4->x; 00097 y = arc4->y; 00098 00099 while(length--) 00100 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state); 00101 00102 arc4->x = (byte)x; 00103 arc4->y = (byte)y; 00104 00105 return ret; 00106 } 00107 00108 /* Initialize Arc4 for use with async device */ 00109 int wc_Arc4Init(Arc4* arc4, void* heap, int devId) 00110 { 00111 int ret = 0; 00112 00113 if (arc4 == NULL) 00114 return BAD_FUNC_ARG; 00115 00116 arc4->heap = heap; 00117 00118 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) 00119 ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4, 00120 arc4->heap, devId); 00121 #else 00122 (void)devId; 00123 #endif /* WOLFSSL_ASYNC_CRYPT */ 00124 00125 return ret; 00126 } 00127 00128 00129 /* Free Arc4 from use with async device */ 00130 void wc_Arc4Free(Arc4* arc4) 00131 { 00132 if (arc4 == NULL) 00133 return; 00134 00135 #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) 00136 wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4); 00137 #endif /* WOLFSSL_ASYNC_CRYPT */ 00138 } 00139 00140 #endif /* NO_RC4 */ 00141 00142
Generated on Tue Jul 12 2022 23:30:53 by
1.7.2
