-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathexample_optiga_util_protected_update.c
495 lines (436 loc) · 17.9 KB
/
example_optiga_util_protected_update.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/**
* SPDX-FileCopyrightText: 2021-2024 Infineon Technologies AG
* SPDX-License-Identifier: MIT
*
* \author Infineon Technologies AG
*
* \file example_optiga_util_protected_update.c
*
* \brief This file provides the example for util protected update operation using #optiga_util_protected_update_start,
* #optiga_util_protected_update_continue & #optiga_util_protected_update_final.
*
* \ingroup grOptigaExamples
*
* @{
*/
#include "protected_update_data_set/example_optiga_util_protected_update.h"
#include "optiga_example.h"
#include "optiga_util.h"
#ifndef OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
extern void example_optiga_init(void);
extern void example_optiga_deinit(void);
#endif
/**
* Callback when optiga_util_xxxx operation is completed asynchronously
*/
static volatile optiga_lib_status_t optiga_lib_status;
// lint --e{818} suppress "argument "context" is not used in the sample provided"
static void optiga_util_callback(void *context, optiga_lib_status_t return_status) {
optiga_lib_status = return_status;
if (NULL != context) {
// callback to upper layer here
}
}
/**
* ECC-256 Trust Anchor
*/
const uint8_t trust_anchor[] = {
0x30, 0x82, 0x02, 0x58, 0x30, 0x82, 0x01, 0xFF, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x2F,
0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30, 0x56, 0x31, 0x0B,
0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x4E, 0x31, 0x0D, 0x30, 0x0B, 0x06,
0x03, 0x55, 0x04, 0x0A, 0x0C, 0x04, 0x49, 0x46, 0x49, 0x4E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03,
0x55, 0x04, 0x0B, 0x0C, 0x03, 0x43, 0x43, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
0x03, 0x0C, 0x0A, 0x49, 0x6E, 0x74, 0x43, 0x41, 0x20, 0x50, 0x32, 0x35, 0x36, 0x31, 0x15, 0x30,
0x13, 0x06, 0x03, 0x55, 0x04, 0x2E, 0x13, 0x0C, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x41, 0x6E,
0x63, 0x68, 0x6F, 0x72, 0x30, 0x1E, 0x17, 0x0D, 0x31, 0x36, 0x30, 0x35, 0x32, 0x36, 0x30, 0x38,
0x30, 0x31, 0x33, 0x37, 0x5A, 0x17, 0x0D, 0x31, 0x37, 0x30, 0x36, 0x30, 0x35, 0x30, 0x38, 0x30,
0x31, 0x33, 0x37, 0x5A, 0x30, 0x5A, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
0x02, 0x49, 0x4E, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x04, 0x49, 0x46,
0x49, 0x4E, 0x31, 0x0C, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x43, 0x43, 0x53,
0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0E, 0x65, 0x6E, 0x64, 0x45, 0x6E,
0x74, 0x69, 0x74, 0x79, 0x20, 0x50, 0x32, 0x35, 0x36, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55,
0x04, 0x2E, 0x13, 0x0C, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x41, 0x6E, 0x63, 0x68, 0x6F, 0x72,
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A,
0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x19, 0xB5, 0xB2, 0x17, 0x0D,
0xF5, 0x98, 0x5E, 0xD4, 0xD9, 0x72, 0x16, 0xEF, 0x61, 0x39, 0x3F, 0x14, 0x58, 0xAF, 0x5C, 0x02,
0x78, 0x07, 0xCA, 0x48, 0x8F, 0x2A, 0xE3, 0x90, 0xB9, 0x03, 0xA1, 0xD2, 0x46, 0x20, 0x09, 0x21,
0x52, 0x98, 0xDC, 0x8E, 0x88, 0x84, 0x67, 0x8E, 0x83, 0xD1, 0xDE, 0x0F, 0x1C, 0xE5, 0x19, 0x1D,
0x0C, 0x74, 0x60, 0x41, 0x58, 0x5B, 0x36, 0x55, 0xF8, 0x3D, 0xAB, 0xA3, 0x81, 0xB9, 0x30, 0x81,
0xB6, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1D, 0x06, 0x03,
0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xB5, 0x97, 0xFD, 0xAB, 0x36, 0x1A, 0xA0, 0xA2, 0x23,
0xA7, 0x68, 0x25, 0x25, 0xFB, 0x82, 0x55, 0xD0, 0x4F, 0xCF, 0xB8, 0x30, 0x7A, 0x06, 0x03, 0x55,
0x1D, 0x23, 0x04, 0x73, 0x30, 0x71, 0x80, 0x14, 0x1A, 0xBB, 0x56, 0x44, 0x65, 0x8C, 0x4D, 0x4F,
0xCD, 0x29, 0xA2, 0x3F, 0x4C, 0xC6, 0xBC, 0xA8, 0x8B, 0xA4, 0x0A, 0xDA, 0xA1, 0x56, 0xA4, 0x54,
0x30, 0x52, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x4E, 0x31,
0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x04, 0x49, 0x46, 0x49, 0x4E, 0x31, 0x0C,
0x30, 0x0A, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x03, 0x43, 0x43, 0x53, 0x31, 0x0F, 0x30, 0x0D,
0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x06, 0x52, 0x6F, 0x6F, 0x74, 0x43, 0x41, 0x31, 0x15, 0x30,
0x13, 0x06, 0x03, 0x55, 0x04, 0x2E, 0x13, 0x0C, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20, 0x41, 0x6E,
0x63, 0x68, 0x6F, 0x72, 0x82, 0x01, 0x2E, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x1D, 0x0F, 0x01, 0x01,
0xFF, 0x04, 0x04, 0x03, 0x02, 0x00, 0x81, 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x68, 0xFD, 0x9C, 0x8F, 0x35, 0x33,
0x0B, 0xB8, 0x32, 0x8C, 0xAF, 0x1C, 0x81, 0x4E, 0x41, 0x29, 0x26, 0xCB, 0xB7, 0x10, 0xA0, 0x75,
0xFC, 0x89, 0xAE, 0xC5, 0x1D, 0x92, 0x8E, 0x72, 0xEF, 0x5C, 0x02, 0x20, 0x7D, 0xC1, 0xEB, 0x58,
0x21, 0xF1, 0xFD, 0xFB, 0x5E, 0xD7, 0xDE, 0x06, 0xC9, 0xB4, 0xFF, 0x59, 0x8D, 0x37, 0x8C, 0x7A,
0x48, 0xCD, 0x2D, 0x99, 0x74, 0x77, 0x58, 0x9D, 0x95, 0x51, 0x8F, 0x5D};
/**
* Metadata for reset version tag :
* Version Tag = 00.
*/
const uint8_t reset_version_tag_metadata[] = {0x20, 0x04, 0xC1, 0x02, 0x00, 0x00};
/**
* Metadata for target OID :
* Change access condition = Integrity protected using 0xE0E3.
*/
const uint8_t target_oid_metadata[] = {
0x20,
0x05,
0xD0,
0x03,
0x21,
0xE0,
0xE3,
};
/**
* Metadata for target OID with confidentiality :
* Change access condition = Integrity protected using 0xE0E3 & Confidentiality using 0xF1D1
*/
const uint8_t target_oid_metadata_with_confidentiality[] = {
0x20,
0x09,
// 0xC1, 0x02, 0x00, 0x00,
0xD0,
0x07,
0x21,
0xE0,
0xE3,
0xFD,
0x20,
0xF1,
0xD1};
/**
* Metadata for secure update of target OID metadata with confidentiality :
* Change access condition = Integrity protected using 0xE0E3 & Confidentiality using 0xF1D1
*/
const uint8_t target_oid_metadata_for_secure_metadata_update[] =
{0x20, 0x0C, 0xD8, 0x07, 0x21, 0xE0, 0xE3, 0xFD, 0x20, 0xF1, 0xD1, 0xF0, 0x01, 0x01};
/**
* Metadata for Trust Anchor :
* Execute access condition = Always
* Data object type = Trust Anchor
*/
uint8_t trust_anchor_metadata[] = {0x20, 0x06, 0xD3, 0x01, 0x00, 0xE8, 0x01, 0x11};
/**
* Metadata for target key OID :
* Change access condition = Integrity protected using 0xE0E3
* Execute access condition = Always
*/
const uint8_t target_key_oid_metadata[] =
{0x20, 0x0C, 0xC1, 0x02, 0x00, 0x00, 0xD0, 0x03, 0x21, 0xE0, 0xE3, 0xD3, 0x01, 0x00};
/**
* Metadata for shared secret OID :
* Execute access condition = Always
* Data object type = Protected updated secret
*/
uint8_t confidentiality_oid_metadata[] = {0x20, 0x06, 0xD3, 0x01, 0x00, 0xE8, 0x01, 0x23};
/**
* Shared secret data
*/
const unsigned char shared_secret[] = {
0x49, 0xC9, 0xF4, 0x92, 0xA9, 0x92, 0xF6, 0xD4, 0xC5, 0x4F, 0x5B, 0x12, 0xC5, 0x7E, 0xDB, 0x27,
0xCE, 0xD2, 0x24, 0x04, 0x8F, 0x25, 0x48, 0x2A, 0xA1, 0x49, 0xC9, 0xF4, 0x92, 0xA9, 0x92, 0xF6,
0x49, 0xC9, 0xF4, 0x92, 0xA9, 0x92, 0xF6, 0xD4, 0xC5, 0x4F, 0x5B, 0x12, 0xC5, 0x7E, 0xDB, 0x27,
0xCE, 0xD2, 0x24, 0x04, 0x8F, 0x25, 0x48, 0x2A, 0xA1, 0x49, 0xC9, 0xF4, 0x92, 0xA9, 0x92, 0xF6};
const optiga_protected_update_data_configuration_t optiga_protected_update_data_set[] = {
#ifdef INTEGRITY_PROTECTED
{0xE0E1,
target_oid_metadata,
sizeof(target_oid_metadata),
&data_integrity_configuration,
"Protected Update - Integrity"},
#endif
#ifdef CONFIDENTIALITY_PROTECTED
{0xE0E1,
target_oid_metadata_with_confidentiality,
sizeof(target_oid_metadata_with_confidentiality),
&data_confidentiality_configuration,
"Protected Update - Confidentiality"},
#endif
#ifdef AES_KEY_UPDATE
{0xE200,
target_oid_metadata,
sizeof(target_oid_metadata),
&data_aes_key_configuration,
"Protected Update - AES Key"},
#endif
#ifdef ECC_KEY_UPDATE
{0xE0F1,
target_key_oid_metadata,
sizeof(target_key_oid_metadata),
&data_ecc_key_configuration,
"Protected Update - ECC Key"},
#endif
#ifdef METADATA_UPDATE
{0xE0E2,
target_oid_metadata_for_secure_metadata_update,
sizeof(target_oid_metadata_for_secure_metadata_update),
&metadata_update_configuration,
"Protected Update - Metadata"},
#endif
#ifdef RSA_KEY_UPDATE
{0xE0FC,
target_key_oid_metadata,
sizeof(target_key_oid_metadata),
&data_rsa_key_configuration,
"Protected Update - RSA Key"},
#endif
};
/**
* Local functions prototype
*/
static optiga_lib_status_t
write_metadata(optiga_util_t *me, uint16_t oid, uint8_t *metadata, uint8_t metadata_length) {
optiga_lib_status_t return_status = OPTIGA_LIB_SUCCESS;
do {
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_util_write_metadata(me, oid, metadata, metadata_length);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
while (OPTIGA_LIB_BUSY == optiga_lib_status) {
// Wait until the optiga_util_write_metadata operation is completed
}
if (OPTIGA_LIB_SUCCESS != optiga_lib_status) {
// writing metadata to a data object failed.
return_status = optiga_lib_status;
break;
}
} while (FALSE);
return (return_status);
}
// Precondition 2
static optiga_lib_status_t
write_confidentiality_oid(optiga_util_t *me, uint16_t confidentiality_oid) {
optiga_lib_status_t return_status = OPTIGA_UTIL_ERROR;
do {
/**
* Precondition :
* Metadata for 0xF1D1 :
* Execute access condition = Always
* Data object type = Protected updated secret
*/
return_status = write_metadata(
me,
confidentiality_oid,
confidentiality_oid_metadata,
sizeof(confidentiality_oid_metadata)
);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
/**
* Precondition :
* Write shared secret in OID 0xF1D1
*/
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_util_write_data(
me,
confidentiality_oid,
OPTIGA_UTIL_ERASE_AND_WRITE,
0,
shared_secret,
sizeof(shared_secret)
);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
while (OPTIGA_LIB_BUSY == optiga_lib_status) {
// Wait until the optiga_util_write_data operation is completed
}
if (OPTIGA_LIB_SUCCESS != optiga_lib_status) {
// writing data to a data object failed.
return_status = optiga_lib_status;
break;
}
} while (FALSE);
return (return_status);
}
// Precondition 1
static optiga_lib_status_t write_trust_anchor(optiga_util_t *me, uint16_t trust_anchor_oid) {
optiga_lib_status_t return_status = OPTIGA_LIB_SUCCESS;
do {
/**
* Precondition :
* Update Metadata for 0xE0E3 :
* Execute access condition = Always
* Data object type = Trust Anchor
*/
return_status = write_metadata(
me,
trust_anchor_oid,
trust_anchor_metadata,
sizeof(trust_anchor_metadata)
);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_util_write_data(
me,
trust_anchor_oid,
OPTIGA_UTIL_ERASE_AND_WRITE,
0,
trust_anchor,
sizeof(trust_anchor)
);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
while (OPTIGA_LIB_BUSY == optiga_lib_status) {
// Wait until the optiga_util_write_data operation is completed
}
if (OPTIGA_LIB_SUCCESS != optiga_lib_status) {
// writing data to a data object failed.
return_status = optiga_lib_status;
break;
}
} while (FALSE);
return (return_status);
}
void example_optiga_util_protected_update(void) {
optiga_lib_status_t return_status = OPTIGA_LIB_SUCCESS;
optiga_util_t *me = NULL;
uint32_t time_taken = 0;
uint16_t trust_anchor_oid = 0xE0E3;
uint16_t confidentiality_oid = 0xF1D1;
uint16_t data_config = 0;
do {
#ifndef OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
/**
* Open the application on OPTIGA which is a precondition to perform any other operations
* using optiga_util_open_application
*/
example_optiga_init();
#endif // OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
OPTIGA_EXAMPLE_LOG_MESSAGE(__FUNCTION__);
/**
* Create OPTIGA util Instance
*
*/
me = optiga_util_create(0, optiga_util_callback, NULL);
if (NULL == me) {
break;
}
/**
* Precondition 1 :
* Update the metadata and trust anchor in OID 0xE0E3
*/
return_status = write_trust_anchor(me, trust_anchor_oid);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
/**
* Precondition 2 :
* Update the metadata and secret for confidentiality in OID 0xF1D1
*/
return_status = write_confidentiality_oid(me, confidentiality_oid);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
for (data_config = 0;
data_config < sizeof(optiga_protected_update_data_set)
/ sizeof(optiga_protected_update_data_configuration_t);
data_config++)
{
/**
* Precondition 3 :
* Update the metadata of target OID
*/
return_status = write_metadata(
me,
optiga_protected_update_data_set[data_config].target_oid,
(uint8_t *)optiga_protected_update_data_set[data_config].target_oid_metadata,
(uint8_t)optiga_protected_update_data_set[data_config].target_oid_metadata_length
);
if (OPTIGA_LIB_SUCCESS != return_status)
{
break;
}
START_PERFORMANCE_MEASUREMENT(time_taken);
OPTIGA_EXAMPLE_LOG_MESSAGE(
optiga_protected_update_data_set[data_config].set_prot_example_string
);
/**
* Send the manifest using optiga_util_protected_update_start
*/
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_util_protected_update_start(
me,
optiga_protected_update_data_set[data_config].data_config->manifest_version,
optiga_protected_update_data_set[data_config].data_config->manifest_data,
optiga_protected_update_data_set[data_config].data_config->manifest_length
);
WAIT_AND_CHECK_STATUS(return_status, optiga_lib_status);
if (NULL
!= optiga_protected_update_data_set[data_config]
.data_config->continue_fragment_data) {
/**
* Send the first fragment using optiga_util_protected_update_continue
*/
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_util_protected_update_continue(
me,
optiga_protected_update_data_set[data_config]
.data_config->continue_fragment_data,
optiga_protected_update_data_set[data_config]
.data_config->continue_fragment_length
);
WAIT_AND_CHECK_STATUS(return_status, optiga_lib_status);
}
/**
* Send the last fragment using optiga_util_protected_update_final
*/
optiga_lib_status = OPTIGA_LIB_BUSY;
return_status = optiga_util_protected_update_final(
me,
optiga_protected_update_data_set[data_config].data_config->final_fragment_data,
optiga_protected_update_data_set[data_config].data_config->final_fragment_length
);
WAIT_AND_CHECK_STATUS(return_status, optiga_lib_status);
READ_PERFORMANCE_MEASUREMENT(time_taken);
OPTIGA_EXAMPLE_PROTECTED_UPDATE_PERFORMANCE_VALUE(time_taken);
/**
* Revert the version tag of metadata configuration to re-run the protected update examples
* Update the metadata of target OID for version tag to be 00
*/
return_status = write_metadata(
me,
optiga_protected_update_data_set[data_config].target_oid,
(uint8_t *)reset_version_tag_metadata,
(uint8_t)sizeof(reset_version_tag_metadata)
);
if (OPTIGA_LIB_SUCCESS != return_status) {
break;
}
}
} while (FALSE);
OPTIGA_EXAMPLE_LOG_STATUS(return_status);
#ifndef OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
/**
* Close the application on OPTIGA after all the operations are executed
* using optiga_util_close_application
*/
example_optiga_deinit();
#endif // OPTIGA_INIT_DEINIT_DONE_EXCLUSIVELY
if (me) {
// Destroy the instance after the completion of usecase if not required.
return_status = optiga_util_destroy(me);
if (OPTIGA_LIB_SUCCESS != return_status) {
// lint --e{774} suppress This is a generic macro
OPTIGA_EXAMPLE_LOG_STATUS(return_status);
}
}
}
/**
* @}
*/