Coverage Report

Created: 2026-05-18 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-fastmath/wolfcrypt/src/arc4.c
Line
Count
Source
1
/* arc4.c
2
 *
3
 * Copyright (C) 2006-2026 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
#ifdef NO_INLINE
29
    #include <wolfssl/wolfcrypt/misc.h>
30
#else
31
    #define WOLFSSL_MISC_INCLUDED
32
    #include <wolfcrypt/src/misc.c>
33
#endif
34
35
36
int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
37
7
{
38
7
    int ret = 0;
39
7
    word32 i;
40
7
    word32 keyIndex = 0, stateIndex = 0;
41
42
7
    if (arc4 == NULL || key == NULL || length == 0) {
43
0
        return BAD_FUNC_ARG;
44
0
    }
45
46
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
47
        defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
48
    if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
49
        return NitroxArc4SetKey(arc4, key, length);
50
    }
51
#endif
52
53
7
    arc4->x = 1;
54
7
    arc4->y = 0;
55
56
1.79k
    for (i = 0; i < ARC4_STATE_SIZE; i++)
57
1.79k
        arc4->state[i] = (byte)i;
58
59
1.79k
    for (i = 0; i < ARC4_STATE_SIZE; i++) {
60
1.79k
        word32 a = arc4->state[i];
61
1.79k
        stateIndex += key[keyIndex] + a;
62
1.79k
        stateIndex &= 0xFF;
63
1.79k
        arc4->state[i] = arc4->state[stateIndex];
64
1.79k
        arc4->state[stateIndex] = (byte)a;
65
66
1.79k
        if (++keyIndex >= length)
67
112
            keyIndex = 0;
68
1.79k
    }
69
70
7
    return ret;
71
7
}
72
73
74
static WC_INLINE byte MakeByte(word32* x, word32* y, byte* s)
75
1.14k
{
76
1.14k
    word32 a = s[*x], b;
77
1.14k
    *y = (*y+a) & 0xff;
78
79
1.14k
    b = s[*y];
80
1.14k
    s[*x] = (byte)b;
81
1.14k
    s[*y] = (byte)a;
82
1.14k
    *x = (*x+1) & 0xff;
83
84
1.14k
    return s[(a+b) & 0xff];
85
1.14k
}
86
87
88
int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
89
9
{
90
9
    int ret = 0;
91
9
    word32 x;
92
9
    word32 y;
93
94
9
    if (arc4 == NULL || out == NULL || in == NULL) {
95
0
        return BAD_FUNC_ARG;
96
0
    }
97
98
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
99
        defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
100
    if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
101
        return NitroxArc4Process(arc4, out, in, length);
102
    }
103
#endif
104
105
9
    x = arc4->x;
106
9
    y = arc4->y;
107
108
1.14k
    while(length--)
109
1.14k
        *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
110
111
9
    arc4->x = (byte)x;
112
9
    arc4->y = (byte)y;
113
114
9
    return ret;
115
9
}
116
117
/* Initialize Arc4 for use with async device */
118
int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
119
4
{
120
4
    int ret = 0;
121
122
4
    if (arc4 == NULL)
123
0
        return BAD_FUNC_ARG;
124
125
4
    arc4->heap = heap;
126
127
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
128
    ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
129
        arc4->heap, devId);
130
#else
131
4
    (void)devId;
132
4
#endif /* WOLFSSL_ASYNC_CRYPT */
133
134
4
    return ret;
135
4
}
136
137
138
/* Free Arc4 from use with async device */
139
void wc_Arc4Free(Arc4* arc4)
140
193k
{
141
193k
    if (arc4 == NULL)
142
193k
        return;
143
144
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
145
    wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
146
#endif /* WOLFSSL_ASYNC_CRYPT */
147
148
4
    ForceZero(arc4->state, sizeof(arc4->state));
149
4
    arc4->x = 0;
150
4
    arc4->y = 0;
151
4
}
152
153
#endif /* NO_RC4 */
154