【问题标题】:Public key encryption on TPM2 using TSS.NET使用 TSS.NET 在 TPM2 上进行公钥加密
【发布时间】:2020-05-12 09:34:24
【问题描述】:

是否可以在 .Net 应用程序中使用 Microsoft 的 TPM2 和 TSS.NET library 实现公钥加密? 更具体地说,TPM 中应该完成的部分是使用私钥解密,因此公钥应该能够导出到其他机器。 此外,密钥应存储在 TPM2 模块中。

我研究/研究了 Microsoft 提供的示例并阅读了 documentation,但我没有找到有关该主题的信息,或者我以某种方式错过了它。

为了在 TPM2 中存储数据,我找到了 nv 内存,但据我了解,这是用于将 TPM2 模块外部的数据存储在其中,但我不知道是否/如何存储TPM2 模块内部的某种密钥在其 nv 内存中,以便在下次启动时再次使用。

【问题讨论】:

    标签: .net public-key-encryption tpm


    【解决方案1】:

    似乎不存在 C# 的示例代码(目前),但是有 some sample C++ code 可以演示您想要的:

    void Samples::RsaEncryptDecrypt()
    {
        Announce("RsaEncryptDecrypt");
    
        // This sample demostrates the use of the TPM for RSA operations.
    
        // We will make a key in the "null hierarchy".
        TPMT_PUBLIC primTempl(TPM_ALG_ID::SHA1,
                              TPMA_OBJECT::decrypt | TPMA_OBJECT::userWithAuth | TPMA_OBJECT::sensitiveDataOrigin,
                              null,  // No policy
                              TPMS_RSA_PARMS(null, TPMS_SCHEME_OAEP(TPM_ALG_ID::SHA1), 2048, 65537),
                              TPM2B_PUBLIC_KEY_RSA());
    
        // Create the key
        auto storagePrimary = tpm.CreatePrimary(TPM_RH_NULL, null, primTempl, null, null);
    
        TPM_HANDLE& keyHandle = storagePrimary.handle;
    
        ByteVec dataToEncrypt = TPM_HASH::FromHashOfString(TPM_ALG_ID::SHA1, "secret");
        cout << "Data to encrypt: " << dataToEncrypt << endl;
    
        auto enc = tpm.RSA_Encrypt(keyHandle, dataToEncrypt, TPMS_NULL_ASYM_SCHEME(), null);
        cout << "RSA-encrypted data: " << enc << endl;
    
        auto dec = tpm.RSA_Decrypt(keyHandle, enc, TPMS_NULL_ASYM_SCHEME(), null);
        cout << "decrypted data: " << dec << endl;
        if (dec == dataToEncrypt)
            cout << "Decryption worked" << endl;
        _ASSERT(dataToEncrypt == dec);
    
        // Now encrypt using TSS.C++ library functions
        ByteVec mySecret = tpm._GetRandLocal(20);
        enc = storagePrimary.outPublic.Encrypt(mySecret, null);
        dec = tpm.RSA_Decrypt(keyHandle, enc, TPMS_NULL_ASYM_SCHEME(), null);
        cout << "My           secret: " << mySecret << endl;
        cout << "My decrypted secret: " << dec << endl;
        _ASSERT(mySecret == dec);
    
        // Now with padding
        ByteVec pad { 1, 2, 3, 4, 5, 6, 0 };
        enc = storagePrimary.outPublic.Encrypt(mySecret, pad);
        dec = tpm.RSA_Decrypt(keyHandle, enc, TPMS_NULL_ASYM_SCHEME(), pad);
        cout << "My           secret: " << mySecret << endl;
        cout << "My decrypted secret: " << dec << endl;
        _ASSERT(mySecret == dec);
    
        tpm.FlushContext(keyHandle);
    } // RsaEncryptDecrypt()
    

    当然,您可能希望使用 SRK 或子键,而不是创建新的主键。

    【讨论】:

    • 谢谢,这似乎是我想要的,但我得到了一个例外,尽管我将代码改编为 c#/.Net 并使用了正确的常量和构造函数而不是预处理器宏(如TpmRh.Null 而不是 TPM_RH_NULL)。 The Exception is Tpm2Lib.TpmException: Error {Symmetric} was returned for command CreatePrimary. Details: [Code=TpmRc.Symmetric],[RawCode=0x2D6,726] [ErrorEntity=Parameter], [ParmNum=2] [ParmName=inPublic]
    • 非常感谢您的帮助。我把它上传到pastebin 这是我当前的代码。它基本上只是“翻译”为 C# 的 C++ 代码,我使用 Authorization-Sample 作为代码的“框架”。被注释掉的参数是我已经尝试过的,而不是同一行中的当前参数。我尝试了我认为合乎逻辑的方法。我想我使用了一些错误的参数,但我无法弄清楚我必须改变什么。
    猜你喜欢
    • 1970-01-01
    • 2013-03-20
    • 2011-08-16
    • 2012-04-11
    • 2019-08-06
    • 2020-12-21
    • 2016-05-19
    • 2011-01-12
    • 2015-04-02
    相关资源
    最近更新 更多