|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * XIP Multi-Key Encryption Library |
| 5 | + * Self-contained library for XIP encryption support in MCUBoot. |
| 6 | + * Interfaces with MCUBoot via boot_image_check_hook (existing hook). |
| 7 | + */ |
| 8 | +#ifndef XIP_ENC_H |
| 9 | +#define XIP_ENC_H |
| 10 | + |
| 11 | +#include <stdint.h> |
| 12 | +#include <stdbool.h> |
| 13 | +#include <string.h> |
| 14 | +#include "bootutil/bootutil.h" |
| 15 | +#include "bootutil/image.h" |
| 16 | +#include "flash_map_backend/flash_map_backend.h" |
| 17 | +#include "mcuboot_config/mcuboot_config.h" |
| 18 | + |
| 19 | +#if defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_ENC_IMAGES_XIP) |
| 20 | +#error "MCUBOOT_ENC_IMAGES and MCUBOOT_ENC_IMAGES_XIP are mutually exclusive." |
| 21 | +#endif |
| 22 | + |
| 23 | +/** |
| 24 | + * Secure zeroization -- uses volatile pointer to prevent compiler optimization. |
| 25 | + */ |
| 26 | +static inline void xip_enc_zeroize(void *p, size_t n) |
| 27 | +{ |
| 28 | + volatile unsigned char *v = (volatile unsigned char *)p; |
| 29 | + for (size_t i = 0; i < n; i++) { |
| 30 | + v[i] = 0; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Constant-time comparison to avoid timing side-channels. |
| 36 | + * Returns 0 if equal, non-zero otherwise. |
| 37 | + */ |
| 38 | +static inline int xip_enc_ct_compare(const uint8_t *a, const uint8_t *b, |
| 39 | + size_t size) |
| 40 | +{ |
| 41 | + uint8_t result = 0; |
| 42 | + for (size_t i = 0; i < size; i++) { |
| 43 | + result |= a[i] ^ b[i]; |
| 44 | + } |
| 45 | + return (int)result; |
| 46 | +} |
| 47 | + |
| 48 | +#define XIP_ENC_KEY_SIZE 16u |
| 49 | +#define XIP_ENC_IV_SIZE 16u |
| 50 | +#define XIP_ENC_MAX_IMAGES 3u |
| 51 | + |
| 52 | +/* |
| 53 | + * boot_image_check_hook() is declared in bootutil/boot_hooks.h (upstream). |
| 54 | + * The library provides the implementation in xip_enc_validate.c. |
| 55 | + * Do NOT redeclare here. |
| 56 | + */ |
| 57 | + |
| 58 | +/** |
| 59 | + * Called by MCUBoot after fill_rsp() to populate xip_key/xip_iv in boot_rsp. |
| 60 | + * Copies key/IV from library-internal storage for the specified image. |
| 61 | + * |
| 62 | + * @param img_index Image index (passed via BOOT_CURR_IMG from MCUBoot) |
| 63 | + * @param rsp Boot response to populate |
| 64 | + */ |
| 65 | +void boot_xip_populate_rsp(int img_index, struct boot_rsp *rsp); |
| 66 | + |
| 67 | +/** |
| 68 | + * Platform-provided: decrypt image payload using SMIF hardware. |
| 69 | + * Used during hash computation to decrypt AES-CTR encrypted payload. |
| 70 | + * |
| 71 | + * @param image_index Current image index |
| 72 | + * @param fap Flash area of the image |
| 73 | + * @param off Offset within flash area |
| 74 | + * @param sz Size of data to decrypt |
| 75 | + * @param buf Buffer with data to decrypt (in-place) |
| 76 | + * |
| 77 | + * @return 0 on success, negative on error |
| 78 | + */ |
| 79 | +int boot_decrypt_xip(int image_index, const struct flash_area *fap, |
| 80 | + uint32_t off, uint32_t sz, uint8_t *buf); |
| 81 | + |
| 82 | +/** |
| 83 | + * Store key/IV for an image (called by validation hook after ECIES unwrap). |
| 84 | + */ |
| 85 | +void xip_enc_store_key(int img_index, const uint8_t *key, const uint8_t *iv); |
| 86 | + |
| 87 | +/** |
| 88 | + * Retrieve stored key/IV for an image. |
| 89 | + * |
| 90 | + * @return 0 on success, -1 if not valid |
| 91 | + */ |
| 92 | +int xip_enc_get_key(int img_index, uint8_t *key, uint8_t *iv); |
| 93 | + |
| 94 | +/** |
| 95 | + * Zeroize all stored keys. Call before launching application. |
| 96 | + */ |
| 97 | +void xip_enc_clear_keys(void); |
| 98 | + |
| 99 | +/** |
| 100 | + * ECIES-P256 key unwrap with extended TLV support. |
| 101 | + * Extracts AES key and XIP IV from ECIES envelope. |
| 102 | + * |
| 103 | + * @param tlv_buf ECIES TLV data read from image |
| 104 | + * @param tlv_len Length of TLV data (113 standard, up to 177 extended) |
| 105 | + * @param out_key Output: 16-byte AES key |
| 106 | + * @param out_iv Output: 16-byte XIP IV |
| 107 | + * |
| 108 | + * @return 0 on success, negative on error |
| 109 | + */ |
| 110 | +int xip_enc_ecies_unwrap(const uint8_t *tlv_buf, uint16_t tlv_len, |
| 111 | + uint8_t *out_key, uint8_t *out_iv); |
| 112 | + |
| 113 | +#endif /* XIP_ENC_H */ |
0 commit comments