source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/arc4.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 7 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

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