I want to wrap a private key out of a HSM, using an external EC key pair (master key) and then verify that I can recover it.
The wrapping occurs as follow:
- Generate a secret AES key in the HSM, using the public part of the EC master key, the private part of the internal key pair and a derivation mechanism
CKM_ECDH1_DERIVE
. The derivation parameters for this mechanism are: a derivation functionCKD_SHA256_KDF
, shared data and public data (public data are taken from the public EC master key). - Wrap the private key, using the secret AES key and a mechanims such as
CKM_AES_GCM
,CKM_AES_KEY_WRAP_PAD
orCKM_AES_CBC_PAD
. - The HSM returns a byte array.
Then I would like to verify if the wrapped private is the expected one.
I know how to decrypt the private key once I have recovered the secret key used ot protect it. Because it is not like RSA, I have to derive the same secret key using some elements I have, but I don't know how to do this with BC.
I'm trying to use something like this, trying to find an concrete implementation of AlgorithmParamSpec
:
KeyAgreement agreement = KeyAgreement.getInstance("ECCDHwithSHA256CKDF", "BC");
agreement.init(externalEcMasterKey.getPrivate(), someAlgorithmParamSpec);
agreement.doPhase(internalEcKeyPair.asJavaPublic(), true);
SecretKey agreedKey = agreement.generateSecret("AES[256]");
Unfortunately, with UserKeyingMaterialSpec
for example, it returns a different key at each time, which is not what I want :)
Thanks in advance