Software APIs
cbor.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_CBOR_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_CBOR_H_
7 
8 #include "sw/device/silicon_creator/lib/error.h"
9 
10 struct cbor_out {
11  uint8_t *start;
12  size_t offset;
13 };
14 
15 typedef struct cbor_out cbor_out_t;
16 
17 /**
18  * Initialize the cbor_out_t.
19  *
20  * @param[in,out] p cbor_out structure
21  * @param buf buffer to be written
22  */
23 static inline void cbor_out_init(cbor_out_t *p, void *buf) {
24  p->start = buf;
25  p->offset = 0;
26 }
27 
28 /**
29  * Return the current written size in the cbor_out_t.
30  *
31  * @param[in] p cbor_out structure
32  * @return current written size
33  */
34 static inline size_t cbor_out_size(cbor_out_t *p) { return p->offset; }
35 
36 /**
37  * Calculate the required size of "CBOR argument".
38  *
39  * @param value unsigned argument
40  * @return required size
41  */
42 size_t cbor_calc_arg_size(uint64_t value);
43 
44 /**
45  * Calculate the required size to encode the signed integer as "CBOR argument".
46  *
47  * @param value signed integer
48  * @return required size
49  */
50 size_t cbor_calc_int_size(int64_t value);
51 
52 /**
53  * Write the raw bytes if cbor_out is not NULL.
54  *
55  * @param[in,out] p cbor_out structure
56  * @param raw pointer to raw bytes
57  * @param raw_size the size of raw bytes
58  * @return size to be written
59  */
60 size_t cbor_write_raw_bytes(cbor_out_t *p, const uint8_t *raw, size_t raw_size);
61 
62 /**
63  * Write the integer if cbor_out is not NULL.
64  *
65  * @param[in,out] p cbor_out structure
66  * @param value signed integer
67  * @return size to be written
68  */
69 size_t cbor_write_int(cbor_out_t *p, int64_t value);
70 
71 /**
72  * Write the byte string header if cbor_out is not NULL.
73  *
74  * @param[in,out] p cbor_out structure
75  * @param bstr_size size of the bstr
76  * @return size to be written
77  */
78 size_t cbor_write_bstr_header(cbor_out_t *p, size_t bstr_size);
79 
80 /**
81  * A helper function to write the byte string if cbor_out is not NULL.
82  *
83  * @param[in,out] p cbor_out structure
84  * @param bstr pointer to the bstr
85  * @param bstr_size size of the bstr
86  * @return size to be written
87  */
88 static inline size_t cbor_write_bstr(cbor_out_t *p, const uint8_t *bstr,
89  size_t bstr_size) {
90  size_t sz = 0;
91  sz += cbor_write_bstr_header(p, bstr_size);
92  sz += cbor_write_raw_bytes(p, bstr, bstr_size);
93  return sz;
94 }
95 
96 /**
97  * Write the text string header if cbor_out is not NULL.
98  *
99  * @param[in,out] p cbor_out structure
100  * @param bstr_size size of the tstr
101  * @return size to be written
102  */
103 size_t cbor_write_tstr_header(cbor_out_t *p, size_t tstr_size);
104 
105 /**
106  * A helper function to write the text string if cbor_out is not NULL.
107  *
108  * @param[in,out] p cbor_out structure
109  * @param tstr pointer to the tstr
110  * @param bstr_size size of the tstr
111  * @return size to be written
112  */
113 static inline size_t cbor_write_tstr(cbor_out_t *p, const uint8_t *tstr,
114  size_t tstr_size) {
115  size_t sz = 0;
116  sz += cbor_write_tstr_header(p, tstr_size);
117  sz += cbor_write_raw_bytes(p, tstr, tstr_size);
118  return sz;
119 }
120 
121 /**
122  * Write the array header if cbor_out is not NULL.
123  *
124  * @param[in,out] p cbor_out structure
125  * @param num_elements the number of elements in the array
126  * @return size to be written
127  */
128 size_t cbor_write_array_header(cbor_out_t *p, size_t num_elements);
129 
130 /**
131  * Write the map header if cbor_out is not NULL.
132  *
133  * @param[in,out] p cbor_out structure
134  * @param num_pairs the number of pairs in the map
135  * @return size to be written
136  */
137 size_t cbor_write_map_header(cbor_out_t *p, size_t num_pairs);
138 
139 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_CBOR_H_