Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 7 | #include <arch_helpers.h> |
Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 8 | #include <cdefs.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | /* |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 12 | * This is only a toy implementation to generate a seemingly random |
| 13 | * 128-bit key from sp, x30 and cntpct_el0 values. |
| 14 | * A production system must re-implement this function to generate |
| 15 | * keys from a reliable randomness source. |
Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 16 | */ |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 17 | uint128_t plat_init_apkey(void) |
Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 18 | { |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 19 | uint64_t return_addr = (uint64_t)__builtin_return_address(0U); |
| 20 | uint64_t frame_addr = (uint64_t)__builtin_frame_address(0U); |
| 21 | uint64_t cntpct = read_cntpct_el0(); |
Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 22 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 23 | /* Generate 128-bit key */ |
| 24 | uint64_t key_lo = (return_addr << 13) ^ frame_addr ^ cntpct; |
| 25 | uint64_t key_hi = (frame_addr << 15) ^ return_addr ^ cntpct; |
Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 26 | |
Alexei Fedorov | f41355c | 2019-09-13 14:11:59 +0100 | [diff] [blame] | 27 | return ((uint128_t)(key_hi) << 64) | key_lo; |
Antonio Nino Diaz | 9c852aa | 2019-01-31 11:01:10 +0000 | [diff] [blame] | 28 | } |