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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | /* |
| 23 | * The ARCFOUR algorithm was publicly disclosed on 94/09. |
| 24 | * |
| 25 | * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0 |
| 26 | */ |
| 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/arc4.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 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 51 | /* Implementation that should never be optimized out by the compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | static void mbedtls_zeroize( void *v, size_t n ) { |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 53 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 54 | } |
| 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | void mbedtls_arc4_init( mbedtls_arc4_context *ctx ) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 57 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | memset( ctx, 0, sizeof( mbedtls_arc4_context ) ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | void mbedtls_arc4_free( mbedtls_arc4_context *ctx ) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 62 | { |
| 63 | if( ctx == NULL ) |
| 64 | return; |
| 65 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | /* |
| 70 | * ARC4 key schedule |
| 71 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 73 | unsigned int keylen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 75 | int i, j, a; |
| 76 | unsigned int k; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | unsigned char *m; |
| 78 | |
| 79 | ctx->x = 0; |
| 80 | ctx->y = 0; |
| 81 | m = ctx->m; |
| 82 | |
| 83 | for( i = 0; i < 256; i++ ) |
| 84 | m[i] = (unsigned char) i; |
| 85 | |
| 86 | j = k = 0; |
| 87 | |
| 88 | for( i = 0; i < 256; i++, k++ ) |
| 89 | { |
| 90 | if( k >= keylen ) k = 0; |
| 91 | |
| 92 | a = m[i]; |
| 93 | j = ( j + a + key[k] ) & 0xFF; |
| 94 | m[i] = m[j]; |
| 95 | m[j] = (unsigned char) a; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * ARC4 cipher function |
| 101 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | 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] | 103 | unsigned char *output ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 105 | int x, y, a, b; |
| 106 | size_t i; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | unsigned char *m; |
| 108 | |
| 109 | x = ctx->x; |
| 110 | y = ctx->y; |
| 111 | m = ctx->m; |
| 112 | |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 113 | for( i = 0; i < length; i++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | { |
| 115 | x = ( x + 1 ) & 0xFF; a = m[x]; |
| 116 | y = ( y + a ) & 0xFF; b = m[y]; |
| 117 | |
| 118 | m[x] = (unsigned char) b; |
| 119 | m[y] = (unsigned char) a; |
| 120 | |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 121 | output[i] = (unsigned char) |
| 122 | ( input[i] ^ m[(unsigned char)( a + b )] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | ctx->x = x; |
| 126 | ctx->y = y; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 127 | |
| 128 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | #endif /* !MBEDTLS_ARC4_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | /* |
| 135 | * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: |
| 136 | * |
| 137 | * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0 |
| 138 | */ |
| 139 | static const unsigned char arc4_test_key[3][8] = |
| 140 | { |
| 141 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
| 142 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
| 143 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
| 144 | }; |
| 145 | |
| 146 | static const unsigned char arc4_test_pt[3][8] = |
| 147 | { |
| 148 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
| 149 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
| 150 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
| 151 | }; |
| 152 | |
| 153 | static const unsigned char arc4_test_ct[3][8] = |
| 154 | { |
| 155 | { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 }, |
| 156 | { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 }, |
| 157 | { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A } |
| 158 | }; |
| 159 | |
| 160 | /* |
| 161 | * Checkup routine |
| 162 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | int mbedtls_arc4_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | { |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 165 | int i, ret = 0; |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 166 | unsigned char ibuf[8]; |
| 167 | unsigned char obuf[8]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | mbedtls_arc4_context ctx; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | mbedtls_arc4_init( &ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 171 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | for( i = 0; i < 3; i++ ) |
| 173 | { |
| 174 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_printf( " ARC4 test #%d: ", i + 1 ); |
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 | memcpy( ibuf, arc4_test_pt[i], 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 ); |
| 180 | mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | |
Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 182 | if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | { |
| 184 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 187 | ret = 1; |
| 188 | goto exit; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | mbedtls_printf( "passed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | mbedtls_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 197 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 198 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | mbedtls_arc4_free( &ctx ); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 200 | |
| 201 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | #endif /* MBEDTLS_ARC4_C */ |