Coverage Report

Created: 2025-07-23 06:59

/src/wolfssl-fastmath/wolfcrypt/src/arc4.c
Line
Count
Source (jump to first uncovered line)
1
/* arc4.c
2
 *
3
 * Copyright (C) 2006-2025 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
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 3 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-1335, USA
20
 */
21
22
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
23
24
#ifndef NO_RC4
25
26
#include <wolfssl/wolfcrypt/arc4.h>
27
28
29
int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
30
37
{
31
37
    int ret = 0;
32
37
    word32 i;
33
37
    word32 keyIndex = 0, stateIndex = 0;
34
35
37
    if (arc4 == NULL || key == NULL || length == 0) {
36
0
        return BAD_FUNC_ARG;
37
0
    }
38
39
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
40
        defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
41
    if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
42
        return NitroxArc4SetKey(arc4, key, length);
43
    }
44
#endif
45
46
37
    arc4->x = 1;
47
37
    arc4->y = 0;
48
49
9.50k
    for (i = 0; i < ARC4_STATE_SIZE; i++)
50
9.47k
        arc4->state[i] = (byte)i;
51
52
9.50k
    for (i = 0; i < ARC4_STATE_SIZE; i++) {
53
9.47k
        word32 a = arc4->state[i];
54
9.47k
        stateIndex += key[keyIndex] + a;
55
9.47k
        stateIndex &= 0xFF;
56
9.47k
        arc4->state[i] = arc4->state[stateIndex];
57
9.47k
        arc4->state[stateIndex] = (byte)a;
58
59
9.47k
        if (++keyIndex >= length)
60
592
            keyIndex = 0;
61
9.47k
    }
62
63
37
    return ret;
64
37
}
65
66
67
static WC_INLINE byte MakeByte(word32* x, word32* y, byte* s)
68
6.65k
{
69
6.65k
    word32 a = s[*x], b;
70
6.65k
    *y = (*y+a) & 0xff;
71
72
6.65k
    b = s[*y];
73
6.65k
    s[*x] = (byte)b;
74
6.65k
    s[*y] = (byte)a;
75
6.65k
    *x = (*x+1) & 0xff;
76
77
6.65k
    return s[(a+b) & 0xff];
78
6.65k
}
79
80
81
int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
82
70
{
83
70
    int ret = 0;
84
70
    word32 x;
85
70
    word32 y;
86
87
70
    if (arc4 == NULL || out == NULL || in == NULL) {
88
0
        return BAD_FUNC_ARG;
89
0
    }
90
91
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
92
        defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
93
    if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
94
        return NitroxArc4Process(arc4, out, in, length);
95
    }
96
#endif
97
98
70
    x = arc4->x;
99
70
    y = arc4->y;
100
101
6.72k
    while(length--)
102
6.65k
        *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
103
104
70
    arc4->x = (byte)x;
105
70
    arc4->y = (byte)y;
106
107
70
    return ret;
108
70
}
109
110
/* Initialize Arc4 for use with async device */
111
int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
112
6
{
113
6
    int ret = 0;
114
115
6
    if (arc4 == NULL)
116
0
        return BAD_FUNC_ARG;
117
118
6
    arc4->heap = heap;
119
120
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
121
    ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
122
        arc4->heap, devId);
123
#else
124
6
    (void)devId;
125
6
#endif /* WOLFSSL_ASYNC_CRYPT */
126
127
6
    return ret;
128
6
}
129
130
131
/* Free Arc4 from use with async device */
132
void wc_Arc4Free(Arc4* arc4)
133
173k
{
134
173k
    if (arc4 == NULL)
135
173k
        return;
136
137
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
138
    wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
139
#endif /* WOLFSSL_ASYNC_CRYPT */
140
173k
}
141
142
#endif /* NO_RC4 */
143