David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 1 | /* |
Balint Matyi | 69e2d2e | 2020-07-08 10:53:54 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Balint Matyi | 69e2d2e | 2020-07-08 10:53:54 +0100 | [diff] [blame] | 8 | #include "bootutil/security_cnt.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 9 | #include "../../platform/include/tfm_plat_nv_counters.h" |
| 10 | #include "../../platform/include/tfm_plat_defs.h" |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 11 | #include "bootutil/fault_injection_hardening.h" |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 12 | #include <stdint.h> |
| 13 | |
| 14 | #define TFM_BOOT_NV_COUNTER_0 PLAT_NV_COUNTER_3 /* NV counter of Image 0 */ |
David Vincze | 9d96328 | 2020-06-05 09:18:35 +0200 | [diff] [blame] | 15 | #define TFM_BOOT_NV_COUNTER_1 PLAT_NV_COUNTER_4 /* NV counter of Image 1 */ |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 16 | #define TFM_BOOT_NV_COUNTER_MAX PLAT_NV_COUNTER_MAX |
| 17 | |
| 18 | static enum tfm_nv_counter_t get_nv_counter_from_image_id(uint32_t image_id) |
| 19 | { |
| 20 | uint32_t nv_counter; |
| 21 | |
| 22 | /* Avoid integer overflow */ |
| 23 | if ((UINT32_MAX - TFM_BOOT_NV_COUNTER_0) < image_id) { |
| 24 | return TFM_BOOT_NV_COUNTER_MAX; |
| 25 | } |
| 26 | |
| 27 | nv_counter = TFM_BOOT_NV_COUNTER_0 + image_id; |
| 28 | |
| 29 | /* Check the existence of the enumerated counter value */ |
| 30 | if (nv_counter >= TFM_BOOT_NV_COUNTER_MAX) { |
| 31 | return TFM_BOOT_NV_COUNTER_MAX; |
| 32 | } |
| 33 | |
| 34 | return (enum tfm_nv_counter_t)nv_counter; |
| 35 | } |
| 36 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 37 | fih_int boot_nv_security_counter_init(void) |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 38 | { |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 39 | fih_int fih_rc = FIH_FAILURE; |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 40 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 41 | fih_rc = fih_int_encode_zero_equality(tfm_plat_init_nv_counter()); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 42 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 43 | FIH_RET(fih_rc); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 44 | } |
| 45 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 46 | fih_int boot_nv_security_counter_get(uint32_t image_id, fih_int *security_cnt) |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 47 | { |
| 48 | enum tfm_nv_counter_t nv_counter; |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 49 | fih_int fih_rc = FIH_FAILURE; |
| 50 | uint32_t security_cnt_soft; |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 51 | |
| 52 | /* Check if it's a null-pointer. */ |
| 53 | if (!security_cnt) { |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 54 | FIH_RET(FIH_FAILURE); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | nv_counter = get_nv_counter_from_image_id(image_id); |
| 58 | if (nv_counter == TFM_BOOT_NV_COUNTER_MAX) { |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 59 | FIH_RET(FIH_FAILURE); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 62 | fih_rc = fih_int_encode_zero_equality( |
| 63 | tfm_plat_read_nv_counter(nv_counter, |
| 64 | sizeof(security_cnt_soft), |
| 65 | (uint8_t *)&security_cnt_soft)); |
| 66 | *security_cnt = fih_int_encode(security_cnt_soft); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 67 | |
Tamas Ban | 1bfc9da | 2020-07-09 13:55:38 +0100 | [diff] [blame^] | 68 | FIH_RET(fih_rc); |
David Vincze | 060968d | 2019-05-23 01:13:14 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int32_t boot_nv_security_counter_update(uint32_t image_id, |
| 72 | uint32_t img_security_cnt) |
| 73 | { |
| 74 | enum tfm_nv_counter_t nv_counter; |
| 75 | enum tfm_plat_err_t err; |
| 76 | |
| 77 | nv_counter = get_nv_counter_from_image_id(image_id); |
| 78 | if (nv_counter == TFM_BOOT_NV_COUNTER_MAX) { |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | err = tfm_plat_set_nv_counter(nv_counter, img_security_cnt); |
| 83 | if (err != TFM_PLAT_ERR_SUCCESS) { |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |