|
| 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 | +#include "sw/device/silicon_creator/lib/cert/cbor.h" |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +#include "include/dice/cbor_writer.h" |
| 10 | +#include "sw/device/lib/base/macros.h" |
| 11 | +#include "sw/device/lib/runtime/log.h" |
| 12 | +#include "sw/device/silicon_creator/lib/error.h" |
| 13 | + |
| 14 | +#define CBOR_RETURN_IF_OVERFLOWED(p) \ |
| 15 | + do { \ |
| 16 | + if (CborOutOverflowed(p)) { \ |
| 17 | + LOG_ERROR("CborOutOverflowed!!"); \ |
| 18 | + return kErrorCertInvalidSize; \ |
| 19 | + } \ |
| 20 | + } while (0) |
| 21 | + |
| 22 | +#define CBOR_CHECK_OVERFLOWED_AND_RETURN(p) \ |
| 23 | + do { \ |
| 24 | + CBOR_RETURN_IF_OVERFLOWED(p); \ |
| 25 | + return kErrorOk; \ |
| 26 | + } while (0) |
| 27 | + |
| 28 | +inline rom_error_t cbor_write_out_init(struct CborOut *p, void *buf, |
| 29 | + const size_t buf_size) { |
| 30 | + CborOutInit(buf, buf_size, p); |
| 31 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 32 | +} |
| 33 | + |
| 34 | +inline rom_error_t cbor_map_init(struct CborOut *p, const size_t num_pairs) { |
| 35 | + CborWriteMap(num_pairs, p); |
| 36 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 37 | +} |
| 38 | + |
| 39 | +inline rom_error_t cbor_array_init(struct CborOut *p, |
| 40 | + const size_t num_elements) { |
| 41 | + CborWriteArray(num_elements, p); |
| 42 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 43 | +} |
| 44 | + |
| 45 | +inline rom_error_t cbor_write_string(struct CborOut *p, const char *str) { |
| 46 | + CborWriteTstr(str, p); |
| 47 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 48 | +} |
| 49 | + |
| 50 | +inline rom_error_t cbor_write_bytes(struct CborOut *p, const uint8_t *data, |
| 51 | + const size_t data_size) { |
| 52 | + CborWriteBstr(data_size, data, p); |
| 53 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 54 | +} |
| 55 | + |
| 56 | +// Wrappers to encode a pair of data for cbor-map |
| 57 | +inline rom_error_t cbor_write_pair_uint_uint(struct CborOut *p, uint64_t key, |
| 58 | + uint64_t value) { |
| 59 | + CborWriteUint(key, p); |
| 60 | + CborWriteUint(value, p); |
| 61 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 62 | +} |
| 63 | + |
| 64 | +inline rom_error_t cbor_write_pair_int_uint(struct CborOut *p, int64_t key, |
| 65 | + uint64_t value) { |
| 66 | + CborWriteInt(key, p); |
| 67 | + CborWriteUint(value, p); |
| 68 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 69 | +} |
| 70 | + |
| 71 | +inline rom_error_t cbor_write_pair_uint_int(struct CborOut *p, uint64_t key, |
| 72 | + int64_t value) { |
| 73 | + CborWriteUint(key, p); |
| 74 | + CborWriteInt(value, p); |
| 75 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 76 | +} |
| 77 | + |
| 78 | +inline rom_error_t cbor_write_pair_int_bytes(struct CborOut *p, int64_t key, |
| 79 | + const uint8_t *value, |
| 80 | + const size_t value_size) { |
| 81 | + CborWriteInt(key, p); |
| 82 | + CborWriteBstr(value_size, value, p); |
| 83 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 84 | +} |
| 85 | + |
| 86 | +inline rom_error_t cbor_write_pair_uint_tstr(struct CborOut *p, uint64_t key, |
| 87 | + const char *value) { |
| 88 | + CborWriteUint(key, p); |
| 89 | + CborWriteTstr(value, p); |
| 90 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 91 | +} |
| 92 | + |
| 93 | +inline rom_error_t cbor_write_pair_int_tstr(struct CborOut *p, int64_t key, |
| 94 | + const char *value) { |
| 95 | + CborWriteInt(key, p); |
| 96 | + CborWriteTstr(value, p); |
| 97 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 98 | +} |
| 99 | + |
| 100 | +// Helpers for the auto-gen template, to |
| 101 | +// - calculate the size of a given cbor argument |
| 102 | +// - add a bstr/tstr header with size, and rewind the cursor |
| 103 | +// - copy the war data from input pointer to the CborOut buffer |
| 104 | +size_t cbor_calc_arg_size(uint64_t value) { |
| 105 | + if (value <= 23) { |
| 106 | + return 0; |
| 107 | + } else if (value <= 0xff) { |
| 108 | + return 1; |
| 109 | + } else if (value <= 0xffff) { |
| 110 | + return 2; |
| 111 | + } else if (value <= 0xffffffff) { |
| 112 | + return 4; |
| 113 | + } else { |
| 114 | + return 8; |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +size_t cbor_calc_int_size(int64_t value) { |
| 119 | + if (value < 0) |
| 120 | + return cbor_calc_arg_size((uint64_t)(-(value + 1))); |
| 121 | + |
| 122 | + return cbor_calc_arg_size((uint64_t)value); |
| 123 | +} |
| 124 | + |
| 125 | +rom_error_t cbor_write_bstr_header(struct CborOut *p, const size_t bstr_size) { |
| 126 | + if (NULL == CborAllocBstr(bstr_size, p)) |
| 127 | + return kErrorCertInvalidSize; |
| 128 | + p->cursor -= bstr_size; |
| 129 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 130 | +} |
| 131 | + |
| 132 | +rom_error_t cbor_write_tstr_header(struct CborOut *p, const size_t tstr_size) { |
| 133 | + if (NULL == CborAllocTstr(tstr_size, p)) |
| 134 | + return kErrorCertInvalidSize; |
| 135 | + p->cursor -= tstr_size; |
| 136 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 137 | +} |
| 138 | + |
| 139 | +rom_error_t cbor_write_raw_bytes(struct CborOut *p, const uint8_t *raw, |
| 140 | + const size_t raw_size) { |
| 141 | + if (p->cursor + raw_size > p->buffer_size) |
| 142 | + return kErrorCertInvalidSize; |
| 143 | + memcpy(&p->buffer[p->cursor], raw, raw_size); |
| 144 | + p->cursor += raw_size; |
| 145 | + CBOR_CHECK_OVERFLOWED_AND_RETURN(p); |
| 146 | +} |
0 commit comments