I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

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?

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