3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <cyassl/ctaocrypt/settings.h>
30 #include <cyassl/ctaocrypt/arc4.h>
34 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length);
35 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
40 void Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
43 word32 keyIndex = 0, stateIndex = 0;
46 if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
47 return Arc4CaviumSetKey(arc4, key, length);
53 for (i = 0; i < ARC4_STATE_SIZE; i++)
54 arc4->state[i] = (byte)i;
56 for (i = 0; i < ARC4_STATE_SIZE; i++) {
57 word32 a = arc4->state[i];
58 stateIndex += key[keyIndex] + a;
60 arc4->state[i] = arc4->state[stateIndex];
61 arc4->state[stateIndex] = (byte)a;
63 if (++keyIndex >= length)
69 static INLINE byte MakeByte(word32* x, word32* y, byte* s)
79 return s[(a+b) & 0xff];
83 void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
89 if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
90 return Arc4CaviumProcess(arc4, out, in, length);
97 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
106 #include <cyassl/ctaocrypt/logging.h>
107 #include "cavium_common.h"
109 /* Initiliaze Arc4 for use with Nitrox device */
110 int Arc4InitCavium(Arc4* arc4, int devId)
115 if (CspAllocContext(CONTEXT_SSL, &arc4->contextHandle, devId) != 0)
119 arc4->magic = CYASSL_ARC4_CAVIUM_MAGIC;
125 /* Free Arc4 from use with Nitrox device */
126 void Arc4FreeCavium(Arc4* arc4)
131 if (arc4->magic != CYASSL_ARC4_CAVIUM_MAGIC)
134 CspFreeContext(CONTEXT_SSL, arc4->contextHandle, arc4->devId);
139 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length)
143 if (CspInitializeRc4(CAVIUM_BLOCKING, arc4->contextHandle, length,
144 (byte*)key, &requestId, arc4->devId) != 0) {
145 CYASSL_MSG("Bad Cavium Arc4 Init");
150 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
156 while (length > CYASSL_MAX_16BIT) {
157 word16 slen = (word16)CYASSL_MAX_16BIT;
158 if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE,
159 slen, (byte*)in + offset, out + offset, &requestId,
161 CYASSL_MSG("Bad Cavium Arc4 Encrypt");
163 length -= CYASSL_MAX_16BIT;
164 offset += CYASSL_MAX_16BIT;
167 word16 slen = (word16)length;
168 if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE,
169 slen, (byte*)in + offset, out + offset, &requestId,
171 CYASSL_MSG("Bad Cavium Arc4 Encrypt");
176 #endif /* HAVE_CAVIUM */