Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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