Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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