【问题标题】:How to decrypt softhsm wrappedKey如何解密softhsm WrappedKey
【发布时间】:2021-09-28 14:39:02
【问题描述】:

我正在使用带有 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,但在我的情况下,这不是一个可接受的解决方案。

【问题讨论】:

    标签: c# encryption hsm pkcs11interop


    【解决方案1】:

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

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

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

    祝你的项目好运!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多