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 * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
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 HAVEGE RNG was designed by Andre Seznec in 2002.
elessair 0:f269e3021894 23 *
elessair 0:f269e3021894 24 * http://www.irisa.fr/caps/projects/hipsor/publi.php
elessair 0:f269e3021894 25 *
elessair 0:f269e3021894 26 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
elessair 0:f269e3021894 27 */
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 #if !defined(MBEDTLS_CONFIG_FILE)
elessair 0:f269e3021894 30 #include "mbedtls/config.h"
elessair 0:f269e3021894 31 #else
elessair 0:f269e3021894 32 #include MBEDTLS_CONFIG_FILE
elessair 0:f269e3021894 33 #endif
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 #if defined(MBEDTLS_HAVEGE_C)
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 #include "mbedtls/havege.h"
elessair 0:f269e3021894 38 #include "mbedtls/timing.h"
elessair 0:f269e3021894 39
elessair 0:f269e3021894 40 #include <string.h>
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 /* Implementation that should never be optimized out by the compiler */
elessair 0:f269e3021894 43 static void mbedtls_zeroize( void *v, size_t n ) {
elessair 0:f269e3021894 44 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
elessair 0:f269e3021894 45 }
elessair 0:f269e3021894 46
elessair 0:f269e3021894 47 /* ------------------------------------------------------------------------
elessair 0:f269e3021894 48 * On average, one iteration accesses two 8-word blocks in the havege WALK
elessair 0:f269e3021894 49 * table, and generates 16 words in the RES array.
elessair 0:f269e3021894 50 *
elessair 0:f269e3021894 51 * The data read in the WALK table is updated and permuted after each use.
elessair 0:f269e3021894 52 * The result of the hardware clock counter read is used for this update.
elessair 0:f269e3021894 53 *
elessair 0:f269e3021894 54 * 25 conditional tests are present. The conditional tests are grouped in
elessair 0:f269e3021894 55 * two nested groups of 12 conditional tests and 1 test that controls the
elessair 0:f269e3021894 56 * permutation; on average, there should be 6 tests executed and 3 of them
elessair 0:f269e3021894 57 * should be mispredicted.
elessair 0:f269e3021894 58 * ------------------------------------------------------------------------
elessair 0:f269e3021894 59 */
elessair 0:f269e3021894 60
elessair 0:f269e3021894 61 #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
elessair 0:f269e3021894 64 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 #define TST1_LEAVE U1++; }
elessair 0:f269e3021894 67 #define TST2_LEAVE U2++; }
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 #define ONE_ITERATION \
elessair 0:f269e3021894 70 \
elessair 0:f269e3021894 71 PTEST = PT1 >> 20; \
elessair 0:f269e3021894 72 \
elessair 0:f269e3021894 73 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
elessair 0:f269e3021894 74 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
elessair 0:f269e3021894 75 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
elessair 0:f269e3021894 76 \
elessair 0:f269e3021894 77 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
elessair 0:f269e3021894 78 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
elessair 0:f269e3021894 79 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
elessair 0:f269e3021894 80 \
elessair 0:f269e3021894 81 PTX = (PT1 >> 18) & 7; \
elessair 0:f269e3021894 82 PT1 &= 0x1FFF; \
elessair 0:f269e3021894 83 PT2 &= 0x1FFF; \
elessair 0:f269e3021894 84 CLK = (int) mbedtls_timing_hardclock(); \
elessair 0:f269e3021894 85 \
elessair 0:f269e3021894 86 i = 0; \
elessair 0:f269e3021894 87 A = &WALK[PT1 ]; RES[i++] ^= *A; \
elessair 0:f269e3021894 88 B = &WALK[PT2 ]; RES[i++] ^= *B; \
elessair 0:f269e3021894 89 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
elessair 0:f269e3021894 90 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
elessair 0:f269e3021894 91 \
elessair 0:f269e3021894 92 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
elessair 0:f269e3021894 93 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
elessair 0:f269e3021894 94 *B = IN ^ U1; \
elessair 0:f269e3021894 95 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
elessair 0:f269e3021894 96 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
elessair 0:f269e3021894 97 \
elessair 0:f269e3021894 98 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
elessair 0:f269e3021894 99 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
elessair 0:f269e3021894 100 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
elessair 0:f269e3021894 101 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
elessair 0:f269e3021894 102 \
elessair 0:f269e3021894 103 if( PTEST & 1 ) SWAP( A, C ); \
elessair 0:f269e3021894 104 \
elessair 0:f269e3021894 105 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
elessair 0:f269e3021894 106 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
elessair 0:f269e3021894 107 *B = IN; CLK = (int) mbedtls_timing_hardclock(); \
elessair 0:f269e3021894 108 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
elessair 0:f269e3021894 109 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
elessair 0:f269e3021894 110 \
elessair 0:f269e3021894 111 A = &WALK[PT1 ^ 4]; \
elessair 0:f269e3021894 112 B = &WALK[PT2 ^ 1]; \
elessair 0:f269e3021894 113 \
elessair 0:f269e3021894 114 PTEST = PT2 >> 1; \
elessair 0:f269e3021894 115 \
elessair 0:f269e3021894 116 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
elessair 0:f269e3021894 117 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
elessair 0:f269e3021894 118 PTY = (PT2 >> 10) & 7; \
elessair 0:f269e3021894 119 \
elessair 0:f269e3021894 120 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
elessair 0:f269e3021894 121 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
elessair 0:f269e3021894 122 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
elessair 0:f269e3021894 123 \
elessair 0:f269e3021894 124 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
elessair 0:f269e3021894 125 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
elessair 0:f269e3021894 126 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
elessair 0:f269e3021894 127 \
elessair 0:f269e3021894 128 C = &WALK[PT1 ^ 5]; \
elessair 0:f269e3021894 129 D = &WALK[PT2 ^ 5]; \
elessair 0:f269e3021894 130 \
elessair 0:f269e3021894 131 RES[i++] ^= *A; \
elessair 0:f269e3021894 132 RES[i++] ^= *B; \
elessair 0:f269e3021894 133 RES[i++] ^= *C; \
elessair 0:f269e3021894 134 RES[i++] ^= *D; \
elessair 0:f269e3021894 135 \
elessair 0:f269e3021894 136 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
elessair 0:f269e3021894 137 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
elessair 0:f269e3021894 138 *B = IN ^ U2; \
elessair 0:f269e3021894 139 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
elessair 0:f269e3021894 140 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
elessair 0:f269e3021894 141 \
elessair 0:f269e3021894 142 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
elessair 0:f269e3021894 143 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
elessair 0:f269e3021894 144 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
elessair 0:f269e3021894 145 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
elessair 0:f269e3021894 146 \
elessair 0:f269e3021894 147 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
elessair 0:f269e3021894 148 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
elessair 0:f269e3021894 149 *B = IN; \
elessair 0:f269e3021894 150 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
elessair 0:f269e3021894 151 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
elessair 0:f269e3021894 152 \
elessair 0:f269e3021894 153 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
elessair 0:f269e3021894 154 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
elessair 0:f269e3021894 155 PT1 ^= (PT2 ^ 0x10) & 0x10; \
elessair 0:f269e3021894 156 \
elessair 0:f269e3021894 157 for( n++, i = 0; i < 16; i++ ) \
elessair 0:f269e3021894 158 hs->pool[n % MBEDTLS_HAVEGE_COLLECT_SIZE] ^= RES[i];
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 /*
elessair 0:f269e3021894 161 * Entropy gathering function
elessair 0:f269e3021894 162 */
elessair 0:f269e3021894 163 static void havege_fill( mbedtls_havege_state *hs )
elessair 0:f269e3021894 164 {
elessair 0:f269e3021894 165 int i, n = 0;
elessair 0:f269e3021894 166 int U1, U2, *A, *B, *C, *D;
elessair 0:f269e3021894 167 int PT1, PT2, *WALK, RES[16];
elessair 0:f269e3021894 168 int PTX, PTY, CLK, PTEST, IN;
elessair 0:f269e3021894 169
elessair 0:f269e3021894 170 WALK = hs->WALK;
elessair 0:f269e3021894 171 PT1 = hs->PT1;
elessair 0:f269e3021894 172 PT2 = hs->PT2;
elessair 0:f269e3021894 173
elessair 0:f269e3021894 174 PTX = U1 = 0;
elessair 0:f269e3021894 175 PTY = U2 = 0;
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 (void)PTX;
elessair 0:f269e3021894 178
elessair 0:f269e3021894 179 memset( RES, 0, sizeof( RES ) );
elessair 0:f269e3021894 180
elessair 0:f269e3021894 181 while( n < MBEDTLS_HAVEGE_COLLECT_SIZE * 4 )
elessair 0:f269e3021894 182 {
elessair 0:f269e3021894 183 ONE_ITERATION
elessair 0:f269e3021894 184 ONE_ITERATION
elessair 0:f269e3021894 185 ONE_ITERATION
elessair 0:f269e3021894 186 ONE_ITERATION
elessair 0:f269e3021894 187 }
elessair 0:f269e3021894 188
elessair 0:f269e3021894 189 hs->PT1 = PT1;
elessair 0:f269e3021894 190 hs->PT2 = PT2;
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 hs->offset[0] = 0;
elessair 0:f269e3021894 193 hs->offset[1] = MBEDTLS_HAVEGE_COLLECT_SIZE / 2;
elessair 0:f269e3021894 194 }
elessair 0:f269e3021894 195
elessair 0:f269e3021894 196 /*
elessair 0:f269e3021894 197 * HAVEGE initialization
elessair 0:f269e3021894 198 */
elessair 0:f269e3021894 199 void mbedtls_havege_init( mbedtls_havege_state *hs )
elessair 0:f269e3021894 200 {
elessair 0:f269e3021894 201 memset( hs, 0, sizeof( mbedtls_havege_state ) );
elessair 0:f269e3021894 202
elessair 0:f269e3021894 203 havege_fill( hs );
elessair 0:f269e3021894 204 }
elessair 0:f269e3021894 205
elessair 0:f269e3021894 206 void mbedtls_havege_free( mbedtls_havege_state *hs )
elessair 0:f269e3021894 207 {
elessair 0:f269e3021894 208 if( hs == NULL )
elessair 0:f269e3021894 209 return;
elessair 0:f269e3021894 210
elessair 0:f269e3021894 211 mbedtls_zeroize( hs, sizeof( mbedtls_havege_state ) );
elessair 0:f269e3021894 212 }
elessair 0:f269e3021894 213
elessair 0:f269e3021894 214 /*
elessair 0:f269e3021894 215 * HAVEGE rand function
elessair 0:f269e3021894 216 */
elessair 0:f269e3021894 217 int mbedtls_havege_random( void *p_rng, unsigned char *buf, size_t len )
elessair 0:f269e3021894 218 {
elessair 0:f269e3021894 219 int val;
elessair 0:f269e3021894 220 size_t use_len;
elessair 0:f269e3021894 221 mbedtls_havege_state *hs = (mbedtls_havege_state *) p_rng;
elessair 0:f269e3021894 222 unsigned char *p = buf;
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 while( len > 0 )
elessair 0:f269e3021894 225 {
elessair 0:f269e3021894 226 use_len = len;
elessair 0:f269e3021894 227 if( use_len > sizeof(int) )
elessair 0:f269e3021894 228 use_len = sizeof(int);
elessair 0:f269e3021894 229
elessair 0:f269e3021894 230 if( hs->offset[1] >= MBEDTLS_HAVEGE_COLLECT_SIZE )
elessair 0:f269e3021894 231 havege_fill( hs );
elessair 0:f269e3021894 232
elessair 0:f269e3021894 233 val = hs->pool[hs->offset[0]++];
elessair 0:f269e3021894 234 val ^= hs->pool[hs->offset[1]++];
elessair 0:f269e3021894 235
elessair 0:f269e3021894 236 memcpy( p, &val, use_len );
elessair 0:f269e3021894 237
elessair 0:f269e3021894 238 len -= use_len;
elessair 0:f269e3021894 239 p += use_len;
elessair 0:f269e3021894 240 }
elessair 0:f269e3021894 241
elessair 0:f269e3021894 242 return( 0 );
elessair 0:f269e3021894 243 }
elessair 0:f269e3021894 244
elessair 0:f269e3021894 245 #endif /* MBEDTLS_HAVEGE_C */