mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arc4.c Source File

arc4.c

00001 /*
00002  *  An implementation of the ARCFOUR algorithm
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 /*
00022  *  The ARCFOUR algorithm was publicly disclosed on 94/09.
00023  *
00024  *  http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
00025  */
00026 
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "mbedtls/config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #if defined(MBEDTLS_ARC4_C)
00034 
00035 #include "mbedtls/arc4.h"
00036 
00037 #include <string.h>
00038 
00039 #if defined(MBEDTLS_SELF_TEST)
00040 #if defined(MBEDTLS_PLATFORM_C)
00041 #include "mbedtls/platform.h"
00042 #else
00043 #include <stdio.h>
00044 #define mbedtls_printf printf
00045 #endif /* MBEDTLS_PLATFORM_C */
00046 #endif /* MBEDTLS_SELF_TEST */
00047 
00048 #if !defined(MBEDTLS_ARC4_ALT)
00049 
00050 /* Implementation that should never be optimized out by the compiler */
00051 static void mbedtls_zeroize( void *v, size_t n ) {
00052     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
00053 }
00054 
00055 void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
00056 {
00057     memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
00058 }
00059 
00060 void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
00061 {
00062     if( ctx == NULL )
00063         return;
00064 
00065     mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
00066 }
00067 
00068 /*
00069  * ARC4 key schedule
00070  */
00071 void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
00072                  unsigned int keylen )
00073 {
00074     int i, j, a;
00075     unsigned int k;
00076     unsigned char *m;
00077 
00078     ctx->x  = 0;
00079     ctx->y  = 0;
00080     m = ctx->m ;
00081 
00082     for( i = 0; i < 256; i++ )
00083         m[i] = (unsigned char) i;
00084 
00085     j = k = 0;
00086 
00087     for( i = 0; i < 256; i++, k++ )
00088     {
00089         if( k >= keylen ) k = 0;
00090 
00091         a = m[i];
00092         j = ( j + a + key[k] ) & 0xFF;
00093         m[i] = m[j];
00094         m[j] = (unsigned char) a;
00095     }
00096 }
00097 
00098 /*
00099  * ARC4 cipher function
00100  */
00101 int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
00102                 unsigned char *output )
00103 {
00104     int x, y, a, b;
00105     size_t i;
00106     unsigned char *m;
00107 
00108     x = ctx->x ;
00109     y = ctx->y ;
00110     m = ctx->m ;
00111 
00112     for( i = 0; i < length; i++ )
00113     {
00114         x = ( x + 1 ) & 0xFF; a = m[x];
00115         y = ( y + a ) & 0xFF; b = m[y];
00116 
00117         m[x] = (unsigned char) b;
00118         m[y] = (unsigned char) a;
00119 
00120         output[i] = (unsigned char)
00121             ( input[i] ^ m[(unsigned char)( a + b )] );
00122     }
00123 
00124     ctx->x  = x;
00125     ctx->y  = y;
00126 
00127     return( 0 );
00128 }
00129 
00130 #endif /* !MBEDTLS_ARC4_ALT */
00131 
00132 #if defined(MBEDTLS_SELF_TEST)
00133 /*
00134  * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
00135  *
00136  * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
00137  */
00138 static const unsigned char arc4_test_key[3][8] =
00139 {
00140     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00141     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00142     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00143 };
00144 
00145 static const unsigned char arc4_test_pt[3][8] =
00146 {
00147     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00148     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
00149     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00150 };
00151 
00152 static const unsigned char arc4_test_ct[3][8] =
00153 {
00154     { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
00155     { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
00156     { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
00157 };
00158 
00159 /*
00160  * Checkup routine
00161  */
00162 int mbedtls_arc4_self_test( int verbose )
00163 {
00164     int i, ret = 0;
00165     unsigned char ibuf[8];
00166     unsigned char obuf[8];
00167     mbedtls_arc4_context ctx;
00168 
00169     mbedtls_arc4_init( &ctx );
00170 
00171     for( i = 0; i < 3; i++ )
00172     {
00173         if( verbose != 0 )
00174             mbedtls_printf( "  ARC4 test #%d: ", i + 1 );
00175 
00176         memcpy( ibuf, arc4_test_pt[i], 8 );
00177 
00178         mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
00179         mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
00180 
00181         if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
00182         {
00183             if( verbose != 0 )
00184                 mbedtls_printf( "failed\n" );
00185 
00186             ret = 1;
00187             goto exit;
00188         }
00189 
00190         if( verbose != 0 )
00191             mbedtls_printf( "passed\n" );
00192     }
00193 
00194     if( verbose != 0 )
00195         mbedtls_printf( "\n" );
00196 
00197 exit:
00198     mbedtls_arc4_free( &ctx );
00199 
00200     return( ret );
00201 }
00202 
00203 #endif /* MBEDTLS_SELF_TEST */
00204 
00205 #endif /* MBEDTLS_ARC4_C */