mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Brian Daniels
Date:
Thu Apr 07 11:11:18 2016 +0100
Revision:
4:bef26f687287
Parent:
1:24750b9ad5ef
Adding ported selftest test case

Who changed what in which revision?

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