0.2.0-dev Download ZIP
89 lines ·2.8 KB Raw
| 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "tf_psa_crypto_common.h" |
| 9 | |
| 10 | #include <string.h> |
| 11 | |
| 12 | #if defined(MBEDTLS_ENTROPY_C) |
| 13 | |
| 14 | #include "mbedtls/platform.h" |
| 15 | #include "mbedtls/private/entropy.h" |
| 16 | #include "entropy_poll.h" |
| 17 | #include "mbedtls/private/error_common.h" |
| 18 | #include "mbedtls/private/error_common.h" |
| 19 | #include <psa/crypto_driver_random.h> |
| 20 | |
| 21 | /* In principle, we could support both a built-in source and a custom |
| 22 | * source. However, it isn't a common need. So for now the two |
| 23 | * callback functions have the same name and there can only be one. */ |
| 24 | #if defined(MBEDTLS_PSA_BUILTIN_GET_ENTROPY) || defined(MBEDTLS_PSA_DRIVER_GET_ENTROPY) |
| 25 | |
| 26 | /* We currently only support a single "true" entropy source (other than the |
| 27 | * "fake" source which is the NV seed). It can be either the built-in one |
| 28 | * or a user-provided callback. */ |
| 29 | #if defined(MBEDTLS_PSA_DRIVER_GET_ENTROPY) && defined(MBEDTLS_PSA_BUILTIN_GET_ENTROPY) |
| 30 | #error "MBEDTLS_PSA_DRIVER_GET_ENTROPY and MBEDTLS_PSA_BUILTIN_GET_ENTROPY " \ |
| 31 | "are currently incompatible." |
| 32 | #endif |
| 33 | |
| 34 | int mbedtls_entropy_poll_platform(void *data, unsigned char *output, size_t len, size_t *olen) |
| 35 | { |
| 36 | int ret; |
| 37 | size_t estimate_bits = 0; |
| 38 | (void) data; |
| 39 | |
| 40 | /* Historically, in PolarSSL and Mbed TLS, the entropy callback provided |
| 41 | * full-entropy output, and reported how many bytes in the output buffer |
| 42 | * had useful data. Reporting the length was not very useful because |
| 43 | * we get the same amount of entropy by processing the whole output bufer, |
| 44 | * processing the whole buffer barely costs more CPU time since the buffer |
| 45 | * is small, and most entropy sources just fill the whole buffer anyway. |
| 46 | * So since TF-PSA-Crypto 1.0, we process the whole buffer. |
| 47 | */ |
| 48 | *olen = len; |
| 49 | |
| 50 | ret = mbedtls_platform_get_entropy(PSA_DRIVER_GET_ENTROPY_FLAGS_NONE, |
| 51 | &estimate_bits, output, len); |
| 52 | if (ret != 0) { |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | if (estimate_bits < (8 * len)) { |
| 57 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | #endif /* MBEDTLS_PSA_BUILTIN_GET_ENTROPY || MBEDTLS_PSA_DRIVER_GET_ENTROPY */ |
| 63 | |
| 64 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 65 | int mbedtls_nv_seed_poll(void *data, |
| 66 | unsigned char *output, size_t len, size_t *olen) |
| 67 | { |
| 68 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 69 | size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
| 70 | ((void) data); |
| 71 | |
| 72 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 73 | |
| 74 | if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) { |
| 75 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 76 | } |
| 77 | |
| 78 | if (len < use_len) { |
| 79 | use_len = len; |
| 80 | } |
| 81 | |
| 82 | memcpy(output, buf, use_len); |
| 83 | *olen = use_len; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 88 | |
| 89 | #endif /* MBEDTLS_ENTROPY_C */ |