Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 2 | * Hardware entropy collector for the K64F, using Freescale's RNGA |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | /* |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 23 | * WARNING: this is temporary! |
| 24 | * This should be in a separate yotta module which would be a target |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 25 | * dependency of mbedtls (see IOTSSL-313) |
| 26 | */ |
| 27 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 28 | #if defined(TARGET_LIKE_K64F) |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 30 | /* |
| 31 | * Reference: "K64 Sub-Family Reference Manual, Rev. 2", chapter 34 |
| 32 | */ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 34 | #include "fsl_clock_manager.h" |
| 35 | |
| 36 | /* |
| 37 | * Get one byte of entropy from the RNG, assuming it is up and running. |
| 38 | * As recommended (34.1.1), get only one bit of each output. |
| 39 | */ |
| 40 | static void rng_get_byte( unsigned char *byte ) |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 41 | { |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 42 | size_t bit; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 44 | /* 34.5 Steps 3-4-5: poll SR and read from OR when ready */ |
| 45 | for( bit = 0; bit < 8; bit++ ) |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 46 | { |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 47 | while( ( RNG->SR & RNG_SR_OREG_LVL_MASK ) == 0 ); |
| 48 | *byte |= ( RNG->OR & 1 ) << bit; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 49 | } |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 52 | /* |
| 53 | * Get len bytes of entropy from the hardware RNG. |
| 54 | */ |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 55 | int mbedtls_hardware_poll( void *data, |
| 56 | unsigned char *output, size_t len, size_t *olen ) |
| 57 | { |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 58 | size_t i; |
| 59 | int ret; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 60 | ((void) data); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 62 | CLOCK_SYS_EnableRngaClock( 0 ); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 64 | /* Set "Interrupt Mask", "High Assurance" and "Go", |
| 65 | * unset "Clear interrupt" and "Sleep" */ |
| 66 | RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK; |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 67 | |
Manuel Pégourié-Gonnard | 0e4d9af | 2015-08-18 13:33:14 +0200 | [diff] [blame] | 68 | for( i = 0; i < len; i++ ) |
| 69 | rng_get_byte( output + i ); |
| 70 | |
| 71 | /* Just be extra sure that we didn't do it wrong */ |
| 72 | if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 ) |
| 73 | { |
| 74 | ret = -1; |
| 75 | goto cleanup; |
| 76 | } |
| 77 | |
| 78 | *olen = len; |
| 79 | ret = 0; |
| 80 | |
| 81 | cleanup: |
| 82 | /* Disable clock to save power - assume we're the only users of RNG */ |
| 83 | CLOCK_SYS_DisableRngaClock( 0 ); |
| 84 | |
| 85 | return( ret ); |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | #endif |