1+ /**
2+ ******************************************************************************
3+ * @file Apps_utils.c
4+ * @author CS application team
5+ * @brief Application utilities implementation file providing common helper
6+ * functions for STSAFE-A120 examples
7+ ******************************************************************************
8+ * COPYRIGHT 2022 STMicroelectronics
9+ *
10+ * This software is licensed under terms that can be found in the LICENSE file
11+ * in the root directory of this software component.
12+ * If no LICENSE file comes with this software, it is provided AS-IS.
13+ *
14+ ******************************************************************************
15+ */
16+
117#include "Apps_utils.h"
218
19+ /**
20+ * @brief Hard fault exception handler
21+ * @note This function is called when a hard fault exception occurs and enters
22+ * an infinite loop to halt the system
23+ */
324void HardFault_Handler (void ) {
425 printf ("\n\r ## SYSTEM : HARDFAULT ## " );
526 while (1 ) {
627 /* Infinite loop */
728 }
829}
930
10- /* STDIO redirect */
31+ /**
32+ * @brief STDIO redirection definitions for different compilers
33+ * @note Redirects standard I/O operations (putchar/getchar) to UART functions
34+ */
1135#if defined(__GNUC__ ) && !defined(__ARMCC_VERSION )
1236#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
1337#define GETCHAR_PROTOTYPE int __io_getchar()
1438#else
1539#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
1640#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
1741#endif /* __GNUC__ */
42+
43+ /**
44+ * @brief Redirect putchar to UART output
45+ * @param ch Character to output
46+ * @return The character written
47+ */
1848PUTCHAR_PROTOTYPE {
1949 uart_putc (ch );
2050 return ch ;
2151}
52+
53+ /**
54+ * @brief Redirect getchar to UART input
55+ * @return The character read from UART
56+ */
2257GETCHAR_PROTOTYPE {
2358 return uart_getc ();
2459}
2560
61+ /**
62+ * @brief Get string representation of ECC key type
63+ * @param key_type The ECC key type enumeration value
64+ * @return Pointer to string containing the key type name
65+ * @note Returns "bad curve ID" string if key type is not recognized
66+ */
2667char * get_key_type_str (stse_ecc_key_type_t key_type ) {
2768 switch (key_type ) {
2869#ifdef STSE_CONF_ECC_NIST_P_256
@@ -62,6 +103,13 @@ char *get_key_type_str(stse_ecc_key_type_t key_type) {
62103 }
63104}
64105
106+ /**
107+ * @brief Convert curve ID to key type enumeration
108+ * @param curve_id The ECC curve identifier to convert
109+ * @param pKey_type Pointer to store the corresponding key type
110+ * @return STSE_OK on success, STSE_UNEXPECTED_ERROR if curve ID not found
111+ * @note Iterates through known curve IDs to find a match
112+ */
65113stse_ReturnCode_t get_curve_id_key_type (stsafea_ecc_curve_id_t curve_id , stse_ecc_key_type_t * pKey_type ) {
66114 stse_ecc_key_type_t curve_id_index ;
67115 int diff ;
@@ -85,6 +133,15 @@ stse_ReturnCode_t get_curve_id_key_type(stsafea_ecc_curve_id_t curve_id, stse_ec
85133 return STSE_OK ;
86134}
87135
136+ /**
137+ * @brief Print formatted symmetric key table information
138+ * @param pSTSE Pointer to STSE handler structure
139+ * @note Retrieves and displays all symmetric key slot information including:
140+ * - Key type (AES-128/AES-256)
141+ * - Mode of operation (CCM*/ CMAC /ECB /GCM /HKDF /HMAC )
142+ * - Key usage flags (derive , MAC generation /verification , encryption /decryption )
143+ * - Parameters (MAC length , auth tag length , counter values , HKDF settings )
144+ * /
88145void apps_print_symmetric_key_table_info (stse_Handler_t * pSTSE ) {
89146 stse_ReturnCode_t ret ;
90147 PLAT_UI8 i ;
@@ -166,6 +223,17 @@ void apps_print_symmetric_key_table_info(stse_Handler_t *pSTSE) {
166223 }
167224}
168225
226+ /**
227+ * @brief Print symmetric key table provisioning control fields
228+ * @param pSTSE Pointer to STSE handler structure
229+ * @note Displays provisioning control field information for each symmetric key slot:
230+ * - Change right
231+ * - Derived key flag
232+ * - Plaintext provisioning
233+ * - Put key access
234+ * - Wrapped provisioning with authentication key
235+ * - ECDHE provisioning with authentication key
236+ */
169237void apps_print_symmetric_key_table_provisioning_control_fields (stse_Handler_t * pSTSE ) {
170238 stse_ReturnCode_t ret ;
171239 PLAT_UI8 i ;
@@ -214,6 +282,16 @@ void apps_print_symmetric_key_table_provisioning_control_fields(stse_Handler_t *
214282 }
215283}
216284
285+ /**
286+ * @brief Print formatted asymmetric (ECC) key table information
287+ * @param pSTSE Pointer to STSE handler structure
288+ * @note Retrieves and displays ECC key slot information including:
289+ * - Global change right and usage limit
290+ * - Per-slot: presence, key type (NIST, Brainpool, Curve25519, Ed25519)
291+ * - EdDSA variant (Pure/PreHashed)
292+ * - Generate key access control
293+ * - Signature generation and key establishment capabilities
294+ */
217295void apps_print_asymmetric_key_table_info (stse_Handler_t * pSTSE ) {
218296 stse_ReturnCode_t ret ;
219297 PLAT_UI8 i ;
@@ -276,6 +354,16 @@ void apps_print_asymmetric_key_table_info(stse_Handler_t *pSTSE) {
276354 }
277355}
278356
357+ /**
358+ * @brief Print host key provisioning control fields
359+ * @param pSTSE Pointer to STSE handler structure
360+ * @note Displays host key provisioning control information:
361+ * - Change right
362+ * - Re-provision capability
363+ * - Plaintext provisioning
364+ * - Wrapped anonymous provisioning
365+ * - Wrapped/DH derived authentication key slot number
366+ */
279367void apps_print_host_key_provisioning_control_fields (stse_Handler_t * pSTSE ) {
280368 stsafea_host_key_provisioning_ctrl_fields_t provisioning_ctrl_fields ;
281369 stse_ReturnCode_t ret ;
@@ -299,6 +387,16 @@ void apps_print_host_key_provisioning_control_fields(stse_Handler_t *pSTSE) {
299387 printf ("\n\r --------------+--------------+-----------+-------------------+----------------------------" );
300388}
301389
390+ /**
391+ * @brief Print generic public key slot configuration flags
392+ * @param pSTSE Pointer to STSE handler structure
393+ * @param slot_number Slot number to query and display
394+ * @note Displays configuration information for the specified generic public key slot:
395+ * - Change right
396+ * - Establish symmetric key capability
397+ * - Start volatile KEK session capability
398+ * - Key presence status
399+ */
302400void apps_print_generic_public_key_slot_configuration_flags (stse_Handler_t * pSTSE , PLAT_UI8 slot_number ) {
303401 stse_ReturnCode_t ret ;
304402 PLAT_UI8 generic_public_key_presence ;
@@ -328,6 +426,12 @@ void apps_print_generic_public_key_slot_configuration_flags(stse_Handler_t *pSTS
328426 printf ("\n\r ------+--------------+--------------------------+----------------------------+--------------" );
329427}
330428
429+ /**
430+ * @brief Print buffer content in hexadecimal format
431+ * @param buffer Pointer to buffer to display
432+ * @param buffer_size Size of the buffer in bytes
433+ * @note Formats output with 16 bytes per line, each byte as "0xXX"
434+ */
331435void apps_print_hex_buffer (uint8_t * buffer , uint16_t buffer_size ) {
332436 uint16_t i ;
333437 for (i = 0 ; i < buffer_size ; i ++ ) {
@@ -338,6 +442,17 @@ void apps_print_hex_buffer(uint8_t *buffer, uint16_t buffer_size) {
338442 }
339443}
340444
445+ /**
446+ * @brief Print data partition record table information
447+ * @param pSTSE Pointer to STSE handler structure
448+ * @note Retrieves and displays data storage partition information for each zone:
449+ * - Zone ID
450+ * - Counter presence flag
451+ * - Data segment size
452+ * - Read access control and change right
453+ * - Update access control and change right
454+ * - Current counter value
455+ */
341456void apps_print_data_partition_record_table (stse_Handler_t * pSTSE ) {
342457 uint8_t i ;
343458 stse_ReturnCode_t ret ;
@@ -422,6 +537,16 @@ void apps_print_data_partition_record_table(stse_Handler_t *pSTSE) {
422537 }
423538}
424539
540+ /**
541+ * @brief Print command authorization record table
542+ * @param command_ac_record_table Pointer to array of command authorization records
543+ * @param total_command_count Number of commands in the table
544+ * @note Displays command access control information:
545+ * - Command header and extended header
546+ * - Access conditions (NEVER/FREE/ADMIN/HOST/ADMIN_OR_PWD/ADMIN_OR_HOST)
547+ * - Command encryption flag
548+ * - Response encryption flag
549+ */
425550void apps_print_command_ac_record_table (stse_cmd_authorization_record_t * command_ac_record_table ,
426551 uint8_t total_command_count ) {
427552 uint8_t i ;
@@ -471,6 +596,12 @@ void apps_print_command_ac_record_table(stse_cmd_authorization_record_t *command
471596 }
472597}
473598
599+ /**
600+ * @brief Print human-readable life cycle state
601+ * @param life_cycle_state The life cycle state enumeration value
602+ * @note Prints one of: BORN, PATCHING, OPERATIONAL, TERMINATED,
603+ * BORN_AND_LOCKED, OPERATIONAL_AND_LOCKED, or UNKNOWN
604+ */
474605void apps_print_life_cycle_state (stsafea_life_cycle_state_t life_cycle_state ) {
475606 switch (life_cycle_state ) {
476607 case STSAFEA_LCS_BORN :
@@ -497,6 +628,12 @@ void apps_print_life_cycle_state(stsafea_life_cycle_state_t life_cycle_state) {
497628 }
498629};
499630
631+ /**
632+ * @brief Initialize terminal for application output
633+ * @param baudrate Baudrate for UART communication (currently hardcoded to 115200)
634+ * @note Initializes UART, disables I/O buffering for stdout/stdin,
635+ * and clears the terminal screen
636+ */
500637void apps_terminal_init (uint32_t baudrate ) {
501638 (void )baudrate ;
502639 /* - Initialize UART for example output log (baud 115200) */
@@ -509,6 +646,14 @@ void apps_terminal_init(uint32_t baudrate) {
509646 printf (PRINT_RESET PRINT_CLEAR_SCREEN );
510647}
511648
649+ /**
650+ * @brief Read string from terminal
651+ * @param string Pointer to buffer to store the read string (can be NULL)
652+ * @param length Pointer to maximum length, updated with actual read length (can be NULL)
653+ * @return 0 on success, 1 if maximum length exceeded
654+ * @note Reads characters until carriage return ('\r') is received
655+ * Echoes each character back to the terminal
656+ */
512657uint8_t apps_terminal_read_string (char * string , uint8_t * length ) {
513658 char c = 1 ;
514659 uint16_t i = 0 ;
@@ -529,6 +674,12 @@ uint8_t apps_terminal_read_string(char *string, uint8_t *length) {
529674 return 0 ;
530675}
531676
677+ /**
678+ * @brief Read unsigned integer from terminal
679+ * @param integer Pointer to store the read integer value
680+ * @return 0 on success, 1 on error
681+ * @note Reads a string of up to 6 characters and converts to integer using atoi()
682+ */
532683uint8_t apps_terminal_read_unsigned_integer (uint16_t * integer ) {
533684 uint8_t length = 6 ;
534685 char string [length ];
@@ -541,16 +692,35 @@ uint8_t apps_terminal_read_unsigned_integer(uint16_t *integer) {
541692 return 0 ;
542693}
543694
695+ /**
696+ * @brief Generate a random 32-bit number
697+ * @return Random 32-bit unsigned integer
698+ * @note Uses the platform's random number generator
699+ */
544700uint32_t apps_generate_random_number (void ) {
545701 return rng_generate_random_number ();
546702}
547703
704+ /**
705+ * @brief Fill buffer with random data
706+ * @param pBuffer Pointer to buffer to fill
707+ * @param buffer_length Length of the buffer in bytes
708+ * @note Each byte is filled with a random value using the platform RNG
709+ */
548710void apps_randomize_buffer (uint8_t * pBuffer , uint16_t buffer_length ) {
549711 for (uint16_t i = 0 ; i < buffer_length ; i ++ ) {
550712 * (pBuffer + i ) = (rng_generate_random_number () & 0xFF );
551713 }
552714}
553715
716+ /**
717+ * @brief Compare two buffers byte by byte
718+ * @param pBuffer1 Pointer to first buffer
719+ * @param pBuffer2 Pointer to second buffer
720+ * @param buffers_length Length of buffers to compare in bytes
721+ * @return 0 if buffers are identical, 1 if any byte differs
722+ * @note Compares buffers element by element
723+ */
554724uint8_t apps_compare_buffers (uint8_t * pBuffer1 , uint8_t * pBuffer2 , uint16_t buffers_length ) {
555725
556726 for (uint16_t i = 0 ; i < buffers_length ; i ++ ) {
@@ -562,10 +732,20 @@ uint8_t apps_compare_buffers(uint8_t *pBuffer1, uint8_t *pBuffer2, uint16_t buff
562732 return 0 ;
563733}
564734
735+ /**
736+ * @brief Delay execution for specified milliseconds
737+ * @param ms Number of milliseconds to delay
738+ * @note Wrapper around platform delay function
739+ */
565740void apps_delay_ms (uint16_t ms ) {
566741 delay_ms (ms );
567742}
568743
744+ /**
745+ * @brief Process error by entering infinite loop
746+ * @param err Error code (currently unused)
747+ * @note This function never returns - used for error handling in examples
748+ */
569749void apps_process_error (uint32_t err ) {
570750 while (1 ) {
571751 /* Infinite loop */
0 commit comments