/src/wolfBoot/include/wolfboot/wolfboot.h
Line | Count | Source |
1 | | /* wolfboot.h |
2 | | * |
3 | | * The wolfBoot API definitions. |
4 | | * |
5 | | * Copyright (C) 2026 wolfSSL Inc. |
6 | | * |
7 | | * This file is part of wolfBoot. |
8 | | * |
9 | | * wolfBoot is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU General Public License as published by |
11 | | * the Free Software Foundation; either version 3 of the License, or |
12 | | * (at your option) any later version. |
13 | | * |
14 | | * wolfBoot is distributed in the hope that it will be useful, |
15 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | | * GNU General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU General Public License |
20 | | * along with this program; if not, write to the Free Software |
21 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
22 | | */ |
23 | | |
24 | | |
25 | | #ifndef WOLFBOOT_H |
26 | | #define WOLFBOOT_H |
27 | | |
28 | | #ifdef __cplusplus |
29 | | extern "C" { |
30 | | #endif |
31 | | |
32 | | #include <stdint.h> |
33 | | #ifdef __WOLFBOOT |
34 | | /* Either hand-craft a device target.h file in [WOLFBOOT_ROOT]/include |
35 | | * or let build process auto-create one from .config file or cmake presets. |
36 | | * |
37 | | * See template: [WOLFBOOT_ROOT]/include/target.h.in |
38 | | * or unit test: [WOLFBOOT_ROOT]/tools/unit-tests/target.h |
39 | | */ |
40 | | #include "target.h" |
41 | | #endif |
42 | | #include "wolfboot/version.h" |
43 | | #include "wolfboot/wc_secure.h" |
44 | | |
45 | | |
46 | | #ifndef RAMFUNCTION |
47 | | # if defined(__WOLFBOOT) && defined(RAM_CODE) |
48 | | # if defined(ARCH_ARM) |
49 | | # if defined(__has_attribute) |
50 | | # if __has_attribute(long_call) |
51 | | # define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) |
52 | | # else |
53 | | # define RAMFUNCTION __attribute__((used,section(".ramcode"))) |
54 | | # endif |
55 | | # elif defined(__GNUC__) |
56 | | # define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call)) |
57 | | # else |
58 | | # define RAMFUNCTION __attribute__((used,section(".ramcode"))) |
59 | | # endif |
60 | | # elif defined(ARCH_PPC) |
61 | | # define RAMFUNCTION __attribute__((used,section(".ramcode"),longcall)) |
62 | | # else |
63 | | # define RAMFUNCTION __attribute__((used,section(".ramcode"))) |
64 | | # endif |
65 | | # else |
66 | | # define RAMFUNCTION |
67 | | #endif |
68 | | #endif |
69 | | |
70 | | #ifndef WEAKFUNCTION |
71 | | # if defined(__GNUC__) || defined(__CC_ARM) |
72 | | # define WEAKFUNCTION __attribute__((weak)) |
73 | | # else |
74 | | # define WEAKFUNCTION |
75 | | # endif |
76 | | #endif |
77 | | |
78 | | #ifndef UNUSEDFUNCTION |
79 | | # if defined(__GNUC__) || defined(__CC_ARM) |
80 | | # define UNUSEDFUNCTION __attribute__((unused)) |
81 | | # else |
82 | | # define UNUSEDFUNCTION |
83 | | # endif |
84 | | #endif |
85 | | |
86 | | #ifndef NOINLINEFUNCTION |
87 | | # if defined(__has_attribute) |
88 | | # if __has_attribute(noinline) |
89 | | # define NOINLINEFUNCTION __attribute__((noinline)) |
90 | | # else |
91 | | # define NOINLINEFUNCTION |
92 | | # endif |
93 | | # elif defined(__GNUC__) || defined(__CC_ARM) |
94 | | # define NOINLINEFUNCTION __attribute__((noinline)) |
95 | | # else |
96 | | # define NOINLINEFUNCTION |
97 | | # endif |
98 | | #endif |
99 | | |
100 | | |
101 | | /* Helpers for memory alignment */ |
102 | | #ifndef XALIGNED |
103 | | #if defined(__GNUC__) || defined(__llvm__) || \ |
104 | | defined(__IAR_SYSTEMS_ICC__) |
105 | | #define XALIGNED(x) __attribute__ ( (aligned (x))) |
106 | | #elif defined(__KEIL__) |
107 | | #define XALIGNED(x) __align(x) |
108 | | #elif defined(_MSC_VER) |
109 | | /* disable align warning, we want alignment ! */ |
110 | | #pragma warning(disable: 4324) |
111 | | #define XALIGNED(x) __declspec (align (x)) |
112 | | #else |
113 | | #define XALIGNED(x) /* null expansion */ |
114 | | #endif |
115 | | #endif |
116 | | |
117 | | #ifndef XALIGNED_STACK |
118 | | /* Don't enforce stack alignment on IAR */ |
119 | | #if defined(__IAR_SYSTEMS_ICC__) |
120 | | #define XALIGNED_STACK(x) |
121 | | #else |
122 | | #define XALIGNED_STACK(x) XALIGNED(x) |
123 | | #endif |
124 | | #endif |
125 | | |
126 | | |
127 | | #ifndef IMAGE_HEADER_SIZE |
128 | | /* Largest cases first */ |
129 | | # if defined(WOLFBOOT_SIGN_RSA4096) || defined(WOLFBOOT_SIGN_RSAPSS4096) |
130 | | # define IMAGE_HEADER_SIZE 1024 |
131 | | |
132 | | /* RSA3072/RSAPSS3072 + strong hash */ |
133 | | # elif ((defined(WOLFBOOT_SIGN_RSA3072) || defined(WOLFBOOT_SIGN_RSAPSS3072)) && \ |
134 | | (defined(WOLFBOOT_HASH_SHA384) || defined(WOLFBOOT_HASH_SHA3_384))) |
135 | | # define IMAGE_HEADER_SIZE 1024 |
136 | | |
137 | | /* RSA2048/RSAPSS2048 + SHA256 */ |
138 | | # elif (defined(WOLFBOOT_SIGN_RSA2048) || defined(WOLFBOOT_SIGN_RSAPSS2048)) && defined(WOLFBOOT_HASH_SHA256) |
139 | | # define IMAGE_HEADER_SIZE 512 |
140 | | |
141 | | /* ECC384 requires 512 with SHA256 */ |
142 | | # elif defined(WOLFBOOT_SIGN_ECC384) && defined(WOLFBOOT_HASH_SHA256) |
143 | | # define IMAGE_HEADER_SIZE 512 |
144 | | |
145 | | /* ED25519 + any 384-bit or SHA3 hash */ |
146 | | # elif defined(WOLFBOOT_SIGN_ED25519) && \ |
147 | | (defined(WOLFBOOT_HASH_SHA384) || \ |
148 | | defined(WOLFBOOT_HASH_SHA3) || \ |
149 | | defined(WOLFBOOT_HASH_SHA3_384)) |
150 | | # define IMAGE_HEADER_SIZE 256 |
151 | | |
152 | | /* ECC256 + any 384-bit hash */ |
153 | | # elif defined(WOLFBOOT_SIGN_ECC256) && \ |
154 | | (defined(WOLFBOOT_HASH_SHA384) || defined(WOLFBOOT_HASH_SHA3_384)) |
155 | | # define IMAGE_HEADER_SIZE 256 |
156 | | |
157 | | /* Secondary 512-byte fallbacks */ |
158 | | # elif defined(WOLFBOOT_SIGN_RSA3072) || defined(WOLFBOOT_SIGN_RSAPSS3072) || \ |
159 | | defined(WOLFBOOT_SIGN_ECC521) || \ |
160 | | defined(WOLFBOOT_SIGN_ED448) || \ |
161 | | defined(WOLFBOOT_HASH_SHA384) || \ |
162 | | defined(WOLFBOOT_HASH_SHA3_384) |
163 | | # define IMAGE_HEADER_SIZE 512 |
164 | | |
165 | | /* Default header size */ |
166 | | # else |
167 | | # define IMAGE_HEADER_SIZE 256 |
168 | | # endif |
169 | | |
170 | | #endif /* IMAGE_HEADER_SIZE */ |
171 | | #define IMAGE_HEADER_OFFSET (2 * sizeof(uint32_t)) |
172 | | |
173 | | #ifndef FLASHBUFFER_SIZE |
174 | | # ifdef NVM_FLASH_WRITEONCE |
175 | | # define FLASHBUFFER_SIZE WOLFBOOT_SECTOR_SIZE |
176 | | # else |
177 | | # define FLASHBUFFER_SIZE IMAGE_HEADER_SIZE |
178 | | # endif |
179 | | #endif |
180 | | |
181 | | #ifdef WOLFBOOT_SELF_HEADER |
182 | | #ifndef WOLFBOOT_SELF_HEADER_SIZE |
183 | | #define WOLFBOOT_SELF_HEADER_SIZE IMAGE_HEADER_SIZE |
184 | | #endif |
185 | | #if (WOLFBOOT_SELF_HEADER_SIZE < IMAGE_HEADER_SIZE) |
186 | | #error "WOLFBOOT_SELF_HEADER_SIZE must be at least IMAGE_HEADER_SIZE" |
187 | | #endif |
188 | | #ifdef __WOLFBOOT |
189 | | #if !defined(WOLFBOOT_PART_USE_ARCH_OFFSET) |
190 | | #if (WOLFBOOT_PARTITION_SELF_HEADER_ADDRESS % WOLFBOOT_SECTOR_SIZE) |
191 | | #error "WOLFBOOT_PARTITION_SELF_HEADER_ADDRESS must be sector aligned" |
192 | | #endif |
193 | | #endif |
194 | | #endif |
195 | | #ifdef WOLFBOOT_SELF_HEADER_EXT |
196 | | #ifndef EXT_FLASH |
197 | | #error "WOLFBOOT_SELF_HEADER_EXT requires EXT_FLASH" |
198 | | #endif |
199 | | #endif |
200 | | #endif /* WOLFBOOT_SELF_HEADER */ |
201 | | |
202 | | #if defined(WOLFBOOT_SKIP_BOOT_VERIFY) && !defined(WOLFBOOT_SELF_HEADER) |
203 | | #error "WOLFBOOT_SKIP_BOOT_VERIFY requires WOLFBOOT_SELF_HEADER" |
204 | | #endif |
205 | | |
206 | | #if defined(WOLFBOOT_SKIP_BOOT_VERIFY) && \ |
207 | | !defined(WOLFBOOT_SELF_UPDATE_MONOLITHIC) |
208 | | #error "WOLFBOOT_SKIP_BOOT_VERIFY requires WOLFBOOT_SELF_UPDATE_MONOLITHIC" |
209 | | #endif |
210 | | |
211 | | #ifdef BIG_ENDIAN_ORDER |
212 | | # define WOLFBOOT_MAGIC 0x574F4C46 /* WOLF */ |
213 | | # define WOLFBOOT_MAGIC_TRAIL 0x424F4F54 /* BOOT */ |
214 | | #else |
215 | | # define WOLFBOOT_MAGIC 0x464C4F57 /* WOLF */ |
216 | | # define WOLFBOOT_MAGIC_TRAIL 0x544F4F42 /* BOOT */ |
217 | | #endif |
218 | | |
219 | | #define HDR_END 0x00 |
220 | | #define HDR_VERSION 0x01 |
221 | | #define HDR_TIMESTAMP 0x02 |
222 | | #define HDR_SHA256 0x03 |
223 | | #define HDR_IMG_TYPE 0x04 |
224 | | #define HDR_IMG_DELTA_BASE 0x05 |
225 | | #define HDR_IMG_DELTA_SIZE 0x06 |
226 | | #define HDR_IMG_DELTA_BASE_HASH 0x07 |
227 | | #define HDR_PUBKEY 0x10 |
228 | | #define HDR_SECONDARY_CIPHER 0x11 |
229 | | #define HDR_SECONDARY_PUBKEY 0x12 |
230 | | #define HDR_SHA3_384 0x13 |
231 | | #define HDR_SHA384 0x14 |
232 | | #define HDR_IMG_DELTA_INVERSE 0x15 |
233 | | #define HDR_IMG_DELTA_INVERSE_SIZE 0x16 |
234 | | #define HDR_SIGNATURE 0x20 |
235 | | #define HDR_POLICY_SIGNATURE 0x21 |
236 | | #define HDR_SECONDARY_SIGNATURE 0x22 |
237 | | #define HDR_CERT_CHAIN 0x23 |
238 | | #define HDR_PADDING 0xFF |
239 | | |
240 | | /* Auth Key types */ |
241 | | #define AUTH_KEY_NONE 0x00 |
242 | | #define AUTH_KEY_ED25519 0x01 |
243 | | #define AUTH_KEY_ECC256 0x02 |
244 | | #define AUTH_KEY_RSA2048 0x03 |
245 | | #define AUTH_KEY_RSA4096 0x04 |
246 | | #define AUTH_KEY_ED448 0x05 |
247 | | #define AUTH_KEY_ECC384 0x06 |
248 | | #define AUTH_KEY_ECC521 0x07 |
249 | | #define AUTH_KEY_RSA3072 0x08 |
250 | | #define AUTH_KEY_LMS 0x09 |
251 | | #define AUTH_KEY_XMSS 0x0A |
252 | | #define AUTH_KEY_ML_DSA 0x0B |
253 | | #define AUTH_KEY_RSAPSS2048 0x0C |
254 | | #define AUTH_KEY_RSAPSS3072 0x0D |
255 | | #define AUTH_KEY_RSAPSS4096 0x0E |
256 | | #define AUTH_KEY_NUM 0x0F |
257 | | |
258 | | /* |
259 | | * 8 bits: auth type |
260 | | * 4 bits: extra features |
261 | | * 4 bits: partition id (0 = bootloader) |
262 | | * |
263 | | */ |
264 | | #define HDR_IMG_TYPE_AUTH_MASK 0xFF00 |
265 | | #define HDR_IMG_TYPE_AUTH_NONE 0xFF00 |
266 | | #define HDR_IMG_TYPE_AUTH_ED25519 (AUTH_KEY_ED25519 << 8) |
267 | | #define HDR_IMG_TYPE_AUTH_ECC256 (AUTH_KEY_ECC256 << 8) |
268 | | #define HDR_IMG_TYPE_AUTH_RSA2048 (AUTH_KEY_RSA2048 << 8) |
269 | | #define HDR_IMG_TYPE_AUTH_RSA4096 (AUTH_KEY_RSA4096 << 8) |
270 | | #define HDR_IMG_TYPE_AUTH_ED448 (AUTH_KEY_ED448 << 8) |
271 | | #define HDR_IMG_TYPE_AUTH_ECC384 (AUTH_KEY_ECC384 << 8) |
272 | | #define HDR_IMG_TYPE_AUTH_ECC521 (AUTH_KEY_ECC521 << 8) |
273 | | #define HDR_IMG_TYPE_AUTH_RSA3072 (AUTH_KEY_RSA3072 << 8) |
274 | | #define HDR_IMG_TYPE_AUTH_LMS (AUTH_KEY_LMS << 8) |
275 | | #define HDR_IMG_TYPE_AUTH_XMSS (AUTH_KEY_XMSS << 8) |
276 | | #define HDR_IMG_TYPE_AUTH_ML_DSA (AUTH_KEY_ML_DSA << 8) |
277 | | #define HDR_IMG_TYPE_AUTH_RSAPSS2048 (AUTH_KEY_RSAPSS2048 << 8) |
278 | | #define HDR_IMG_TYPE_AUTH_RSAPSS3072 (AUTH_KEY_RSAPSS3072 << 8) |
279 | | #define HDR_IMG_TYPE_AUTH_RSAPSS4096 (AUTH_KEY_RSAPSS4096 << 8) |
280 | | |
281 | | #define HDR_IMG_TYPE_DIFF 0x00D0 |
282 | | |
283 | | #define HDR_IMG_TYPE_PART_MASK 0x000F |
284 | | #define HDR_IMG_TYPE_WOLFBOOT 0x0000 |
285 | | #ifndef HDR_IMG_TYPE_APP |
286 | | #define HDR_IMG_TYPE_APP 0x0001 |
287 | | #endif |
288 | | |
289 | | #define KEYSTORE_PUBKEY_SIZE_NONE 0 |
290 | | #define KEYSTORE_PUBKEY_SIZE_ED25519 32 |
291 | | #define KEYSTORE_PUBKEY_SIZE_ED448 57 |
292 | | #define KEYSTORE_PUBKEY_SIZE_ECC256 64 |
293 | | #define KEYSTORE_PUBKEY_SIZE_ECC384 96 |
294 | | #define KEYSTORE_PUBKEY_SIZE_ECC521 132 |
295 | | #define KEYSTORE_PUBKEY_SIZE_RSA2048 320 |
296 | | #define KEYSTORE_PUBKEY_SIZE_RSA3072 448 |
297 | | #define KEYSTORE_PUBKEY_SIZE_RSA4096 576 |
298 | | #define KEYSTORE_PUBKEY_SIZE_RSAPSS2048 KEYSTORE_PUBKEY_SIZE_RSA2048 |
299 | | #define KEYSTORE_PUBKEY_SIZE_RSAPSS3072 KEYSTORE_PUBKEY_SIZE_RSA3072 |
300 | | #define KEYSTORE_PUBKEY_SIZE_RSAPSS4096 KEYSTORE_PUBKEY_SIZE_RSA4096 |
301 | | #define KEYSTORE_PUBKEY_SIZE_LMS 60 |
302 | | #define KEYSTORE_PUBKEY_SIZE_XMSS 68 |
303 | | |
304 | | /* ML-DSA pub key size is a function of parameters. */ |
305 | | #define ML_DSA_L2_PUBKEY_SIZE 1312 |
306 | | #define ML_DSA_L3_PUBKEY_SIZE 1952 |
307 | | #define ML_DSA_L5_PUBKEY_SIZE 2592 |
308 | | |
309 | | /* Configure using ML_DSA_LEVEL: Default is security category 2. */ |
310 | | #ifndef ML_DSA_LEVEL |
311 | | #define ML_DSA_LEVEL 2 |
312 | | #endif |
313 | | |
314 | | #if ML_DSA_LEVEL == 2 |
315 | | #define KEYSTORE_PUBKEY_SIZE_ML_DSA ML_DSA_L2_PUBKEY_SIZE |
316 | | #elif ML_DSA_LEVEL == 3 |
317 | | #define KEYSTORE_PUBKEY_SIZE_ML_DSA ML_DSA_L3_PUBKEY_SIZE |
318 | | #elif ML_DSA_LEVEL == 5 |
319 | | #define KEYSTORE_PUBKEY_SIZE_ML_DSA ML_DSA_L5_PUBKEY_SIZE |
320 | | #endif |
321 | | |
322 | | /* Mask for key permissions */ |
323 | | #define KEY_VERIFY_ALL (0xFFFFFFFFU) |
324 | | #define KEY_VERIFY_ONLY_ID(X) (1U << X) |
325 | | #define KEY_VERIFY_SELF_ONLY KEY_VERIFY_ONLY_ID(0) |
326 | | #define KEY_VERIFY_APP_ONLY KEY_VERIFY_ONLY_ID(1) |
327 | | |
328 | | /* HAL crypto device ID abstraction. |
329 | | * Override per-class via build system -D flags. |
330 | | * Defaults to INVALID_DEVID (software-only). */ |
331 | | #ifndef WOLFBOOT_DEVID_HASH |
332 | | #define WOLFBOOT_DEVID_HASH (-2) /* INVALID_DEVID */ |
333 | | #endif |
334 | | #ifndef WOLFBOOT_DEVID_PUBKEY |
335 | | #define WOLFBOOT_DEVID_PUBKEY (-2) /* INVALID_DEVID */ |
336 | | #endif |
337 | | #ifndef WOLFBOOT_DEVID_CRYPT |
338 | | #define WOLFBOOT_DEVID_CRYPT (-2) /* INVALID_DEVID */ |
339 | | #endif |
340 | | |
341 | | #if defined(__WOLFBOOT) || defined(UNIT_TEST_AUTH) |
342 | | |
343 | | #include "wolfssl/wolfcrypt/settings.h" |
344 | | #include "wolfssl/wolfcrypt/visibility.h" |
345 | | #include "wolfssl/wolfcrypt/wc_port.h" |
346 | | #include "wolfssl/wolfcrypt/types.h" |
347 | | |
348 | | #ifdef WOLFBOOT_RENESAS_TSIP |
349 | | /* Include these before any algorithm headers */ |
350 | | #include "mcu/all/r_bsp_common.h" |
351 | | #include "r_bsp_config.h" |
352 | | #include "r_tsip_rx_if.h" |
353 | | #include "wolfssl/wolfcrypt/port/Renesas/renesas_tsip_types.h" |
354 | | #endif |
355 | | |
356 | | |
357 | | /* Hashing configuration */ |
358 | | #if defined(WOLFBOOT_HASH_SHA256) |
359 | | # ifdef WOLFBOOT_HASH_SHA384 |
360 | | # error "Found WOLFBOOT_HASH_SHA384 with WOLFBOOT_HASH_SHA256. Pick one" |
361 | | # endif |
362 | | # ifdef WOLFBOOT_HASH_SHA3_384 |
363 | | # error "Found WOLFBOOT_HASH_SHA3_384 with WOLFBOOT_HASH_SHA256. Pick one" |
364 | | # endif |
365 | | |
366 | | #include "wolfssl/wolfcrypt/sha256.h" |
367 | | # ifndef WOLFBOOT_SHA_BLOCK_SIZE |
368 | | # define WOLFBOOT_SHA_BLOCK_SIZE (256) |
369 | | # endif |
370 | | # define WOLFBOOT_SHA_HDR HDR_SHA256 |
371 | | # define WOLFBOOT_SHA_DIGEST_SIZE (32) |
372 | | # define image_hash image_sha256 |
373 | | # define header_hash header_sha256 |
374 | | # define update_hash wc_Sha256Update |
375 | | # define key_hash key_sha256 |
376 | | # define self_hash self_sha256 |
377 | | # define final_hash wc_Sha256Final |
378 | | typedef wc_Sha256 wolfBoot_hash_t; |
379 | | # define HDR_HASH HDR_SHA256 |
380 | | #elif defined(WOLFBOOT_HASH_SHA384) |
381 | | # ifdef WOLFBOOT_HASH_SHA256 |
382 | | # error "Found WOLFBOOT_HASH_SHA256 with WOLFBOOT_HASH_SHA384. Pick one" |
383 | | # endif |
384 | | # ifdef WOLFBOOT_HASH_SHA3_384 |
385 | | # error "Found WOLFBOOT_HASH_SHA3_384 with WOLFBOOT_HASH_SHA384. Pick one" |
386 | | # endif |
387 | | |
388 | | #include "wolfssl/wolfcrypt/sha512.h" |
389 | | # ifndef WOLFBOOT_SHA_BLOCK_SIZE |
390 | | # define WOLFBOOT_SHA_BLOCK_SIZE (256) |
391 | | # endif |
392 | | # define WOLFBOOT_SHA_HDR HDR_SHA384 |
393 | | # define WOLFBOOT_SHA_DIGEST_SIZE (48) |
394 | | # define image_hash image_sha384 |
395 | | # define header_hash header_sha384 |
396 | | # define update_hash wc_Sha384Update |
397 | | # define key_hash key_sha384 |
398 | | # define self_hash self_sha384 |
399 | | # define final_hash wc_Sha384Final |
400 | | typedef wc_Sha384 wolfBoot_hash_t; |
401 | | # define HDR_HASH HDR_SHA384 |
402 | | #elif defined(WOLFBOOT_HASH_SHA3_384) |
403 | | # ifdef WOLFBOOT_HASH_SHA256 |
404 | | # error "Found WOLFBOOT_HASH_SHA256 with WOLFBOOT_HASH_SHA3_384. Pick one" |
405 | | # endif |
406 | | # ifdef WOLFBOOT_HASH_SHA384 |
407 | | # error "Found WOLFBOOT_HASH_SHA384 with WOLFBOOT_HASH_SHA3_384. Pick one" |
408 | | # endif |
409 | | |
410 | | #include "wolfssl/wolfcrypt/sha3.h" |
411 | | # ifndef WOLFBOOT_SHA_BLOCK_SIZE |
412 | | # define WOLFBOOT_SHA_BLOCK_SIZE (256) |
413 | | # endif |
414 | | # define WOLFBOOT_SHA_HDR HDR_SHA3_384 |
415 | | # define WOLFBOOT_SHA_DIGEST_SIZE (48) |
416 | | # define image_hash image_sha3_384 |
417 | | # define header_hash header_sha3_384 |
418 | | # define update_hash wc_Sha3Update |
419 | | # define final_hash wc_Sha3Final |
420 | | # define key_hash key_sha3_384 |
421 | | typedef wc_Sha3 wolfBoot_hash_t; |
422 | | # define HDR_HASH HDR_SHA3_384 |
423 | | #else |
424 | | # error "No valid hash algorithm defined!" |
425 | | #endif |
426 | | |
427 | | #ifdef WOLFBOOT_TPM |
428 | | #if defined(WOLFBOOT_HASH_SHA256) |
429 | | #define WOLFBOOT_TPM_HASH_ALG TPM_ALG_SHA256 |
430 | | #elif defined(WOLFBOOT_HASH_SHA384) |
431 | | #define WOLFBOOT_TPM_HASH_ALG TPM_ALG_SHA384 |
432 | | #else |
433 | | #error TPM does not support hash algorithm selection |
434 | | #endif |
435 | | #endif |
436 | | |
437 | | #endif |
438 | | |
439 | | #if defined(__WOLFBOOT) || defined (__FLASH_OTP_PRIMER) || \ |
440 | | defined (UNIT_TEST_AUTH) || defined(WOLFBOOT_TPM) |
441 | | |
442 | | /* Authentication configuration */ |
443 | | #if defined(WOLFBOOT_NO_SIGN) |
444 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_NONE |
445 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
446 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_NONE |
447 | | # endif |
448 | | #elif defined(WOLFBOOT_SIGN_ED25519) |
449 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_ED25519 |
450 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
451 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ED25519 |
452 | | # endif |
453 | | #elif defined(WOLFBOOT_SIGN_ED448) |
454 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_ED448 |
455 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
456 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ED448 |
457 | | # endif |
458 | | #elif defined(WOLFBOOT_SIGN_ECC256) |
459 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_ECC256 |
460 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
461 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ECC256 |
462 | | # endif |
463 | | #elif defined(WOLFBOOT_SIGN_ECC384) |
464 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_ECC384 |
465 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
466 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ECC384 |
467 | | # endif |
468 | | #elif defined(WOLFBOOT_SIGN_ECC521) |
469 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_ECC521 |
470 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
471 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ECC521 |
472 | | # endif |
473 | | #elif defined(WOLFBOOT_SIGN_RSA2048) || defined(WOLFBOOT_SIGN_RSA2048ENC) |
474 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_RSA2048 |
475 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
476 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA2048 |
477 | | # endif |
478 | | #elif defined(WOLFBOOT_SIGN_RSA3072) || defined(WOLFBOOT_SIGN_RSA3072ENC) |
479 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_RSA3072 |
480 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
481 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA3072 |
482 | | # endif |
483 | | #elif defined(WOLFBOOT_SIGN_RSA4096) || defined(WOLFBOOT_SIGN_RSA4096ENC) |
484 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_RSA4096 |
485 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
486 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA4096 |
487 | | # endif |
488 | | #elif defined(WOLFBOOT_SIGN_RSAPSS2048) |
489 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_RSAPSS2048 |
490 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
491 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA2048 |
492 | | # endif |
493 | | #elif defined(WOLFBOOT_SIGN_RSAPSS3072) |
494 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_RSAPSS3072 |
495 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
496 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA3072 |
497 | | # endif |
498 | | #elif defined(WOLFBOOT_SIGN_RSAPSS4096) |
499 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_RSAPSS4096 |
500 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
501 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_RSA4096 |
502 | | # endif |
503 | | #elif defined(WOLFBOOT_SIGN_LMS) |
504 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_LMS |
505 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
506 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_LMS |
507 | | # endif |
508 | | #elif defined(WOLFBOOT_SIGN_XMSS) |
509 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_XMSS |
510 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
511 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_XMSS |
512 | | # endif |
513 | | #elif defined(WOLFBOOT_SIGN_ML_DSA) |
514 | | # define HDR_IMG_TYPE_AUTH HDR_IMG_TYPE_AUTH_ML_DSA |
515 | | # ifndef WOLFBOOT_UNIVERSAL_KEYSTORE |
516 | | # define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ML_DSA |
517 | | # endif |
518 | | #else |
519 | | # error "No valid authentication mechanism selected. " \ |
520 | | "Please select a valid SIGN= option." |
521 | | #endif /* authentication options */ |
522 | | |
523 | | #include "keystore.h" |
524 | | |
525 | | #endif /* defined WOLFBOOT */ |
526 | | |
527 | | /* Monolithic self-update: imply DISABLE_BACKUP and enforce prerequisites */ |
528 | | #ifdef WOLFBOOT_SELF_UPDATE_MONOLITHIC |
529 | | #ifndef DISABLE_BACKUP |
530 | | #define DISABLE_BACKUP |
531 | | #endif |
532 | | #ifdef DELTA_UPDATES |
533 | | #error "DELTA_UPDATES is not compatible with WOLFBOOT_SELF_UPDATE_MONOLITHIC" |
534 | | #endif |
535 | | #ifdef NVM_FLASH_WRITEONCE |
536 | | #error "NVM_FLASH_WRITEONCE is not compatible with WOLFBOOT_SELF_UPDATE_MONOLITHIC" |
537 | | #endif |
538 | | #ifndef RAM_CODE |
539 | | #error "WOLFBOOT_SELF_UPDATE_MONOLITHIC requires RAM_CODE" |
540 | | #endif |
541 | | #endif |
542 | | |
543 | | #if defined(DISABLE_BACKUP) && defined(DELTA_UPDATES) |
544 | | #error "DELTA_UPDATES requires swap partition (incompatible with DISABLE_BACKUP)" |
545 | | #endif |
546 | | |
547 | | #define PART_BOOT 0 |
548 | | #define PART_UPDATE 1 |
549 | | #define PART_SWAP 2 |
550 | | #define PART_SELF 3 |
551 | | #define PART_NONE 0xFF |
552 | | |
553 | | #define PART_DTS (0x10) |
554 | | #define PART_DTS_BOOT (PART_DTS | PART_BOOT) |
555 | | #define PART_DTS_UPDATE (PART_DTS | PART_UPDATE) |
556 | | |
557 | | #ifndef WOLFBOOT_FLAGS_INVERT |
558 | | #define IMG_STATE_NEW 0xFF |
559 | | #define IMG_STATE_UPDATING 0x70 |
560 | | /* now just an intermediary state, update state will always be either new or |
561 | | * updating before the application boots*/ |
562 | | #define IMG_STATE_FINAL_FLAGS 0x30 |
563 | | /* ELF loading state - only valid on boot partition so doesn't conflict with |
564 | | * IMAGE_STATE_UPDATING */ |
565 | | #define IMG_STATE_TESTING 0x10 |
566 | | #define IMG_STATE_SUCCESS 0x00 |
567 | | #define FLASH_BYTE_ERASED 0xFF |
568 | | #define FLASH_WORD_ERASED 0xFFFFFFFFUL |
569 | | #else |
570 | | #define IMG_STATE_NEW 0x00 |
571 | | #define IMG_STATE_UPDATING 0x8F |
572 | | #define IMG_STATE_TESTING 0xEF |
573 | | #define IMG_STATE_FINAL_FLAGS 0xBF |
574 | | #define IMG_STATE_SUCCESS 0xFF |
575 | | #define FLASH_BYTE_ERASED 0x00 |
576 | | #define FLASH_WORD_ERASED 0x00000000UL |
577 | | #endif |
578 | | |
579 | | #ifdef __WOLFBOOT |
580 | | /* include after PART_* are defined */ |
581 | | /* for wolfBoot_verify_integrity and wolfBoot_verify_authenticity */ |
582 | | #include "image.h" |
583 | | #endif |
584 | | |
585 | | void wolfBoot_update_trigger(void); |
586 | | void wolfBoot_success(void); |
587 | | uint32_t wolfBoot_image_size(uint8_t *image); |
588 | | uint32_t wolfBoot_get_blob_version(uint8_t *blob); |
589 | | uint16_t wolfBoot_get_blob_type(uint8_t *blob); |
590 | | uint32_t wolfBoot_get_blob_diffbase_version(uint8_t *blob); |
591 | | #ifdef WOLFBOOT_SELF_HEADER |
592 | | uint8_t* wolfBoot_get_self_header(void); |
593 | | uint32_t wolfBoot_get_self_version(void); |
594 | | #endif |
595 | | |
596 | | uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr); |
597 | | |
598 | | /* Get partition ID from manifest header */ |
599 | 0 | static inline uint8_t wolfBoot_get_blob_partition_id(uint8_t *blob) { |
600 | 0 | return wolfBoot_get_blob_type(blob) & HDR_IMG_TYPE_PART_MASK; |
601 | 0 | } |
602 | | |
603 | | #ifdef WOLFBOOT_FIXED_PARTITIONS |
604 | | void wolfBoot_erase_partition(uint8_t part); |
605 | | uint32_t wolfBoot_get_image_version(uint8_t part); |
606 | | uint16_t wolfBoot_get_image_type(uint8_t part); |
607 | | uint32_t wolfBoot_get_diffbase_version(uint8_t part); |
608 | | #define wolfBoot_current_firmware_version() \ |
609 | | wolfBoot_get_image_version(PART_BOOT) |
610 | | #define wolfBoot_update_firmware_version() \ |
611 | | wolfBoot_get_image_version(PART_UPDATE) |
612 | | #endif |
613 | | |
614 | | int wolfBoot_fallback_is_possible(void); |
615 | | int wolfBoot_dualboot_candidate(void); |
616 | | |
617 | | int wolfBoot_dualboot_candidate_addr(void**); |
618 | | int wolfBoot_get_partition_state(uint8_t part, uint8_t *st); |
619 | | |
620 | | #ifdef WOLFBOOT_PERSIST_FAILURE_STATUS |
621 | | /* Phase in which the failure was detected */ |
622 | | #define WOLFBOOT_FAILURE_PHASE_UPDATE 1 /* update image rejected before swap */ |
623 | | #define WOLFBOOT_FAILURE_PHASE_BOOT 2 /* boot image failed verification */ |
624 | | #define WOLFBOOT_FAILURE_PHASE_ROLLBACK 3 /* rolled back to a previous image */ |
625 | | #define WOLFBOOT_FAILURE_PHASE_RECOVERY 4 /* emergency-update image also failed |
626 | | * verification (device unbootable) */ |
627 | | #define WOLFBOOT_FAILURE_PHASE_SELF_UPDATE 5 /* bootloader self-update image |
628 | | * rejected */ |
629 | | |
630 | | /* Cause of the failure */ |
631 | | #define WOLFBOOT_FAILURE_CAUSE_HEADER 1 /* bad/invalid image header */ |
632 | | #define WOLFBOOT_FAILURE_CAUSE_HASH 2 /* hash/integrity check failed */ |
633 | | #define WOLFBOOT_FAILURE_CAUSE_SIGNATURE 3 /* signature/auth check failed */ |
634 | | #define WOLFBOOT_FAILURE_CAUSE_NOT_CONFIRMED 4 /* image never confirmed via |
635 | | * wolfBoot_success() */ |
636 | | |
637 | | /* Persisted failure record. */ |
638 | | struct wolfBoot_failure_record { |
639 | | uint32_t seq; /* monotonic sequence number (higher = newer) */ |
640 | | uint8_t phase; /* WOLFBOOT_FAILURE_PHASE_* */ |
641 | | uint8_t cause; /* WOLFBOOT_FAILURE_CAUSE_* */ |
642 | | uint8_t partition; /* PART_BOOT / PART_UPDATE */ |
643 | | uint8_t reserved; |
644 | | uint32_t fw_version; /* version of the offending image, 0 if unknown */ |
645 | | uint32_t crc; /* CRC32 over the preceding 12 bytes */ |
646 | | }; |
647 | | |
648 | | /* Public API */ |
649 | | int wolfBoot_get_failure_count(void); |
650 | | int wolfBoot_get_failure(int index, struct wolfBoot_failure_record *out); |
651 | | int wolfBoot_clear_failures(void); |
652 | | |
653 | | #if defined(__WOLFBOOT) || defined(UNIT_TEST) |
654 | | /* Internal API */ |
655 | | int wolfBoot_record_failure(uint8_t phase, uint8_t cause, uint8_t partition, |
656 | | uint32_t fw_version); |
657 | | #endif |
658 | | #endif /* WOLFBOOT_PERSIST_FAILURE_STATUS */ |
659 | | |
660 | | |
661 | | /* Encryption algorithm constants - always available for tools */ |
662 | | #define ENCRYPT_BLOCK_SIZE_CHACHA 64 |
663 | | #define ENCRYPT_BLOCK_SIZE_AES 16 |
664 | | |
665 | | #define ENCRYPT_KEY_SIZE_CHACHA 32 /* ChaCha20 - 256bit */ |
666 | | #define ENCRYPT_KEY_SIZE_AES128 16 /* AES128 */ |
667 | | #define ENCRYPT_KEY_SIZE_AES256 32 /* AES256 */ |
668 | | |
669 | | #define ENCRYPT_NONCE_SIZE_CHACHA 12 /* 96 bit */ |
670 | | #define ENCRYPT_NONCE_SIZE_AES 16 /* AES IV size */ |
671 | | |
672 | | |
673 | | #ifdef EXT_ENCRYPTED |
674 | | /* Encryption support - compile-time algorithm selection */ |
675 | | |
676 | | #if defined(ENCRYPT_WITH_CHACHA) |
677 | | #define ENCRYPT_BLOCK_SIZE ENCRYPT_BLOCK_SIZE_CHACHA |
678 | | #define ENCRYPT_KEY_SIZE ENCRYPT_KEY_SIZE_CHACHA |
679 | | #define ENCRYPT_NONCE_SIZE ENCRYPT_NONCE_SIZE_CHACHA |
680 | | #elif defined(ENCRYPT_WITH_AES128) |
681 | | #define ENCRYPT_BLOCK_SIZE ENCRYPT_BLOCK_SIZE_AES |
682 | | #define ENCRYPT_KEY_SIZE ENCRYPT_KEY_SIZE_AES128 |
683 | | #define ENCRYPT_NONCE_SIZE ENCRYPT_NONCE_SIZE_AES |
684 | | #elif defined(ENCRYPT_WITH_AES256) |
685 | | #define ENCRYPT_BLOCK_SIZE ENCRYPT_BLOCK_SIZE_AES |
686 | | #define ENCRYPT_KEY_SIZE ENCRYPT_KEY_SIZE_AES256 |
687 | | #define ENCRYPT_NONCE_SIZE ENCRYPT_NONCE_SIZE_AES |
688 | | #elif defined(ENCRYPT_PKCS11) |
689 | | #define ENCRYPT_BLOCK_SIZE ENCRYPT_PKCS11_BLOCK_SIZE |
690 | | /* In this case, the key ID is stored in flash rather than the key itself */ |
691 | | #define ENCRYPT_KEY_SIZE ENCRYPT_PKCS11_KEY_ID_SIZE |
692 | | #define ENCRYPT_NONCE_SIZE ENCRYPT_PKCS11_NONCE_SIZE |
693 | | #else |
694 | | # error "Encryption ON, but no encryption algorithm selected." |
695 | | #endif |
696 | | |
697 | | #endif /* EXT_ENCRYPTED */ |
698 | | |
699 | | #if defined(EXT_ENCRYPTED) && defined(MMU) |
700 | | int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst); |
701 | | #endif |
702 | | |
703 | | #ifdef DELTA_UPDATES |
704 | | int wolfBoot_get_diffbase_hdr(uint8_t part, uint8_t **ptr); |
705 | | #endif |
706 | | |
707 | | int wolfBoot_initialize_encryption(void); |
708 | | int wolfBoot_set_encrypt_key(const uint8_t *key, const uint8_t *nonce); |
709 | | int wolfBoot_get_encrypt_key(uint8_t *key, uint8_t *nonce); |
710 | | int wolfBoot_erase_encrypt_key(void); |
711 | | |
712 | | #if !defined(__WOLFBOOT) && defined(TZEN) |
713 | | |
714 | | /* Applications can access update success/trigger and flash erase/write |
715 | | * via non-secure callable, to facilitate updates |
716 | | */ |
717 | | |
718 | | /* Call wolfBoot_success from non-secure application */ |
719 | | CSME_NSE_API |
720 | | void wolfBoot_nsc_success(void); |
721 | | |
722 | | /* Call wolfBoot_update_trigger from non-secure application */ |
723 | | CSME_NSE_API |
724 | | void wolfBoot_nsc_update_trigger(void); |
725 | | |
726 | | /* Call wolfBoot_get_image_version from non-secure application */ |
727 | | CSME_NSE_API |
728 | | uint32_t wolfBoot_nsc_get_image_version(uint8_t part); |
729 | | #define wolfBoot_nsc_current_firmware_version() wolfBoot_nsc_get_image_version(PART_BOOT) |
730 | | #define wolfBoot_nsc_update_firmware_version() wolfBoot_nsc_get_image_version(PART_UPDATE) |
731 | | |
732 | | /* Call wolfBoot_get_partition_state from non-secure application */ |
733 | | CSME_NSE_API |
734 | | int wolfBoot_nsc_get_partition_state(uint8_t part, uint8_t *st); |
735 | | |
736 | | /* Erase one or more sectors in the update partition. |
737 | | * - address: offset within the update partition ('0' corresponds to PARTITION_UPDATE_ADDRESS) |
738 | | * - len: size, in bytes |
739 | | */ |
740 | | CSME_NSE_API |
741 | | int wolfBoot_nsc_erase_update(uint32_t address, uint32_t len); |
742 | | |
743 | | /* Write the content of buffer `buf` and size `len` to the update partition, |
744 | | * at offset address, from non-secure application |
745 | | * - address: offset within the update partition ('0' corresponds to PARTITION_UPDATE_ADDRESS) |
746 | | * - len: size, in bytes |
747 | | */ |
748 | | CSME_NSE_API |
749 | | int wolfBoot_nsc_write_update(uint32_t address, const uint8_t *buf, uint32_t len); |
750 | | |
751 | | #endif /* !__WOLFBOOT && TZEN */ |
752 | | |
753 | | |
754 | | #ifdef __cplusplus |
755 | | } |
756 | | #endif |
757 | | |
758 | | #endif /* !WOLFBOOT_H */ |