SSL/TLS Library

Dependents:  

CyaSSL is SSL/TLS library for embedded systems.

wolfssl.com

Committer:
wolfSSL
Date:
Sun Apr 20 12:40:57 2014 +0000
Revision:
0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;

Who changed what in which revision?

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