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