如何使用TSS.MSR(C ++)导入Elliptical p256公钥

时间:2019-10-22 16:34:40

标签: c++ tpm

我正在尝试使用Microsoft的MSR.TSS库(C ++)将另一个系统的公钥导入到我的系统中,以便建立Diffie-Hellman密钥交换。

但是我得到以下错误: “ TPM错误-TPM_RC :: SIZE:试图加入或替换驱动器,该驱动器上的目录是先前替换的目标。”

这是我的示例代码:

Dim baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}"
Dim baseString = String.Format(baseFormat, OAUTH_KEY, OAUTH_NONCE, OAUTH_SIGN_METHOD, OAUTH_TIMESTAMP, OAUTH_TOKEN, OAUTH_VERSION, Uri.EscapeDataString(URL_METHOD))
baseString = String.Concat(URL_METHOD & "&", Uri.EscapeDataString(URL_BASE), "&", Uri.EscapeDataString(baseString))

Dim compositeKey = String.Concat(Uri.EscapeDataString(OAUTH_KEY_SECRET), "&", Uri.EscapeDataString(OAUTH_TOKEN_SECRET))
Using hasher As New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))
    OAUTH_SIGN = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))
End Using

几件事要注意: 1)如果我传入一个空向量代替“ pubVector”,它将起作用 2)如果我忽略了0x04(表示未压缩的公钥),它仍然会失败

我的工作基于以下代码: https://github.com/microsoft/TSS.MSR/tree/master/TSS.CPP/Samples

1 个答案:

答案 0 :(得分:0)

我知道了: “创建”方法从头开始创建密钥,我需要“ LoadExternal”方法来加载公共密钥部分:

        //publicKey is a BYTE array of the p256 public key 64 bytes long (without the leading 04 = uncompressed)
        vector<BYTE> pubKeyX(publicKey, publicKey + 32);
        vector<BYTE> pubKeyY(publicKey+32, publicKey + 64);

        TPMT_PUBLIC eccTemplate(TPM_ALG_ID::SHA1,
            TPMA_OBJECT::decrypt | 
            TPMA_OBJECT::fixedParent | 
            TPMA_OBJECT::fixedTPM |
            TPMA_OBJECT::sensitiveDataOrigin | 
            TPMA_OBJECT::userWithAuth,
            NullVec,
            TPMS_ECC_PARMS(
                TPMT_SYM_DEF_OBJECT(TPM_ALG_ID::_NULL, 0, TPM_ALG_ID::_NULL), 
                TPMS_KEY_SCHEME_ECDH(TPM_ALG_ID::SHA256), 
                TPM_ECC_CURVE::NIST_P256, 
                TPMS_NULL_KDF_SCHEME()),
            TPMS_ECC_POINT(pubKeyX, pubKeyY)
        );

        pubHandle = _tpm.LoadExternal(TPMT_SENSITIVE::NullObject(), eccTemplate, TPM_HANDLE::FromReservedHandle(TPM_RH::_NULL));