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