1717
1818/* Includes ------------------------------------------------------------------*/
1919
20- #include <api/stse_symmetric_keys_management.h>
20+ #include "api/stse_symmetric_keys_management.h"
21+ #include "services/stsafea/stsafea_public_key_slots.h"
2122
2223#define STSAFEA_HOST_KEY_WRAPPING_AUTHENTICATION_TAG_LENGTH STSE_KEK_ENVELOPE_MAC_SIZE
2324#define STSE_HOST_KEY_ENVELOPE_FRONT_PADDING_LENGTH 3U
@@ -685,8 +686,8 @@ stse_ReturnCode_t stse_host_key_provisioning_wrapped_authenticated(
685686#ifdef STSE_CONF_USE_HOST_KEY_ESTABLISHMENT
686687stse_ReturnCode_t stse_establish_host_key (
687688 stse_Handler_t * pSTSE ,
688- stse_ecc_key_type_t host_ecdh_key_type ,
689- stsafea_host_key_type_t host_keys_type ,
689+ stse_ecc_key_type_t ecdh_key_type ,
690+ stsafea_host_key_type_t host_secure_channel_keys_type ,
690691 PLAT_UI8 * host_mac_key ,
691692 PLAT_UI8 * host_cipher_key ) {
692693 stse_ReturnCode_t ret ;
@@ -696,18 +697,18 @@ stse_ReturnCode_t stse_establish_host_key(
696697 return (STSE_API_HANDLER_NOT_INITIALISED );
697698 }
698699
699- if (host_keys_type == STSAFEA_AES_INVALID_HOST_KEY || host_mac_key == NULL || host_cipher_key == NULL
700+ if (host_secure_channel_keys_type == STSAFEA_AES_INVALID_HOST_KEY || host_mac_key == NULL || host_cipher_key == NULL
700701#ifdef STSE_CONF_ECC_EDWARD_25519
701- || host_ecdh_key_type == STSE_ECC_KT_ED25519
702+ || ecdh_key_type == STSE_ECC_KT_ED25519
702703#endif /* STSE_CONF_ECC_EDWARD_25519 */
703704 ) {
704705 return STSE_API_INVALID_PARAMETER ;
705706 }
706707
707708 /* - allocate ECDHE buffer sizes according to ECC key type */
708- PLAT_UI16 pub_key_size = stse_ecc_info_table [host_ecdh_key_type ].public_key_size ;
709- PLAT_UI16 priv_key_size = stse_ecc_info_table [host_ecdh_key_type ].private_key_size ;
710- PLAT_UI16 shared_secret_size = stse_ecc_info_table [host_ecdh_key_type ].shared_secret_size ;
709+ PLAT_UI16 pub_key_size = stse_ecc_info_table [ecdh_key_type ].public_key_size ;
710+ PLAT_UI16 priv_key_size = stse_ecc_info_table [ecdh_key_type ].private_key_size ;
711+ PLAT_UI16 shared_secret_size = stse_ecc_info_table [ecdh_key_type ].shared_secret_size ;
711712 PLAT_UI16 hkdf_info_size = 2 * pub_key_size ;
712713
713714 PLAT_UI8 host_ecdhe_public_key [pub_key_size ];
@@ -720,13 +721,13 @@ stse_ReturnCode_t stse_establish_host_key(
720721 PLAT_UI8 pHkdf_info [hkdf_info_size ];
721722
722723 /* - Initialize the OKM length with the confirmation key length */
723- PLAT_UI16 host_mac_key_length = host_keys_type == STSAFEA_AES_256_HOST_KEY ? STSAFEA_HOST_AES_256_MAC_KEY_SIZE : STSAFEA_HOST_AES_128_MAC_KEY_SIZE ;
724- PLAT_UI16 host_cipher_key_length = host_keys_type == STSAFEA_AES_256_HOST_KEY ? STSAFEA_HOST_AES_256_CIPHER_KEY_SIZE : STSAFEA_HOST_AES_128_CIPHER_KEY_SIZE ;
724+ PLAT_UI16 host_mac_key_length = host_secure_channel_keys_type == STSAFEA_AES_256_HOST_KEY ? STSAFEA_HOST_AES_256_MAC_KEY_SIZE : STSAFEA_HOST_AES_128_MAC_KEY_SIZE ;
725+ PLAT_UI16 host_cipher_key_length = host_secure_channel_keys_type == STSAFEA_AES_256_HOST_KEY ? STSAFEA_HOST_AES_256_CIPHER_KEY_SIZE : STSAFEA_HOST_AES_128_CIPHER_KEY_SIZE ;
725726 PLAT_UI8 okm_buffer [host_mac_key_length + host_cipher_key_length ];
726727
727728 /* - Generate local host ECDHE key pair */
728729 ret = stse_platform_ecc_generate_key_pair (
729- host_ecdh_key_type ,
730+ ecdh_key_type ,
730731 host_ecdhe_private_key ,
731732 host_ecdhe_public_key );
732733 if (ret != STSE_OK ) {
@@ -739,7 +740,7 @@ stse_ReturnCode_t stse_establish_host_key(
739740 /* - Generate ECDHE key pair on target SE */
740741 ret = stsafea_generate_ECDHE_key_pair (
741742 pSTSE ,
742- host_ecdh_key_type ,
743+ ecdh_key_type ,
743744 stsafe_ecdhe_public_key );
744745 if (ret != STSE_OK ) {
745746 /* Clear key pairs info on SE ECDHE key pair generation error */
@@ -751,7 +752,7 @@ stse_ReturnCode_t stse_establish_host_key(
751752
752753 /* - Process Diffie-Hellman on host side */
753754 ret = stse_platform_ecc_ecdh (
754- host_ecdh_key_type ,
755+ ecdh_key_type ,
755756 stsafe_ecdhe_public_key ,
756757 host_ecdhe_private_key ,
757758 shared_secret );
@@ -775,7 +776,7 @@ stse_ReturnCode_t stse_establish_host_key(
775776 ret = stse_platform_hmac_sha256_compute (pHkdf_salt ,
776777 STSAFEA_KEK_HKDF_SALT_SIZE ,
777778 shared_secret ,
778- stse_ecc_info_table [host_ecdh_key_type ].shared_secret_size ,
779+ stse_ecc_info_table [ecdh_key_type ].shared_secret_size ,
779780 pHkdf_info ,
780781 2 * pub_key_size ,
781782 okm_buffer ,
@@ -793,9 +794,9 @@ stse_ReturnCode_t stse_establish_host_key(
793794 /* - Establish the host keys through STSAFE */
794795 ret = stsafea_establish_host_key (
795796 pSTSE ,
796- host_ecdh_key_type ,
797+ ecdh_key_type ,
797798 host_ecdhe_public_key ,
798- host_keys_type );
799+ host_secure_channel_keys_type );
799800
800801 memset (host_ecdhe_public_key , 0 , sizeof (host_ecdhe_public_key ));
801802 if (ret != STSE_OK ) {
@@ -813,6 +814,255 @@ stse_ReturnCode_t stse_establish_host_key(
813814
814815 return ret ;
815816}
817+
818+ stse_ReturnCode_t stse_establish_host_key_authenticated (
819+ stse_Handler_t * pSTSE ,
820+ stse_ecc_key_type_t ecdh_key_type ,
821+ stsafea_host_key_type_t host_secure_channel_keys_type ,
822+ stse_hash_algorithm_t tbs_hash_algo ,
823+ PLAT_UI8 tbs_public_key_slot ,
824+ PLAT_UI8 * tbs_private_key ,
825+ PLAT_UI8 * host_mac_key ,
826+ PLAT_UI8 * host_cipher_key ) {
827+ stse_ReturnCode_t ret ;
828+
829+ /* - Check function parameters */
830+ if (pSTSE == NULL ) {
831+ return (STSE_API_HANDLER_NOT_INITIALISED );
832+ }
833+
834+ if (host_secure_channel_keys_type == STSAFEA_AES_INVALID_HOST_KEY || host_mac_key == NULL || host_cipher_key == NULL
835+ #ifdef STSE_CONF_ECC_EDWARD_25519
836+ || ecdh_key_type == STSE_ECC_KT_ED25519
837+ #endif /* STSE_CONF_ECC_EDWARD_25519 */
838+ ) {
839+ return STSE_API_INVALID_PARAMETER ;
840+ }
841+
842+ /* - allocate ECDHE buffer sizes according to ECC key type */
843+ PLAT_UI16 pub_key_size = stse_ecc_info_table [ecdh_key_type ].public_key_size ;
844+ PLAT_UI16 priv_key_size = stse_ecc_info_table [ecdh_key_type ].private_key_size ;
845+ PLAT_UI16 shared_secret_size = stse_ecc_info_table [ecdh_key_type ].shared_secret_size ;
846+ PLAT_UI16 hkdf_info_size = 2 * pub_key_size ;
847+
848+ PLAT_UI8 host_ecdhe_public_key [pub_key_size ];
849+ PLAT_UI8 host_ecdhe_private_key [priv_key_size ];
850+ PLAT_UI8 stsafe_ecdhe_public_key [pub_key_size ];
851+
852+ PLAT_UI8 shared_secret [2 * shared_secret_size ];
853+
854+ PLAT_UI8 pHkdf_salt [STSAFEA_KEK_HKDF_SALT_SIZE ] = STSAFEA_KEK_HKDF_SALT ;
855+ PLAT_UI8 pHkdf_info [hkdf_info_size ];
856+
857+ /* Get public key slot type & configuration */
858+ PLAT_UI8 tbs_presence_flag = 0 ;
859+ stsafea_generic_public_key_configuration_flags_t tbs_configuration_flags = {0 };
860+ stse_ecc_key_type_t tbs_key_type ;
861+ ret = stsafea_query_generic_public_key_slot_info (pSTSE , tbs_public_key_slot , & tbs_presence_flag , & tbs_configuration_flags , & tbs_key_type );
862+ if (ret != STSE_OK ) {
863+ return ret ;
864+ }
865+ if (tbs_presence_flag == 0 ) {
866+ return STSE_API_KEY_NOT_FOUND ;
867+ }
868+ if (tbs_configuration_flags .establish_host_key == 0 ) {
869+ return STSE_API_INVALID_SIGNATURE ;
870+ }
871+
872+ /* Allocate signature buffer */
873+ PLAT_UI8 signature [stse_ecc_info_table [tbs_key_type ].signature_size ];
874+
875+ /* - Initialize the OKM length with the confirmation key length */
876+ PLAT_UI16 host_mac_key_length = host_secure_channel_keys_type == STSAFEA_AES_256_HOST_KEY ? STSAFEA_HOST_AES_256_MAC_KEY_SIZE : STSAFEA_HOST_AES_128_MAC_KEY_SIZE ;
877+ PLAT_UI16 host_cipher_key_length = host_secure_channel_keys_type == STSAFEA_AES_256_HOST_KEY ? STSAFEA_HOST_AES_256_CIPHER_KEY_SIZE : STSAFEA_HOST_AES_128_CIPHER_KEY_SIZE ;
878+ PLAT_UI8 okm_buffer [host_mac_key_length + host_cipher_key_length ];
879+
880+ /* - Generate local host ECDHE key pair */
881+ ret = stse_platform_ecc_generate_key_pair (
882+ ecdh_key_type ,
883+ host_ecdhe_private_key ,
884+ host_ecdhe_public_key );
885+ if (ret != STSE_OK ) {
886+ /* Clear generated keypair on ECC key generation failure */
887+ memset (host_ecdhe_public_key , 0 , sizeof (host_ecdhe_public_key ));
888+ memset (host_ecdhe_private_key , 0 , sizeof (host_ecdhe_private_key ));
889+ return (STSE_UNEXPECTED_ERROR );
890+ }
891+
892+ /* - Generate ECDHE key pair on target SE */
893+ ret = stsafea_generate_ECDHE_key_pair (
894+ pSTSE ,
895+ ecdh_key_type ,
896+ stsafe_ecdhe_public_key );
897+ if (ret != STSE_OK ) {
898+ /* Clear key pairs info on SE ECDHE key pair generation error */
899+ memset (stsafe_ecdhe_public_key , 0 , sizeof (stsafe_ecdhe_public_key ));
900+ memset (host_ecdhe_public_key , 0 , sizeof (host_ecdhe_public_key ));
901+ memset (host_ecdhe_private_key , 0 , sizeof (host_ecdhe_private_key ));
902+ return ret ;
903+ }
904+
905+ /* - Process Diffie-Hellman on host side */
906+ ret = stse_platform_ecc_ecdh (
907+ ecdh_key_type ,
908+ stsafe_ecdhe_public_key ,
909+ host_ecdhe_private_key ,
910+ shared_secret );
911+ memset (host_ecdhe_private_key , 0 , sizeof (host_ecdhe_private_key ));
912+ if (ret != STSE_OK ) {
913+ memset (stsafe_ecdhe_public_key , 0 , sizeof (stsafe_ecdhe_public_key ));
914+ memset (host_ecdhe_public_key , 0 , sizeof (host_ecdhe_public_key ));
915+ return (STSE_UNEXPECTED_ERROR );
916+ }
917+
918+ /* - Derive host keys from shared secret */
919+ memcpy (pHkdf_info ,
920+ host_ecdhe_public_key ,
921+ pub_key_size );
922+
923+ memcpy (pHkdf_info + pub_key_size ,
924+ stsafe_ecdhe_public_key ,
925+ pub_key_size );
926+
927+ ret = stse_platform_hmac_sha256_compute (pHkdf_salt ,
928+ STSAFEA_KEK_HKDF_SALT_SIZE ,
929+ shared_secret ,
930+ stse_ecc_info_table [ecdh_key_type ].shared_secret_size ,
931+ pHkdf_info ,
932+ 2 * pub_key_size ,
933+ okm_buffer ,
934+ host_mac_key_length + host_cipher_key_length );
935+ memset (shared_secret , 0 , sizeof (shared_secret ));
936+ if (ret != STSE_OK ) {
937+ memset (stsafe_ecdhe_public_key , 0 , sizeof (stsafe_ecdhe_public_key ));
938+ memset (host_ecdhe_public_key , 0 , sizeof (host_ecdhe_public_key ));
939+ memset (okm_buffer , 0 , sizeof (okm_buffer ));
940+ return (STSE_UNEXPECTED_ERROR );
941+ }
942+
943+ /* Allocate TBS buffer */
944+ PLAT_UI16 tbs_pub_key_length = pub_key_size ;
945+ #ifdef STSE_CONF_ECC_CURVE_25519
946+ if (ecdh_key_type == STSE_ECC_KT_CURVE25519 ) {
947+ tbs_pub_key_length += STSE_ECC_GENERIC_LENGTH_SIZE ;
948+ } else
949+ #endif
950+ {
951+ tbs_pub_key_length += STSE_NIST_BRAINPOOL_POINT_REPRESENTATION_ID_SIZE + 2 * STSE_ECC_GENERIC_LENGTH_SIZE ;
952+ }
953+ PLAT_UI16 tbs_length = stse_ecc_info_table [ecdh_key_type ].curve_id_total_length + 2 * tbs_pub_key_length ;
954+ PLAT_UI8 pTBS [tbs_length ];
955+
956+ /* Copy curve ID into TBS buffer */
957+ memcpy (pTBS ,
958+ (PLAT_UI8 * )& stse_ecc_info_table [ecdh_key_type ].curve_id ,
959+ stse_ecc_info_table [ecdh_key_type ].curve_id_total_length );
960+ PLAT_UI16 copy_index = stse_ecc_info_table [ecdh_key_type ].curve_id_total_length ;
961+
962+ /* Copy Host ECDHE public key into TBS */
963+ #ifdef STSE_CONF_ECC_CURVE_25519
964+ if (ecdh_key_type != STSE_ECC_KT_CURVE25519 )
965+ #endif
966+ {
967+ pTBS [copy_index ] = STSE_NIST_BRAINPOOL_POINT_REPRESENTATION_ID ;
968+ copy_index += STSE_NIST_BRAINPOOL_POINT_REPRESENTATION_ID_SIZE ;
969+ }
970+
971+ pTBS [copy_index ] = UI16_B1 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
972+ pTBS [copy_index + 1 ] = UI16_B0 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
973+ copy_index += STSE_ECC_GENERIC_LENGTH_SIZE ;
974+
975+ memcpy (pTBS + copy_index ,
976+ host_ecdhe_public_key ,
977+ stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
978+ copy_index += stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size ;
979+
980+ #ifdef STSE_CONF_ECC_CURVE_25519
981+ if (ecdh_key_type != STSE_ECC_KT_CURVE25519 )
982+ #endif
983+ {
984+ pTBS [copy_index ] = UI16_B1 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
985+ pTBS [copy_index + 1 ] = UI16_B0 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
986+ copy_index += STSE_ECC_GENERIC_LENGTH_SIZE ;
987+
988+ memcpy (pTBS + copy_index ,
989+ host_ecdhe_public_key + stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size ,
990+ stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
991+ copy_index += stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size ;
992+ }
993+
994+ /* Copy STSAFE-A public key into TBS */
995+ #ifdef STSE_CONF_ECC_CURVE_25519
996+ if (ecdh_key_type != STSE_ECC_KT_CURVE25519 )
997+ #endif
998+ {
999+ pTBS [copy_index ] = STSE_NIST_BRAINPOOL_POINT_REPRESENTATION_ID ;
1000+ copy_index += STSE_NIST_BRAINPOOL_POINT_REPRESENTATION_ID_SIZE ;
1001+ }
1002+
1003+ pTBS [copy_index ] = UI16_B1 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
1004+ pTBS [copy_index + 1 ] = UI16_B0 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
1005+ copy_index += STSE_ECC_GENERIC_LENGTH_SIZE ;
1006+
1007+ memcpy (pTBS + copy_index ,
1008+ stsafe_ecdhe_public_key ,
1009+ stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
1010+ copy_index += stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size ;
1011+
1012+ #ifdef STSE_CONF_ECC_CURVE_25519
1013+ if (ecdh_key_type != STSE_ECC_KT_CURVE25519 )
1014+ #endif
1015+ {
1016+ pTBS [copy_index ] = UI16_B1 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
1017+ pTBS [copy_index + 1 ] = UI16_B0 (stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
1018+ copy_index += STSE_ECC_GENERIC_LENGTH_SIZE ;
1019+
1020+ memcpy (pTBS + copy_index ,
1021+ stsafe_ecdhe_public_key + stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size ,
1022+ stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size );
1023+ copy_index += stse_ecc_info_table [ecdh_key_type ].coordinate_or_key_size ;
1024+ }
1025+
1026+ memset (stsafe_ecdhe_public_key , 0 , sizeof (stsafe_ecdhe_public_key ));
1027+
1028+ /* Generate signature */
1029+ ret = stsafea_sign_for_generic_public_key_slot (pSTSE ,
1030+ tbs_key_type ,
1031+ tbs_private_key ,
1032+ tbs_hash_algo ,
1033+ tbs_length ,
1034+ pTBS ,
1035+ signature );
1036+ if (ret != STSE_OK ) {
1037+ memset (okm_buffer , 0 , sizeof (okm_buffer ));
1038+ return ret ;
1039+ }
1040+
1041+ /* - Establish the host keys through STSAFE with authentication */
1042+ ret = stsafea_establish_host_key_authenticated (pSTSE ,
1043+ ecdh_key_type ,
1044+ host_ecdhe_public_key ,
1045+ host_secure_channel_keys_type ,
1046+ tbs_public_key_slot ,
1047+ tbs_key_type ,
1048+ tbs_hash_algo ,
1049+ signature );
1050+ memset (host_ecdhe_public_key , 0 , sizeof (host_ecdhe_public_key ));
1051+ if (ret != STSE_OK ) {
1052+ memset (okm_buffer , 0 , sizeof (okm_buffer ));
1053+ return ret ;
1054+ }
1055+
1056+ memcpy (host_mac_key ,
1057+ okm_buffer ,
1058+ host_mac_key_length );
1059+ memcpy (host_cipher_key ,
1060+ okm_buffer + host_mac_key_length ,
1061+ host_cipher_key_length );
1062+ memset (okm_buffer , 0 , sizeof (okm_buffer ));
1063+
1064+ return ret ;
1065+ }
8161066#endif /* STSE_CONF_USE_HOST_KEY_ESTABLISHMENT */
8171067
8181068/* ------------------------------------------------------------------------------------------
0 commit comments