Skip to content

Commit e6ba2ed

Browse files
[dice,ctw] Add more cbor wrappers for auto-gen cwt template
The autogen source from templates needs some more APIs to utilize the Cbor structure, including - calculate the size of a given cbor argument - add a bstr/tstr header with size, and rewind the cursor - copy the war data from input pointer to the CborOut buffer
1 parent cea6a90 commit e6ba2ed

File tree

3 files changed

+189
-90
lines changed

3 files changed

+189
-90
lines changed

sw/device/silicon_creator/lib/cert/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ cc_library(
112112

113113
cc_library(
114114
name = "cbor",
115+
srcs = ["cbor.c"],
115116
hdrs = ["cbor.h"],
116117
deps = [
117118
"//sw/device/lib/base:status",
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}

sw/device/silicon_creator/lib/cert/cbor.h

Lines changed: 42 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,100 +6,52 @@
66
#define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_CBOR_H_
77

88
#include "include/dice/cbor_writer.h"
9-
#include "sw/device/lib/runtime/log.h"
109
#include "sw/device/silicon_creator/lib/error.h"
1110

12-
#define CBOR_CHECK_OVERFLOWED_AND_RETURN(p) \
13-
do { \
14-
if (CborOutOverflowed(p)) { \
15-
LOG_ERROR("CborOutOverflowed!!"); \
16-
return kErrorCertInvalidSize; \
17-
} \
18-
return kErrorOk; \
19-
} while (0)
20-
2111
// Wrappers for each CBOR type and CBOR handle initialization
22-
static inline rom_error_t cbor_write_out_init(struct CborOut *p, void *buf,
23-
const size_t buf_size) {
24-
CborOutInit(buf, buf_size, p);
25-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
26-
}
27-
28-
static inline rom_error_t cbor_map_init(struct CborOut *p,
29-
const size_t num_pairs) {
30-
CborWriteMap(num_pairs, p);
31-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
32-
}
33-
34-
static inline rom_error_t cbor_array_init(struct CborOut *p,
35-
const size_t num_elements) {
36-
CborWriteArray(num_elements, p);
37-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
38-
}
39-
40-
static inline rom_error_t cbor_write_string(struct CborOut *p,
41-
const char *str) {
42-
CborWriteTstr(str, p);
43-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
44-
}
45-
46-
static inline rom_error_t cbor_write_bytes(struct CborOut *p,
47-
const uint8_t *data,
48-
const size_t data_size) {
49-
CborWriteBstr(data_size, data, p);
50-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
51-
}
12+
rom_error_t cbor_write_out_init(struct CborOut *p, void *buf,
13+
const size_t buf_size);
14+
15+
rom_error_t cbor_map_init(struct CborOut *p, const size_t num_pairs);
16+
17+
rom_error_t cbor_array_init(struct CborOut *p, const size_t num_elements);
18+
19+
rom_error_t cbor_write_string(struct CborOut *p, const char *str);
20+
21+
rom_error_t cbor_write_bytes(struct CborOut *p, const uint8_t *data,
22+
const size_t data_size);
5223

5324
// Wrappers to encode a pair of data for cbor-map
54-
static inline rom_error_t cbor_write_pair_uint_uint(struct CborOut *p,
55-
uint64_t key,
56-
uint64_t value) {
57-
CborWriteUint(key, p);
58-
CborWriteUint(value, p);
59-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
60-
}
61-
62-
static inline rom_error_t cbor_write_pair_int_uint(struct CborOut *p,
63-
int64_t key,
64-
uint64_t value) {
65-
CborWriteInt(key, p);
66-
CborWriteUint(value, p);
67-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
68-
}
69-
70-
static inline rom_error_t cbor_write_pair_uint_int(struct CborOut *p,
71-
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-
static inline rom_error_t cbor_write_pair_int_bytes(struct CborOut *p,
79-
int64_t key,
80-
const uint8_t *value,
81-
const size_t value_size) {
82-
CborWriteInt(key, p);
83-
CborWriteBstr(value_size, value, p);
84-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
85-
}
86-
87-
static inline rom_error_t cbor_write_pair_uint_tstr(struct CborOut *p,
88-
uint64_t key,
89-
const char *value) {
90-
CborWriteUint(key, p);
91-
CborWriteTstr(value, p);
92-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
93-
}
94-
95-
static inline rom_error_t cbor_write_pair_int_tstr(struct CborOut *p,
96-
int64_t key,
97-
const char *value) {
98-
CborWriteInt(key, p);
99-
CborWriteTstr(value, p);
100-
CBOR_CHECK_OVERFLOWED_AND_RETURN(p);
101-
}
102-
103-
#undef CBOR_CHECK_OVERFLOWED_AND_RETURN
25+
rom_error_t cbor_write_pair_uint_uint(struct CborOut *p, uint64_t key,
26+
uint64_t value);
27+
28+
rom_error_t cbor_write_pair_int_uint(struct CborOut *p, int64_t key,
29+
uint64_t value);
30+
31+
rom_error_t cbor_write_pair_uint_int(struct CborOut *p, uint64_t key,
32+
int64_t value);
33+
34+
rom_error_t cbor_write_pair_int_bytes(struct CborOut *p, int64_t key,
35+
const uint8_t *value,
36+
const size_t value_size);
37+
38+
rom_error_t cbor_write_pair_uint_tstr(struct CborOut *p, uint64_t key,
39+
const char *value);
40+
41+
rom_error_t cbor_write_pair_int_tstr(struct CborOut *p, int64_t key,
42+
const char *value);
43+
44+
// Helpers for the auto-gen template
45+
// Calculate the size of a given cbor argument, in int64 or uint64
46+
size_t cbor_calc_arg_size(uint64_t value);
47+
size_t cbor_calc_int_size(int64_t value);
48+
49+
// Add a bstr/tstr header with size, and rewind the cursor
50+
rom_error_t cbor_write_bstr_header(struct CborOut *p, const size_t bstr_size);
51+
rom_error_t cbor_write_tstr_header(struct CborOut *p, const size_t tstr_size);
52+
53+
// Copy the rwa data from input pointer to the CborOut buffer
54+
rom_error_t cbor_write_raw_bytes(struct CborOut *p, const uint8_t *raw,
55+
const size_t raw_size);
10456

10557
#endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_CERT_CBOR_H_

0 commit comments

Comments
 (0)