Files
wolfssl/wolfcrypt/src/arc4.c
T

144 lines
3.4 KiB
C
Raw Normal View History

2011-02-05 11:14:47 -08:00
/* arc4.c
*
2026-02-18 09:52:21 -07:00
* Copyright (C) 2006-2026 wolfSSL Inc.
2011-02-05 11:14:47 -08:00
*
2016-03-17 16:02:13 -06:00
* This file is part of wolfSSL.
2011-02-05 11:14:47 -08:00
*
2014-12-17 09:58:11 -07:00
* wolfSSL is free software; you can redistribute it and/or modify
2011-02-05 11:14:47 -08:00
* it under the terms of the GNU General Public License as published by
2025-07-10 16:01:52 -06:00
* the Free Software Foundation; either version 3 of the License, or
2011-02-05 11:14:47 -08:00
* (at your option) any later version.
*
2014-12-17 09:58:11 -07:00
* wolfSSL is distributed in the hope that it will be useful,
2011-02-05 11:14:47 -08:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2016-03-17 16:02:13 -06:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2011-02-05 11:14:47 -08:00
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#ifndef NO_RC4
2014-12-17 09:58:11 -07:00
#include <wolfssl/wolfcrypt/arc4.h>
2013-01-29 16:22:49 -08:00
int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
2011-02-05 11:14:47 -08:00
{
int ret = 0;
2011-02-05 11:14:47 -08:00
word32 i;
word32 keyIndex = 0, stateIndex = 0;
2019-12-17 13:28:50 -07:00
if (arc4 == NULL || key == NULL || length == 0) {
2017-06-01 15:58:42 -06:00
return BAD_FUNC_ARG;
}
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
return NitroxArc4SetKey(arc4, key, length);
}
2013-01-29 16:22:49 -08:00
#endif
2011-02-05 11:14:47 -08:00
arc4->x = 1;
arc4->y = 0;
for (i = 0; i < ARC4_STATE_SIZE; i++)
arc4->state[i] = (byte)i;
2011-02-05 11:14:47 -08:00
for (i = 0; i < ARC4_STATE_SIZE; i++) {
word32 a = arc4->state[i];
stateIndex += key[keyIndex] + a;
stateIndex &= 0xFF;
arc4->state[i] = arc4->state[stateIndex];
arc4->state[stateIndex] = (byte)a;
2011-02-05 11:14:47 -08:00
if (++keyIndex >= length)
keyIndex = 0;
}
return ret;
2011-02-05 11:14:47 -08:00
}
2018-06-26 11:40:34 -07:00
static WC_INLINE byte MakeByte(word32* x, word32* y, byte* s)
2011-02-05 11:14:47 -08:00
{
word32 a = s[*x], b;
*y = (*y+a) & 0xff;
b = s[*y];
s[*x] = (byte)b;
s[*y] = (byte)a;
2011-02-05 11:14:47 -08:00
*x = (*x+1) & 0xff;
return s[(a+b) & 0xff];
}
int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
2011-02-05 11:14:47 -08:00
{
int ret = 0;
2013-01-29 16:22:49 -08:00
word32 x;
word32 y;
2017-06-01 15:58:42 -06:00
if (arc4 == NULL || out == NULL || in == NULL) {
return BAD_FUNC_ARG;
}
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
return NitroxArc4Process(arc4, out, in, length);
}
2013-01-29 16:22:49 -08:00
#endif
x = arc4->x;
y = arc4->y;
2011-02-05 11:14:47 -08:00
while(length--)
*out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
arc4->x = (byte)x;
arc4->y = (byte)y;
return ret;
2011-02-05 11:14:47 -08:00
}
/* Initialize Arc4 for use with async device */
int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
2013-01-29 16:22:49 -08:00
{
int ret = 0;
2013-01-29 16:22:49 -08:00
if (arc4 == NULL)
return BAD_FUNC_ARG;
2013-01-29 16:22:49 -08:00
arc4->heap = heap;
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
arc4->heap, devId);
#else
(void)devId;
#endif /* WOLFSSL_ASYNC_CRYPT */
return ret;
2013-01-29 16:22:49 -08:00
}
/* Free Arc4 from use with async device */
void wc_Arc4Free(Arc4* arc4)
2013-01-29 16:22:49 -08:00
{
if (arc4 == NULL)
return;
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
#endif /* WOLFSSL_ASYNC_CRYPT */
}
2015-03-27 10:17:22 -07:00
#endif /* NO_RC4 */