Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * An implementation of the ARCFOUR algorithm |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 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 |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 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. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * The ARCFOUR algorithm was publicly disclosed on 94/09. |
| 21 | * |
| 22 | * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0 |
| 23 | */ |
| 24 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 25 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/arc4.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 30 | #include "mbedtls/platform_util.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if !defined(MBEDTLS_ARC4_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 37 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 38 | void mbedtls_arc4_init(mbedtls_arc4_context *ctx) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 39 | { |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 40 | memset(ctx, 0, sizeof(mbedtls_arc4_context)); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 41 | } |
| 42 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 43 | void mbedtls_arc4_free(mbedtls_arc4_context *ctx) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 44 | { |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 45 | if (ctx == NULL) { |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 46 | return; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 47 | } |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 48 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 49 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_arc4_context)); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | /* |
| 53 | * ARC4 key schedule |
| 54 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 55 | void mbedtls_arc4_setup(mbedtls_arc4_context *ctx, const unsigned char *key, |
| 56 | unsigned int keylen) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 58 | int i, j, a; |
| 59 | unsigned int k; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | unsigned char *m; |
| 61 | |
| 62 | ctx->x = 0; |
| 63 | ctx->y = 0; |
| 64 | m = ctx->m; |
| 65 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 66 | for (i = 0; i < 256; i++) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | m[i] = (unsigned char) i; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 68 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
| 70 | j = k = 0; |
| 71 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 72 | for (i = 0; i < 256; i++, k++) { |
| 73 | if (k >= keylen) { |
| 74 | k = 0; |
| 75 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
| 77 | a = m[i]; |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 78 | j = (j + a + key[k]) & 0xFF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | m[i] = m[j]; |
| 80 | m[j] = (unsigned char) a; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * ARC4 cipher function |
| 86 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 87 | int mbedtls_arc4_crypt(mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, |
| 88 | unsigned char *output) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 90 | int x, y, a, b; |
| 91 | size_t i; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | unsigned char *m; |
| 93 | |
| 94 | x = ctx->x; |
| 95 | y = ctx->y; |
| 96 | m = ctx->m; |
| 97 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 98 | for (i = 0; i < length; i++) { |
| 99 | x = (x + 1) & 0xFF; a = m[x]; |
| 100 | y = (y + a) & 0xFF; b = m[y]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | |
| 102 | m[x] = (unsigned char) b; |
| 103 | m[y] = (unsigned char) a; |
| 104 | |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 105 | output[i] = (unsigned char) |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 106 | (input[i] ^ m[(unsigned char) (a + b)]); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ctx->x = x; |
| 110 | ctx->y = y; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 111 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 112 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | #endif /* !MBEDTLS_ARC4_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | /* |
| 119 | * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: |
| 120 | * |
| 121 | * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0 |
| 122 | */ |
| 123 | static const unsigned char arc4_test_key[3][8] = |
| 124 | { |
| 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 } |
| 128 | }; |
| 129 | |
| 130 | static const unsigned char arc4_test_pt[3][8] = |
| 131 | { |
| 132 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
| 133 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
| 134 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
| 135 | }; |
| 136 | |
| 137 | static const unsigned char arc4_test_ct[3][8] = |
| 138 | { |
| 139 | { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 }, |
| 140 | { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 }, |
| 141 | { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A } |
| 142 | }; |
| 143 | |
| 144 | /* |
| 145 | * Checkup routine |
| 146 | */ |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 147 | int mbedtls_arc4_self_test(int verbose) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | { |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 149 | int i, ret = 0; |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 150 | unsigned char ibuf[8]; |
| 151 | unsigned char obuf[8]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | mbedtls_arc4_context ctx; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 154 | mbedtls_arc4_init(&ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 155 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 156 | for (i = 0; i < 3; i++) { |
| 157 | if (verbose != 0) { |
| 158 | mbedtls_printf(" ARC4 test #%d: ", i + 1); |
| 159 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 161 | memcpy(ibuf, arc4_test_pt[i], 8); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 163 | mbedtls_arc4_setup(&ctx, arc4_test_key[i], 8); |
| 164 | mbedtls_arc4_crypt(&ctx, 8, ibuf, obuf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 166 | if (memcmp(obuf, arc4_test_ct[i], 8) != 0) { |
| 167 | if (verbose != 0) { |
| 168 | mbedtls_printf("failed\n"); |
| 169 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 171 | ret = 1; |
| 172 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | } |
| 174 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 175 | if (verbose != 0) { |
| 176 | mbedtls_printf("passed\n"); |
| 177 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | } |
| 179 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 180 | if (verbose != 0) { |
| 181 | mbedtls_printf("\n"); |
| 182 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 184 | exit: |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 185 | mbedtls_arc4_free(&ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 186 | |
David Horstmann | ceeaeb9 | 2023-01-05 15:44:23 +0000 | [diff] [blame^] | 187 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_ARC4_C */ |