1 /* 2 * Copyright 2024 The ChromiumOS Authors 3 * Use of this source code is governed by a BSD-style license that can be 4 * found in the LICENSE file. 5 */ 6 7 #ifndef __GSC_UTILS_BOOT_PARAM_CBOR_BASIC_H 8 #define __GSC_UTILS_BOOT_PARAM_CBOR_BASIC_H 9 10 #include <stdbool.h> 11 #include <stddef.h> 12 #include <stdint.h> 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 #define CBOR_MAJOR_UINT ((uint8_t)(0 << 5)) 19 #define CBOR_MAJOR_NINT ((uint8_t)(1 << 5)) 20 #define CBOR_MAJOR_BSTR ((uint8_t)(2 << 5)) 21 #define CBOR_MAJOR_TSTR ((uint8_t)(3 << 5)) 22 #define CBOR_MAJOR_ARR ((uint8_t)(4 << 5)) 23 #define CBOR_MAJOR_MAP ((uint8_t)(5 << 5)) 24 #define CBOR_MAJOR_TAG ((uint8_t)(6 << 5)) 25 #define CBOR_MAJOR_SIMPLE ((uint8_t)(7 << 5)) 26 27 #define CBOR_HDR1(major, value) ((uint8_t)(major) | (uint8_t)((value) & 0x1f)) 28 29 #define CBOR_FALSE CBOR_HDR1(CBOR_MAJOR_SIMPLE, 20) 30 #define CBOR_TRUE CBOR_HDR1(CBOR_MAJOR_SIMPLE, 21) 31 #define CBOR_NULL CBOR_HDR1(CBOR_MAJOR_SIMPLE, 22) 32 33 #define CBOR_BYTES1 24 34 #define CBOR_BYTES2 25 35 #define CBOR_BYTES4 26 36 #define CBOR_BYTES8 27 37 38 /* NINTs in [-24..-1] range ("0 bytes") */ 39 #define CBOR_NINT0_LEN 1 40 #define CBOR_NINT0(label) CBOR_HDR1(CBOR_MAJOR_NINT, -(label) - 1) 41 42 /* UINTs in [0..23] range ("0 bytes") */ 43 #define CBOR_UINT0_LEN 1 44 #define CBOR_UINT0(label) CBOR_HDR1(CBOR_MAJOR_UINT, (label)) 45 46 /* Building block for 32 bit (4 bytes) integer representations */ 47 #define CBOR_INT32_LEN (1 + 4) 48 #define CBOR_INT32(major, value) \ 49 { \ 50 CBOR_HDR1(major, CBOR_BYTES4), \ 51 (uint8_t)(((value) & 0xFF000000) >> 24), \ 52 (uint8_t)(((value) & 0x00FF0000) >> 16), \ 53 (uint8_t)(((value) & 0x0000FF00) >> 8), \ 54 (uint8_t)((value) & 0x000000FF) \ 55 } 56 57 /* 32 bit (4 bytes) negative integers */ 58 #define CBOR_NINT32_LEN CBOR_INT32_LEN 59 #define CBOR_NINT32(value) CBOR_INT32(CBOR_MAJOR_NINT, (-(value) - 1)) 60 61 /* 32 bit (4 bytes) positive integers */ 62 #define CBOR_UINT32_LEN CBOR_INT32_LEN 63 #define CBOR_UINT32(value) CBOR_INT32(CBOR_MAJOR_UINT, value) 64 65 /* BSTR with 1 byte size */ 66 #define CBOR_BSTR_HDR8(size) \ 67 { CBOR_HDR1(CBOR_MAJOR_BSTR, CBOR_BYTES1), size } 68 69 /* TSTR with 1 byte size */ 70 #define CBOR_TSTR_HDR8(size) \ 71 { CBOR_HDR1(CBOR_MAJOR_TSTR, CBOR_BYTES1), size } 72 73 /* BSTR with 2 byte size */ 74 #define CBOR_BSTR_HDR16(size) \ 75 { \ 76 CBOR_HDR1(CBOR_MAJOR_BSTR, CBOR_BYTES2), \ 77 (uint8_t)(((size) & 0xFF00) >> 8), \ 78 (uint8_t)((size) & 0x00FF) \ 79 } 80 81 /* BSTR of length 1 */ 82 struct cbor_bstr1_s { 83 uint8_t cbor_hdr; 84 uint8_t value; 85 }; 86 #define CBOR_BSTR1_HDR CBOR_HDR1(CBOR_MAJOR_BSTR, 1) 87 #define CBOR_BSTR1_EMPTY { CBOR_BSTR1_HDR, 0 } 88 89 /* BSTR of length 32: UDS, CDI, digest, key seeds */ 90 struct cbor_bstr32_s { 91 uint8_t cbor_hdr[2]; 92 uint8_t value[32]; 93 }; 94 #define CBOR_BSTR32_HDR CBOR_BSTR_HDR8(32) 95 #define CBOR_BSTR32_EMPTY \ 96 { \ 97 CBOR_BSTR32_HDR, \ 98 { \ 99 0, 0, 0, 0, 0, 0, 0, 0, \ 100 0, 0, 0, 0, 0, 0, 0, 0, \ 101 0, 0, 0, 0, 0, 0, 0, 0, \ 102 0, 0, 0, 0, 0, 0, 0, 0 \ 103 } \ 104 } 105 106 /* TSTR of length 2*20 = 40: UDS_ID, CDI_ID as hex */ 107 struct cbor_tstr40_s { 108 uint8_t cbor_hdr[2]; 109 uint8_t value[40]; 110 }; 111 #define CBOR_TSTR40_HDR CBOR_TSTR_HDR8(40) 112 #define CBOR_TSTR40_EMPTY \ 113 { \ 114 CBOR_TSTR40_HDR, \ 115 { \ 116 0, 0, 0, 0, 0, 0, 0, 0, \ 117 0, 0, 0, 0, 0, 0, 0, 0, \ 118 0, 0, 0, 0, 0, 0, 0, 0, \ 119 0, 0, 0, 0, 0, 0, 0, 0, \ 120 0, 0, 0, 0, 0, 0, 0, 0 \ 121 } \ 122 } 123 124 /* BSTR of length 64: signature, entropy */ 125 struct cbor_bstr64_s { 126 uint8_t cbor_hdr[2]; 127 uint8_t value[64]; 128 }; 129 #define CBOR_BSTR64_HDR CBOR_BSTR_HDR8(64) 130 #define CBOR_BSTR64_EMPTY \ 131 { \ 132 CBOR_BSTR64_HDR, \ 133 { \ 134 0, 0, 0, 0, 0, 0, 0, 0, \ 135 0, 0, 0, 0, 0, 0, 0, 0, \ 136 0, 0, 0, 0, 0, 0, 0, 0, \ 137 0, 0, 0, 0, 0, 0, 0, 0, \ 138 0, 0, 0, 0, 0, 0, 0, 0, \ 139 0, 0, 0, 0, 0, 0, 0, 0, \ 140 0, 0, 0, 0, 0, 0, 0, 0, \ 141 0, 0, 0, 0, 0, 0, 0, 0 \ 142 } \ 143 } 144 145 /* UINT32 */ 146 struct cbor_uint32_s { 147 uint8_t cbor_hdr; 148 uint8_t value[4]; 149 }; 150 #define CBOR_UINT32_HDR CBOR_HDR1(CBOR_MAJOR_UINT, CBOR_BYTES4) 151 #define CBOR_UINT32_ZERO \ 152 { \ 153 CBOR_UINT32_HDR, \ 154 { \ 155 0, 0, 0, 0 \ 156 } \ 157 } 158 159 #ifdef __cplusplus 160 } /* extern "C" */ 161 #endif 162 163 #endif /* __CGSC_UTILS_BOOT_PARAM_CBOR_BASIC_H */ 164