cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Committer:
ashleymills
Date:
Fri Apr 26 16:59:36 2013 +0000
Revision:
1:b211d97b0068
Parent:
0:e979170e02e7
nothing

Who changed what in which revision?

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