Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

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-2014, Brainspark B.V.
00005  *
00006  *  This file is part of PolarSSL (http://www.polarssl.org)
00007  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
00008  *
00009  *  All rights reserved.
00010  *
00011  *  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public License along
00022  *  with this program; if not, write to the Free Software Foundation, Inc.,
00023  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00024  */
00025 /*
00026  *  The ARCFOUR algorithm was publicly disclosed on 94/09.
00027  *
00028  *  http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
00029  */
00030 
00031 #if !defined(POLARSSL_CONFIG_FILE)
00032 #include "polarssl/config.h"
00033 #else
00034 #include POLARSSL_CONFIG_FILE
00035 #endif
00036 
00037 #if defined(POLARSSL_ARC4_C)
00038 
00039 #include "polarssl/arc4.h"
00040 
00041 #if defined(POLARSSL_PLATFORM_C)
00042 #include "polarssl/platform.h"
00043 #else
00044 #define polarssl_printf printf
00045 #endif
00046 
00047 #if !defined(POLARSSL_ARC4_ALT)
00048 
00049 /*
00050  * ARC4 key schedule
00051  */
00052 void arc4_setup( arc4_context *ctx, const unsigned char *key,
00053                  unsigned int keylen )
00054 {
00055     int i, j, a;
00056     unsigned int k;
00057     unsigned char *m;
00058 
00059     ctx->x  = 0;
00060     ctx->y  = 0;
00061     m = ctx->m ;
00062 
00063     for( i = 0; i < 256; i++ )
00064         m[i] = (unsigned char) i;
00065 
00066     j = k = 0;
00067 
00068     for( i = 0; i < 256; i++, k++ )
00069     {
00070         if( k >= keylen ) k = 0;
00071 
00072         a = m[i];
00073         j = ( j + a + key[k] ) & 0xFF;
00074         m[i] = m[j];
00075         m[j] = (unsigned char) a;
00076     }
00077 }
00078 
00079 /*
00080  * ARC4 cipher function
00081  */
00082 int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
00083                 unsigned char *output )
00084 {
00085     int x, y, a, b;
00086     size_t i;
00087     unsigned char *m;
00088 
00089     x = ctx->x ;
00090     y = ctx->y ;
00091     m = ctx->m ;
00092 
00093     for( i = 0; i < length; i++ )
00094     {
00095         x = ( x + 1 ) & 0xFF; a = m[x];
00096         y = ( y + a ) & 0xFF; b = m[y];
00097 
00098         m[x] = (unsigned char) b;
00099         m[y] = (unsigned char) a;
00100 
00101         output[i] = (unsigned char)
00102             ( input[i] ^ m[(unsigned char)( a + b )] );
00103     }
00104 
00105     ctx->x  = x;
00106     ctx->y  = y;
00107 
00108     return( 0 );
00109 }
00110 
00111 #endif /* !POLARSSL_ARC4_ALT */
00112 
00113 #if defined(POLARSSL_SELF_TEST)
00114 
00115 #include <string.h>
00116 #include <stdio.h>
00117 
00118 /*
00119  * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
00120  *
00121  * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
00122  */
00123 static const unsigned char arc4_test_key[3][8] =
00124 {
00125     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00126     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00127     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00128 };
00129 
00130 static const unsigned char arc4_test_pt[3][8] =
00131 {
00132     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
00133     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
00134     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
00135 };
00136 
00137 static const unsigned char arc4_test_ct[3][8] =
00138 {
00139     { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
00140     { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
00141     { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
00142 };
00143 
00144 /*
00145  * Checkup routine
00146  */
00147 int arc4_self_test( int verbose )
00148 {
00149     int i;
00150     unsigned char ibuf[8];
00151     unsigned char obuf[8];
00152     arc4_context ctx;
00153 
00154     for( i = 0; i < 3; i++ )
00155     {
00156         if( verbose != 0 )
00157             polarssl_printf( "  ARC4 test #%d: ", i + 1 );
00158 
00159         memcpy( ibuf, arc4_test_pt[i], 8 );
00160 
00161         arc4_setup( &ctx, arc4_test_key[i], 8 );
00162         arc4_crypt( &ctx, 8, ibuf, obuf );
00163 
00164         if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
00165         {
00166             if( verbose != 0 )
00167                 polarssl_printf( "failed\n" );
00168 
00169             return( 1 );
00170         }
00171 
00172         if( verbose != 0 )
00173             polarssl_printf( "passed\n" );
00174     }
00175 
00176     if( verbose != 0 )
00177         polarssl_printf( "\n" );
00178 
00179     return( 0 );
00180 }
00181 
00182 #endif /* POLARSSL_SELF_TEST */
00183 
00184 #endif /* POLARSSL_ARC4_C */
00185 
00186