|
| 1 | +/* |
| 2 | + * True Random Number Generator (TRNG) driver for Silicon Labs devices |
| 3 | + * |
| 4 | + * Copyright (C) 2016, Silicon Labs, http://www.silabs.com |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | + * not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +#include "sl_trng.h" |
| 20 | + |
| 21 | +#if defined(TRNG_PRESENT) |
| 22 | +#include "em_cmu.h" |
| 23 | +#include "em_common.h" |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#define FIFO_LEVEL_RETRY (1000) |
| 27 | +#define TEST_WORDS_MIN (257) |
| 28 | + |
| 29 | +#define TEST_VECTOR_CONDITIONING_KEY_SIZE (4) |
| 30 | +static const uint32_t |
| 31 | +test_vector_conditioning_key[TEST_VECTOR_CONDITIONING_KEY_SIZE] = |
| 32 | + {0x16157E2B, 0xA6D2AE28, 0x8815F7AB, 0x3C4FCF09}; |
| 33 | + |
| 34 | +#define TEST_VECTOR_CONDITIONING_INPUT_SIZE (16) |
| 35 | +static const uint32_t |
| 36 | +test_vector_conditioning_input[TEST_VECTOR_CONDITIONING_INPUT_SIZE] = |
| 37 | + {0xE1BCC06B, 0x9199452A, 0x1A7434E1, 0x25199E7F, |
| 38 | + 0x578A2DAE, 0x9CAC031E, 0xAC6FB79E, 0x518EAF45, |
| 39 | + 0x461CC830, 0x11E45CA3, 0x19C1FBE5, 0xEF520A1A, |
| 40 | + 0x45249FF6, 0x179B4FDF, 0x7B412BAD, 0x10376CE6}; |
| 41 | + |
| 42 | +#define TEST_VECTOR_CONDITIONING_OUTPUT_SIZE (4) |
| 43 | +static const uint32_t |
| 44 | +test_vector_conditioning_output[TEST_VECTOR_CONDITIONING_OUTPUT_SIZE] = |
| 45 | + {0xA1CAF13F, 0x09AC1F68, 0x30CA0E12, 0xA7E18675}; |
| 46 | + |
| 47 | +#define TRNG_STARTUP_TEST_WAIT_RETRY (10000) |
| 48 | + |
| 49 | +typedef struct { |
| 50 | + TRNG_TypeDef *instance; |
| 51 | + CMU_Clock_TypeDef clock; |
| 52 | +} sl_trng_device_t; |
| 53 | + |
| 54 | +static const sl_trng_device_t sl_trng_devices[TRNG_COUNT] = |
| 55 | +{ |
| 56 | +#if defined(TRNG0) |
| 57 | +{ |
| 58 | + TRNG0, |
| 59 | + cmuClock_TRNG0 |
| 60 | +}, |
| 61 | +#endif |
| 62 | +}; |
| 63 | + |
| 64 | +static CMU_Clock_TypeDef sl_trng_get_clock( TRNG_TypeDef *device ) |
| 65 | +{ |
| 66 | + for(int i = 0; i < TRNG_COUNT; i++) { |
| 67 | + if(sl_trng_devices[i].instance == device) { |
| 68 | + return sl_trng_devices[i].clock; |
| 69 | + } |
| 70 | + } |
| 71 | + return cmuClock_TRNG0; |
| 72 | +} |
| 73 | + |
| 74 | +void sl_trng_init( TRNG_TypeDef *device ) |
| 75 | +{ |
| 76 | + int i; |
| 77 | + |
| 78 | + /* Enable the TRNG's clock. */ |
| 79 | + CMU_ClockEnable( sl_trng_get_clock(device), true ); |
| 80 | + |
| 81 | + device->CONTROL = |
| 82 | + TRNG_CONTROL_ENABLE | |
| 83 | + TRNG_CONTROL_REPCOUNTIEN | |
| 84 | + TRNG_CONTROL_APT64IEN | |
| 85 | + TRNG_CONTROL_APT4096IEN | |
| 86 | + TRNG_CONTROL_PREIEN | |
| 87 | + TRNG_CONTROL_ALMIEN; |
| 88 | + |
| 89 | + /* Apply software reset */ |
| 90 | + sl_trng_soft_reset(device); |
| 91 | + |
| 92 | + /* Wait for TRNG to complete startup tests and start filling the FIFO. */ |
| 93 | + for (i=0; (device->FIFOLEVEL == 0) && (i<TRNG_STARTUP_TEST_WAIT_RETRY); i++); |
| 94 | + EFM_ASSERT(i<TRNG_STARTUP_TEST_WAIT_RETRY); |
| 95 | +} |
| 96 | + |
| 97 | +void sl_trng_free( TRNG_TypeDef *device ) |
| 98 | +{ |
| 99 | + /* Disable TRNG. */ |
| 100 | + device->CONTROL = 0; |
| 101 | + |
| 102 | + /* Disable the TRNG clock. */ |
| 103 | + CMU_ClockEnable( sl_trng_get_clock(device), false ); |
| 104 | +} |
| 105 | + |
| 106 | +void sl_trng_soft_reset( TRNG_TypeDef *device ) |
| 107 | +{ |
| 108 | + uint32_t ctrl = device->CONTROL; |
| 109 | + |
| 110 | + ctrl |= TRNG_CONTROL_SOFTRESET; |
| 111 | + |
| 112 | + device->CONTROL = ctrl; |
| 113 | + |
| 114 | + ctrl &= ~TRNG_CONTROL_SOFTRESET; |
| 115 | + device->CONTROL = ctrl; |
| 116 | +} |
| 117 | + |
| 118 | +static void sl_trng_clear_fifo( TRNG_TypeDef *device ) |
| 119 | +{ |
| 120 | + volatile uint32_t val32; |
| 121 | + |
| 122 | + /* Empty FIFO */ |
| 123 | + while ( device->FIFOLEVEL ) |
| 124 | + { |
| 125 | + val32 = device->FIFO; |
| 126 | + (void)val32; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +int sl_trng_set_key( TRNG_TypeDef *device, const unsigned char *key ) |
| 131 | +{ |
| 132 | + uint32_t *_key = (uint32_t*) key; |
| 133 | + |
| 134 | + sl_trng_clear_fifo(device); |
| 135 | + |
| 136 | + /* Program key in KEY registers of the TRNG. */ |
| 137 | + device->KEY0 = *_key++; |
| 138 | + device->KEY1 = *_key++; |
| 139 | + device->KEY2 = *_key++; |
| 140 | + device->KEY3 = *_key++; |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +static int sl_trng_check_status( TRNG_TypeDef *device ) |
| 146 | +{ |
| 147 | + uint32_t status = device->STATUS; |
| 148 | + |
| 149 | + if ( (status & (TRNG_STATUS_PREIF |
| 150 | + | TRNG_STATUS_REPCOUNTIF |
| 151 | + | TRNG_STATUS_APT64IF |
| 152 | + | TRNG_STATUS_APT4096IF |
| 153 | + | TRNG_STATUS_ALMIF)) == 0 ) |
| 154 | + { |
| 155 | + /* No errors */ |
| 156 | + return 0; |
| 157 | + } |
| 158 | + |
| 159 | + if ( status & TRNG_STATUS_PREIF ) |
| 160 | + { |
| 161 | + /* On a preliminary noise alarm we clear the FIFO and clear |
| 162 | + * the alarm. The preliminary noise alarm is not critical. */ |
| 163 | + status &= ~TRNG_STATUS_PREIF; |
| 164 | + device->STATUS = status; |
| 165 | + sl_trng_clear_fifo(device); |
| 166 | + return SL_TRNG_ERR_PRELIMINARY_NOISE_ALARM; |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + /* Clear alarm conditions by doing a TRNG soft reset. */ |
| 171 | + sl_trng_soft_reset( device ); |
| 172 | + if ( status & TRNG_STATUS_REPCOUNTIF ) |
| 173 | + { |
| 174 | + return SL_TRNG_ERR_REPETITION_COUNT_TEST_FAILED; |
| 175 | + } |
| 176 | + if ( status & TRNG_STATUS_APT64IF ) |
| 177 | + { |
| 178 | + return SL_TRNG_ERR_ADAPTIVE_PROPORTION_TEST_64_FAILED; |
| 179 | + } |
| 180 | + if ( status & TRNG_STATUS_APT4096IF ) |
| 181 | + { |
| 182 | + return SL_TRNG_ERR_ADAPTIVE_PROPORTION_TEST_4096_FAILED; |
| 183 | + } |
| 184 | + if ( status & TRNG_STATUS_ALMIF ) |
| 185 | + { |
| 186 | + return SL_TRNG_ERR_NOISE_ALARM; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return 0; |
| 191 | +} |
| 192 | + |
| 193 | +static void sl_trng_read_chunk( TRNG_TypeDef *device, |
| 194 | + unsigned char *output, |
| 195 | + size_t len ) |
| 196 | +{ |
| 197 | + uint32_t * out32 = (uint32_t *) output; |
| 198 | + uint32_t tmp; |
| 199 | + |
| 200 | + /* Read known good available data. */ |
| 201 | + while ( len >= 4) |
| 202 | + { |
| 203 | + *out32++ = device->FIFO; |
| 204 | + len -= 4; |
| 205 | + } |
| 206 | + |
| 207 | + /* Handle the case where len is not a multiple of 4. */ |
| 208 | + if ( len < 4 ) |
| 209 | + { |
| 210 | + tmp = device->FIFO; |
| 211 | + memcpy((uint8_t *)out32, (const uint8_t *) &tmp, len); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +int sl_trng_poll( TRNG_TypeDef *device, |
| 216 | + unsigned char *output, |
| 217 | + size_t len, |
| 218 | + size_t *olen ) |
| 219 | +{ |
| 220 | + size_t output_len = 0; |
| 221 | + size_t chunk_len = 0; |
| 222 | + size_t available; |
| 223 | + int ret = 0; |
| 224 | + |
| 225 | + while (output_len < len) |
| 226 | + { |
| 227 | + available = device->FIFOLEVEL * 4; |
| 228 | + if (available == 0) |
| 229 | + { |
| 230 | + break; |
| 231 | + } |
| 232 | + |
| 233 | +#if !defined(SL_TRNG_IGNORE_ALL_ALARMS) |
| 234 | + /* Check status for current data in FIFO |
| 235 | + * and handle any error conditions. */ |
| 236 | + ret = sl_trng_check_status( device ); |
| 237 | +#if defined(SL_TRNG_IGNORE_NOISE_ALARMS) |
| 238 | + /* Ignore noise alarms by returning 0 (OK) if they occur and |
| 239 | + * keeping the already generated random data. */ |
| 240 | + if ( (ret == SL_TRNG_ERR_PRELIMINARY_NOISE_ALARM) || |
| 241 | + (ret == SL_TRNG_ERR_NOISE_ALARM) ) |
| 242 | + { |
| 243 | + ret = 0; |
| 244 | + continue; |
| 245 | + } |
| 246 | +#else |
| 247 | + /* Noise alarms trigger a FIFO clearing, and we need to throw |
| 248 | + * away the collected entropy. */ |
| 249 | + if ( (ret == SL_TRNG_ERR_PRELIMINARY_NOISE_ALARM) || |
| 250 | + (ret == SL_TRNG_ERR_NOISE_ALARM) ) |
| 251 | + { |
| 252 | + ret = 0; |
| 253 | + output_len = 0; |
| 254 | + continue; |
| 255 | + } |
| 256 | +#endif |
| 257 | + /* Alarm has been signaled so we throw the generated data away. */ |
| 258 | + if (ret != 0) |
| 259 | + { |
| 260 | + output_len = 0; |
| 261 | + break; |
| 262 | + } |
| 263 | +#endif |
| 264 | + |
| 265 | + chunk_len = SL_MIN(len - output_len, available); |
| 266 | + sl_trng_read_chunk(device, output + output_len, chunk_len); |
| 267 | + output_len += chunk_len; |
| 268 | + } |
| 269 | + |
| 270 | + *olen = output_len; |
| 271 | + return ret; |
| 272 | +} |
| 273 | + |
| 274 | +#endif /* TRNG_PRESENT */ |
0 commit comments