Skip to content

Commit 4efab44

Browse files
authored
Merge pull request #3 from AndrzejKurek/verification_pubkey_importing
Add verification and public key importing
2 parents 2acf3be + 3a7353e commit 4efab44

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

atecc608a_utils.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "atca_basic.h"
2525

26-
psa_status_t atecc608a_get_serial_number(uint8_t* buffer,
26+
psa_status_t atecc608a_get_serial_number(uint8_t *buffer,
2727
size_t buffer_size,
2828
size_t *buffer_length)
2929
{
@@ -61,3 +61,19 @@ psa_status_t atecc608a_check_config_locked()
6161
}
6262
return status;
6363
}
64+
65+
psa_status_t atecc608a_generate_key(uint16_t slot, uint8_t *pubkey, size_t pubkey_size)
66+
{
67+
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
68+
if (pubkey != NULL && pubkey_size < ATCA_PUB_KEY_SIZE)
69+
{
70+
return PSA_ERROR_BUFFER_TOO_SMALL;
71+
}
72+
73+
ASSERT_SUCCESS_PSA(atecc608a_init());
74+
ASSERT_SUCCESS(atcab_genkey(slot, pubkey));
75+
76+
exit:
77+
atecc608a_deinit();
78+
return status;
79+
}

atecc608a_utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@
5656
#define ASSERT_SUCCESS_PSA(expression) ASSERT_STATUS(expression, PSA_SUCCESS, \
5757
ASSERT_result)
5858

59-
psa_status_t atecc608a_get_serial_number(uint8_t* buffer, size_t buffer_size,
59+
psa_status_t atecc608a_get_serial_number(uint8_t *buffer, size_t buffer_size,
6060
size_t *buffer_length);
6161

6262
psa_status_t atecc608a_check_config_locked();
6363

64+
psa_status_t atecc608a_generate_key(uint16_t slot, uint8_t *pubkey, size_t pubkey_size);
65+
6466
#endif /* ATECC608A_SE_H */

main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ psa_status_t atecc608a_print_serial_number()
105105
return status;
106106
}
107107

108+
psa_status_t atecc608a_print_config_zone()
109+
{
110+
uint8_t config_buffer[ATCA_ECC_CONFIG_SIZE] = {0};
111+
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
112+
ASSERT_SUCCESS_PSA(atecc608a_init());
113+
ASSERT_SUCCESS(atcab_read_config_zone(config_buffer));
114+
atcab_printbin_label("Config zone: ", config_buffer, ATCA_ECC_CONFIG_SIZE);
115+
exit:
116+
atecc608a_deinit();
117+
return status;
118+
}
119+
108120
int main(void)
109121
{
110122
enum {
@@ -131,8 +143,12 @@ int main(void)
131143
static uint8_t pubkey[pubkey_size];
132144
size_t pubkey_len = 0;
133145
psa_key_slot_number_t atecc608a_key_slot_device = 0;
146+
psa_key_slot_number_t atecc608a_public_key_slot = 9;
134147

135148
atecc608a_print_serial_number();
149+
atecc608a_print_config_zone();
150+
ASSERT_SUCCESS_PSA(atecc608a_generate_key(atecc608a_key_slot_device, pubkey, pubkey_size));
151+
atcab_printbin_label("pubKey generated: ", pubkey, ATCA_PUB_KEY_SIZE);
136152

137153
ASSERT_SUCCESS_PSA(atecc608a_hash_sha256(hash_input1,
138154
sizeof(hash_input1) - 1,
@@ -147,17 +163,27 @@ int main(void)
147163
ASSERT_SUCCESS_PSA(psa_crypto_init());
148164

149165
atecc608a_print_locked_zones();
166+
150167
/* Verify that the device has a locked config before doing anything */
151168
ASSERT_SUCCESS_PSA(atecc608a_check_config_locked());
152169

153170
ASSERT_SUCCESS_PSA(atecc608a_drv_info.p_key_management->p_export(
154171
atecc608a_key_slot_device, pubkey, sizeof(pubkey),
155172
&pubkey_len));
156173

174+
ASSERT_SUCCESS_PSA(atecc608a_drv_info.p_key_management->p_import(
175+
atecc608a_public_key_slot,
176+
atecc608a_drv_info.lifetime,
177+
key_type, alg, PSA_KEY_USAGE_VERIFY, pubkey,
178+
pubkey_len));
179+
157180
ASSERT_SUCCESS_PSA(atecc608a_drv_info.p_asym->p_sign(
158181
atecc608a_key_slot_device, alg, hash, sizeof(hash),
159182
signature, sizeof(signature), &signature_length));
160183

184+
ASSERT_SUCCESS_PSA(atecc608a_drv_info.p_asym->p_verify(
185+
atecc608a_public_key_slot, alg, hash, sizeof(hash),
186+
signature, signature_length));
161187
/*
162188
* Import the secure element's public key into a volatile key slot.
163189
*/

0 commit comments

Comments
 (0)