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.
arc4.c
00001 /* arc4.c 00002 * 00003 * Copyright (C) 2006-2013 wolfSSL Inc. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL 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 * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00020 */ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <cyassl/ctaocrypt/settings.h> 00027 00028 #ifndef NO_RC4 00029 00030 #include <cyassl/ctaocrypt/arc4.h> 00031 00032 00033 #ifdef HAVE_CAVIUM 00034 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length); 00035 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in, 00036 word32 length); 00037 #endif 00038 00039 00040 void Arc4SetKey(Arc4* arc4, const byte* key, word32 length) 00041 { 00042 word32 i; 00043 word32 keyIndex = 0, stateIndex = 0; 00044 00045 #ifdef HAVE_CAVIUM 00046 if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC) 00047 return Arc4CaviumSetKey(arc4, key, length); 00048 #endif 00049 00050 arc4->x = 1; 00051 arc4->y = 0; 00052 00053 for (i = 0; i < ARC4_STATE_SIZE; i++) 00054 arc4->state[i] = (byte)i; 00055 00056 for (i = 0; i < ARC4_STATE_SIZE; i++) { 00057 word32 a = arc4->state[i]; 00058 stateIndex += key[keyIndex] + a; 00059 stateIndex &= 0xFF; 00060 arc4->state[i] = arc4->state[stateIndex]; 00061 arc4->state[stateIndex] = (byte)a; 00062 00063 if (++keyIndex >= length) 00064 keyIndex = 0; 00065 } 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 void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length) 00084 { 00085 word32 x; 00086 word32 y; 00087 00088 #ifdef HAVE_CAVIUM 00089 if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC) 00090 return Arc4CaviumProcess(arc4, out, in, length); 00091 #endif 00092 00093 x = arc4->x; 00094 y = arc4->y; 00095 00096 while(length--) 00097 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state); 00098 00099 arc4->x = (byte)x; 00100 arc4->y = (byte)y; 00101 } 00102 00103 00104 #ifdef HAVE_CAVIUM 00105 00106 #include <cyassl/ctaocrypt/logging.h> 00107 #include "cavium_common.h" 00108 00109 /* Initiliaze Arc4 for use with Nitrox device */ 00110 int Arc4InitCavium(Arc4* arc4, int devId) 00111 { 00112 if (arc4 == NULL) 00113 return -1; 00114 00115 if (CspAllocContext(CONTEXT_SSL, &arc4->contextHandle, devId) != 0) 00116 return -1; 00117 00118 arc4->devId = devId; 00119 arc4->magic = CYASSL_ARC4_CAVIUM_MAGIC; 00120 00121 return 0; 00122 } 00123 00124 00125 /* Free Arc4 from use with Nitrox device */ 00126 void Arc4FreeCavium(Arc4* arc4) 00127 { 00128 if (arc4 == NULL) 00129 return; 00130 00131 if (arc4->magic != CYASSL_ARC4_CAVIUM_MAGIC) 00132 return; 00133 00134 CspFreeContext(CONTEXT_SSL, arc4->contextHandle, arc4->devId); 00135 arc4->magic = 0; 00136 } 00137 00138 00139 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length) 00140 { 00141 word32 requestId; 00142 00143 if (CspInitializeRc4(CAVIUM_BLOCKING, arc4->contextHandle, length, 00144 (byte*)key, &requestId, arc4->devId) != 0) { 00145 CYASSL_MSG("Bad Cavium Arc4 Init"); 00146 } 00147 } 00148 00149 00150 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in, 00151 word32 length) 00152 { 00153 word offset = 0; 00154 word32 requestId; 00155 00156 while (length > CYASSL_MAX_16BIT) { 00157 word16 slen = (word16)CYASSL_MAX_16BIT; 00158 if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE, 00159 slen, (byte*)in + offset, out + offset, &requestId, 00160 arc4->devId) != 0) { 00161 CYASSL_MSG("Bad Cavium Arc4 Encrypt"); 00162 } 00163 length -= CYASSL_MAX_16BIT; 00164 offset += CYASSL_MAX_16BIT; 00165 } 00166 if (length) { 00167 word16 slen = (word16)length; 00168 if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE, 00169 slen, (byte*)in + offset, out + offset, &requestId, 00170 arc4->devId) != 0) { 00171 CYASSL_MSG("Bad Cavium Arc4 Encrypt"); 00172 } 00173 } 00174 } 00175 00176 #endif /* HAVE_CAVIUM */ 00177 00178 #endif /* NO_ARC4 */ 00179
Generated on Tue Jul 12 2022 20:12:50 by
