UL AES alt pk
[RRG-proxmark3.git] / common / mbedtls / arc4.c
blob9a7cfff6f3955cc1cac6ed3182069773200957c1
1 /*
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
25 #include "common.h"
27 #if defined(MBEDTLS_ARC4_C)
29 #include "mbedtls/arc4.h"
30 #include "mbedtls/platform_util.h"
32 #include <string.h>
34 #if defined(MBEDTLS_SELF_TEST)
35 #if defined(MBEDTLS_PLATFORM_C)
36 #include "mbedtls/platform.h"
37 #else
38 #include <stdio.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) {
50 if (ctx == NULL)
51 return;
53 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_arc4_context));
57 * ARC4 key schedule
59 void mbedtls_arc4_setup(mbedtls_arc4_context *ctx, const unsigned char *key,
60 unsigned int keylen) {
61 int i, j, a;
62 unsigned int k;
63 unsigned char *m;
65 ctx->x = 0;
66 ctx->y = 0;
67 m = ctx->m;
69 for (i = 0; i < 256; i++)
70 m[i] = (unsigned char) i;
72 j = k = 0;
74 for (i = 0; i < 256; i++, k++) {
75 if (k >= keylen) k = 0;
77 a = m[i];
78 j = (j + a + key[k]) & 0xFF;
79 m[i] = m[j];
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) {
89 int x, y, a, b;
90 size_t i;
91 unsigned char *m;
93 x = ctx->x;
94 y = ctx->y;
95 m = ctx->m;
97 for (i = 0; i < length; i++) {
98 x = (x + 1) & 0xFF;
99 a = m[x];
100 y = (y + a) & 0xFF;
101 b = m[y];
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)]);
110 ctx->x = x;
111 ctx->y = y;
113 return (0);
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 }
143 * Checkup routine
145 int mbedtls_arc4_self_test(int verbose) {
146 int i, ret = 0;
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++) {
154 if (verbose != 0)
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) {
163 if (verbose != 0)
164 mbedtls_printf("failed\n");
166 ret = 1;
167 goto exit;
170 if (verbose != 0)
171 mbedtls_printf("passed\n");
174 if (verbose != 0)
175 mbedtls_printf("\n");
177 exit:
178 mbedtls_arc4_free(&ctx);
180 return (ret);
183 #endif /* MBEDTLS_SELF_TEST */
185 #endif /* MBEDTLS_ARC4_C */