Skip to content

Commit 88000c1

Browse files
committed
Add status numbers to errors
Add status numbers to errors, so that the Mbed TLS's PSA decoder tool "psa_constant_names" can be used to display a more readable error message. This helps with debugging. $ psa_constant_names error -134 PSA_ERROR_NOT_SUPPORTED
1 parent 53a551d commit 88000c1

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

getting-started/main.cpp

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static void import_a_key(const uint8_t *key, size_t key_len)
120120
/* Initialize PSA Crypto */
121121
status = psa_crypto_init();
122122
if (status != PSA_SUCCESS) {
123-
printf("Failed to initialize PSA Crypto\n");
123+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
124124
return;
125125
}
126126

@@ -133,7 +133,7 @@ static void import_a_key(const uint8_t *key, size_t key_len)
133133
/* Import the key */
134134
status = psa_import_key(&attributes, key, key_len, &id);
135135
if (status != PSA_SUCCESS) {
136-
printf("Failed to import key\n");
136+
printf("Failed to import key (%ld)\n", status);
137137
return;
138138
}
139139
printf("Imported a key\n");
@@ -162,7 +162,7 @@ static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
162162
/* Initialize PSA Crypto */
163163
status = psa_crypto_init();
164164
if (status != PSA_SUCCESS) {
165-
printf("Failed to initialize PSA Crypto\n");
165+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
166166
return;
167167
}
168168

@@ -175,7 +175,7 @@ static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
175175
/* Import the key */
176176
status = psa_import_key(&attributes, key, key_len, &id);
177177
if (status != PSA_SUCCESS) {
178-
printf("Failed to import key\n");
178+
printf("Failed to import key (%ld)\n", status);
179179
return;
180180
}
181181

@@ -185,7 +185,7 @@ static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
185185
signature, sizeof(signature),
186186
&signature_length);
187187
if (status != PSA_SUCCESS) {
188-
printf("Failed to sign\n");
188+
printf("Failed to sign (%ld)\n", status);
189189
return;
190190
}
191191

@@ -223,7 +223,7 @@ static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
223223
status = psa_crypto_init();
224224
if (status != PSA_SUCCESS)
225225
{
226-
printf("Failed to initialize PSA Crypto\n");
226+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
227227
return;
228228
}
229229

@@ -234,32 +234,32 @@ static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
234234
psa_set_key_bits(&attributes, 128);
235235
status = psa_import_key(&attributes, key, key_len, &id);
236236
if (status != PSA_SUCCESS) {
237-
printf("Failed to import a key\n");
237+
printf("Failed to import a key (%ld)\n", status);
238238
return;
239239
}
240240
psa_reset_key_attributes(&attributes);
241241

242242
/* Encrypt the plaintext */
243243
status = psa_cipher_encrypt_setup(&operation, id, alg);
244244
if (status != PSA_SUCCESS) {
245-
printf("Failed to begin cipher operation\n");
245+
printf("Failed to begin cipher operation (%ld)\n", status);
246246
return;
247247
}
248248
status = psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len);
249249
if (status != PSA_SUCCESS) {
250-
printf("Failed to generate IV\n");
250+
printf("Failed to generate IV (%ld)\n", status);
251251
return;
252252
}
253253
status = psa_cipher_update(&operation, plaintext, sizeof(plaintext),
254254
output, sizeof(output), &output_len);
255255
if (status != PSA_SUCCESS) {
256-
printf("Failed to update cipher operation\n");
256+
printf("Failed to update cipher operation (%ld)\n", status);
257257
return;
258258
}
259259
status = psa_cipher_finish(&operation, output + output_len,
260260
sizeof(output) - output_len, &output_len);
261261
if (status != PSA_SUCCESS) {
262-
printf("Failed to finish cipher operation\n");
262+
printf("Failed to finish cipher operation (%ld)\n", status);
263263
return;
264264
}
265265
printf("Encrypted plaintext\n");
@@ -295,7 +295,7 @@ static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
295295
status = psa_crypto_init();
296296
if (status != PSA_SUCCESS)
297297
{
298-
printf("Failed to initialize PSA Crypto\n");
298+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
299299
return;
300300
}
301301

@@ -306,32 +306,32 @@ static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
306306
psa_set_key_bits(&attributes, 128);
307307
status = psa_import_key(&attributes, key, key_len, &id);
308308
if (status != PSA_SUCCESS) {
309-
printf("Failed to import a key\n");
309+
printf("Failed to import a key (%ld)\n", status);
310310
return;
311311
}
312312
psa_reset_key_attributes(&attributes);
313313

