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