简体   繁体   English

Web Crypto API – ECDH-是否可以仅使用私钥组件而不使用公钥组件导入私钥?

[英]Web Crypto API – ECDH - Possible to import private key with only the private key component, without the public key components?

Given a P-256 Elliptical Curve Diffie-Hellman Private Key (which is simply a random 256-bit integer): is it possible to import this private key into a CryptoKey object, using the window.crypto.subtle.importKey() method of the Web Crypto API – without having the public key components? 给定一个P-256椭圆曲线Diffie-Hellman私钥(它只是一个256位随机整数):是否可以使用的window.crypto.subtle.importKey()方法将此私钥导入CryptoKey对象。 Web Crypto API –没有公共密钥组件?

I know that it's possible to import the private key if the public key components (which are derived from the private key component) are available as well. 我知道,如果公钥组件(从私钥组件派生)也可用,则可以导入私钥。 For example, the code below runs successfully: 例如,以下代码成功运行:

window.crypto.subtle.importKey(
    "jwk", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
    {
    "crv":"P-256",
    "d":"eM8u2176zFk9bwDP_jbJqnm-TlSo6GX702D9I_1AqBU",
    "ext":true,
    "key_ops":["deriveKey","deriveBits"],
    "kty":"EC",
    "x":"5Uw_SuaGZTFAuQuDArnLEmmyp4TpHx3AlBxL4EUEzbQ",
    "y":"RO5t581VBuAKTQZVPSB__ebV6y9GCzrl8lBV2-p9BlM"
    },
    {   //these are the algorithm options
    name: "ECDH",
    namedCurve: "P-256", //can be "P-256", "P-384", or "P-521"
    },
    true, //whether the key is extractable (i.e. can be used in exportKey)
    ["deriveKey"] //"deriveKey" and/or "deriveBits" for private keys only (just put an empty list if importing a public key)
)
.then(function(key) {
    console.log(key);
})
.catch(function(err){
    console.error(err);
});

However, if only the private key component is available, without the public key components, as in the code below, the Web Crypto API throws a DOMException with the message 'Data provided to an operation does not meet requirements'. 但是,如果只有私钥组件可用,而没有公钥组件可用,如下面的代码所示,则Web Crypto API会引发DOMException,并显示消息“提供给操作的数据不符合要求”。

window.crypto.subtle.importKey(
    "jwk", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
    {
    "crv":"P-256",
    "d":"eM8u2176zFk9bwDP_jbJqnm-TlSo6GX702D9I_1AqBU",
    "ext":true,
    "key_ops":["deriveKey","deriveBits"],
    "kty":"EC"
    },
    {   //these are the algorithm options
    name: "ECDH",
    namedCurve: "P-256", //can be "P-256", "P-384", or "P-521"
    },
    true, //whether the key is extractable (i.e. can be used in exportKey)
    ["deriveKey"] //"deriveKey" and/or "deriveBits" for private keys only (just put an empty list if importing a public key)
)
.then(function(key) {
    console.log(key);
})
.catch(function(err){
    console.error(err);
});

I've also tried using pkcs8 format for the key, instead of JWK, but no luck there either. 我也尝试过使用pkcs8格式作为密钥,而不是JWK,但是那里也没有运气。

The documentation for the Web Crypto API shows that it is possible to import a ECDH private key in JWK format – so, it seems that it should be possible to do so without the public key components as well (the Web Crypto API should be able to calculate the public key components, if needed, from the private key component internally, as it seems to do with the .generateKey() method). Web Crypto API的文档显示,可以导入JWK格式的ECDH私钥–因此,似乎也可以在没有公钥组件的情况下进行操作(Web Crypto API应该能够根据需要从内部从私钥组件计算公钥组件,这似乎与.generateKey()方法有关)。 However, the importKey() method seems to only work if the public key components are included. 但是,importKey()方法似乎仅在包含公钥组件的情况下才起作用。

Am I missing something here? 我在这里想念什么吗? If not, does anyone know of a solution or workaround, short of calculating the public key components separately prior to importing, and including them with the private key component in the importKey() method (which seems overly cumbersome and unnecessary)? 如果不是,是否有人知道解决方案或解决方法,除了在导入之前分别计算公钥组件并将其与私有密钥组件一起包含在importKey()方法中(似乎过于繁琐和不必要)之外,还有其他人知道吗?

You can always derive the public point from the private key by scalar multiplying the private key with the base point (or the generator point) of the curve you chosen. 您始终可以通过将私钥与所选曲线的基点(或生成器点)进行标量乘积,从私钥中得出公钥。 How you complete this depends on your run-time environment. 如何完成此操作取决于您的运行时环境。

如果您具有pkcs8格式,则可以使用以下格式:

crypto.subtle.importKey("pkcs8", [privateKeyBuffer],{name:"ECDH",namedCurve:"P-256"} , true, ["deriveKey"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM