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