suit.h
1 /*
2  * SPDX-FileCopyrightText: 2019 Koen Zandberg
3  * SPDX-FileCopyrightText: 2019 Kaspar Schleiser <kaspar@schleiser.de>
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
30 #include <stddef.h>
31 #include <stdint.h>
32 
33 #include "cose/sign.h"
34 #include "nanocbor/nanocbor.h"
35 #include "uuid.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
44 #ifndef SUIT_COSE_BUF_SIZE
45 #define SUIT_COSE_BUF_SIZE (180U)
46 #endif
47 
51 #ifndef CONFIG_SUIT_COMPONENT_MAX
52 #define CONFIG_SUIT_COMPONENT_MAX (1U)
53 #endif
54 
58 #ifndef CONFIG_SUIT_COMPONENT_MAX_NAME_LEN
59 #define CONFIG_SUIT_COMPONENT_MAX_NAME_LEN (32U)
60 #endif
61 
68 #define SUIT_VERSION (1)
69 
79 #define SUIT_STATE_HAVE_COMPONENTS (1 << 0)
80 
84 #define SUIT_STATE_COSE_AUTHENTICATED (1 << 1)
85 
89 #define SUIT_STATE_FULLY_AUTHENTICATED (1 << 2)
95 typedef enum {
96  SUIT_OK = 0,
110 } suit_error_t;
111 
118 typedef enum {
123 } suit_digest_t;
124 
131 typedef enum {
137 
144 enum {
148 };
149 
154 typedef enum {
155  SUIT_PARAMETER_VENDOR_IDENTIFIER = 1,
156  SUIT_PARAMETER_CLASS_IDENTIFIER = 2,
157  SUIT_PARAMETER_IMAGE_DIGEST = 3,
158  SUIT_PARAMETER_USE_BEFORE = 4,
159  SUIT_PARAMETER_COMPONENT_OFFSET = 5,
160  SUIT_PARAMETER_STRICT_ORDER = 12,
161  SUIT_PARAMETER_SOFT_FAILURE = 13,
162  SUIT_PARAMETER_IMAGE_SIZE = 14,
163  SUIT_PARAMETER_ENCRYPTION_INFO = 18,
164  SUIT_PARAMETER_COMPRESSION_INFO = 19,
165  SUIT_PARAMETER_UNPACK_INFO = 20,
166  SUIT_PARAMETER_URI = 21,
167  SUIT_PARAMETER_SOURCE_COMPONENT = 22,
168  SUIT_PARAMETER_RUN_ARGS = 23,
169  SUIT_PARAMETER_DEVICE_IDENTIFIER = 24,
170  SUIT_PARAMETER_MINIMUM_BATTERY = 26,
171  SUIT_PARAMETER_UPDATE_PRIORITY = 27,
172  SUIT_PARAMETER_VERSION = 28,
173  SUIT_PARAMETER_WAIT_INFO = 29,
174  SUIT_PARAMETER_URI_LIST = 30,
175 } suit_parameter_t;
183 typedef struct {
184  uint16_t offset;
186 
193 #define SUIT_COMPONENT_STATE_FETCHED (1 << 0)
194 #define SUIT_COMPONENT_STATE_FETCH_FAILED (1 << 1)
195 #define SUIT_COMPONENT_STATE_VERIFIED (1 << 2)
196 #define SUIT_COMPONENT_STATE_INSTALLED (1 << 3)
197 #define SUIT_COMPONENT_STATE_FINALIZED (1 << 4)
205 typedef struct suit_storage suit_storage_ref_t;
206 
212 typedef struct {
214  uint16_t state;
227 
231 typedef struct {
232  const uint8_t *buf;
233  size_t len;
234  const uint8_t *cose_payload;
236  uint32_t validated;
237  uint32_t state;
240  unsigned components_len;
243  uint8_t validation_buf[SUIT_COSE_BUF_SIZE];
244  char *urlbuf;
245  size_t urlbuf_len;
246  uint32_t seq_number;
248 
254 #define SUIT_MANIFEST_COMPONENT_ALL (UINT8_MAX)
255 
261 #define SUIT_MANIFEST_COMPONENT_NONE (SUIT_MANIFEST_COMPONENT_ALL - 1)
262 
276 int suit_parse(suit_manifest_t *manifest, const uint8_t *buf, size_t len);
277 
287 
294 static inline void suit_component_set_flag(suit_component_t *component,
295  uint16_t flag)
296 {
297  component->state |= flag;
298 }
299 
308 static inline bool suit_component_check_flag(suit_component_t *component,
309  uint16_t flag)
310 {
311  return (component->state & flag);
312 }
313 
323  const suit_component_t *component,
324  char separator, char *buf, size_t buf_len);
325 
334 bool suit_get_public_key(uint8_t idx, cose_key_t *key);
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
#define SUIT_COSE_BUF_SIZE
Buffer size used for Cose.
Definition: suit.h:45
#define CONFIG_SUIT_COMPONENT_MAX
Maximum number of components supported in a SUIT manifest.
Definition: suit.h:52
suit_error_t
SUIT error codes.
Definition: suit.h:95
suit_digest_type_t
SUIT payload digest types.
Definition: suit.h:131
int suit_parse(suit_manifest_t *manifest, const uint8_t *buf, size_t len)
Parse a manifest.
static void suit_component_set_flag(suit_component_t *component, uint16_t flag)
Set a component flag.
Definition: suit.h:294
bool suit_get_public_key(uint8_t idx, cose_key_t *key)
Get public key accepted by SUIT.
suit_digest_t
SUIT payload digest algorithms.
Definition: suit.h:118
int suit_policy_check(suit_manifest_t *manifest)
Check a manifest policy.
static bool suit_component_check_flag(suit_component_t *component, uint16_t flag)
Check a component flag.
Definition: suit.h:308
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_COMPONENT_IDENTIFIER
Identifier component.
Definition: suit.h:145
@ SUIT_COMPONENT_SIZE
Size component.
Definition: suit.h:146
@ SUIT_COMPONENT_DIGEST
Digest component.
Definition: suit.h:147
@ SUIT_ERR_INVALID_MANIFEST
Unexpected CBOR structure detected.
Definition: suit.h:97
@ SUIT_ERR_NO_MEM
Out of memory condition.
Definition: suit.h:106
@ SUIT_ERR_STORAGE_UNAVAILABLE
Backend location not available.
Definition: suit.h:109
@ SUIT_ERR_COND
Conditionals evaluate to false.
Definition: suit.h:100
@ SUIT_ERR_UNSUPPORTED
Unsupported SUIT feature detected.
Definition: suit.h:98
@ SUIT_ERR_DIGEST_MISMATCH
Digest mismatch with COSE and SUIT.
Definition: suit.h:104
@ SUIT_ERR_STORAGE
Backend returned an error.
Definition: suit.h:107
@ SUIT_OK
Manifest parsed and validated.
Definition: suit.h:96
@ SUIT_ERR_NOT_SUPPORTED
Unsupported features detected.
Definition: suit.h:99
@ SUIT_ERR_STORAGE_EXCEEDED
Backend out of space.
Definition: suit.h:108
@ SUIT_ERR_SIGNATURE
Unable to verify signature.
Definition: suit.h:103
@ SUIT_ERR_POLICY_FORBIDDEN
Denied because of policy mismatch.
Definition: suit.h:105
@ SUIT_ERR_SEQUENCE_NUMBER
Sequence number less or equal to current sequence number.
Definition: suit.h:101
@ SUIT_DIGEST_TYPE_CIPHERTEXT
Ciphertext digest.
Definition: suit.h:134
@ SUIT_DIGEST_TYPE_INSTALLED
Installed firmware digest.
Definition: suit.h:133
@ SUIT_DIGEST_TYPE_PREIMAGE
Pre-image digest.
Definition: suit.h:135
@ SUIT_DIGEST_TYPE_RAW
Raw payload digest.
Definition: suit.h:132
@ SUIT_DIGEST_SHA256
SHA256.
Definition: suit.h:120
@ SUIT_DIGEST_NONE
No digest algo supplied.
Definition: suit.h:119
@ SUIT_DIGEST_SHA512
SHA512.
Definition: suit.h:122
@ SUIT_DIGEST_SHA384
SHA384.
Definition: suit.h:121
SUIT component struct as decoded from the manifest.
Definition: suit.h:212
suit_param_ref_t param_size
Payload size.
Definition: suit.h:220
suit_param_ref_t param_class_id
Class ID.
Definition: suit.h:217
suit_param_ref_t param_uri
Payload fetch URI.
Definition: suit.h:219
suit_param_ref_t param_digest
Payload verification digest.
Definition: suit.h:218
suit_storage_ref_t * storage_backend
Storage backend used.
Definition: suit.h:213
suit_param_ref_t param_vendor_id
Vendor ID.
Definition: suit.h:216
suit_param_ref_t identifier
Component identifier.
Definition: suit.h:215
uint16_t state
Component status flags.
Definition: suit.h:214
suit_param_ref_t param_component_offset
Component offset inside the device memory.
Definition: suit.h:225
SUIT manifest struct.
Definition: suit.h:231
size_t len
length of the manifest
Definition: suit.h:233
char * urlbuf
Buffer containing the manifest url.
Definition: suit.h:244
uint32_t state
bitfield holding state information
Definition: suit.h:237
const uint8_t * buf
ptr to the buffer of the manifest
Definition: suit.h:232
uint32_t validated
bitfield of validated policies
Definition: suit.h:236
size_t cose_payload_len
length of the COSE payload
Definition: suit.h:235
const uint8_t * cose_payload
ptr to the payload of the COSE sign
Definition: suit.h:234
size_t urlbuf_len
Length of the manifest url.
Definition: suit.h:245
uint32_t seq_number
Set sequence number.
Definition: suit.h:246
uint8_t component_current
Current component index.
Definition: suit.h:241
unsigned components_len
Current number of components.
Definition: suit.h:240
SUIT parameter reference.
Definition: suit.h:183
uint16_t offset
offset to the start of the content
Definition: suit.h:184
Generic storage backend state.
Definition: storage.h:290
RFC 4122 UUID functions