Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:1239e9b70ca2 1 /* arc4.c
wolfSSL 0:1239e9b70ca2 2 *
wolfSSL 0:1239e9b70ca2 3 * Copyright (C) 2006-2014 wolfSSL Inc.
wolfSSL 0:1239e9b70ca2 4 *
wolfSSL 0:1239e9b70ca2 5 * This file is part of CyaSSL.
wolfSSL 0:1239e9b70ca2 6 *
wolfSSL 0:1239e9b70ca2 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:1239e9b70ca2 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:1239e9b70ca2 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:1239e9b70ca2 10 * (at your option) any later version.
wolfSSL 0:1239e9b70ca2 11 *
wolfSSL 0:1239e9b70ca2 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:1239e9b70ca2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:1239e9b70ca2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:1239e9b70ca2 15 * GNU General Public License for more details.
wolfSSL 0:1239e9b70ca2 16 *
wolfSSL 0:1239e9b70ca2 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:1239e9b70ca2 18 * along with this program; if not, write to the Free Software
wolfSSL 0:1239e9b70ca2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:1239e9b70ca2 20 */
wolfSSL 0:1239e9b70ca2 21
wolfSSL 0:1239e9b70ca2 22 #ifdef HAVE_CONFIG_H
wolfSSL 0:1239e9b70ca2 23 #include <config.h>
wolfSSL 0:1239e9b70ca2 24 #endif
wolfSSL 0:1239e9b70ca2 25
wolfSSL 0:1239e9b70ca2 26 #include <cyassl/ctaocrypt/settings.h>
wolfSSL 0:1239e9b70ca2 27
wolfSSL 0:1239e9b70ca2 28 #ifndef NO_RC4
wolfSSL 0:1239e9b70ca2 29
wolfSSL 0:1239e9b70ca2 30 #include <cyassl/ctaocrypt/arc4.h>
wolfSSL 0:1239e9b70ca2 31
wolfSSL 0:1239e9b70ca2 32
wolfSSL 0:1239e9b70ca2 33 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 34 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length);
wolfSSL 0:1239e9b70ca2 35 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
wolfSSL 0:1239e9b70ca2 36 word32 length);
wolfSSL 0:1239e9b70ca2 37 #endif
wolfSSL 0:1239e9b70ca2 38
wolfSSL 0:1239e9b70ca2 39
wolfSSL 0:1239e9b70ca2 40 void Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
wolfSSL 0:1239e9b70ca2 41 {
wolfSSL 0:1239e9b70ca2 42 word32 i;
wolfSSL 0:1239e9b70ca2 43 word32 keyIndex = 0, stateIndex = 0;
wolfSSL 0:1239e9b70ca2 44
wolfSSL 0:1239e9b70ca2 45 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 46 if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 47 return Arc4CaviumSetKey(arc4, key, length);
wolfSSL 0:1239e9b70ca2 48 #endif
wolfSSL 0:1239e9b70ca2 49
wolfSSL 0:1239e9b70ca2 50 arc4->x = 1;
wolfSSL 0:1239e9b70ca2 51 arc4->y = 0;
wolfSSL 0:1239e9b70ca2 52
wolfSSL 0:1239e9b70ca2 53 for (i = 0; i < ARC4_STATE_SIZE; i++)
wolfSSL 0:1239e9b70ca2 54 arc4->state[i] = (byte)i;
wolfSSL 0:1239e9b70ca2 55
wolfSSL 0:1239e9b70ca2 56 for (i = 0; i < ARC4_STATE_SIZE; i++) {
wolfSSL 0:1239e9b70ca2 57 word32 a = arc4->state[i];
wolfSSL 0:1239e9b70ca2 58 stateIndex += key[keyIndex] + a;
wolfSSL 0:1239e9b70ca2 59 stateIndex &= 0xFF;
wolfSSL 0:1239e9b70ca2 60 arc4->state[i] = arc4->state[stateIndex];
wolfSSL 0:1239e9b70ca2 61 arc4->state[stateIndex] = (byte)a;
wolfSSL 0:1239e9b70ca2 62
wolfSSL 0:1239e9b70ca2 63 if (++keyIndex >= length)
wolfSSL 0:1239e9b70ca2 64 keyIndex = 0;
wolfSSL 0:1239e9b70ca2 65 }
wolfSSL 0:1239e9b70ca2 66 }
wolfSSL 0:1239e9b70ca2 67
wolfSSL 0:1239e9b70ca2 68
wolfSSL 0:1239e9b70ca2 69 static INLINE byte MakeByte(word32* x, word32* y, byte* s)
wolfSSL 0:1239e9b70ca2 70 {
wolfSSL 0:1239e9b70ca2 71 word32 a = s[*x], b;
wolfSSL 0:1239e9b70ca2 72 *y = (*y+a) & 0xff;
wolfSSL 0:1239e9b70ca2 73
wolfSSL 0:1239e9b70ca2 74 b = s[*y];
wolfSSL 0:1239e9b70ca2 75 s[*x] = (byte)b;
wolfSSL 0:1239e9b70ca2 76 s[*y] = (byte)a;
wolfSSL 0:1239e9b70ca2 77 *x = (*x+1) & 0xff;
wolfSSL 0:1239e9b70ca2 78
wolfSSL 0:1239e9b70ca2 79 return s[(a+b) & 0xff];
wolfSSL 0:1239e9b70ca2 80 }
wolfSSL 0:1239e9b70ca2 81
wolfSSL 0:1239e9b70ca2 82
wolfSSL 0:1239e9b70ca2 83 void Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
wolfSSL 0:1239e9b70ca2 84 {
wolfSSL 0:1239e9b70ca2 85 word32 x;
wolfSSL 0:1239e9b70ca2 86 word32 y;
wolfSSL 0:1239e9b70ca2 87
wolfSSL 0:1239e9b70ca2 88 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 89 if (arc4->magic == CYASSL_ARC4_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 90 return Arc4CaviumProcess(arc4, out, in, length);
wolfSSL 0:1239e9b70ca2 91 #endif
wolfSSL 0:1239e9b70ca2 92
wolfSSL 0:1239e9b70ca2 93 x = arc4->x;
wolfSSL 0:1239e9b70ca2 94 y = arc4->y;
wolfSSL 0:1239e9b70ca2 95
wolfSSL 0:1239e9b70ca2 96 while(length--)
wolfSSL 0:1239e9b70ca2 97 *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
wolfSSL 0:1239e9b70ca2 98
wolfSSL 0:1239e9b70ca2 99 arc4->x = (byte)x;
wolfSSL 0:1239e9b70ca2 100 arc4->y = (byte)y;
wolfSSL 0:1239e9b70ca2 101 }
wolfSSL 0:1239e9b70ca2 102
wolfSSL 0:1239e9b70ca2 103
wolfSSL 0:1239e9b70ca2 104 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 105
wolfSSL 0:1239e9b70ca2 106 #include <cyassl/ctaocrypt/logging.h>
wolfSSL 0:1239e9b70ca2 107 #include "cavium_common.h"
wolfSSL 0:1239e9b70ca2 108
wolfSSL 0:1239e9b70ca2 109 /* Initiliaze Arc4 for use with Nitrox device */
wolfSSL 0:1239e9b70ca2 110 int Arc4InitCavium(Arc4* arc4, int devId)
wolfSSL 0:1239e9b70ca2 111 {
wolfSSL 0:1239e9b70ca2 112 if (arc4 == NULL)
wolfSSL 0:1239e9b70ca2 113 return -1;
wolfSSL 0:1239e9b70ca2 114
wolfSSL 0:1239e9b70ca2 115 if (CspAllocContext(CONTEXT_SSL, &arc4->contextHandle, devId) != 0)
wolfSSL 0:1239e9b70ca2 116 return -1;
wolfSSL 0:1239e9b70ca2 117
wolfSSL 0:1239e9b70ca2 118 arc4->devId = devId;
wolfSSL 0:1239e9b70ca2 119 arc4->magic = CYASSL_ARC4_CAVIUM_MAGIC;
wolfSSL 0:1239e9b70ca2 120
wolfSSL 0:1239e9b70ca2 121 return 0;
wolfSSL 0:1239e9b70ca2 122 }
wolfSSL 0:1239e9b70ca2 123
wolfSSL 0:1239e9b70ca2 124
wolfSSL 0:1239e9b70ca2 125 /* Free Arc4 from use with Nitrox device */
wolfSSL 0:1239e9b70ca2 126 void Arc4FreeCavium(Arc4* arc4)
wolfSSL 0:1239e9b70ca2 127 {
wolfSSL 0:1239e9b70ca2 128 if (arc4 == NULL)
wolfSSL 0:1239e9b70ca2 129 return;
wolfSSL 0:1239e9b70ca2 130
wolfSSL 0:1239e9b70ca2 131 if (arc4->magic != CYASSL_ARC4_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 132 return;
wolfSSL 0:1239e9b70ca2 133
wolfSSL 0:1239e9b70ca2 134 CspFreeContext(CONTEXT_SSL, arc4->contextHandle, arc4->devId);
wolfSSL 0:1239e9b70ca2 135 arc4->magic = 0;
wolfSSL 0:1239e9b70ca2 136 }
wolfSSL 0:1239e9b70ca2 137
wolfSSL 0:1239e9b70ca2 138
wolfSSL 0:1239e9b70ca2 139 static void Arc4CaviumSetKey(Arc4* arc4, const byte* key, word32 length)
wolfSSL 0:1239e9b70ca2 140 {
wolfSSL 0:1239e9b70ca2 141 word32 requestId;
wolfSSL 0:1239e9b70ca2 142
wolfSSL 0:1239e9b70ca2 143 if (CspInitializeRc4(CAVIUM_BLOCKING, arc4->contextHandle, length,
wolfSSL 0:1239e9b70ca2 144 (byte*)key, &requestId, arc4->devId) != 0) {
wolfSSL 0:1239e9b70ca2 145 CYASSL_MSG("Bad Cavium Arc4 Init");
wolfSSL 0:1239e9b70ca2 146 }
wolfSSL 0:1239e9b70ca2 147 }
wolfSSL 0:1239e9b70ca2 148
wolfSSL 0:1239e9b70ca2 149
wolfSSL 0:1239e9b70ca2 150 static void Arc4CaviumProcess(Arc4* arc4, byte* out, const byte* in,
wolfSSL 0:1239e9b70ca2 151 word32 length)
wolfSSL 0:1239e9b70ca2 152 {
wolfSSL 0:1239e9b70ca2 153 word offset = 0;
wolfSSL 0:1239e9b70ca2 154 word32 requestId;
wolfSSL 0:1239e9b70ca2 155
wolfSSL 0:1239e9b70ca2 156 while (length > CYASSL_MAX_16BIT) {
wolfSSL 0:1239e9b70ca2 157 word16 slen = (word16)CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 158 if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE,
wolfSSL 0:1239e9b70ca2 159 slen, (byte*)in + offset, out + offset, &requestId,
wolfSSL 0:1239e9b70ca2 160 arc4->devId) != 0) {
wolfSSL 0:1239e9b70ca2 161 CYASSL_MSG("Bad Cavium Arc4 Encrypt");
wolfSSL 0:1239e9b70ca2 162 }
wolfSSL 0:1239e9b70ca2 163 length -= CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 164 offset += CYASSL_MAX_16BIT;
wolfSSL 0:1239e9b70ca2 165 }
wolfSSL 0:1239e9b70ca2 166 if (length) {
wolfSSL 0:1239e9b70ca2 167 word16 slen = (word16)length;
wolfSSL 0:1239e9b70ca2 168 if (CspEncryptRc4(CAVIUM_BLOCKING, arc4->contextHandle,CAVIUM_UPDATE,
wolfSSL 0:1239e9b70ca2 169 slen, (byte*)in + offset, out + offset, &requestId,
wolfSSL 0:1239e9b70ca2 170 arc4->devId) != 0) {
wolfSSL 0:1239e9b70ca2 171 CYASSL_MSG("Bad Cavium Arc4 Encrypt");
wolfSSL 0:1239e9b70ca2 172 }
wolfSSL 0:1239e9b70ca2 173 }
wolfSSL 0:1239e9b70ca2 174 }
wolfSSL 0:1239e9b70ca2 175
wolfSSL 0:1239e9b70ca2 176 #endif /* HAVE_CAVIUM */
wolfSSL 0:1239e9b70ca2 177
wolfSSL 0:1239e9b70ca2 178 #endif /* NO_ARC4 */
wolfSSL 0:1239e9b70ca2 179
wolfSSL 0:1239e9b70ca2 180