dp-arm | df9a39e | 2017-02-27 12:21:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <assert.h> |
| 32 | #include <mmio.h> |
| 33 | #include <string.h> |
Scott Branden | 53d9c9c | 2017-04-10 11:45:52 -0700 | [diff] [blame] | 34 | #include <utils_def.h> |
dp-arm | df9a39e | 2017-02-27 12:21:43 +0000 | [diff] [blame] | 35 | #include "juno_def.h" |
| 36 | |
| 37 | #define NSAMPLE_CLOCKS 1 /* min 1 cycle, max 231 cycles */ |
| 38 | #define NRETRIES 5 |
| 39 | |
| 40 | static inline int output_valid(void) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < NRETRIES; i++) { |
| 45 | uint32_t val; |
| 46 | |
| 47 | val = mmio_read_32(TRNG_BASE + TRNG_STATUS); |
| 48 | if (val & 1U) |
| 49 | break; |
| 50 | } |
| 51 | if (i >= NRETRIES) |
| 52 | return 0; /* No output data available. */ |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * This function fills `buf` with `len` bytes of entropy. |
| 58 | * It uses the Trusted Entropy Source peripheral on Juno. |
| 59 | * Returns 0 when the buffer has been filled with entropy |
| 60 | * successfully and -1 otherwise. |
| 61 | */ |
| 62 | int juno_getentropy(void *buf, size_t len) |
| 63 | { |
| 64 | uint8_t *bp = buf; |
| 65 | |
| 66 | assert(buf); |
| 67 | assert(len); |
| 68 | assert(!check_uptr_overflow((uintptr_t)bp, len)); |
| 69 | |
| 70 | /* Disable interrupt mode. */ |
| 71 | mmio_write_32(TRNG_BASE + TRNG_INTMASK, 0); |
| 72 | /* Program TRNG to sample for `NSAMPLE_CLOCKS`. */ |
| 73 | mmio_write_32(TRNG_BASE + TRNG_CONFIG, NSAMPLE_CLOCKS); |
| 74 | |
| 75 | while (len > 0) { |
| 76 | int i; |
| 77 | |
| 78 | /* Start TRNG. */ |
| 79 | mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1); |
| 80 | |
| 81 | /* Check if output is valid. */ |
| 82 | if (!output_valid()) |
| 83 | return -1; |
| 84 | |
| 85 | /* Fill entropy buffer. */ |
| 86 | for (i = 0; i < TRNG_NOUTPUTS; i++) { |
| 87 | size_t n; |
| 88 | uint32_t val; |
| 89 | |
| 90 | val = mmio_read_32(TRNG_BASE + i * sizeof(uint32_t)); |
| 91 | n = MIN(len, sizeof(uint32_t)); |
| 92 | memcpy(bp, &val, n); |
| 93 | bp += n; |
| 94 | len -= n; |
| 95 | if (len == 0) |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | /* Reset TRNG outputs. */ |
| 100 | mmio_write_32(TRNG_BASE + TRNG_STATUS, 1); |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |