0

我正在使用带有 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;

}

之后我包装这个密钥(用于包装的密钥是相同的):

IObjectHandle generatedKey;
var mechanism = session.Factories.MechanismFactory.Create(CKM.CKM_AES_KEY_WRAP);
byte[] wrappedKey = session.WrapKey(mechanism, generatedKey, generatedKey)// result has 40 bytes

然后我尝试解密密钥以将其发送到另一台设备。

我的问题是,当我包装密钥时,我有 40 个字节的数组长度(不知道为什么是 40 而不是 32)。我不知道如何以编程方式或使用 hsm 解密它以获得 32 字节的 aes 密钥。这是 wrapedKye 的一些特定格式吗?是否有任何示例如何解密包装的密钥?

我可以获得密钥的cka_value,但就我而言,这不是一个可接受的解决方案。

4

1 回答 1

0

机制CKM_AES_KEY_WRAP使用NIST 特别出版物 800-38F(也在RFC 3394中描述)中定义的算法,这不是简单的加密。

对于简单的键值加密使用CKM_AES_ECBCKM_AES_CBC或类似的(取决于您的要求)。

注意:将密钥与自身包装以传输到另一台设备没有意义(看起来像鸡蛋问题)。

祝你的项目好运!

免责声明:我不是加密专家,所以请验证我的想法。

于 2021-10-25T14:43:16.930 回答