Could someone tell me if I'm doing right
This is generally the way this is performed yes, as IES as explained in your other question.
The internal key pair needs to be ephemeral, i.e. you'd need to generate a new key pair for each key that is wrapped. This means that it should be a session key in the HSM.
Note that the IV is generally static. This is a huge issue as this means that private key information of the wrapped key may be leaked. For CBC mode you'd think that it would be relatively secure, but please remember that asymmetric, private keys are generally not fully randomized.
what I should use as shared and public data when the HSM generate the secret key
The shared data is the data used within the KDF as optional parameter. The shared data is inconsequential in this case, and might be NULL
, or something like the ASCII representation of "WRAPPING_KEY"
or something similar.
The public data is simply the public key of the EC "master" key pair.
Then, how could I recover this private key, with the help of the private part of the EC master key pair, in order to be sure that the process works well?
You perform the same operation using the public key of the internal key pair which you need to export and the EC master key. You'd have to use the same key derivation mechanism (KDF), with the same shared data. For EC key derivation you'd need to perform the KDF over the statically sized X-value of the ECDH (called "Z" in the standard, you'd need to get the ANSI or ISO standard to get why). Then you perform the decrypt / unwrap, giving you the encoded private key, which you'll probably have to decode.