This is a port of cyaSSL 2.7.0.

Dependents:   CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet

Committer:
ashleymills
Date:
Thu Sep 05 15:55:50 2013 +0000
Revision:
1:c0ce1562443a
Parent:
0:714293de3836
Nothing;

Who changed what in which revision?

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