suit.h
1 /*
2  * Copyright (C) 2019 Koen Zandberg
3  * 2019 Kaspar Schleiser <kaspar@schleiser.de>
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
30 #ifndef SUIT_H
31 #define SUIT_H
32 
33 #include <stddef.h>
34 #include <stdint.h>
35 
36 #include "cose/sign.h"
37 #include "nanocbor/nanocbor.h"
38 #include "uuid.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
47 #ifndef SUIT_COSE_BUF_SIZE
48 #define SUIT_COSE_BUF_SIZE (180U)
49 #endif
50 
54 #ifndef CONFIG_SUIT_COMPONENT_MAX
55 #define CONFIG_SUIT_COMPONENT_MAX (1U)
56 #endif
57 
61 #ifndef CONFIG_SUIT_COMPONENT_MAX_NAME_LEN
62 #define CONFIG_SUIT_COMPONENT_MAX_NAME_LEN (32U)
63 #endif
64 
71 #define SUIT_VERSION (1)
72 
82 #define SUIT_STATE_HAVE_COMPONENTS (1 << 0)
83 
87 #define SUIT_STATE_COSE_AUTHENTICATED (1 << 1)
88 
92 #define SUIT_STATE_FULLY_AUTHENTICATED (1 << 2)
93 
98 typedef enum {
99  SUIT_OK = 0,
113 } suit_error_t;
114 
121 typedef enum {
126 } suit_digest_t;
127 
134 typedef enum {
140 
147 enum {
151 };
152 
157 typedef enum {
158  SUIT_PARAMETER_VENDOR_IDENTIFIER = 1,
159  SUIT_PARAMETER_CLASS_IDENTIFIER = 2,
160  SUIT_PARAMETER_IMAGE_DIGEST = 3,
161  SUIT_PARAMETER_USE_BEFORE = 4,
162  SUIT_PARAMETER_COMPONENT_OFFSET = 5,
163  SUIT_PARAMETER_STRICT_ORDER = 12,
164  SUIT_PARAMETER_SOFT_FAILURE = 13,
165  SUIT_PARAMETER_IMAGE_SIZE = 14,
166  SUIT_PARAMETER_ENCRYPTION_INFO = 18,
167  SUIT_PARAMETER_COMPRESSION_INFO = 19,
168  SUIT_PARAMETER_UNPACK_INFO = 20,
169  SUIT_PARAMETER_URI = 21,
170  SUIT_PARAMETER_SOURCE_COMPONENT = 22,
171  SUIT_PARAMETER_RUN_ARGS = 23,
172  SUIT_PARAMETER_DEVICE_IDENTIFIER = 24,
173  SUIT_PARAMETER_MINIMUM_BATTERY = 26,
174  SUIT_PARAMETER_UPDATE_PRIORITY = 27,
175  SUIT_PARAMETER_VERSION = 28,
176  SUIT_PARAMETER_WAIT_INFO = 29,
177  SUIT_PARAMETER_URI_LIST = 30,
178 } suit_parameter_t;
186 typedef struct {
187  uint16_t offset;
189 
196 #define SUIT_COMPONENT_STATE_FETCHED (1 << 0)
197 #define SUIT_COMPONENT_STATE_FETCH_FAILED (1 << 1)
198 #define SUIT_COMPONENT_STATE_VERIFIED (1 << 2)
199 #define SUIT_COMPONENT_STATE_FINALIZED (1 << 3)
207 typedef struct suit_storage suit_storage_ref_t;
208 
214 typedef struct {
216  uint16_t state;
229 
233 typedef struct {
234  const uint8_t *buf;
235  size_t len;
236  const uint8_t *cose_payload;
238  uint32_t validated;
239  uint32_t state;
242  unsigned components_len;
245  uint8_t validation_buf[SUIT_COSE_BUF_SIZE];
246  char *urlbuf;
247  size_t urlbuf_len;
249 
255 #define SUIT_MANIFEST_COMPONENT_ALL (UINT8_MAX)
256 
262 #define SUIT_MANIFEST_COMPONENT_NONE (SUIT_MANIFEST_COMPONENT_ALL - 1)
263 
277 int suit_parse(suit_manifest_t *manifest, const uint8_t *buf, size_t len);
278 
287 int suit_policy_check(suit_manifest_t *manifest);
288 
295 static inline void suit_component_set_flag(suit_component_t *component,
296  uint16_t flag)
297 {
298  component->state |= flag;
299 }
300 
309 static inline bool suit_component_check_flag(suit_component_t *component,
310  uint16_t flag)
311 {
312  return (component->state & flag);
313 }
314 
324  const suit_component_t *component,
325  char separator, char *buf, size_t buf_len);
326 
339 int suit_storage_helper(void *arg, size_t offset, uint8_t *buf, size_t len,
340  int more);
341 
342 #ifdef __cplusplus
343 }
344 #endif
345 
346 #endif /* SUIT_H */
347 
SUIT_DIGEST_SHA256
@ SUIT_DIGEST_SHA256
SHA256.
Definition: suit.h:123
suit_storage_helper
int suit_storage_helper(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
Helper function for writing bytes on flash a specified offset.
SUIT_ERR_SEQUENCE_NUMBER
@ SUIT_ERR_SEQUENCE_NUMBER
Sequence number less or equal to current sequence number.
Definition: suit.h:104
suit_manifest_t::components_len
unsigned components_len
Current number of components.
Definition: suit.h:242
suit_component_set_flag
static void suit_component_set_flag(suit_component_t *component, uint16_t flag)
Set a component flag.
Definition: suit.h:295
SUIT_ERR_NOT_SUPPORTED
@ SUIT_ERR_NOT_SUPPORTED
Unsupported features detected.
Definition: suit.h:102
SUIT_DIGEST_SHA384
@ SUIT_DIGEST_SHA384
SHA384.
Definition: suit.h:124
suit_component_check_flag
static bool suit_component_check_flag(suit_component_t *component, uint16_t flag)
Check a component flag.
Definition: suit.h:309
suit_policy_check
int suit_policy_check(suit_manifest_t *manifest)
Check a manifest policy.
suit_storage
Generic storage backend state.
Definition: storage.h:293
suit_manifest_t::component_current
uint8_t component_current
Current component index.
Definition: suit.h:243
suit_manifest_t::len
size_t len
length of the manifest
Definition: suit.h:235
suit_component_t::identifier
suit_param_ref_t identifier
Component identifier.
Definition: suit.h:217
suit_digest_type_t
suit_digest_type_t
SUIT payload digest types.
Definition: suit.h:134
SUIT_ERR_SIGNATURE
@ SUIT_ERR_SIGNATURE
Unable to verify signature.
Definition: suit.h:106
SUIT_COMPONENT_DIGEST
@ SUIT_COMPONENT_DIGEST
Digest component.
Definition: suit.h:150
SUIT_ERR_COND
@ SUIT_ERR_COND
Conditionals evaluate to false.
Definition: suit.h:103
suit_manifest_t::urlbuf
char * urlbuf
Buffer containing the manifest url.
Definition: suit.h:246
SUIT_ERR_STORAGE_EXCEEDED
@ SUIT_ERR_STORAGE_EXCEEDED
Backend out of space.
Definition: suit.h:111
suit_error_t
suit_error_t
SUIT error codes.
Definition: suit.h:98
SUIT_COMPONENT_IDENTIFIER
@ SUIT_COMPONENT_IDENTIFIER
Identifier component.
Definition: suit.h:148
SUIT_ERR_STORAGE
@ SUIT_ERR_STORAGE
Backend returned an error.
Definition: suit.h:110
SUIT_DIGEST_TYPE_CIPHERTEXT
@ SUIT_DIGEST_TYPE_CIPHERTEXT
Ciphertext digest.
Definition: suit.h:137
suit_manifest_t::validated
uint32_t validated
bitfield of validated policies
Definition: suit.h:238
CONFIG_SUIT_COMPONENT_MAX
#define CONFIG_SUIT_COMPONENT_MAX
Maximum number of components supported in a SUIT manifest.
Definition: suit.h:55
SUIT_ERR_STORAGE_UNAVAILABLE
@ SUIT_ERR_STORAGE_UNAVAILABLE
Backend location not available.
Definition: suit.h:112
suit_manifest_t::state
uint32_t state
bitfield holding state information
Definition: suit.h:239
suit_param_ref_t
SUIT parameter reference.
Definition: suit.h:186
SUIT_COMPONENT_SIZE
@ SUIT_COMPONENT_SIZE
Size component.
Definition: suit.h:149
suit_component_t::storage_backend
suit_storage_ref_t * storage_backend
Storage backend used.
Definition: suit.h:215
suit_component_t::state
uint16_t state
Component status flags.
Definition: suit.h:216
suit_component_t::param_uri
suit_param_ref_t param_uri
Payload fetch URI.
Definition: suit.h:221
suit_manifest_t::cose_payload
const uint8_t * cose_payload
ptr to the payload of the COSE sign
Definition: suit.h:236
SUIT_ERR_POLICY_FORBIDDEN
@ SUIT_ERR_POLICY_FORBIDDEN
Denied because of policy mismatch.
Definition: suit.h:108
suit_parse
int suit_parse(suit_manifest_t *manifest, const uint8_t *buf, size_t len)
Parse a manifest.
suit_component_t::param_size
suit_param_ref_t param_size
Payload size.
Definition: suit.h:222
uuid.h
RFC 4122 UUID functions
suit_component_t::param_component_offset
suit_param_ref_t param_component_offset
Component offset inside the device memory.
Definition: suit.h:227
suit_component_t::param_vendor_id
suit_param_ref_t param_vendor_id
Vendor ID.
Definition: suit.h:218
suit_manifest_t::urlbuf_len
size_t urlbuf_len
Length of the manifest url.
Definition: suit.h:247
suit_manifest_t::cose_payload_len
size_t cose_payload_len
length of the COSE payload
Definition: suit.h:237
SUIT_DIGEST_SHA512
@ SUIT_DIGEST_SHA512
SHA512.
Definition: suit.h:125
SUIT_DIGEST_NONE
@ SUIT_DIGEST_NONE
No digest algo supplied.
Definition: suit.h:122
suit_manifest_t::buf
const uint8_t * buf
ptr to the buffer of the manifest
Definition: suit.h:234
SUIT_ERR_DIGEST_MISMATCH
@ SUIT_ERR_DIGEST_MISMATCH
Digest mismatch with COSE and SUIT.
Definition: suit.h:107
suit_component_t::param_digest
suit_param_ref_t param_digest
Payload verification digest.
Definition: suit.h:220
suit_component_t::param_class_id
suit_param_ref_t param_class_id
Class ID.
Definition: suit.h:219
SUIT_DIGEST_TYPE_PREIMAGE
@ SUIT_DIGEST_TYPE_PREIMAGE
Pre-image digest.
Definition: suit.h:138
SUIT_DIGEST_TYPE_INSTALLED
@ SUIT_DIGEST_TYPE_INSTALLED
Installed firmware digest.
Definition: suit.h:136
SUIT_ERR_NO_MEM
@ SUIT_ERR_NO_MEM
Out of memory condition.
Definition: suit.h:109
suit_component_t
SUIT component struct as decoded from the manifest.
Definition: suit.h:214
SUIT_OK
@ SUIT_OK
Manifest parsed and validated.
Definition: suit.h:99
suit_component_name_to_string
int suit_component_name_to_string(const suit_manifest_t *manifest, const suit_component_t *component, char separator, char *buf, size_t buf_len)
Convert a component name to a string.
SUIT_DIGEST_TYPE_RAW
@ SUIT_DIGEST_TYPE_RAW
Raw payload digest.
Definition: suit.h:135
suit_digest_t
suit_digest_t
SUIT payload digest algorithms.
Definition: suit.h:121
SUIT_ERR_INVALID_MANIFEST
@ SUIT_ERR_INVALID_MANIFEST
Unexpected CBOR structure detected.
Definition: suit.h:100
SUIT_ERR_UNSUPPORTED
@ SUIT_ERR_UNSUPPORTED
Unsupported SUIT feature detected.
Definition: suit.h:101
suit_param_ref_t::offset
uint16_t offset
offset to the start of the content
Definition: suit.h:187
suit_manifest_t
SUIT manifest struct.
Definition: suit.h:233
SUIT_COSE_BUF_SIZE
#define SUIT_COSE_BUF_SIZE
Buffer size used for Cose.
Definition: suit.h:48