Roy Want / Mbed OS beaconCompileReadyFork
Committer:
roywant
Date:
Mon Sep 19 00:59:11 2016 +0000
Revision:
0:ed0152b5c495
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
roywant 0:ed0152b5c495 1 /* mbed Microcontroller Library
roywant 0:ed0152b5c495 2 * Copyright (c) 2006-2015 ARM Limited
roywant 0:ed0152b5c495 3 *
roywant 0:ed0152b5c495 4 * Licensed under the Apache License, Version 2.0 (the "License");
roywant 0:ed0152b5c495 5 * you may not use this file except in compliance with the License.
roywant 0:ed0152b5c495 6 * You may obtain a copy of the License at
roywant 0:ed0152b5c495 7 *
roywant 0:ed0152b5c495 8 * http://www.apache.org/licenses/LICENSE-2.0
roywant 0:ed0152b5c495 9 *
roywant 0:ed0152b5c495 10 * Unless required by applicable law or agreed to in writing, software
roywant 0:ed0152b5c495 11 * distributed under the License is distributed on an "AS IS" BASIS,
roywant 0:ed0152b5c495 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
roywant 0:ed0152b5c495 13 * See the License for the specific language governing permissions and
roywant 0:ed0152b5c495 14 * limitations under the License.
roywant 0:ed0152b5c495 15 */
roywant 0:ed0152b5c495 16 #include "../EntropySource.h"
roywant 0:ed0152b5c495 17
roywant 0:ed0152b5c495 18 #if defined(TARGET_NRF51822) || defined(TARGET_MCU_NRF52832) /* Persistent storage supported on nrf51 platforms */
roywant 0:ed0152b5c495 19
roywant 0:ed0152b5c495 20 #include "nrf_soc.h"
roywant 0:ed0152b5c495 21 #include "nrf_error.h"
roywant 0:ed0152b5c495 22 #include "mbed.h"
roywant 0:ed0152b5c495 23 #include <mbedtls/entropy.h>
roywant 0:ed0152b5c495 24
roywant 0:ed0152b5c495 25 /*
roywant 0:ed0152b5c495 26 * nRF51 has a TRNG that we can access using SoftDevice.
roywant 0:ed0152b5c495 27 */
roywant 0:ed0152b5c495 28 int eddystoneEntropyPoll(void *data, unsigned char *output, size_t len, size_t *olen)
roywant 0:ed0152b5c495 29 {
roywant 0:ed0152b5c495 30 uint8_t bytes_available = 0;
roywant 0:ed0152b5c495 31
roywant 0:ed0152b5c495 32 // get the number of random bytes available
roywant 0:ed0152b5c495 33 if (sd_rand_application_bytes_available_get(&bytes_available) != NRF_SUCCESS) {
roywant 0:ed0152b5c495 34 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
roywant 0:ed0152b5c495 35 }
roywant 0:ed0152b5c495 36
roywant 0:ed0152b5c495 37 // if there is more bytes available that what is requested,
roywant 0:ed0152b5c495 38 // truncate the number of bytes in output to len, otherwise use the total
roywant 0:ed0152b5c495 39 // of bytes available.
roywant 0:ed0152b5c495 40 const uint8_t output_len = bytes_available > len ? len : bytes_available;
roywant 0:ed0152b5c495 41
roywant 0:ed0152b5c495 42 if (output_len) {
roywant 0:ed0152b5c495 43 // transfer "output_len" random bytes to output.
roywant 0:ed0152b5c495 44 if (sd_rand_application_vector_get(output, output_len) != NRF_SUCCESS) {
roywant 0:ed0152b5c495 45 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
roywant 0:ed0152b5c495 46 }
roywant 0:ed0152b5c495 47 }
roywant 0:ed0152b5c495 48
roywant 0:ed0152b5c495 49 // Everything went fine, commit the output_len to the output parameter
roywant 0:ed0152b5c495 50 *olen = output_len;
roywant 0:ed0152b5c495 51 return 0;
roywant 0:ed0152b5c495 52 }
roywant 0:ed0152b5c495 53
roywant 0:ed0152b5c495 54 int eddystoneRegisterEntropySource( mbedtls_entropy_context* ctx) {
roywant 0:ed0152b5c495 55 uint8_t pool_capacity;
roywant 0:ed0152b5c495 56 sd_rand_application_pool_capacity_get(&pool_capacity);
roywant 0:ed0152b5c495 57
roywant 0:ed0152b5c495 58 return mbedtls_entropy_add_source(
roywant 0:ed0152b5c495 59 ctx,
roywant 0:ed0152b5c495 60 eddystoneEntropyPoll, // entropy source function
roywant 0:ed0152b5c495 61 NULL, // entropy source data, NULL in this case
roywant 0:ed0152b5c495 62 pool_capacity, // minimum number of bytes the entropy pool should wait on from this callback before releasing entropy
roywant 0:ed0152b5c495 63 MBEDTLS_ENTROPY_SOURCE_STRONG
roywant 0:ed0152b5c495 64 );
roywant 0:ed0152b5c495 65 }
roywant 0:ed0152b5c495 66
roywant 0:ed0152b5c495 67 #endif /* #ifdef TARGET_NRF51822 */