cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arc4.c Source File

arc4.c

00001 /* arc4.c
00002  *
00003  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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/arc4.h>
00027 
00028 
00029 #ifdef HAVE_CAVIUM
00030     static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length);
00031     static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
00032                                   word32 length);
00033 #endif
00034 
00035 
00036 void Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
00037 {
00038     word32 i;
00039     word32 keyIndex = 0, stateIndex = 0;
00040 
00041 #ifdef HAVE_CAVIUM
00042     if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
00043         return Arc4CaviumSetKey(arc4, key, length);
00044 #endif
00045 
00046     arc4->x = 1;
00047     arc4->y = 0;
00048 
00049     for (i = 0; i < ARC4_STATE_SIZE; i++)
00050         arc4->state[i] = (byte)i;
00051 
00052     for (i = 0; i < ARC4_STATE_SIZE; i++) {
00053         word32 a = arc4->state[i];
00054         stateIndex += key[keyIndex] + a;
00055         stateIndex &= 0xFF;
00056         arc4->state[i] = arc4->state[stateIndex];
00057         arc4->state[stateIndex] = (byte)a;
00058 
00059         if (++keyIndex >= length)
00060             keyIndex = 0;
00061     }
00062 }
00063 
00064 
00065 static INLINE byte MakeByte(word32* x, word32* y, byte* s)
00066 {
00067     word32 a = s[*x], b;
00068     *y = (*y+a) & 0xff;
00069 
00070     b = s[*y];
00071     s[*x] = (byte)b;
00072     s[*y] = (byte)a;
00073     *x = (*x+1) & 0xff;
00074 
00075     return s[(a+b) & 0xff];
00076 }
00077 
00078 
00079 void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
00080 {
00081     word32 x;
00082     word32 y;
00083 
00084 #ifdef HAVE_CAVIUM
00085     if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
00086         return Arc4CaviumProcess(arc4, out, in, length);
00087 #endif
00088 
00089     x = arc4->x;
00090     y = arc4->y;
00091 
00092     while(length--)
00093         *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
00094 
00095     arc4->x = (byte)x;
00096     arc4->y = (byte)y;
00097 }
00098 
00099 
00100 #ifdef HAVE_CAVIUM
00101 
00102 #include <cyassl/ctaocrypt/logging.h>
00103 #include "cavium_common.h"
00104 
00105 /* Initiliaze Arc4 for use with Nitrox device */
00106 int Arc4InitCavium(Arc4* arc4, int devId)
00107 {
00108     if (arc4 == NULL)
00109         return -1;
00110 
00111     if (CspAllocContext(CONTEXT_SSL, &arc4->contextHandle, devId) != 0)
00112         return -1;
00113 
00114     arc4->devId = devId;
00115     arc4->magic = CYASSL_ARC4_CAVIUM_MAGIC;
00116    
00117     return 0;
00118 }
00119 
00120 
00121 /* Free Arc4 from use with Nitrox device */
00122 void Arc4FreeCavium(Arc4* arc4)
00123 {
00124     if (arc4 == NULL)
00125         return;
00126 
00127     if (arc4->magic != CYASSL_ARC4_CAVIUM_MAGIC)
00128         return;
00129 
00130     CspFreeContext(CONTEXT_SSL, arc4->contextHandle, arc4->devId);
00131     arc4->magic = 0;
00132 }
00133 
00134 
00135 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length)
00136 {
00137     word32 requestId;
00138 
00139     if (CspInitializeRc4(CAVIUM_BLOCKING, arc4->contextHandle, length,
00140                          (byte*)key, &requestId, arc4->devId) != 0) {
00141         CYASSL_MSG("Bad Cavium Arc4 Init");
00142     }
00143 }
00144 
00145 
00146 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
00147                               word32 length)
00148 {
00149     word   offset = 0;
00150     word32 requestId;
00151 
00152     while (length > CYASSL_MAX_16BIT) {
00153         word16 slen = (word16)CYASSL_MAX_16BIT;
00154         if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE,
00155                           slen, (byte*)in + offset, out + offset, &requestId,
00156                           arc4->devId) != 0) {
00157             CYASSL_MSG("Bad Cavium Arc4 Encrypt");
00158         }
00159         length -= CYASSL_MAX_16BIT;
00160         offset += CYASSL_MAX_16BIT;
00161     }
00162     if (length) {
00163         word16 slen = (word16)length;
00164         if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE,
00165                           slen, (byte*)in + offset, out + offset, &requestId,
00166                           arc4->devId) != 0) {
00167             CYASSL_MSG("Bad Cavium Arc4 Encrypt");
00168         }
00169     }
00170 }
00171 
00172 #endif /* HAVE_CAVIUM */