Move print_buf into mbedtls_test_print_buf helper function in sample programs

Reduce code duplication and fix missing-prototype error for print_buf

Signed-off-by: Michael Schuster <michael@schuster.ms>
This commit is contained in:
Michael Schuster
2024-06-01 21:00:33 +02:00
committed by Minos Galanakis
parent b4d55bb5db
commit 3a4c43174c
6 changed files with 26 additions and 46 deletions
+3 -11
View File
@@ -36,6 +36,8 @@
#include "psa/crypto.h"
#include <test/helpers.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -81,16 +83,6 @@ const unsigned char msg2_part2[] = { 0x15, 0x16, 0x17 };
* 32-byte is enough to all the key size supported by this program. */
const unsigned char key_bytes[32] = { 0x2a };
/* Print the contents of a buffer in hex */
void print_buf(const char *title, uint8_t *buf, size_t len)
{
printf("%s:", title);
for (size_t i = 0; i < len; i++) {
printf(" %02x", buf[i]);
}
printf("\n");
}
/* Run a PSA function and bail out if it fails.
* The symbolic name of the error code can be recovered using:
* programs/psa/psa_constant_name status <value> */
@@ -216,7 +208,7 @@ static int aead_encrypt(psa_key_id_t key, psa_algorithm_t alg,
p += olen_tag;
olen = p - out;
print_buf("out", out, olen);
mbedtls_test_print_buf("out", out, olen);
exit:
psa_aead_abort(&op); // required on errors, harmless on success
+4 -12
View File
@@ -32,6 +32,8 @@
#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
#include <test/helpers.h>
#include <stdlib.h>
#include <stdio.h>
@@ -58,16 +60,6 @@ const unsigned char msg2_part2[] = { 0x06, 0x06 };
* This example program uses SHA-256, so a 32-byte key makes sense. */
const unsigned char key_bytes[32] = { 0 };
/* Print the contents of a buffer in hex */
void print_buf(const char *title, uint8_t *buf, size_t len)
{
printf("%s:", title);
for (size_t i = 0; i < len; i++) {
printf(" %02x", buf[i]);
}
printf("\n");
}
/* Run a PSA function and bail out if it fails.
* The symbolic name of the error code can be recovered using:
* programs/psa/psa_constant_name status <value> */
@@ -122,14 +114,14 @@ psa_status_t hmac_demo(void)
PSA_CHECK(psa_mac_update(&op, msg1_part1, sizeof(msg1_part1)));
PSA_CHECK(psa_mac_update(&op, msg1_part2, sizeof(msg1_part2)));
PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
print_buf("msg1", out, out_len);
mbedtls_test_print_buf("msg1", out, out_len);
/* compute HMAC(key, msg2_part1 | msg2_part2) */
PSA_CHECK(psa_mac_sign_setup(&op, key, alg));
PSA_CHECK(psa_mac_update(&op, msg2_part1, sizeof(msg2_part1)));
PSA_CHECK(psa_mac_update(&op, msg2_part2, sizeof(msg2_part2)));
PSA_CHECK(psa_mac_sign_finish(&op, out, sizeof(out), &out_len));
print_buf("msg2", out, out_len);
mbedtls_test_print_buf("msg2", out, out_len);
exit:
psa_mac_abort(&op); // needed on error, harmless on success