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