Skip to content

Commit 9e868b2

Browse files
author
Andrzej Kurek
committed
Change the format of a public key exported when generating a private key
Add a '0x04' prefix before the x and y parts of the key to be compliant with the PSA format.
1 parent 2898067 commit 9e868b2

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

atecc608a_se.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ static psa_status_t atecc608a_export_public_key(psa_key_slot_number_t key,
142142
uint8_t *p_data, size_t data_size,
143143
size_t *p_data_length)
144144
{
145-
const size_t key_data_len = 65;
145+
const size_t key_data_len = PSA_KEY_EXPORT_MAX_SIZE(
146+
PSA_KEY_TYPE_ECC_PUBLIC_KEY(
147+
PSA_ECC_CURVE_SECP256R1),
148+
256);
146149
const uint16_t slot = key;
147150
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
148151

@@ -223,6 +226,10 @@ static psa_status_t atecc608a_generate_key(psa_key_slot_number_t key_slot,
223226
{
224227
const uint16_t key_id = key_slot;
225228
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
229+
const size_t key_data_len = PSA_KEY_EXPORT_MAX_SIZE(
230+
PSA_KEY_TYPE_ECC_PUBLIC_KEY(
231+
PSA_ECC_CURVE_SECP256R1),
232+
256);
226233

227234
/* The hardware has slots 0-15 */
228235
if (key_slot > 15)
@@ -240,17 +247,29 @@ static psa_status_t atecc608a_generate_key(psa_key_slot_number_t key_slot,
240247
return PSA_ERROR_NOT_SUPPORTED;
241248
}
242249

243-
if (p_pubkey_out != NULL && pubkey_out_size < ATCA_PUB_KEY_SIZE)
250+
if (p_pubkey_out != NULL && pubkey_out_size < key_data_len)
244251
{
245252
return PSA_ERROR_BUFFER_TOO_SMALL;
246253
}
247254

248255
ASSERT_SUCCESS_PSA(atecc608a_init());
249-
ASSERT_SUCCESS(atcab_genkey(key_id, p_pubkey_out));
256+
257+
if (p_pubkey_out != NULL)
258+
{
259+
/* atcab_genkey returns concatenated x and y values, and the desired
260+
* format is 0x04 + x + y. Start at &p_pubkey_out[1] and add a 0x04
261+
* at p_pubkey_out[0]. */
262+
ASSERT_SUCCESS(atcab_genkey(key_id, &p_pubkey_out[1]));
263+
p_pubkey_out[0] = 4;
264+
}
265+
else
266+
{
267+
ASSERT_SUCCESS(atcab_genkey(key_id, NULL));
268+
}
250269

251270
if (p_pubkey_length != NULL)
252271
{
253-
*p_pubkey_length = ATCA_PUB_KEY_SIZE;
272+
*p_pubkey_length = key_data_len;
254273
}
255274

256275
exit:

0 commit comments

Comments
 (0)