xref: /reactos/dll/3rdparty/mbedtls/arc4.c (revision cbda039f)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  *  An implementation of the ARCFOUR algorithm
3c2c66affSColin Finck  *
4218e2596SThomas Faber  *  Copyright The Mbed TLS Contributors
5e57126f5SThomas Faber  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6e57126f5SThomas Faber  *
7e57126f5SThomas Faber  *  This file is provided under the Apache License 2.0, or the
8e57126f5SThomas Faber  *  GNU General Public License v2.0 or later.
9e57126f5SThomas Faber  *
10e57126f5SThomas Faber  *  **********
11e57126f5SThomas Faber  *  Apache License 2.0:
12e57126f5SThomas Faber  *
13e57126f5SThomas Faber  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14e57126f5SThomas Faber  *  not use this file except in compliance with the License.
15e57126f5SThomas Faber  *  You may obtain a copy of the License at
16e57126f5SThomas Faber  *
17e57126f5SThomas Faber  *  http://www.apache.org/licenses/LICENSE-2.0
18e57126f5SThomas Faber  *
19e57126f5SThomas Faber  *  Unless required by applicable law or agreed to in writing, software
20e57126f5SThomas Faber  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21e57126f5SThomas Faber  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22e57126f5SThomas Faber  *  See the License for the specific language governing permissions and
23e57126f5SThomas Faber  *  limitations under the License.
24e57126f5SThomas Faber  *
25e57126f5SThomas Faber  *  **********
26e57126f5SThomas Faber  *
27e57126f5SThomas Faber  *  **********
28e57126f5SThomas Faber  *  GNU General Public License v2.0 or later:
29c2c66affSColin Finck  *
30c2c66affSColin Finck  *  This program is free software; you can redistribute it and/or modify
31c2c66affSColin Finck  *  it under the terms of the GNU General Public License as published by
32c2c66affSColin Finck  *  the Free Software Foundation; either version 2 of the License, or
33c2c66affSColin Finck  *  (at your option) any later version.
34c2c66affSColin Finck  *
35c2c66affSColin Finck  *  This program is distributed in the hope that it will be useful,
36c2c66affSColin Finck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37c2c66affSColin Finck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38c2c66affSColin Finck  *  GNU General Public License for more details.
39c2c66affSColin Finck  *
40c2c66affSColin Finck  *  You should have received a copy of the GNU General Public License along
41c2c66affSColin Finck  *  with this program; if not, write to the Free Software Foundation, Inc.,
42c2c66affSColin Finck  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43c2c66affSColin Finck  *
44e57126f5SThomas Faber  *  **********
45c2c66affSColin Finck  */
46c2c66affSColin Finck /*
47c2c66affSColin Finck  *  The ARCFOUR algorithm was publicly disclosed on 94/09.
48c2c66affSColin Finck  *
49c2c66affSColin Finck  *  http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
50c2c66affSColin Finck  */
51c2c66affSColin Finck 
52c2c66affSColin Finck #if !defined(MBEDTLS_CONFIG_FILE)
53c2c66affSColin Finck #include "mbedtls/config.h"
54c2c66affSColin Finck #else
55c2c66affSColin Finck #include MBEDTLS_CONFIG_FILE
56c2c66affSColin Finck #endif
57c2c66affSColin Finck 
58c2c66affSColin Finck #if defined(MBEDTLS_ARC4_C)
59c2c66affSColin Finck 
60c2c66affSColin Finck #include "mbedtls/arc4.h"
61*cbda039fSThomas Faber #include "mbedtls/platform_util.h"
62c2c66affSColin Finck 
63c2c66affSColin Finck #include <string.h>
64c2c66affSColin Finck 
65c2c66affSColin Finck #if defined(MBEDTLS_SELF_TEST)
66c2c66affSColin Finck #if defined(MBEDTLS_PLATFORM_C)
67c2c66affSColin Finck #include "mbedtls/platform.h"
68c2c66affSColin Finck #else
69c2c66affSColin Finck #include <stdio.h>
70c2c66affSColin Finck #define mbedtls_printf printf
71c2c66affSColin Finck #endif /* MBEDTLS_PLATFORM_C */
72c2c66affSColin Finck #endif /* MBEDTLS_SELF_TEST */
73c2c66affSColin Finck 
74c2c66affSColin Finck #if !defined(MBEDTLS_ARC4_ALT)
75c2c66affSColin Finck 
mbedtls_arc4_init(mbedtls_arc4_context * ctx)76c2c66affSColin Finck void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
77c2c66affSColin Finck {
78c2c66affSColin Finck     memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
79c2c66affSColin Finck }
80c2c66affSColin Finck 
mbedtls_arc4_free(mbedtls_arc4_context * ctx)81c2c66affSColin Finck void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
82c2c66affSColin Finck {
83c2c66affSColin Finck     if( ctx == NULL )
84c2c66affSColin Finck         return;
85c2c66affSColin Finck 
86*cbda039fSThomas Faber     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
87c2c66affSColin Finck }
88c2c66affSColin Finck 
89c2c66affSColin Finck /*
90c2c66affSColin Finck  * ARC4 key schedule
91c2c66affSColin Finck  */
mbedtls_arc4_setup(mbedtls_arc4_context * ctx,const unsigned char * key,unsigned int keylen)92c2c66affSColin Finck void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
93c2c66affSColin Finck                  unsigned int keylen )
94c2c66affSColin Finck {
95c2c66affSColin Finck     int i, j, a;
96c2c66affSColin Finck     unsigned int k;
97c2c66affSColin Finck     unsigned char *m;
98c2c66affSColin Finck 
99c2c66affSColin Finck     ctx->x = 0;
100c2c66affSColin Finck     ctx->y = 0;
101c2c66affSColin Finck     m = ctx->m;
102c2c66affSColin Finck 
103c2c66affSColin Finck     for( i = 0; i < 256; i++ )
104c2c66affSColin Finck         m[i] = (unsigned char) i;
105c2c66affSColin Finck 
106c2c66affSColin Finck     j = k = 0;
107c2c66affSColin Finck 
108c2c66affSColin Finck     for( i = 0; i < 256; i++, k++ )
109c2c66affSColin Finck     {
110c2c66affSColin Finck         if( k >= keylen ) k = 0;
111c2c66affSColin Finck 
112c2c66affSColin Finck         a = m[i];
113c2c66affSColin Finck         j = ( j + a + key[k] ) & 0xFF;
114c2c66affSColin Finck         m[i] = m[j];
115c2c66affSColin Finck         m[j] = (unsigned char) a;
116c2c66affSColin Finck     }
117c2c66affSColin Finck }
118c2c66affSColin Finck 
119c2c66affSColin Finck /*
120c2c66affSColin Finck  * ARC4 cipher function
121c2c66affSColin Finck  */
mbedtls_arc4_crypt(mbedtls_arc4_context * ctx,size_t length,const unsigned char * input,unsigned char * output)122c2c66affSColin Finck int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
123c2c66affSColin Finck                 unsigned char *output )
124c2c66affSColin Finck {
125c2c66affSColin Finck     int x, y, a, b;
126c2c66affSColin Finck     size_t i;
127c2c66affSColin Finck     unsigned char *m;
128c2c66affSColin Finck 
129c2c66affSColin Finck     x = ctx->x;
130c2c66affSColin Finck     y = ctx->y;
131c2c66affSColin Finck     m = ctx->m;
132c2c66affSColin Finck 
133c2c66affSColin Finck     for( i = 0; i < length; i++ )
134c2c66affSColin Finck     {
135c2c66affSColin Finck         x = ( x + 1 ) & 0xFF; a = m[x];
136c2c66affSColin Finck         y = ( y + a ) & 0xFF; b = m[y];
137c2c66affSColin Finck 
138c2c66affSColin Finck         m[x] = (unsigned char) b;
139c2c66affSColin Finck         m[y] = (unsigned char) a;
140c2c66affSColin Finck 
141c2c66affSColin Finck         output[i] = (unsigned char)
142c2c66affSColin Finck             ( input[i] ^ m[(unsigned char)( a + b )] );
143c2c66affSColin Finck     }
144c2c66affSColin Finck 
145c2c66affSColin Finck     ctx->x = x;
146c2c66affSColin Finck     ctx->y = y;
147c2c66affSColin Finck 
148c2c66affSColin Finck     return( 0 );
149c2c66affSColin Finck }
150c2c66affSColin Finck 
151c2c66affSColin Finck #endif /* !MBEDTLS_ARC4_ALT */
152c2c66affSColin Finck 
153c2c66affSColin Finck #if defined(MBEDTLS_SELF_TEST)
154c2c66affSColin Finck /*
155c2c66affSColin Finck  * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
156c2c66affSColin Finck  *
157c2c66affSColin Finck  * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
158c2c66affSColin Finck  */
159c2c66affSColin Finck static const unsigned char arc4_test_key[3][8] =
160c2c66affSColin Finck {
161c2c66affSColin Finck     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
162c2c66affSColin Finck     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
163c2c66affSColin Finck     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
164c2c66affSColin Finck };
165c2c66affSColin Finck 
166c2c66affSColin Finck static const unsigned char arc4_test_pt[3][8] =
167c2c66affSColin Finck {
168c2c66affSColin Finck     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
169c2c66affSColin Finck     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
170c2c66affSColin Finck     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
171c2c66affSColin Finck };
172c2c66affSColin Finck 
173c2c66affSColin Finck static const unsigned char arc4_test_ct[3][8] =
174c2c66affSColin Finck {
175c2c66affSColin Finck     { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
176c2c66affSColin Finck     { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
177c2c66affSColin Finck     { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
178c2c66affSColin Finck };
179c2c66affSColin Finck 
180c2c66affSColin Finck /*
181c2c66affSColin Finck  * Checkup routine
182c2c66affSColin Finck  */
mbedtls_arc4_self_test(int verbose)183c2c66affSColin Finck int mbedtls_arc4_self_test( int verbose )
184c2c66affSColin Finck {
185c2c66affSColin Finck     int i, ret = 0;
186c2c66affSColin Finck     unsigned char ibuf[8];
187c2c66affSColin Finck     unsigned char obuf[8];
188c2c66affSColin Finck     mbedtls_arc4_context ctx;
189c2c66affSColin Finck 
190c2c66affSColin Finck     mbedtls_arc4_init( &ctx );
191c2c66affSColin Finck 
192c2c66affSColin Finck     for( i = 0; i < 3; i++ )
193c2c66affSColin Finck     {
194c2c66affSColin Finck         if( verbose != 0 )
195c2c66affSColin Finck             mbedtls_printf( "  ARC4 test #%d: ", i + 1 );
196c2c66affSColin Finck 
197c2c66affSColin Finck         memcpy( ibuf, arc4_test_pt[i], 8 );
198c2c66affSColin Finck 
199c2c66affSColin Finck         mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
200c2c66affSColin Finck         mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
201c2c66affSColin Finck 
202c2c66affSColin Finck         if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
203c2c66affSColin Finck         {
204c2c66affSColin Finck             if( verbose != 0 )
205c2c66affSColin Finck                 mbedtls_printf( "failed\n" );
206c2c66affSColin Finck 
207c2c66affSColin Finck             ret = 1;
208c2c66affSColin Finck             goto exit;
209c2c66affSColin Finck         }
210c2c66affSColin Finck 
211c2c66affSColin Finck         if( verbose != 0 )
212c2c66affSColin Finck             mbedtls_printf( "passed\n" );
213c2c66affSColin Finck     }
214c2c66affSColin Finck 
215c2c66affSColin Finck     if( verbose != 0 )
216c2c66affSColin Finck         mbedtls_printf( "\n" );
217c2c66affSColin Finck 
218c2c66affSColin Finck exit:
219c2c66affSColin Finck     mbedtls_arc4_free( &ctx );
220c2c66affSColin Finck 
221c2c66affSColin Finck     return( ret );
222c2c66affSColin Finck }
223c2c66affSColin Finck 
224c2c66affSColin Finck #endif /* MBEDTLS_SELF_TEST */
225c2c66affSColin Finck 
226c2c66affSColin Finck #endif /* MBEDTLS_ARC4_C */
227