Skip to content

Commit a82c710

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 f3320a9 commit a82c710

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

getting-started/main.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static void import_a_key(const uint8_t *key, size_t key_len)
123123
/* Import the key */
124124
status = psa_import_key(&attributes, key, key_len, &id);
125125
if (status != PSA_SUCCESS) {
126-
printf("Failed to import key\n");
126+
printf("Failed to import key (%" PRIu32 ")\n", status);
127127
return;
128128
}
129129
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
/* Import the key */
163163
status = psa_import_key(&attributes, key, key_len, &id);
164164
if (status != PSA_SUCCESS) {
165-
printf("Failed to import key\n");
165+
printf("Failed to import key (%" PRIu32 ")\n", status);
166166
return;
167167
}
168168

@@ -172,7 +172,7 @@ static void sign_a_message_using_rsa(const uint8_t *key, size_t key_len)
172172
signature, sizeof(signature),
173173
&signature_length);
174174
if (status != PSA_SUCCESS) {
175-
printf("Failed to sign\n");
175+
printf("Failed to sign (%" PRIu32 ")\n", status);
176176
return;
177177
}
178178

@@ -211,32 +211,32 @@ static void encrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
211211
psa_set_key_bits(&attributes, 128);
212212
status = psa_import_key(&attributes, key, key_len, &id);
213213
if (status != PSA_SUCCESS) {
214-
printf("Failed to import a key\n");
214+
printf("Failed to import a key (%" PRIu32 ")\n", status);
215215
return;
216216
}
217217
psa_reset_key_attributes(&attributes);
218218

219219
/* Encrypt the plaintext */
220220
status = psa_cipher_encrypt_setup(&operation, id, alg);
221221
if (status != PSA_SUCCESS) {
222-
printf("Failed to begin cipher operation\n");
222+
printf("Failed to begin cipher operation (%" PRIu32 ")\n", status);
223223
return;
224224
}
225225
status = psa_cipher_generate_iv(&operation, iv, sizeof(iv), &iv_len);
226226
if (status != PSA_SUCCESS) {
227-
printf("Failed to generate IV\n");
227+
printf("Failed to generate IV (%" PRIu32 ")\n", status);
228228
return;
229229
}
230230
status = psa_cipher_update(&operation, plaintext, sizeof(plaintext),
231231
output, sizeof(output), &output_len);
232232
if (status != PSA_SUCCESS) {
233-
printf("Failed to update cipher operation\n");
233+
printf("Failed to update cipher operation (%" PRIu32 ")\n", status);
234234
return;
235235
}
236236
status = psa_cipher_finish(&operation, output + output_len,
237237
sizeof(output) - output_len, &output_len);
238238
if (status != PSA_SUCCESS) {
239-
printf("Failed to finish cipher operation\n");
239+
printf("Failed to finish cipher operation (%" PRIu32 ")\n", status);
240240
return;
241241
}
242242
printf("Encrypted plaintext\n");
@@ -273,32 +273,32 @@ static void decrypt_with_symmetric_ciphers(const uint8_t *key, size_t key_len)
273273
psa_set_key_bits(&attributes, 128);
274274
status = psa_import_key(&attributes, key, key_len, &id);
275275
if (status != PSA_SUCCESS) {
276-
printf("Failed to import a key\n");
276+
printf("Failed to import a key (%" PRIu32 ")\n", status);
277277
return;
278278
}
279279
psa_reset_key_attributes(&attributes);
280280

