PKCS11Interop 使用 SHA256 散列并使用 RSA 分两步签名

PKCS11Interop Hash with SHA256 and Sign with RSA in two steps

我有两个应用程序,一个计算文档的 SHA-256 哈希,另一个进行 RSA 签名。 尝试不同的事情我得出的结论是制作 CKM_SHA256 然后制作 CKM_RSA_PKCS 给出的结果与仅仅制作文档本身的 CKM_SHA256_RSA_PKCS 不同。

所以我的问题是,这两种实现之间有什么区别? CKM_SHA256_RSA_PKCS 机制中的散列中添加了哪些信息以产生完全不同的签名?

Mechanims CKM_SHA256_RSA_PKCS 做以下事情:

  1. CKM_SHA256一样计算数据的 SHA256 哈希值
  2. 构造在RFC 8017
  3. 中定义的DER编码DigestInfo结构
  4. 使用私钥签署 DigestInfo 结构,就像 CKM_RSA_PKCS 所做的那样

在构建 DER 编码的 DigestInfo 结构时,可以采用多种方法:

  1. Pkcs11Admin application I did use BouncyCastle 库中:
public static byte[] CreateDigestInfo(byte[] hash, string hashOid)
{
    DerObjectIdentifier derObjectIdentifier = new DerObjectIdentifier(hashOid);
    AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(derObjectIdentifier, null);
    DigestInfo digestInfo = new DigestInfo(algorithmIdentifier, hash);
    return digestInfo.GetDerEncoded();
}
  1. Pkcs11Interop.X509Store 库中我确实使用了预先计算的数组:
/// <summary>
/// Creates DER encoded PKCS#1 DigestInfo structure defined in RFC 8017
/// </summary>
/// <param name="hash">Hash value</param>
/// <param name="hashAlgorithm">Hash algorithm</param>
/// <returns>DER encoded PKCS#1 DigestInfo structure or null</returns>
private static byte[] CreatePkcs1DigestInfo(byte[] hash, HashAlgorithmName hashAlgorithm)
{
    if (hash == null || hash.Length == 0)
        throw new ArgumentNullException(nameof(hash));

    byte[] pkcs1DigestInfo = null;

    if (hashAlgorithm == HashAlgorithmName.MD5)
    {
        if (hash.Length != 16)
            throw new ArgumentException("Invalid lenght of hash value");

        pkcs1DigestInfo = new byte[] { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length);
    }
    else if (hashAlgorithm == HashAlgorithmName.SHA1)
    {
        if (hash.Length != 20)
            throw new ArgumentException("Invalid lenght of hash value");

        pkcs1DigestInfo = new byte[] { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length);
    }
    else if (hashAlgorithm == HashAlgorithmName.SHA256)
    {
        if (hash.Length != 32)
            throw new ArgumentException("Invalid lenght of hash value");

        pkcs1DigestInfo = new byte[] { 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length);
    }
    else if (hashAlgorithm == HashAlgorithmName.SHA384)
    {
        if (hash.Length != 48)
            throw new ArgumentException("Invalid lenght of hash value");

        pkcs1DigestInfo = new byte[] { 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length);
    }
    else if (hashAlgorithm == HashAlgorithmName.SHA512)
    {
        if (hash.Length != 64)
            throw new ArgumentException("Invalid lenght of hash value");

        pkcs1DigestInfo = new byte[] { 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
        Array.Copy(hash, 0, pkcs1DigestInfo, pkcs1DigestInfo.Length - hash.Length, hash.Length);
    }

    return pkcs1DigestInfo;
}