Some update: provided code was intended mainly for S32K1 and its CSEc. So, make sure that "CSEC" is not defined because EmptyKey needs to be all '0' for HSE:
#ifdef CSEC
static uint8 au8EmptyKey[SHE_KEY_SIZE] =
{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
#else
/* Array to be passed to Crypto driver during various operations as output buffer */
static uint8 au8EmptyKey[SHE_KEY_SIZE] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#endif
And next thing - CSEc on S32K1 has either 5 or 6 attribute key flags. SHE define 5 flags and there could be one more flag enabled (VERIFY_ONLY). HSE always expects 6 flags. But I can see that the code expects 5 flags only:
aM2Plain[3] |= (flags & 0x1e) >> 1U;
aM2Plain[4] |= (flags & 0x01) << 7U;
You can update it to this:
aM2Plain[3] |= (flags & 0x3c) >> 2U;
aM2Plain[4] |= (flags & 0x03) << 6U;
To understand where it goes - there's 28bit counter in M2 value and then the flags...

But if you used flags=0, I don't think this will make a difference.