Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: nRF51_Vdd TextLCD BME280
arc4.c
00001 /* 00002 * An implementation of the ARCFOUR algorithm 00003 * 00004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00005 * SPDX-License-Identifier: Apache-2.0 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00008 * not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 * 00019 * This file is part of mbed TLS (https://tls.mbed.org) 00020 */ 00021 /* 00022 * The ARCFOUR algorithm was publicly disclosed on 94/09. 00023 * 00024 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0 00025 */ 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "mbedtls/config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #if defined(MBEDTLS_ARC4_C) 00034 00035 #include "mbedtls/arc4.h" 00036 #include "mbedtls/platform_util.h" 00037 00038 #include <string.h> 00039 00040 #if defined(MBEDTLS_SELF_TEST) 00041 #if defined(MBEDTLS_PLATFORM_C) 00042 #include "mbedtls/platform.h" 00043 #else 00044 #include <stdio.h> 00045 #define mbedtls_printf printf 00046 #endif /* MBEDTLS_PLATFORM_C */ 00047 #endif /* MBEDTLS_SELF_TEST */ 00048 00049 #if !defined(MBEDTLS_ARC4_ALT) 00050 00051 void mbedtls_arc4_init( mbedtls_arc4_context *ctx ) 00052 { 00053 memset( ctx, 0, sizeof( mbedtls_arc4_context ) ); 00054 } 00055 00056 void mbedtls_arc4_free( mbedtls_arc4_context *ctx ) 00057 { 00058 if( ctx == NULL ) 00059 return; 00060 00061 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_arc4_context ) ); 00062 } 00063 00064 /* 00065 * ARC4 key schedule 00066 */ 00067 void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, 00068 unsigned int keylen ) 00069 { 00070 int i, j, a; 00071 unsigned int k; 00072 unsigned char *m; 00073 00074 ctx->x = 0; 00075 ctx->y = 0; 00076 m = ctx->m ; 00077 00078 for( i = 0; i < 256; i++ ) 00079 m[i] = (unsigned char) i; 00080 00081 j = k = 0; 00082 00083 for( i = 0; i < 256; i++, k++ ) 00084 { 00085 if( k >= keylen ) k = 0; 00086 00087 a = m[i]; 00088 j = ( j + a + key[k] ) & 0xFF; 00089 m[i] = m[j]; 00090 m[j] = (unsigned char) a; 00091 } 00092 } 00093 00094 /* 00095 * ARC4 cipher function 00096 */ 00097 int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, 00098 unsigned char *output ) 00099 { 00100 int x, y, a, b; 00101 size_t i; 00102 unsigned char *m; 00103 00104 x = ctx->x ; 00105 y = ctx->y ; 00106 m = ctx->m ; 00107 00108 for( i = 0; i < length; i++ ) 00109 { 00110 x = ( x + 1 ) & 0xFF; a = m[x]; 00111 y = ( y + a ) & 0xFF; b = m[y]; 00112 00113 m[x] = (unsigned char) b; 00114 m[y] = (unsigned char) a; 00115 00116 output[i] = (unsigned char) 00117 ( input[i] ^ m[(unsigned char)( a + b )] ); 00118 } 00119 00120 ctx->x = x; 00121 ctx->y = y; 00122 00123 return( 0 ); 00124 } 00125 00126 #endif /* !MBEDTLS_ARC4_ALT */ 00127 00128 #if defined(MBEDTLS_SELF_TEST) 00129 /* 00130 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: 00131 * 00132 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0 00133 */ 00134 static const unsigned char arc4_test_key[3][8] = 00135 { 00136 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, 00137 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, 00138 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } 00139 }; 00140 00141 static const unsigned char arc4_test_pt[3][8] = 00142 { 00143 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, 00144 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 00145 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } 00146 }; 00147 00148 static const unsigned char arc4_test_ct[3][8] = 00149 { 00150 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 }, 00151 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 }, 00152 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A } 00153 }; 00154 00155 /* 00156 * Checkup routine 00157 */ 00158 int mbedtls_arc4_self_test( int verbose ) 00159 { 00160 int i, ret = 0; 00161 unsigned char ibuf[8]; 00162 unsigned char obuf[8]; 00163 mbedtls_arc4_context ctx; 00164 00165 mbedtls_arc4_init( &ctx ); 00166 00167 for( i = 0; i < 3; i++ ) 00168 { 00169 if( verbose != 0 ) 00170 mbedtls_printf( " ARC4 test #%d: ", i + 1 ); 00171 00172 memcpy( ibuf, arc4_test_pt[i], 8 ); 00173 00174 mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 ); 00175 mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf ); 00176 00177 if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) 00178 { 00179 if( verbose != 0 ) 00180 mbedtls_printf( "failed\n" ); 00181 00182 ret = 1; 00183 goto exit; 00184 } 00185 00186 if( verbose != 0 ) 00187 mbedtls_printf( "passed\n" ); 00188 } 00189 00190 if( verbose != 0 ) 00191 mbedtls_printf( "\n" ); 00192 00193 exit: 00194 mbedtls_arc4_free( &ctx ); 00195 00196 return( ret ); 00197 } 00198 00199 #endif /* MBEDTLS_SELF_TEST */ 00200 00201 #endif /* MBEDTLS_ARC4_C */
Generated on Tue Jul 12 2022 15:15:39 by
