2 * An implementation of the ARCFOUR algorithm
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 * The ARCFOUR algorithm was publicly disclosed on 94/09.
22 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
27 #if defined(MBEDTLS_ARC4_C)
29 #include "mbedtls/arc4.h"
30 #include "mbedtls/platform_util.h"
34 #if defined(MBEDTLS_SELF_TEST)
35 #if defined(MBEDTLS_PLATFORM_C)
36 #include "mbedtls/platform.h"
39 #define mbedtls_printf printf
40 #endif /* MBEDTLS_PLATFORM_C */
41 #endif /* MBEDTLS_SELF_TEST */
43 #if !defined(MBEDTLS_ARC4_ALT)
45 void mbedtls_arc4_init(mbedtls_arc4_context
*ctx
) {
46 memset(ctx
, 0, sizeof(mbedtls_arc4_context
));
49 void mbedtls_arc4_free(mbedtls_arc4_context
*ctx
) {
53 mbedtls_platform_zeroize(ctx
, sizeof(mbedtls_arc4_context
));
59 void mbedtls_arc4_setup(mbedtls_arc4_context
*ctx
, const unsigned char *key
,
60 unsigned int keylen
) {
69 for (i
= 0; i
< 256; i
++)
70 m
[i
] = (unsigned char) i
;
74 for (i
= 0; i
< 256; i
++, k
++) {
75 if (k
>= keylen
) k
= 0;
78 j
= (j
+ a
+ key
[k
]) & 0xFF;
80 m
[j
] = (unsigned char) a
;
85 * ARC4 cipher function
87 int mbedtls_arc4_crypt(mbedtls_arc4_context
*ctx
, size_t length
, const unsigned char *input
,
88 unsigned char *output
) {
97 for (i
= 0; i
< length
; i
++) {
103 m
[x
] = (unsigned char) b
;
104 m
[y
] = (unsigned char) a
;
106 output
[i
] = (unsigned char)
107 (input
[i
] ^ m
[(unsigned char)(a
+ b
)]);
116 #endif /* !MBEDTLS_ARC4_ALT */
118 #if defined(MBEDTLS_SELF_TEST)
120 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
122 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
124 static const unsigned char arc4_test_key
[3][8] = {
125 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
126 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
127 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
130 static const unsigned char arc4_test_pt
[3][8] = {
131 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
132 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
133 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
136 static const unsigned char arc4_test_ct
[3][8] = {
137 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
138 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
139 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
145 int mbedtls_arc4_self_test(int verbose
) {
147 unsigned char ibuf
[8];
148 unsigned char obuf
[8];
149 mbedtls_arc4_context ctx
;
151 mbedtls_arc4_init(&ctx
);
153 for (i
= 0; i
< 3; i
++) {
155 mbedtls_printf(" ARC4 test #%d: ", i
+ 1);
157 memcpy(ibuf
, arc4_test_pt
[i
], 8);
159 mbedtls_arc4_setup(&ctx
, arc4_test_key
[i
], 8);
160 mbedtls_arc4_crypt(&ctx
, 8, ibuf
, obuf
);
162 if (memcmp(obuf
, arc4_test_ct
[i
], 8) != 0) {
164 mbedtls_printf("failed\n");
171 mbedtls_printf("passed\n");
175 mbedtls_printf("\n");
178 mbedtls_arc4_free(&ctx
);
183 #endif /* MBEDTLS_SELF_TEST */
185 #endif /* MBEDTLS_ARC4_C */