Skip to content

Commit f3320a9

Browse files
committed
Update to PSA Crypto from Mbed TLS 2.25.0
1 parent e9b4f57 commit f3320a9

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

getting-started/main.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static void import_a_key(const uint8_t *key, size_t key_len)
109109
{
110110
psa_status_t status;
111111
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
112-
psa_key_handle_t handle;
112+
psa_key_id_t id;
113113

114114
printf("Import an AES key...\t");
115115
fflush(stdout);
@@ -121,7 +121,7 @@ static void import_a_key(const uint8_t *key, size_t key_len)
121121
psa_set_key_bits(&attributes, 128);
122122

123123
/* Import the key */
124-
status = psa_import_key(&attributes, key, key_len, &handle);
124+
status = psa_import_key(&attributes, key, key_len, &id);
125125
if (status != PSA_SUCCESS) {
126126
printf("Failed to import key\n");
127127
return;
@@ -132,7 +132,7 @@ static void import_a_key(const uint8_t *key, size_t key_len)
132132
psa_reset_key_attributes(&attributes);
133133

134134
/* Destroy the key */
135-
psa_destroy_key(handle);
135+
psa_destroy_key(id);
136136
}
137137

138138
static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
@@ -146,31 +146,31 @@ static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
146146
0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
147147
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
148148
};
149-
uint8_t signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
149+
uint8_t signature[PSA_SIGNATURE_MAX_SIZE] = {0};
150150
size_t signature_length;
151-
psa_key_handle_t handle;
151+
psa_key_id_t id;
152152

153153
printf("Sign a message...\t");
154154
fflush(stdout);
155155

156156
/* Set key attributes */
157-
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN);
157+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
158158
psa_set_key_algorithm(&attributes, alg);
159159
psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
160160
psa_set_key_bits(&attributes, 1024);
161161

162162
/* Import the key */
163-
status = psa_import_key(&attributes, key, key_len, &handle);
163+
status = psa_import_key(&attributes, key, key_len, &id);
164164
if (status != PSA_SUCCESS) {
165165
printf("Failed to import key\n");
166166
return;
167167
}
168168

169169
/* Sign message using the key */
170-
status = psa_asymmetric_sign(handle, alg,
171-
hash, sizeof(hash),
172-
signature, sizeof(signature),
173-
&signature_length);
170+
status = psa_sign_hash(id, alg,
171+
hash, sizeof(hash),
172+
signature, sizeof(signature),
173+
&signature_length);
174174
if (status != PSA_SUCCESS) {
175175
printf("Failed to sign\n");
176176
return;
@@ -182,7 +182,7 @@ static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
182182
psa_reset_key_attributes(&attributes);
183183

184184
/* Destroy the key */
185-
psa_destroy_key(handle);
185+
psa_destroy_key(id);
186186
}
187187

188188
static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
@@ -198,7 +198,7 @@ static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
198198
size_t iv_len;
199199
uint8_t output[block_size];
200200
size_t output_len;
201-
psa_key_handle_t handle;
201+
psa_key_id_t id;
202202
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
203203

204204
printf("Encrypt with cipher...\t");
@@ -209,15 +209,15 @@ static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
209209
psa_set_key_algorithm(&attributes, alg);
210210
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
211211
psa_set_key_bits(&attributes, 128);
212-
status = psa_import_key(&attributes, key, key_len, &handle);
212+
status = psa_import_key(&attributes, key, key_len, &id);
213213
if (status != PSA_SUCCESS) {
214214
printf("Failed to import a key\n");
215215
return;
216216
}
217217
psa_reset_key_attributes(&attributes);
218218

219219
/* Encrypt the plaintext */
220-
status = psa_cipher_encrypt_setup(&operation, handle, alg);
220+
status = psa_cipher_encrypt_setup(&operation, id, alg);
221221
if (status != PSA_SUCCESS) {
222222
printf("Failed to begin cipher operation\n");
223223
return;
@@ -245,7 +245,7 @@ static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
245245
psa_cipher_abort(&operation);
246246

247247
/* Destroy the key */
248-
psa_destroy_key(handle);
248+
psa_destroy_key(id);
249249
}
250250

251251
static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
@@ -261,7 +261,7 @@ static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
261261
uint8_t iv[block_size] = ENCRYPTED_WITH_IV;
262262
uint8_t output[block_size];
263263
size_t output_len;
264-
psa_key_handle_t handle;
264+
psa_key_id_t id;
265265

266266
printf("Decrypt with cipher...\t");
267267
fflush(stdout);
@@ -271,15 +271,15 @@ static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
271271
psa_set_key_algorithm(&attributes, alg);
272272
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
273273
psa_set_key_bits(&attributes, 128);
274-
status = psa_import_key(&attributes, key, key_len, &handle);
274+
status = psa_import_key(&attributes, key, key_len, &id);
275275
if (status != PSA_SUCCESS) {
276276
printf("Failed to import a key\n");
277277
return;
278278
}
279279
psa_reset_key_attributes(&attributes);
280280

281281
/* Decrypt the ciphertext */
282-
status = psa_cipher_decrypt_setup(&operation, handle, alg);
282+
status = psa_cipher_decrypt_setup(&operation, id, alg);
283283
if (status != PSA_SUCCESS) {
284284
printf("Failed to begin cipher operation\n");
285285
return;
@@ -307,7 +307,7 @@ static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
307307
psa_cipher_abort(&operation);
308308

309309
/* Destroy the key */
310-
psa_destroy_key(handle);
310+
psa_destroy_key(id);
311311
}
312312

