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