5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_TEMPLATE_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_TEMPLATE_H_
10 #include "sw/device/silicon_creator/lib/error.h"
42 const uint8_t *const_end;
44 const uint8_t *out_begin;
52 typedef void *template_pos_t;
62 const uint8_t *const_bytes) {
63 state->out_begin = state->out_end = out_buf;
64 state->const_end = const_bytes;
74 return (uint16_t)(state->out_end - state->out_begin);
88 static inline void template_set_bit(
template_state_t *state,
int byte_offset,
89 int bit_offset,
bool value) {
90 *(state->out_end - byte_offset) |= ((uint8_t) !!value) << bit_offset;
99 static inline void template_push_asn1_bool(
template_state_t *state,
bool val) {
100 *state->out_end = val ? 0xff : 0x00;
112 uint8_t *template_push_hex_impl(uint8_t *out,
const uint8_t *inp,
size_t size);
122 const uint8_t *buf,
size_t size) {
123 state->out_end = template_push_hex_impl(state->out_end, buf, size);
134 const uint8_t *buf,
size_t size) {
135 memcpy(state->out_end, buf, size);
136 state->out_end += size;
145 static inline void template_push_const(
template_state_t *state,
size_t size) {
146 memcpy(state->out_end, state->const_end, size);
147 state->out_end += size;
148 state->const_end += size;
162 uint8_t *template_asn1_integer_impl(uint8_t *out, uint8_t tag,
bool tweak_msb,
163 const uint8_t *bytes_be,
size_t size);
179 static inline void template_asn1_integer(
template_state_t *state, uint8_t tag,
181 const uint8_t *bytes_be,
size_t size) {
182 state->out_end = template_asn1_integer_impl(state->out_end, tag, tweak_msb,
194 static inline void template_asn1_uint32(
template_state_t *state, uint8_t tag,
195 bool tweak_msb, uint32_t value) {
196 uint32_t _value = __builtin_bswap32(value);
197 state->out_end = template_asn1_integer_impl(state->out_end, tag, tweak_msb,
198 (uint8_t *)&_value, 4);
213 return (template_pos_t *)(state->out_end + offset);
219 void template_patch_size_be_impl(template_pos_t memo, uint8_t *out_end);
230 template_pos_t memo) {
231 template_patch_size_be_impl(memo, state->out_end);
237 #define ASSERT_VAR_SIZE_EQ(actual, expected) \
238 static_assert(actual == expected, "Invalid variable size.");
240 #define ASSERT_VAR_SIZE_GE(actual, expected) \
241 static_assert(actual >= expected, "Invalid variable size.");
243 #define ASSERT_VAR_SIZE_LE(actual, expected) \
244 static_assert(actual <= expected, "Invalid variable size.");
252 #define TEMPLATE_ASSERT_FIXED_LENGTH(template, field) \
253 ASSERT_VAR_SIZE_EQ(k##template##Min##field##SizeBytes, \
254 k##template##Max##field##SizeBytes);
263 #define TEMPLATE_CHECK_SIZE(template, field, size) \
264 ASSERT_VAR_SIZE_GE((size), k##template##Min##field##SizeBytes); \
265 ASSERT_VAR_SIZE_LE((size), k##template##Max##field##SizeBytes);
275 #define TEMPLATE_SET(params, template, field, value) \
277 TEMPLATE_CHECK_SIZE(template, field, sizeof(value)); \
278 (params).k##template##Field##field = (void *)(value); \
290 #define TEMPLATE_SET_TRUNCATED(params, template, field, value, size) \
292 ASSERT_VAR_SIZE_GE(sizeof(value), size); \
293 TEMPLATE_CHECK_SIZE(template, field, size); \
294 (params).k##template##Field##field = (void *)(value); \