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