| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  An implementation of the ARCFOUR algorithm | 
|  | 3 | * | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4 | *  Copyright (C) 2006-2013, 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 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 31 | #include "polarssl/config.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 33 | #if defined(POLARSSL_ARC4_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #include "polarssl/arc4.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 |  | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 37 | #if !defined(POLARSSL_ARC4_ALT) | 
|  | 38 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | /* | 
|  | 40 | * ARC4 key schedule | 
|  | 41 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 42 | void arc4_setup( arc4_context *ctx, const unsigned char *key, unsigned int keylen ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 44 | int i, j, a; | 
|  | 45 | unsigned int k; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | unsigned char *m; | 
|  | 47 |  | 
|  | 48 | ctx->x = 0; | 
|  | 49 | ctx->y = 0; | 
|  | 50 | m = ctx->m; | 
|  | 51 |  | 
|  | 52 | for( i = 0; i < 256; i++ ) | 
|  | 53 | m[i] = (unsigned char) i; | 
|  | 54 |  | 
|  | 55 | j = k = 0; | 
|  | 56 |  | 
|  | 57 | for( i = 0; i < 256; i++, k++ ) | 
|  | 58 | { | 
|  | 59 | if( k >= keylen ) k = 0; | 
|  | 60 |  | 
|  | 61 | a = m[i]; | 
|  | 62 | j = ( j + a + key[k] ) & 0xFF; | 
|  | 63 | m[i] = m[j]; | 
|  | 64 | m[j] = (unsigned char) a; | 
|  | 65 | } | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | /* | 
|  | 69 | * ARC4 cipher function | 
|  | 70 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 71 | 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] | 72 | unsigned char *output ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 74 | int x, y, a, b; | 
|  | 75 | size_t i; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | unsigned char *m; | 
|  | 77 |  | 
|  | 78 | x = ctx->x; | 
|  | 79 | y = ctx->y; | 
|  | 80 | m = ctx->m; | 
|  | 81 |  | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 82 | for( i = 0; i < length; i++ ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | { | 
|  | 84 | x = ( x + 1 ) & 0xFF; a = m[x]; | 
|  | 85 | y = ( y + a ) & 0xFF; b = m[y]; | 
|  | 86 |  | 
|  | 87 | m[x] = (unsigned char) b; | 
|  | 88 | m[y] = (unsigned char) a; | 
|  | 89 |  | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 90 | output[i] = (unsigned char) | 
|  | 91 | ( input[i] ^ m[(unsigned char)( a + b )] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | ctx->x = x; | 
|  | 95 | ctx->y = y; | 
| Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 96 |  | 
|  | 97 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 100 | #endif /* !POLARSSL_ARC4_ALT */ | 
|  | 101 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 102 | #if defined(POLARSSL_SELF_TEST) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 |  | 
|  | 104 | #include <string.h> | 
|  | 105 | #include <stdio.h> | 
|  | 106 |  | 
|  | 107 | /* | 
|  | 108 | * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: | 
|  | 109 | * | 
|  | 110 | * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0 | 
|  | 111 | */ | 
|  | 112 | static const unsigned char arc4_test_key[3][8] = | 
|  | 113 | { | 
|  | 114 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, | 
|  | 115 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, | 
|  | 116 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } | 
|  | 117 | }; | 
|  | 118 |  | 
|  | 119 | static const unsigned char arc4_test_pt[3][8] = | 
|  | 120 | { | 
|  | 121 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, | 
|  | 122 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, | 
|  | 123 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } | 
|  | 124 | }; | 
|  | 125 |  | 
|  | 126 | static const unsigned char arc4_test_ct[3][8] = | 
|  | 127 | { | 
|  | 128 | { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 }, | 
|  | 129 | { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 }, | 
|  | 130 | { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A } | 
|  | 131 | }; | 
|  | 132 |  | 
|  | 133 | /* | 
|  | 134 | * Checkup routine | 
|  | 135 | */ | 
|  | 136 | int arc4_self_test( int verbose ) | 
|  | 137 | { | 
|  | 138 | int i; | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 139 | unsigned char ibuf[8]; | 
|  | 140 | unsigned char obuf[8]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | arc4_context ctx; | 
|  | 142 |  | 
|  | 143 | for( i = 0; i < 3; i++ ) | 
|  | 144 | { | 
|  | 145 | if( verbose != 0 ) | 
|  | 146 | printf( "  ARC4 test #%d: ", i + 1 ); | 
|  | 147 |  | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 148 | memcpy( ibuf, arc4_test_pt[i], 8 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 |  | 
| Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 150 | arc4_setup( &ctx, arc4_test_key[i], 8 ); | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 151 | arc4_crypt( &ctx, 8, ibuf, obuf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 |  | 
| Paul Bakker | baad650 | 2010-03-21 15:42:15 +0000 | [diff] [blame] | 153 | if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | { | 
|  | 155 | if( verbose != 0 ) | 
|  | 156 | printf( "failed\n" ); | 
|  | 157 |  | 
|  | 158 | return( 1 ); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | if( verbose != 0 ) | 
|  | 162 | printf( "passed\n" ); | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | if( verbose != 0 ) | 
|  | 166 | printf( "\n" ); | 
|  | 167 |  | 
|  | 168 | return( 0 ); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | #endif | 
|  | 172 |  | 
|  | 173 | #endif |