314314
/* Decrypt the ciphertext */
315315
status = psa_cipher_decrypt_setup(&operation, id, alg);
316316
if (status != PSA_SUCCESS) {
317-
printf("Failed to begin cipher operation\n");
317+
printf("Failed to begin cipher operation (%ld)\n", status);
318318
return;
319319
}
320320
status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
321321
if (status != PSA_SUCCESS) {
322-
printf("Failed to set IV\n");
322+
printf("Failed to set IV (%ld)\n", status);
323323
return;
324324
}
325325
status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext),
326326
output, sizeof(output), &output_len);
327327
if (status != PSA_SUCCESS) {
328-
printf("Failed to update cipher operation\n");
328+
printf("Failed to update cipher operation (%ld)\n", status);
329329
return;
330330
}
331331
status = psa_cipher_finish(&operation, output + output_len,
332332
sizeof(output) - output_len, &output_len);
333333
if (status != PSA_SUCCESS) {
334-
printf("Failed to finish cipher operation\n");
334+
printf("Failed to finish cipher operation (%ld)\n", status);
335335
return;
336336
}
337337
printf("Decrypted ciphertext\n");
@@ -360,25 +360,25 @@ static void hash_a_message(void)
360360
/* Initialize PSA Crypto */
361361
status = psa_crypto_init();
362362
if (status != PSA_SUCCESS) {
363-
printf("Failed to initialize PSA Crypto\n");
363+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
364364
return;
365365
}
366366

367367
/* Compute hash of message */
368368
status = psa_hash_setup(&operation, alg);
369369
if (status != PSA_SUCCESS) {
370-
printf("Failed to begin hash operation\n");
370+
printf("Failed to begin hash operation (%ld)\n", status);
371371
return;
372372
}
373373
status = psa_hash_update(&operation, input, sizeof(input));
374374
if (status != PSA_SUCCESS) {
375-
printf("Failed to update hash operation\n");
375+
printf("Failed to update hash operation (%ld)\n", status);
376376
return;
377377
}
378378
status = psa_hash_finish(&operation, actual_hash, sizeof(actual_hash),
379379
&actual_hash_len);
380380
if (status != PSA_SUCCESS) {
381-
printf("Failed to finish hash operation\n");
381+
printf("Failed to finish hash operation (%ld)\n", status);
382382
return;
383383
}
384384

@@ -409,24 +409,24 @@ static void verify_a_hash(void)
409409
/* Initialize PSA Crypto */
410410
status = psa_crypto_init();
411411
if (status != PSA_SUCCESS) {
412-
printf("Failed to initialize PSA Crypto\n");
412+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
413413
return;
414414
}
415415

416416
/* Verify message hash */
417417
status = psa_hash_setup(&operation, alg);
418418
if (status != PSA_SUCCESS) {
419-
printf("Failed to begin hash operation\n");
419+
printf("Failed to begin hash operation (%ld)\n", status);
420420
return;
421421
}
422422
status = psa_hash_update(&operation, input, sizeof(input));
423423
if (status != PSA_SUCCESS) {
424-
printf("Failed to update hash operation\n");
424+
printf("Failed to update hash operation (%ld)\n", status);
425425
return;
426426
}
427427
status = psa_hash_verify(&operation, expected_hash, expected_hash_len);
428428
if (status != PSA_SUCCESS) {
429-
printf("Failed to verify hash\n");
429+
printf("Failed to verify hash (%ld)\n", status);
430430
return;
431431
}
432432

@@ -449,13 +449,13 @@ static void generate_a_random_value(void)
449449
/* Initialize PSA Crypto */
450450
status = psa_crypto_init();
451451
if (status != PSA_SUCCESS) {
452-
printf("Failed to initialize PSA Crypto\n");
452+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
453453
return;
454454
}
455455

456456
status = psa_generate_random(random, sizeof(random));
457457
if (status != PSA_SUCCESS) {
458-
printf("Failed to generate a random value\n");
458+
printf("Failed to generate a random value (%ld)\n", status);
459459
return;
460460
}
461461

@@ -494,7 +494,7 @@ static void derive_a_new_key_from_an_existing_key(void)
494494
/* Initialize PSA Crypto */
495495
status = psa_crypto_init();
496496
if (status != PSA_SUCCESS) {
497-
printf("Failed to initialize PSA Crypto\n");
497+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
498498
return;
499499
}
500500

@@ -505,41 +505,41 @@ static void derive_a_new_key_from_an_existing_key(void)
505505
psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
506506
status = psa_import_key(&attributes, key, sizeof(key), &base_key);
507507
if (status != PSA_SUCCESS) {
508-
printf("Failed to import a key\n");
508+
printf("Failed to import a key (%ld)\n", status);
509509
return;
510510
}
511511
psa_reset_key_attributes(&attributes);
512512

