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