313313
static void hash_a_message(void)
@@ -422,8 +422,8 @@ static void derive_a_new_key_from_an_existing_key(void)
422422
PSA_KEY_DERIVATION_OPERATION_INIT;
423423
size_t derived_bits = 128;
424424
size_t capacity = PSA_BITS_TO_BYTES(derived_bits);
425-
psa_key_handle_t base_key;
426-
psa_key_handle_t derived_key;
425+
psa_key_id_t base_key;
426+
psa_key_id_t derived_key;
427427

428428
printf("Derive a key (HKDF)...\t");
429429
fflush(stdout);
@@ -515,7 +515,7 @@ static void authenticate_and_encrypt_a_message(void)
515515
size_t output_length = 0;
516516
size_t tag_length = 16;
517517
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
518-
psa_key_handle_t handle;
518+
psa_key_id_t id;
519519

520520
printf("Authenticate encrypt...\t");
521521
fflush(stdout);
@@ -532,11 +532,11 @@ static void authenticate_and_encrypt_a_message(void)
532532
psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
533533
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
534534
psa_set_key_bits(&attributes, 128);
535-
status = psa_import_key(&attributes, key, sizeof(key), &handle);
535+
status = psa_import_key(&attributes, key, sizeof(key), &id);
536536
psa_reset_key_attributes(&attributes);
537537

538538
/* Authenticate and encrypt */
539-
status = psa_aead_encrypt(handle, PSA_ALG_CCM,
539+
status = psa_aead_encrypt(id, PSA_ALG_CCM,
540540
nonce, sizeof(nonce),
541541
additional_data, sizeof(additional_data),
542542
input_data, sizeof(input_data),
@@ -553,7 +553,7 @@ static void authenticate_and_encrypt_a_message(void)
553553
free(output_data);
554554

555555
/* Destroy the key */
556-
psa_destroy_key(handle);
556+
psa_destroy_key(id);
557557
}
558558

559559
static void authenticate_and_decrypt_a_message(void)
@@ -576,7 +576,7 @@ static void authenticate_and_decrypt_a_message(void)
576576
size_t output_size = 0;
577577
size_t output_length = 0;
578578
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
579-
psa_key_handle_t handle;
579+
psa_key_id_t id;
580580

581581
printf("Authenticate decrypt...\t");
582582
fflush(stdout);
@@ -593,15 +593,15 @@ static void authenticate_and_decrypt_a_message(void)
593593
psa_set_key_algorithm(&attributes, PSA_ALG_CCM);
594594
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
595595
psa_set_key_bits(&attributes, 128);
596-
status = psa_import_key(&attributes, key, sizeof(key), &handle);
596+
status = psa_import_key(&attributes, key, sizeof(key), &id);
597597
if (status != PSA_SUCCESS) {
598598
printf("Failed to import a key\n");
599599
return;
600600
}
601601
psa_reset_key_attributes(&attributes);
602602

603603
/* Authenticate and decrypt */
604-
status = psa_aead_decrypt(handle, PSA_ALG_CCM,
604+
status = psa_aead_decrypt(id, PSA_ALG_CCM,
605605
nonce, sizeof(nonce),
606606
additional_data, sizeof(additional_data),
607607
input_data, sizeof(input_data),
@@ -618,7 +618,7 @@ static void authenticate_and_decrypt_a_message(void)
618618
free(output_data);
619619

620620
/* Destroy the key */
621-
psa_destroy_key(handle);
621+
psa_destroy_key(id);
622622
}
623623

624624
static void generate_and_export_a_public_key()
@@ -630,26 +630,26 @@ static void generate_and_export_a_public_key()
630630
size_t exported_length = 0;
631631
static uint8_t exported[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits)];
632632
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
633-
psa_key_handle_t handle;
633+
psa_key_id_t id;
634634

635635
printf("Generate a key pair...\t");
636636
fflush(stdout);
637637

638638
/* Generate a key */
639-
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN);
639+
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
640640
psa_set_key_algorithm(&attributes,
641641
PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256));
642642
psa_set_key_type(&attributes,
643-
PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_CURVE_SECP256R1));
643+
PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
644644
psa_set_key_bits(&attributes, key_bits);
645-
status = psa_generate_key(&attributes, &handle);
645+
status = psa_generate_key(&attributes, &id);
646646
if (status != PSA_SUCCESS) {
647647
printf("Failed to generate key\n");
648648
return;
649649
}
650650
psa_reset_key_attributes(&attributes);
651651

652-
status = psa_export_public_key(handle, exported, sizeof(exported),
652+
status = psa_export_public_key(id, exported, sizeof(exported),
653653
&exported_length);
654654
if (status != PSA_SUCCESS) {
655655
printf("Failed to export public key %ld\n", status);
@@ -659,7 +659,7 @@ static void generate_and_export_a_public_key()
659659
printf("Exported a public key\n");
660660

661661
/* Destroy the key */
662-
psa_destroy_key(handle);
662+
psa_destroy_key(id);
663663
}
664664

665665
int main(void)

0 commit comments

Comments
 (0)