513513
/* Derive a key */
514514
status = psa_key_derivation_setup(&operation, alg);
515515
if (status != PSA_SUCCESS) {
516-
printf("Failed to begin key derivation\n");
516+
printf("Failed to begin key derivation (%ld)\n", status);
517517
return;
518518
}
519519
status = psa_key_derivation_set_capacity(&operation, capacity);
520520
if (status != PSA_SUCCESS) {
521-
printf("Failed to set capacity\n");
521+
printf("Failed to set capacity (%ld)\n", status);
522522
return;
523523
}
524524
status = psa_key_derivation_input_bytes(&operation,
525525
PSA_KEY_DERIVATION_INPUT_SALT,
526526
salt, sizeof(salt));
527527
if (status != PSA_SUCCESS) {
528-
printf("Failed to input salt (extract)\n");
528+
printf("Failed to input salt (extract) (%ld)\n", status);
529529
return;
530530
}
531531
status = psa_key_derivation_input_key(&operation,
532532
PSA_KEY_DERIVATION_INPUT_SECRET,
533533
base_key);
534534
if (status != PSA_SUCCESS) {
535-
printf("Failed to input key (extract)\n");
535+
printf("Failed to input key (extract) (%ld)\n", status);
536536
return;
537537
}
538538
status = psa_key_derivation_input_bytes(&operation,
539539
PSA_KEY_DERIVATION_INPUT_INFO,
540540
info, sizeof(info));
541541
if (status != PSA_SUCCESS) {
542-
printf("Failed to input info (expand)\n");
542+
printf("Failed to input info (expand) (%ld)\n", status);
543543
return;
544544
}
545545
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
@@ -549,7 +549,7 @@ static void derive_a_new_key_from_an_existing_key(void)
549549
status = psa_key_derivation_output_key(&attributes, &operation,
550550
&derived_key);
551551
if (status != PSA_SUCCESS) {
552-
printf("Failed to derive key\n");
552+
printf("Failed to derive key (%ld)\n", status);
553553
return;
554554
}
555555
psa_reset_key_attributes(&attributes);
@@ -595,7 +595,7 @@ static void authenticate_and_encrypt_a_message(void)
595595
/* Initialize PSA Crypto */
596596
status = psa_crypto_init();
597597
if (status != PSA_SUCCESS) {
598-
printf("Failed to initialize PSA Crypto\n");
598+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
599599
return;
600600
}
601601

@@ -622,7 +622,7 @@ static void authenticate_and_encrypt_a_message(void)
622622
output_data, output_size,
623623
&output_length);
624624
if (status != PSA_SUCCESS) {
625-
printf("Failed to authenticate and encrypt\n");
625+
printf("Failed to authenticate and encrypt (%ld)\n", status);
626626
return;
627627
}
628628

@@ -665,7 +665,7 @@ static void authenticate_and_decrypt_a_message(void)
665665
/* Initialize PSA Crypto */
666666
status = psa_crypto_init();
667667
if (status != PSA_SUCCESS) {
668-
printf("Failed to initialize PSA Crypto\n");
668+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
669669
return;
670670
}
671671

@@ -683,7 +683,7 @@ static void authenticate_and_decrypt_a_message(void)
683683
psa_set_key_bits(&attributes, 128);
684684
status = psa_import_key(&attributes, key, sizeof(key), &id);
685685
if (status != PSA_SUCCESS) {
686-
printf("Failed to import a key\n");
686+
printf("Failed to import a key (%ld)\n", status);
687687
return;
688688
}
689689
psa_reset_key_attributes(&attributes);
@@ -696,7 +696,7 @@ static void authenticate_and_decrypt_a_message(void)
696696
output_data, output_size,
697697
&output_length);
698698
if (status != PSA_SUCCESS) {
699-
printf("Failed to authenticate and decrypt\n");
699+
printf("Failed to authenticate and decrypt (%ld)\n", status);
700700
return;
701701
}
702702

@@ -728,7 +728,7 @@ static void generate_and_export_a_public_key()
728728
/* Initialize PSA Crypto */
729729
status = psa_crypto_init();
730730
if (status != PSA_SUCCESS) {
731-
printf("Failed to initialize PSA Crypto\n");
731+
printf("Failed to initialize PSA Crypto (%ld)\n", status);
732732
return;
733733
}
734734

@@ -741,15 +741,15 @@ static void generate_and_export_a_public_key()
741741
psa_set_key_bits(&attributes, key_bits);
742742
status = psa_generate_key(&attributes, &id);
743743
if (status != PSA_SUCCESS) {
744-
printf("Failed to generate key\n");
744+
printf("Failed to generate key (%ld)\n", status);
745745
return;
746746
}
747747
psa_reset_key_attributes(&attributes);
748748

749749
status = psa_export_public_key(id, exported, sizeof(exported),
750750
&exported_length);
751751
if (status != PSA_SUCCESS) {
752-
printf("Failed to export public key %ld\n", status);
752+
printf("Failed to export public key (%ld)\n", status);
753753
return;
754754
}
755755

0 commit comments

Comments
 (0)