1919#include "wpa_supp_if.h"
2020#include <system/fmac_peer.h>
2121
22+ #ifdef CONFIG_NRF71_ON_IPC
23+ #include <psa/crypto.h>
24+ #include "wifi_keys.h"
25+ #endif
26+
2227LOG_MODULE_DECLARE (wifi_nrf , CONFIG_WIFI_NRF70_LOG_LEVEL );
2328
2429K_SEM_DEFINE (wait_for_event_sem , 0 , 1 );
@@ -964,6 +969,113 @@ int nrf_wifi_wpa_supp_associate(void *if_priv, struct wpa_driver_associate_param
964969 return ret ;
965970}
966971
972+ #ifdef CONFIG_NRF71_ON_IPC
973+ static bool is_mic_cipher_suite (unsigned int suite )
974+ {
975+ return (suite == RSN_CIPHER_SUITE_AES_128_CMAC ||
976+ suite == RSN_CIPHER_SUITE_BIP_GMAC_128 ||
977+ suite == RSN_CIPHER_SUITE_BIP_GMAC_256 ||
978+ suite == RSN_CIPHER_SUITE_BIP_CMAC_256 );
979+ }
980+
981+ /* Maximum number of keys we can track (unicast + group keys) */
982+ #define WIFI_CRYPTO_MAX_KEYS 8
983+
984+ /* Track installed keys: key_idx -> key_type mapping */
985+ static struct {
986+ bool valid ;
987+ wifi_keys_key_type_t type ;
988+ uint32_t db_id ;
989+ } installed_keys [WIFI_CRYPTO_MAX_KEYS ];
990+
991+ static int wifi_import_key_to_crypto (unsigned int suite , const unsigned char * key , size_t key_len ,
992+ const unsigned char * addr , int key_idx , uint32_t db_id )
993+ {
994+ wifi_keys_key_type_t type ;
995+ psa_key_attributes_t attr ;
996+ psa_key_id_t key_id ;
997+ psa_status_t status ;
998+ uint32_t key_index ;
999+ bool is_broadcast = false;
1000+
1001+ /* Determine if this is a broadcast/group key or unicast/pairwise key */
1002+ if (addr && is_broadcast_ether_addr (addr )) {
1003+ is_broadcast = true;
1004+ }
1005+
1006+ /* Determine key type based on cipher suite and address */
1007+ if (is_mic_cipher_suite (suite )) {
1008+ type = is_broadcast ? PEER_BCST_MIC : PEER_UCST_MIC ;
1009+ } else {
1010+ type = is_broadcast ? PEER_BCST_ENC : PEER_UCST_ENC ;
1011+ }
1012+
1013+ /* Convert key_idx to uint32_t, ensure it's within valid range */
1014+ key_index = (key_idx < 0 ) ? 0 : (uint32_t )key_idx ;
1015+
1016+ /* Initialize PSA key attributes */
1017+ attr = wifi_keys_key_attributes_init (type , db_id , key_index );
1018+
1019+ LOG_DBG ("%s: Importing key to PSA (suite: 0x%08x, type: %d, idx: %u, len: %zu)" ,
1020+ __func__ , suite , type , key_index , key_len );
1021+
1022+ /* Import key to PSA */
1023+ status = psa_import_key (& attr , key , key_len , & key_id );
1024+ if (status != PSA_SUCCESS ) {
1025+ LOG_ERR ("%s: Failed to import key to PSA: %d" , __func__ , status );
1026+ return - EIO ;
1027+ }
1028+
1029+ /* Track installed key for later destruction */
1030+ if (key_index < WIFI_CRYPTO_MAX_KEYS ) {
1031+ installed_keys [key_index ].valid = true;
1032+ installed_keys [key_index ].type = type ;
1033+ installed_keys [key_index ].db_id = db_id ;
1034+ }
1035+
1036+ LOG_DBG ("%s: Key imported successfully (type: %d, idx: %u)" , __func__ , type , key_index );
1037+
1038+ return 0 ;
1039+ }
1040+
1041+ static int wifi_destroy_key_from_crypto (int key_idx , uint32_t db_id )
1042+ {
1043+ psa_key_attributes_t attr ;
1044+ psa_key_id_t key_id ;
1045+ psa_status_t status ;
1046+ uint32_t key_index ;
1047+
1048+ /* Convert key_idx to uint32_t */
1049+ key_index = (key_idx < 0 ) ? 0 : (uint32_t )key_idx ;
1050+
1051+ if (key_index >= WIFI_CRYPTO_MAX_KEYS || !installed_keys [key_index ].valid ) {
1052+ LOG_WRN ("%s: No tracked key at index %u" , __func__ , key_index );
1053+ /* During init supplicant deletes all keys, so, suppress error */
1054+ return 0 ;
1055+ }
1056+
1057+ /* Get the key type that was used during import */
1058+ attr = wifi_keys_key_attributes_init (installed_keys [key_index ].type ,
1059+ installed_keys [key_index ].db_id , key_index );
1060+ key_id = psa_get_key_id (& attr );
1061+
1062+ LOG_DBG ("%s: Destroying key (type: %d, idx: %u, key_id: 0x%08x)" ,
1063+ __func__ , installed_keys [key_index ].type , key_index , key_id );
1064+
1065+ status = psa_destroy_key (key_id );
1066+ if (status != PSA_SUCCESS ) {
1067+ LOG_ERR ("%s: Failed to destroy key: %d" , __func__ , status );
1068+ return - EIO ;
1069+ }
1070+
1071+ /* Clear tracking entry */
1072+ installed_keys [key_index ].valid = false;
1073+
1074+ LOG_DBG ("%s: Key destroyed successfully" , __func__ );
1075+ return 0 ;
1076+ }
1077+ #endif
1078+
9671079int nrf_wifi_wpa_supp_set_key (void * if_priv , const unsigned char * ifname , enum wpa_alg alg ,
9681080 const unsigned char * addr , int key_idx , int set_tx ,
9691081 const unsigned char * seq , size_t seq_len , const unsigned char * key ,
@@ -972,7 +1084,7 @@ int nrf_wifi_wpa_supp_set_key(void *if_priv, const unsigned char *ifname, enum w
9721084 enum nrf_wifi_status status = NRF_WIFI_STATUS_FAIL ;
9731085 struct nrf_wifi_vif_ctx_zep * vif_ctx_zep = NULL ;
9741086 struct nrf_wifi_ctx_zep * rpu_ctx_zep = NULL ;
975- struct nrf_wifi_umac_key_info key_info ;
1087+ struct nrf_wifi_umac_key_info key_info = { 0 } ;
9761088 const unsigned char * mac_addr = NULL ;
9771089 unsigned int suite ;
9781090 int ret = -1 ;
@@ -1012,7 +1124,15 @@ int nrf_wifi_wpa_supp_set_key(void *if_priv, const unsigned char *ifname, enum w
10121124 goto out ;
10131125 }
10141126
1127+ #ifdef CONFIG_NRF71_ON_IPC
1128+ ret = wifi_import_key_to_crypto (suite , key , key_len , addr , key_idx , 0 );
1129+ if (ret ) {
1130+ LOG_ERR ("%s: Failed to import key to crypto: %d" , __func__ , ret );
1131+ goto out ;
1132+ }
1133+ #else
10151134 memcpy (key_info .key .nrf_wifi_key , key , key_len );
1135+ #endif
10161136
10171137 key_info .key .nrf_wifi_key_len = key_len ;
10181138 key_info .cipher_suite = suite ;
@@ -1050,7 +1170,16 @@ int nrf_wifi_wpa_supp_set_key(void *if_priv, const unsigned char *ifname, enum w
10501170 if (status != NRF_WIFI_STATUS_SUCCESS ) {
10511171 LOG_ERR ("%s: nrf_wifi_sys_fmac_del_key failed" , __func__ );
10521172 } else {
1173+ #ifdef CONFIG_NRF71_ON_IPC
1174+ /* Destroy PSA key after successful del_key */
1175+ ret = wifi_destroy_key_from_crypto (key_idx , 0 );
1176+ if (ret ) {
1177+ LOG_ERR ("%s: Failed to destroy key from crypto: %d" ,
1178+ __func__ , ret );
1179+ }
1180+ #else
10531181 ret = 0 ;
1182+ #endif
10541183 }
10551184 } else {
10561185 status = nrf_wifi_sys_fmac_add_key (rpu_ctx_zep -> rpu_ctx , vif_ctx_zep -> vif_idx ,
0 commit comments