281281
/* Decrypt the ciphertext */
282282
status = psa_cipher_decrypt_setup(&operation, id, alg);
283283
if (status != PSA_SUCCESS) {
284-
printf("Failed to begin cipher operation\n");
284+
printf("Failed to begin cipher operation (%" PRIu32 ")\n", status);
285285
return;
286286
}
287287
status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
288288
if (status != PSA_SUCCESS) {
289-
printf("Failed to set IV\n");
289+
printf("Failed to set IV (%" PRIu32 ")\n", status);
290290
return;
291291
}
292292
status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext),
293293
output, sizeof(output), &output_len);
294294
if (status != PSA_SUCCESS) {
295-
printf("Failed to update cipher operation\n");
295+
printf("Failed to update cipher operation (%" PRIu32 ")\n", status);
296296
return;
297297
}
298298
status = psa_cipher_finish(&operation, output + output_len,
299299
sizeof(output) - output_len, &output_len);
300300
if (status != PSA_SUCCESS) {
301-
printf("Failed to finish cipher operation\n");
301+
printf("Failed to finish cipher operation (%" PRIu32 ")\n", status);
302302
return;
303303
}
304304
printf("Decrypted ciphertext\n");
@@ -325,18 +325,18 @@ static void hash_a_message(void)
325325
/* Compute hash of message */
326326
status = psa_hash_setup(&operation, alg);
327327
if (status != PSA_SUCCESS) {
328-
printf("Failed to begin hash operation\n");
328+
printf("Failed to begin hash operation (%" PRIu32 ")\n", status);
329329
return;
330330
}
331331
status = psa_hash_update(&operation, input, sizeof(input));
332332
if (status != PSA_SUCCESS) {
333-
printf("Failed to update hash operation\n");
333+
printf("Failed to update hash operation (%" PRIu32 ")\n", status);
334334
return;
335335
}
336336
status = psa_hash_finish(&operation, actual_hash, sizeof(actual_hash),
337337
&actual_hash_len);
338338
if (status != PSA_SUCCESS) {
339-
printf("Failed to finish hash operation\n");
339+
printf("Failed to finish hash operation (%" PRIu32 ")\n", status);
340340
return;
341341
}
342342

@@ -365,17 +365,17 @@ static void verify_a_hash(void)
365365
/* Verify message hash */
366366
status = psa_hash_setup(&operation, alg);
367367
if (status != PSA_SUCCESS) {
368-
printf("Failed to begin hash operation\n");
368+
printf("Failed to begin hash operation (%" PRIu32 ")\n", status);
369369
return;
370370
}
371371
status = psa_hash_update(&operation, input, sizeof(input));
372372
if (status != PSA_SUCCESS) {
373-
printf("Failed to update hash operation\n");
373+
printf("Failed to update hash operation (%" PRIu32 ")\n", status);
374374
return;
375375
}
376376
status = psa_hash_verify(&operation, expected_hash, expected_hash_len);
377377
if (status != PSA_SUCCESS) {
378-
printf("Failed to verify hash\n");
378+
printf("Failed to verify hash (%" PRIu32 ")\n", status);
379379
return;
380380
}
381381

@@ -395,7 +395,7 @@ static void generate_a_random_value(void)
395395

396396
status = psa_generate_random(random, sizeof(random));
397397
if (status != PSA_SUCCESS) {
398-
printf("Failed to generate a random value\n");
398+
printf("Failed to generate a random value (%" PRIu32 ")\n", status);
399399
return;
400400
}
401401

@@ -435,41 +435,41 @@ static void derive_a_new_key_from_an_existing_key(void)
435435
psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
436436
status = psa_import_key(&attributes, key, sizeof(key), &base_key);
437437
if (status != PSA_SUCCESS) {
438-
printf("Failed to import a key\n");
438+
printf("Failed to import a key (%" PRIu32 ")\n", status);
439439
return;
440440
}
441441
psa_reset_key_attributes(&attributes);
442442

