@@ -122,6 +122,21 @@ psa_status_t atecc608a_to_psa_error(ATCA_STATUS ret)
122
122
}
123
123
}
124
124
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
+
125
140
static psa_status_t is_public_key_slot (uint16_t key_slot )
126
141
{
127
142
/* 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,
156
171
157
172
ASSERT_SUCCESS_PSA (atecc608a_init ());
158
173
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 );
162
176
163
- p_data [0 ] = 4 ;
164
177
* p_data_length = key_data_len ;
165
178
166
179
#ifdef DEBUG_PRINT
@@ -206,9 +219,7 @@ static psa_status_t atecc608a_import_public_key(psa_key_slot_number_t key_slot,
206
219
207
220
ASSERT_SUCCESS_PSA (atecc608a_init ());
208
221
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 )));
212
223
exit :
213
224
atecc608a_deinit ();
214
225
return status ;
@@ -252,11 +263,8 @@ static psa_status_t atecc608a_generate_key(psa_key_slot_number_t key_slot,
252
263
253
264
if (p_pubkey_out != NULL )
254
265
{
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 );
260
268
}
261
269
else
262
270
{
0 commit comments