|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017-2018 Nuvoton |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#if DEVICE_TRNG |
| 18 | + |
| 19 | +#include "cmsis.h" |
| 20 | +#include <limits.h> |
| 21 | +#include "crypto-misc.h" |
| 22 | +#include "hal/trng_api.h" |
| 23 | +#include "platform/mbed_toolchain.h" |
| 24 | +#include "platform/mbed_critical.h" |
| 25 | +#include "platform/mbed_error.h" |
| 26 | +#include "nu_modutil.h" |
| 27 | +#include "hal_secure.h" |
| 28 | +#include "partition_M2351.h" |
| 29 | + |
| 30 | +#if defined(SCU_INIT_PNSSET5_VAL) && (SCU_INIT_PNSSET5_VAL & (1 << 25)) |
| 31 | +#error("We just support secure TRNG") |
| 32 | +#endif |
| 33 | + |
| 34 | +#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3L) |
| 35 | + |
| 36 | +/* Module init definition: modname, clkidx, clksrc, clkdiv, rstidx, irqnum, misc */ |
| 37 | +static const struct nu_modinit_s trng_modinit = {TRNG_0, TRNG_MODULE, 0, 0, TRNG_RST, TRNG_IRQn, NULL}; |
| 38 | + |
| 39 | +/* TRNG init counter. TRNG is kept active as it is non-zero. */ |
| 40 | +static uint16_t trng_init_counter = 0U; |
| 41 | + |
| 42 | +#endif |
| 43 | + |
| 44 | +void trng_init(trng_t *obj) |
| 45 | +{ |
| 46 | + trng_init_s(obj); |
| 47 | +} |
| 48 | + |
| 49 | +void trng_free(trng_t *obj) |
| 50 | +{ |
| 51 | + trng_free_s(obj); |
| 52 | +} |
| 53 | + |
| 54 | +int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) |
| 55 | +{ |
| 56 | + uint32_t output_length_; |
| 57 | + int32_t rc = trng_get_bytes_s(obj, output, (uint32_t) length, &output_length_); |
| 58 | + if (output_length) { |
| 59 | + *output_length = output_length_; |
| 60 | + } |
| 61 | + return rc; |
| 62 | +} |
| 63 | + |
| 64 | +#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) |
| 65 | + |
| 66 | +extern "C" |
| 67 | +__NONSECURE_ENTRY |
| 68 | +void trng_init_s(MBED_UNUSED void *obj) |
| 69 | +{ |
| 70 | + core_util_critical_section_enter(); |
| 71 | + if (trng_init_counter == USHRT_MAX) { |
| 72 | + core_util_critical_section_exit(); |
| 73 | + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_OVERFLOW), \ |
| 74 | + "TRNG initialization counter would overflow"); |
| 75 | + } |
| 76 | + ++ trng_init_counter; |
| 77 | + if (trng_init_counter == 1) { |
| 78 | + /* Enable IP clock (secure version) */ |
| 79 | + CLK_EnableModuleClock_S(trng_modinit.clkidx); |
| 80 | + |
| 81 | + /* Reset IP (secure version) */ |
| 82 | + SYS_ResetModule_S(trng_modinit.rsetidx); |
| 83 | + |
| 84 | + TRNG_T *trng_base = (TRNG_T *) NU_MODBASE(trng_modinit.modname); |
| 85 | + |
| 86 | + trng_base->ACT |= TRNG_ACT_ACT_Msk; |
| 87 | + while (!(trng_base->CTL & TRNG_CTL_READY_Msk)); |
| 88 | + } |
| 89 | + core_util_critical_section_exit(); |
| 90 | +} |
| 91 | + |
| 92 | +extern "C" |
| 93 | +__NONSECURE_ENTRY |
| 94 | +void trng_free_s(MBED_UNUSED void *obj) |
| 95 | +{ |
| 96 | + core_util_critical_section_enter(); |
| 97 | + if (trng_init_counter == 0) { |
| 98 | + core_util_critical_section_exit(); |
| 99 | + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_HAL, MBED_ERROR_CODE_UNDERFLOW), \ |
| 100 | + "TRNG initialization counter would underflow"); |
| 101 | + } |
| 102 | + -- trng_init_counter; |
| 103 | + if (trng_init_counter == 0) { |
| 104 | + TRNG_T *trng_base = (TRNG_T *) NU_MODBASE(trng_modinit.modname); |
| 105 | + |
| 106 | + trng_base->ACT &= ~TRNG_ACT_ACT_Msk; |
| 107 | + |
| 108 | + /* Disable IP clock (secure version) */ |
| 109 | + CLK_DisableModuleClock_S(trng_modinit.clkidx); |
| 110 | + } |
| 111 | + core_util_critical_section_exit(); |
| 112 | +} |
| 113 | + |
| 114 | +extern "C" |
| 115 | +__NONSECURE_ENTRY |
| 116 | +int32_t trng_get_bytes_s(MBED_UNUSED void *obj, uint8_t *output, uint32_t length, uint32_t *output_length) |
| 117 | +{ |
| 118 | + /* Check augument validity */ |
| 119 | + if (!output && length) { |
| 120 | + return -1; |
| 121 | + } |
| 122 | + |
| 123 | + uint8_t *output_ind = output; |
| 124 | + uint8_t *output_end = output + length; |
| 125 | + |
| 126 | + TRNG_T *trng_base = (TRNG_T *) NU_MODBASE(trng_modinit.modname); |
| 127 | + |
| 128 | + for (; output_ind != output_end; output_ind ++) { |
| 129 | + trng_base->CTL |= TRNG_CTL_TRNGEN_Msk; |
| 130 | + while (!(trng_base->CTL & TRNG_CTL_DVIF_Msk)); |
| 131 | + *output_ind = trng_base->DATA & 0xff; |
| 132 | + } |
| 133 | + |
| 134 | + if (output_length) { |
| 135 | + *output_length = length; |
| 136 | + } |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
| 140 | + |
| 141 | +#endif |
| 142 | +#endif |
0 commit comments