443443
/* Derive a key */
444444
status = psa_key_derivation_setup(&operation, alg);
445445
if (status != PSA_SUCCESS) {
446-
printf("Failed to begin key derivation\n");
446+
printf("Failed to begin key derivation (%" PRIu32 ")\n", status);
447447
return;
448448
}
449449
status = psa_key_derivation_set_capacity(&operation, capacity);
450450
if (status != PSA_SUCCESS) {
451-
printf("Failed to set capacity\n");
451+
printf("Failed to set capacity (%" PRIu32 ")\n", status);
452452
return;
453453
}
454454
status = psa_key_derivation_input_bytes(&operation,
455455
PSA_KEY_DERIVATION_INPUT_SALT,
456456
salt, sizeof(salt));
457457
if (status != PSA_SUCCESS) {
458-
printf("Failed to input salt (extract)\n");
458+
printf("Failed to input salt (extract) (%" PRIu32 ")\n", status);
459459
return;
460460
}
461461
status = psa_key_derivation_input_key(&operation,
462462
PSA_KEY_DERIVATION_INPUT_SECRET,
463463
base_key);
464464
if (status != PSA_SUCCESS) {
465-
printf("Failed to input key (extract)\n");
465+
printf("Failed to input key (extract) (%" PRIu32 ")\n", status);
466466
return;
467467
}
468468
status = psa_key_derivation_input_bytes(&operation,
469469
PSA_KEY_DERIVATION_INPUT_INFO,
470470
info, sizeof(info));
471471
if (status != PSA_SUCCESS) {
472-
printf("Failed to input info (expand)\n");
472+
printf("Failed to input info (expand) (%" PRIu32 ")\n", status);
473473
return;
474474
}
475475
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
@@ -479,7 +479,7 @@ static void derive_a_new_key_from_an_existing_key(void)
479479
status = psa_key_derivation_output_key(&attributes, &operation,
480480
&derived_key);
481481
if (status != PSA_SUCCESS) {
482-
printf("Failed to derive key\n");
482+
printf("Failed to derive key (%" PRIu32 ")\n", status);
483483
return;
484484
}
485485
psa_reset_key_attributes(&attributes);
@@ -543,7 +543,7 @@ static void authenticate_and_encrypt_a_message(void)
543543
output_data, output_size,
544544
&output_length);
545545
if (status != PSA_SUCCESS) {
546-
printf("Failed to authenticate and encrypt\n");
546+
printf("Failed to authenticate and encrypt (%" PRIu32 ")\n", status);
547547
return;
548548
}
549549

@@ -595,7 +595,7 @@ static void authenticate_and_decrypt_a_message(void)
595595
psa_set_key_bits(&attributes, 128);
596596
status = psa_import_key(&attributes, key, sizeof(key), &id);
597597
if (status != PSA_SUCCESS) {
598-
printf("Failed to import a key\n");
598+
printf("Failed to import a key (%" PRIu32 ")\n", status);
599599
return;
600600
}
601601
psa_reset_key_attributes(&attributes);
@@ -608,7 +608,7 @@ static void authenticate_and_decrypt_a_message(void)
608608
output_data, output_size,
609609
&output_length);
610610
if (status != PSA_SUCCESS) {
611-
printf("Failed to authenticate and decrypt\n");
611+
printf("Failed to authenticate and decrypt (%" PRIu32 ")\n", status);
612612
return;
613613
}
614614

@@ -644,15 +644,15 @@ static void generate_and_export_a_public_key()
644644
psa_set_key_bits(&attributes, key_bits);
645645
status = psa_generate_key(&attributes, &id);
646646
if (status != PSA_SUCCESS) {
647-
printf("Failed to generate key\n");
647+
printf("Failed to generate key (%" PRIu32 ")\n", status);
648648
return;
649649
}
650650
psa_reset_key_attributes(&attributes);
651651

652652
status = psa_export_public_key(id, exported, sizeof(exported),
653653
&exported_length);
654654
if (status != PSA_SUCCESS) {
655-
printf("Failed to export public key %ld\n", status);
655+
printf("Failed to export public key (%" PRIu32 ")\n", status);
656656
return;
657657
}
658658

@@ -669,7 +669,7 @@ int main(void)
669669
/* Initialize PSA Crypto */
670670
psa_status_t status = psa_crypto_init();
671671
if (status != PSA_SUCCESS) {
672-
printf("Failed to initialize PSA Crypto\n");
672+
printf("Failed to initialize PSA Crypto (%" PRIu32 ")\n", status);
673673
return -1;
674674
}
675675

0 commit comments

Comments
 (0)