为什么sign方法returns只有四个字节?

Why sign method returns only four bytes?

我正在尝试使用 pkcs11interop 实施 pkcs 11 标准

我有一些 des2 密钥(16 字节),我想使用符号方法

我的问题是符号方法 returns 只有四个字节。但我想要 8 字节符号。

我该怎么办?

PS 1:四字节结果正确。那是我预期结果的第一个字节。

PS 2:我知道签名方法和加密方法一样。所以我的解决方案之一是加密输入并获得前 8 个字节的结果(这就是我已经在做的)。但我对此感觉很糟糕,我认为最好使用 Sign 方法本身。

PS 3:有一个"ICkMacGeneralParams"接口可以用到selectMAC大小。但似乎根本没有影响!我将其设置为 UInt32.MaxValue 和 0,结果没有什么不同。

PS 4: 我知道 Sign 方法通常与 public 和私钥一起使用。但是我需要一键使用

        var data = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };

        //macParams seems not to work at all! result remains same with any input of CreateCkMacGeneralParams method
        var macParams = session.Factories.MechanismParamsFactory.CreateCkMacGeneralParams(8);

        var signMechanism = session.Factories.MechanismFactory.Create(CKM.CKM_DES3_MAC, macParams);
        //handle references to some 16 byte key with CKK_DES2
        var signResult = session.Sign(signMechanism, handle, data);
        //result is always 4 bytes

机制 CKM_DES3_MAC 始终为 DES 提供 4 个字节的输出,引用 "PKCS #11 v2.20",第 12.13.14 节:

It always produces an output of size half as large as <NAME>’s blocksize.

您需要使用 CKM_DES3_MAC_GENERAL,它允许签名长度达到 DES 块大小(请参阅第 12.13.13 节)。所需的签名长度在机制参数 CK_MAC_GENERAL_PARAMS.

中指定

如果您的令牌支持此机制,则以下代码应该有效:

var data = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

var macParams = session.Factories.MechanismParamsFactory.CreateCkMacGeneralParams(8);
var signMechanism = session.Factories.MechanismFactory.Create(CKM.CKM_DES3_MAC_GENERAL, macParams);
    var signResult = session.Sign(signMechanism, handle, data);

祝你好运!