Skip to content

Commit 1d61177

Browse files
author
Andrzej Kurek
committed
Move public key format translations to dedicated functions
1 parent 86529df commit 1d61177

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

atecc608a_se.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ psa_status_t atecc608a_to_psa_error(ATCA_STATUS ret)
122122
}
123123
}
124124

125+
/* The driver works with pubkeys as concatenated x and y values, and the PSA
126+
* format for pubkeys is 0x04 + x + y. Always use a pubkey buffer in PSA
127+
* format, with enough space for the PSA format. To translate this buffer for
128+
* use with cryptoauthlib, use pubkey_for_driver(). To ensure the buffer is in
129+
* valid PSA format after cryptoauthlib operations, call pubkey_for_psa(). */
130+
static uint8_t *pubkey_for_driver(uint8_t *data)
131+
{
132+
return &data[1];
133+
}
134+
135+
static void pubkey_for_psa(uint8_t *data)
136+
{
137+
data[0] = 0x4;
138+
}
139+
125140
static psa_status_t is_public_key_slot(uint16_t key_slot)
126141
{
127142
/* Keys 8 to 15 can store public keys. Slots 1-7 are too small. */
@@ -156,11 +171,9 @@ static psa_status_t atecc608a_export_public_key(psa_key_slot_number_t key,
156171

157172
ASSERT_SUCCESS_PSA(atecc608a_init());
158173

159-
/* atcab_get_pubkey returns concatenated x and y values, and the desired
160-
* format is 0x04 + x + y. Start at &p_data[1] and add a 0x04 at p_data[0]. */
161-
ASSERT_SUCCESS(atcab_get_pubkey(slot, &p_data[1]));
174+
ASSERT_SUCCESS(atcab_get_pubkey(slot, pubkey_for_driver(p_data)));
175+
pubkey_for_psa(p_data);
162176

163-
p_data[0] = 4;
164177
*p_data_length = key_data_len;
165178

166179
#ifdef DEBUG_PRINT
@@ -206,9 +219,7 @@ static psa_status_t atecc608a_import_public_key(psa_key_slot_number_t key_slot,
206219

207220
ASSERT_SUCCESS_PSA(atecc608a_init());
208221

209-
/* PSA public key format is {0x04, X, Y}, and the cryptoauthlib accepts
210-
* raw {X,Y}. */
211-
ASSERT_SUCCESS(atcab_write_pubkey(key_id, p_data + 1));
222+
ASSERT_SUCCESS(atcab_write_pubkey(key_id, pubkey_for_driver(p_data)));
212223
exit:
213224
atecc608a_deinit();
214225
return status;
@@ -252,11 +263,8 @@ static psa_status_t atecc608a_generate_key(psa_key_slot_number_t key_slot,
252263

253264
if (p_pubkey_out != NULL)
254265
{
255-
/* atcab_genkey returns concatenated x and y values, and the desired
256-
* format is 0x04 + x + y. Start at &p_pubkey_out[1] and add a 0x04
257-
* at p_pubkey_out[0]. */
258-
ASSERT_SUCCESS(atcab_genkey(key_id, &p_pubkey_out[1]));
259-
p_pubkey_out[0] = 4;
266+
ASSERT_SUCCESS(atcab_genkey(key_id, pubkey_for_driver(p_pubkey_out)));
267+
pubkey_for_psa(p_pubkey_out);
260268
}
261269
else
262270
{

0 commit comments

Comments
 (0)