Skip to content

Commit 3df9036

Browse files
committed
fixup! Adjust the driver to the new PSA API. Mostly API changes, with an addition of a check-slot function. Changed the lifetime of the driver to fit the 0x00 - 0xFF range.
1 parent 7db36c2 commit 3df9036

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

atecc608a_se.c

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ psa_status_t atecc608a_deinit()
154154

155155
static psa_status_t atecc608a_export_public_key(psa_drv_se_context_t *drv_context,
156156
psa_key_slot_number_t key,
157-
uint8_t *p_data, size_t data_size,
157+
uint8_t *p_data,
158+
size_t data_size,
158159
size_t *p_data_length)
159160
{
160161
const size_t key_data_len = PSA_KEY_EXPORT_MAX_SIZE(
@@ -184,18 +185,18 @@ static psa_status_t atecc608a_export_public_key(psa_drv_se_context_t *drv_contex
184185
atecc608a_deinit();
185186
return status;
186187
}
187-
static psa_status_t atecc608a_import_public_key(psa_drv_se_context_t *drv_context,
188-
psa_key_slot_number_t key_slot,
189-
psa_key_lifetime_t lifetime,
190-
psa_key_type_t type,
191-
psa_algorithm_t alg,
192-
psa_key_usage_t usage,
193-
const uint8_t *p_data,
194-
size_t data_length,
195-
size_t *bits)
188+
static psa_status_t atecc608a_import_public_key(
189+
psa_drv_se_context_t *drv_context,
190+
psa_key_slot_number_t key_slot,
191+
const psa_key_attributes_t *attributes,
192+
const uint8_t *data,
193+
size_t data_length,
194+
size_t *bits)
196195
{
197196
const uint16_t key_id = key_slot;
198197
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
198+
psa_key_type_t type = psa_get_key_type(attributes);
199+
psa_algorithm_t alg = psa_get_key_algorithm(attributes);
199200

200201
ASSERT_SUCCESS_PSA(is_public_key_slot(key_id));
201202

@@ -217,23 +218,29 @@ static psa_status_t atecc608a_import_public_key(psa_drv_se_context_t *drv_contex
217218

218219
ASSERT_SUCCESS_PSA(atecc608a_init());
219220

220-
ASSERT_SUCCESS(atcab_write_pubkey(key_id, pubkey_for_driver((uint8_t *) p_data)));
221+
ASSERT_SUCCESS(atcab_write_pubkey(key_id, pubkey_for_driver((uint8_t *) data)));
222+
223+
if (bits != NULL) {
224+
/* The 64-byte key is written as 72 bytes. See atcab_write_pubkey() for
225+
* why 72 bytes. */
226+
*bits = PSA_BYTES_TO_BITS(72);
227+
}
228+
221229
exit:
222230
atecc608a_deinit();
223231
return status;
224232
}
225233

226-
static psa_status_t atecc608a_generate_key(psa_drv_se_context_t *drv_context,
227-
psa_key_slot_number_t key_slot,
228-
psa_key_type_t type,
229-
psa_key_usage_t usage,
230-
size_t bits,
231-
uint8_t *p_pubkey_out,
232-
size_t pubkey_out_size,
233-
size_t *p_pubkey_length)
234+
static psa_status_t atecc608a_generate_key(
235+
psa_drv_se_context_t *drv_context,
236+
psa_key_slot_number_t key_slot,
237+
const psa_key_attributes_t *attributes,
238+
uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length)
234239
{
235240
const uint16_t key_id = key_slot;
236241
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
242+
psa_key_type_t type = psa_get_key_type(attributes);
243+
size_t bits = psa_get_key_bits(attributes);
237244

238245
/* The hardware has slots 0-15 */
239246
if (key_slot > 15) {
@@ -248,21 +255,21 @@ static psa_status_t atecc608a_generate_key(psa_drv_se_context_t *drv_context,
248255
return PSA_ERROR_NOT_SUPPORTED;
249256
}
250257

251-
if (p_pubkey_out != NULL && pubkey_out_size < 1 + ATCA_PUB_KEY_SIZE) {
258+
if (pubkey != NULL && pubkey_size < 1 + ATCA_PUB_KEY_SIZE) {
252259
return PSA_ERROR_BUFFER_TOO_SMALL;
253260
}
254261

255262
ASSERT_SUCCESS_PSA(atecc608a_init());
256263

257-
if (p_pubkey_out != NULL) {
258-
ASSERT_SUCCESS(atcab_genkey(key_id, pubkey_for_driver(p_pubkey_out)));
259-
pubkey_for_psa(p_pubkey_out);
264+
if (pubkey != NULL) {
265+
ASSERT_SUCCESS(atcab_genkey(key_id, pubkey_for_driver(pubkey)));
266+
pubkey_for_psa(pubkey);
260267
} else {
261268
ASSERT_SUCCESS(atcab_genkey(key_id, NULL));
262269
}
263270

264-
if (p_pubkey_length != NULL) {
265-
*p_pubkey_length = 1 + ATCA_PUB_KEY_SIZE;
271+
if (pubkey_length != NULL) {
272+
*pubkey_length = 1 + ATCA_PUB_KEY_SIZE;
266273
}
267274

268275
exit:
@@ -386,22 +393,35 @@ psa_status_t atecc608a_read(uint16_t slot, size_t offset, uint8_t *data, size_t
386393
return status;
387394
}
388395

389-
psa_status_t atecc608a_check_slot(psa_drv_se_context_t *drv_context,
390-
const psa_key_attributes_t *attributes,
391-
psa_key_slot_number_t key_slot)
396+
static psa_status_t atecc608a_validate_slot_number(
397+
psa_drv_se_context_t *drv_context,
398+
const psa_key_attributes_t *attributes,
399+
psa_key_creation_method_t method,
400+
psa_key_slot_number_t key_slot)
392401
{
393-
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(attributes->type)) {
402+
psa_key_type_t type = psa_get_key_type(attributes);
403+
if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
394404
if (key_slot <= 15) {
395405
return PSA_SUCCESS;
396406
}
397-
} else if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(attributes->type)) {
407+
} else if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type)) {
398408
if (key_slot >= 8 && key_slot <= 15) {
399409
return PSA_SUCCESS;
400410
}
401411
}
402412
return PSA_ERROR_NOT_SUPPORTED;
403413
}
404414

415+
static psa_status_t atecc608a_allocate_key(
416+
psa_drv_se_context_t *drv_context,
417+
void *persistent_data,
418+
const psa_key_attributes_t *attributes,
419+
psa_key_creation_method_t method,
420+
psa_key_slot_number_t *key_slot)
421+
{
422+
return PSA_SUCCESS;
423+
}
424+
405425
static psa_drv_se_asymmetric_t atecc608a_asymmetric = {
406426
.p_sign = atecc608a_asymmetric_sign,
407427
.p_verify = atecc608a_asymmetric_verify,
@@ -411,11 +431,13 @@ static psa_drv_se_asymmetric_t atecc608a_asymmetric = {
411431

412432
static psa_drv_se_key_management_t atecc608a_key_management = {
413433
/* So far there is no public key import function in the API, so use this instead */
434+
.p_allocate = atecc608a_allocate_key,
435+
.p_validate_slot_number = atecc608a_validate_slot_number,
414436
.p_import = atecc608a_import_public_key,
415437
.p_generate = atecc608a_generate_key,
416438
.p_destroy = 0,
439+
.p_export = 0,
417440
.p_export_public = atecc608a_export_public_key,
418-
.p_check_slot = atecc608a_check_slot,
419441
};
420442

421443
psa_drv_se_t atecc608a_drv_info = {

0 commit comments

Comments
 (0)