我在SoftHsm2中使用pkcs11interop库
我已经生成了aes密钥:
var mechanism = session.Factories.MechanismFactory.Create(CKM.CKM_AES_KEY_GEN);
var generatedKey = session.GenerateKey(mechanism, AesKeyAtribute(hsmSession, label));
private List<IObjectAttribute> AesKeyAtribute(IHsmSession hsmSession, string label, bool storeOnToken)
{
List<IObjectAttribute> objectAttributes = new List<IObjectAttribute>();
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VALUE_LEN, 32));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE, CKK.CKK_AES));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ENCRYPT, true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DECRYPT, true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DERIVE, true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_EXTRACTABLE, true));
objectAttributes.Add(session.Factories.ObjectAttributeFactory.Create(CKA.CKA_LABEL, label));
return objectAttributes;
}之后我包装这个key (用于包装的key是相同的):
IObjectHandle generatedKey;
var mechanism = session.Factories.MechanismFactory.Create(CKM.CKM_AES_KEY_WRAP);
byte[] wrappedKey = session.WrapKey(mechanism, generatedKey, generatedKey)// result has 40 bytes然后,我尝试解密密钥以将其发送到另一台设备。
我的问题是,当我包装key时,我有40字节的数组长度(不知道为什么是40而不是32)。我不知道如何通过程序或hsm来解密它以获得32字节的aes密钥。它是wrapedKye的某种特定格式吗?有没有示例说明如何解密封装的密钥?
我可以获得key的cka_value,但在我的例子中,它不是一个被接受的解决方案。
发布于 2021-10-25 14:43:17
CKM_AES_KEY_WRAP机制使用在NIST Special Publication 800-38F (也在RFC 3394中描述)中定义的算法,这不是一种直接的加密。
对于简单的密钥值加密,使用CKM_AES_ECB、CKM_AES_CBC或类似的(取决于您的要求)。
注意:将密钥自身包装起来传输到另一台设备是没有意义的(看起来像是先有鸡还是先有蛋的问题)。
祝你的项目好运!
免责声明:我不是密码专家,所以请确认我的想法。
https://stackoverflow.com/questions/69363801
复制相似问题