2828#include <psa/nrf_platform_key_ids.h>
2929
3030LOG_MODULE_DECLARE (cracen , CONFIG_CRACEN_LOG_LEVEL );
31+ #if CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS
32+ #include "platform_keys/platform_keys.h"
33+ #endif
3134
3235#define NOT_ENABLED_CURVE (0)
3336#define NOT_ENABLED_HASH_ALG (0)
@@ -725,6 +728,58 @@ static int cracen_clean_ik_key(const uint8_t *user_data)
725728 return SX_OK ;
726729}
727730
731+ static bool cracen_is_ikg_key (const psa_key_attributes_t * attributes )
732+ {
733+ #if CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS
734+ return cracen_platform_keys_is_ikg_key (attributes );
735+ #else
736+ switch (MBEDTLS_SVC_KEY_ID_GET_KEY_ID (psa_get_key_id (attributes ))) {
737+ case CRACEN_BUILTIN_IDENTITY_KEY_ID :
738+ case CRACEN_BUILTIN_MKEK_ID :
739+ case CRACEN_BUILTIN_MEXT_ID :
740+ return true;
741+ default :
742+ return false;
743+ }
744+ #endif
745+ };
746+
747+ static psa_status_t cracen_load_ikg_keyref (const psa_key_attributes_t * attributes ,
748+ const uint8_t * key_buffer , size_t key_buffer_size ,
749+ struct sxkeyref * k )
750+ {
751+ k -> prepare_key = cracen_prepare_ik_key ;
752+ k -> clean_key = cracen_clean_ik_key ;
753+
754+ #if CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS
755+ if (key_buffer_size != sizeof (ikg_opaque_key )) {
756+ return PSA_ERROR_INVALID_ARGUMENT ;
757+ }
758+
759+ k -> cfg = ((ikg_opaque_key * )key_buffer )-> slot_number ;
760+ k -> owner_id = ((ikg_opaque_key * )key_buffer )-> owner_id ;
761+ #else
762+ /* IKG keys are identified from the ID */
763+ (void )key_buffer ;
764+ (void )key_buffer_size ;
765+
766+ switch (MBEDTLS_SVC_KEY_ID_GET_KEY_ID (psa_get_key_id (attributes ))) {
767+ case CRACEN_BUILTIN_MKEK_ID :
768+ k -> cfg = CRACEN_INTERNAL_HW_KEY1_ID ;
769+ break ;
770+ case CRACEN_BUILTIN_MEXT_ID :
771+ k -> cfg = CRACEN_INTERNAL_HW_KEY2_ID ;
772+ break ;
773+ default :
774+ return PSA_ERROR_INVALID_ARGUMENT ;
775+ };
776+
777+ k -> owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID (psa_get_key_id (attributes ));
778+ #endif
779+ k -> user_data = (uint8_t * )& k -> owner_id ;
780+ return PSA_SUCCESS ;
781+ }
782+
728783psa_status_t cracen_load_keyref (const psa_key_attributes_t * attributes , const uint8_t * key_buffer ,
729784 size_t key_buffer_size , struct sxkeyref * k )
730785{
@@ -761,39 +816,28 @@ psa_status_t cracen_load_keyref(const psa_key_attributes_t *attributes, const ui
761816 if (PSA_KEY_LIFETIME_GET_LOCATION (psa_get_key_lifetime (attributes )) ==
762817 PSA_KEY_LOCATION_CRACEN ) {
763818
764- k -> prepare_key = cracen_prepare_ik_key ;
765- k -> clean_key = cracen_clean_ik_key ;
819+ if (cracen_is_ikg_key (attributes )) {
820+ return cracen_load_ikg_keyref (attributes , key_buffer , key_buffer_size , k );
821+ }
822+
766823 k -> owner_id = MBEDTLS_SVC_KEY_ID_GET_OWNER_ID (psa_get_key_id (attributes ));
767824 k -> user_data = (uint8_t * )& k -> owner_id ;
825+ k -> prepare_key = NULL ;
826+ k -> clean_key = NULL ;
768827
769828 switch (MBEDTLS_SVC_KEY_ID_GET_KEY_ID (psa_get_key_id (attributes ))) {
770- case CRACEN_BUILTIN_MKEK_ID :
771- k -> cfg = CRACEN_INTERNAL_HW_KEY1_ID ;
772- break ;
773- case CRACEN_BUILTIN_MEXT_ID :
774- k -> cfg = CRACEN_INTERNAL_HW_KEY2_ID ;
775- break ;
776829 case CRACEN_PROTECTED_RAM_AES_KEY0_ID :
777830 k -> sz = 32 ;
778831 k -> key = (uint8_t * )CRACEN_PROTECTED_RAM_AES_KEY0 ;
779- k -> prepare_key = NULL ;
780- k -> clean_key = NULL ;
781832 break ;
782833 default :
783834 if (key_buffer_size == 0 ) {
784835 return PSA_ERROR_CORRUPTION_DETECTED ;
785836 }
786837
787- if (key_buffer_size == sizeof (ikg_opaque_key )) {
788- k -> cfg = ((ikg_opaque_key * )key_buffer )-> slot_number ;
789- k -> owner_id = ((ikg_opaque_key * )key_buffer )-> owner_id ;
790- } else {
791- /* Normal transparent key. */
792- k -> prepare_key = NULL ;
793- k -> clean_key = NULL ;
794- k -> key = key_buffer ;
795- k -> sz = key_buffer_size ;
796- }
838+ /* Normal transparent key. */
839+ k -> key = key_buffer ;
840+ k -> sz = key_buffer_size ;
797841 }
798842 } else {
799843 k -> key = key_buffer ;
@@ -803,30 +847,38 @@ psa_status_t cracen_load_keyref(const psa_key_attributes_t *attributes, const ui
803847 return PSA_SUCCESS ;
804848}
805849
850+ static psa_status_t cracen_get_ikg_opaque_key_size (const psa_key_attributes_t * attributes ,
851+ size_t * key_size )
852+ {
853+ #ifdef CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS
854+ return cracen_platform_keys_get_size (attributes , key_size );
855+ #else
856+ switch (MBEDTLS_SVC_KEY_ID_GET_KEY_ID (psa_get_key_id (attributes ))) {
857+ case CRACEN_BUILTIN_IDENTITY_KEY_ID :
858+ if (psa_get_key_type (attributes ) ==
859+ PSA_KEY_TYPE_ECC_KEY_PAIR (PSA_ECC_FAMILY_SECP_R1 )) {
860+ * key_size = sizeof (ikg_opaque_key );
861+ return PSA_SUCCESS ;
862+ }
863+ break ;
864+ case CRACEN_BUILTIN_MEXT_ID :
865+ case CRACEN_BUILTIN_MKEK_ID :
866+ if (psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ) {
867+ * key_size = sizeof (ikg_opaque_key );
868+ return PSA_SUCCESS ;
869+ }
870+ break ;
871+ }
872+
873+ return PSA_ERROR_INVALID_ARGUMENT ;
874+ #endif /* CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS */
875+ }
876+
806877psa_status_t cracen_get_opaque_size (const psa_key_attributes_t * attributes , size_t * key_size )
807878{
808879 if (PSA_KEY_LIFETIME_GET_LOCATION (psa_get_key_lifetime (attributes )) ==
809880 PSA_KEY_LOCATION_CRACEN ) {
810- switch (MBEDTLS_SVC_KEY_ID_GET_KEY_ID (psa_get_key_id (attributes ))) {
811- case CRACEN_BUILTIN_IDENTITY_KEY_ID :
812- if (psa_get_key_type (attributes ) ==
813- PSA_KEY_TYPE_ECC_KEY_PAIR (PSA_ECC_FAMILY_SECP_R1 )) {
814- * key_size = sizeof (ikg_opaque_key );
815- return PSA_SUCCESS ;
816- }
817- break ;
818- case CRACEN_BUILTIN_MEXT_ID :
819- case CRACEN_BUILTIN_MKEK_ID :
820- if (psa_get_key_type (attributes ) == PSA_KEY_TYPE_AES ) {
821- * key_size = sizeof (ikg_opaque_key );
822- return PSA_SUCCESS ;
823- }
824- break ;
825- #ifdef CONFIG_PSA_NEED_CRACEN_PLATFORM_KEYS
826- default :
827- return cracen_platform_keys_get_size (attributes , key_size );
828- #endif
829- }
881+ return cracen_get_ikg_opaque_key_size (attributes , key_size );
830882 }
831883
832884 if (PSA_KEY_LIFETIME_GET_LOCATION (psa_get_key_lifetime (attributes )) ==
0 commit comments