mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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