Debian Codegraph @

include/rpmc.h

raw
1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2024 Matti Finder
5 * (written by Matti Finder <[email protected]>)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #ifndef __RPMC_H__
18 #define __RPMC_H__ 1
19
20 #include <stdint.h>
21 #include "flash.h" // for flashctx
22
23 /**
24 * @defgroup flashrom-rpmc RPMC operations
25 * @{
26 */
27
28 #define RPMC_OP1_MSG_HEADER_LENGTH 4
29 #define RPMC_SIGNATURE_LENGTH 32
30 #define RPMC_COUNTER_LENGTH 4
31 #define RPMC_KEY_DATA_LENGTH 4
32 #define RPMC_TAG_LENGTH 12
33 #define RPMC_HMAC_KEY_LENGTH 32
34 #define RPMC_TRUNCATED_SIG_LENGTH 28
35
36 enum rpmc_result {
37 RPMC_SUCCESS = 0,
38 RPMC_ERROR_SPI_TRANSMISSION,
39 RPMC_ERROR_OPENSSL,
40 RPMC_ERROR_TAG_MISMATCH,
41 RPMC_ERROR_SIGNATURE_MISMATCH,
42 RPMC_ERROR_INTERNAL,
43 RPMC_ERROR_KEY_READ,
44 RPMC_ERROR_HARDENING_UNSUPPORTED,
45 RPMC_ERROR_COUNTER_OUT_OF_RANGE,
46 RPMC_ERROR_ROOT_KEY_OVERWRITE,
47 RPMC_ERROR_COUNTER_UNINITIALIZED,
48 RPMC_ERROR_COUNTER_DATA_MISMATCH,
49 RPMC_ERROR_HMAC_KEY_REGISTER_UNINITIALIZED,
50 RPMC_ERROR_WRONG_SIGNATURE
51 };
52
53 struct rpmc_status_register {
54 /*
55 * Values:
56 * 0b00000000 -> Power on state
57 * 0b10000000 -> Success
58 * 0b0xxxxxx1 -> Busy
59 * 0b0xxxxx1x -> Error: Root key register overwrite,
60 * counter Address out of range,
61 * truncated signature mismatch
62 * or monotonic counter uninitialized
63 * 0b0xxxx1xx -> Error: Signature mismatch,
64 * counter address out of range,
65 * cmdtype out of range
66 * or incorrect payload size
67 * 0b0xxx1xxx -> Error: Hmac key register uninitialized
68 * 0b0xx1xxxx -> Error: Counter data mismatch
69 * 0b0x1xxxxx -> Fatal device error
70 *
71 * Some bits might exclude others or their meaning might be dependent
72 * on previous commands.
73 * Read JESD260 or your device's data sheet for more details.
74 */
75 uint8_t status;
76 unsigned char tag[RPMC_TAG_LENGTH];
77 uint32_t counter_data;
78 unsigned char signature[RPMC_SIGNATURE_LENGTH];
79 };
80
81 /**
82 * @brief Write root key on flashchip
83 *
84 * @param[in] flash Flash context which rpmc options will be used
85 * @param[in] keyfile Location of 32-byte key to use
86 * @param[in] counter_address Address of counter (starts at 0)
87 *
88 * @return The result of the operation
89 */
90 enum rpmc_result rpmc_write_root_key(struct flashrom_flashctx *flash,
91 const char *keyfile,
92 unsigned int counter_address);
93
94 /**
95 * @brief Update hmac key register
96 *
97 * @param[in] flash Flash context which rpmc options will be used
98 * @param[in] keyfile Location of 32-byte key to use
99 * @param[in] key_data 4-bytes of data to use as key data
100 * @param[in] counter_address Address of counter (starts at 0)
101 *
102 * @return The result of the operation
103 */
104 enum rpmc_result rpmc_update_hmac_key(struct flashrom_flashctx *flash,
105 const char *keyfile,
106 uint32_t key_data,
107 unsigned int counter_address);
108
109 /**
110 * @brief Increment monotonic counter value
111 *
112 * @param[in] flash Flash context which rpmc options will be used
113 * @param[in] keyfile Location of 32-byte key to use
114 * @param[in] key_data 4-bytes of data to use as key data
115 * @param[in] counter_address Address of counter (starts at 0)
116 * @param[in] previous_value Previous value of counter
117 *
118 * @return The result of the operation
119 */
120 enum rpmc_result rpmc_increment_counter(struct flashrom_flashctx *flash,
121 const char *keyfile,
122 uint32_t key_data,
123 unsigned int counter_address,
124 uint32_t previous_value);
125
126 /**
127 * @brief Get monotonic counter value
128 *
129 * @param[in] flash Flash context which rpmc options will be used
130 * @param[in] keyfile Location of 32-byte key to use
131 * @param[in] key_data 4-bytes of data to use as key data
132 * @param[in] counter_address Address of counter (starts at 0)
133 * @param[out] counter_value Pointer to write the counter value to
134 *
135 * @return The result of the operation
136 */
137 enum rpmc_result rpmc_get_monotonic_counter(struct flashrom_flashctx *flash,
138 const char *keyfile,
139 uint32_t key_data,
140 unsigned int counter_address,
141 uint32_t *counter_value);
142
143 /**
144 * @brief Read the full JESD260 extended status register
145 *
146 * @param[in] flash Flash context which rpmc options will be used
147 * @param[out] status Status register to write data into
148 *
149 * @return The result of the operation
150 */
151 enum rpmc_result rpmc_read_data(struct flashrom_flashctx *flash,
152 struct rpmc_status_register *status);
153
154 /**
155 * @brief Get a string description for rpmc result
156 *
157 * @param[in] value A rpmc result
158 *
159 * @return String description
160 */
161 const char * rpmc_describe_result(enum rpmc_result value);
162
163 /** @} */ /* end flashrom-rpmc */
164
165 #endif /* !__RPMC_H__ */