Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
nRFEntropySource.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "../EntropySource.h" 00017 00018 #if defined(TARGET_NRF51822) || defined(TARGET_MCU_NRF52832) /* Persistent storage supported on nrf51 platforms */ 00019 00020 #include "nrf_soc.h" 00021 #include "nrf_error.h" 00022 #include "mbed.h" 00023 #include <mbedtls/entropy.h> 00024 00025 /* 00026 * nRF51 has a TRNG that we can access using SoftDevice. 00027 */ 00028 int eddystoneEntropyPoll(void *data, unsigned char *output, size_t len, size_t *olen) 00029 { 00030 uint8_t bytes_available = 0; 00031 00032 // get the number of random bytes available 00033 if (sd_rand_application_bytes_available_get(&bytes_available) != NRF_SUCCESS) { 00034 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; 00035 } 00036 00037 // if there is more bytes available that what is requested, 00038 // truncate the number of bytes in output to len, otherwise use the total 00039 // of bytes available. 00040 const uint8_t output_len = bytes_available > len ? len : bytes_available; 00041 00042 if (output_len) { 00043 // transfer "output_len" random bytes to output. 00044 if (sd_rand_application_vector_get(output, output_len) != NRF_SUCCESS) { 00045 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; 00046 } 00047 } 00048 00049 // Everything went fine, commit the output_len to the output parameter 00050 *olen = output_len; 00051 return 0; 00052 } 00053 00054 int eddystoneRegisterEntropySource( mbedtls_entropy_context* ctx) { 00055 uint8_t pool_capacity; 00056 sd_rand_application_pool_capacity_get(&pool_capacity); 00057 00058 return mbedtls_entropy_add_source( 00059 ctx, 00060 eddystoneEntropyPoll, // entropy source function 00061 NULL, // entropy source data, NULL in this case 00062 pool_capacity, // minimum number of bytes the entropy pool should wait on from this callback before releasing entropy 00063 MBEDTLS_ENTROPY_SOURCE_STRONG 00064 ); 00065 } 00066 00067 #endif /* #ifdef TARGET_NRF51822 */
Generated on Thu Jul 14 2022 09:28:18 by
1.7.2