2

ECDH我已经从 NodeJS生成了一个公钥和私钥对

function _genPrivateKey(curveName = "secp384r1", encoding = "hex") {
    const private_0 = crypto.createECDH(curveName);
    private_0.generateKeys();
    return private_0.getPrivateKey().toString(encoding);
}

鲍勃私钥

9d0c809d692c83c7d8d1355205dd78e679066fd9c15d12cdea3e1103b041873765264351e8939083b876d89d423301d8486a956da455ddbdc4a91ef60af7dd2325

鲍勃公钥

[hex] 
0400d7a342e89f501e1cd8224e2463ef1ad057c9b64bf45c1d3627cc1f06c055c80f75c2013c27b63dc984b467ecfc5202cf9a126ef1f1487e92b9acfb52abaeb7022e01f4259918ca34f442214a31d1bad329f9be1c67ce98af6621f622e44887264e856ee8c664a51e56f24008c932ee1cb5514c02d03ba27f6b6a1cd0aa0f8eac261250

[jwk] {
  key_ops: [ 'deriveKey' ],
  ext: true,
  kty: 'EC',
  x:'ANejQuifUB4c2CJOJGPvGtBXybZL9FwdNifMHwbAVcgPdcIBPCe2PcmEtGfs_FICz5oSbvHxSH6Suaz7UquutwIu',
  y:'AfQlmRjKNPRCIUox0brTKfm-HGfOmK9mIfYi5EiHJk6FbujGZKUeVvJACMky7hy1UUwC0Duif2tqHNCqD46sJhJQ',
  crv: 'P-521'
}

Alice的密钥来自带有Web Crypto API的网页

const generateAlicesKeyPair = window.crypto.subtle.generateKey({
        name: "ECDH",
        namedCurve: "P-521"
    },
    false,
    ["deriveBits"]
);

爱丽丝公钥

04000eefa90c3de22e79e6742f807806a603059d16afaa9f1bc69f420050aae100d0006e6510fe17a8f6767fe1e69bada039175ef5a375e30af4085e4315cf7527655f00ed9a39552a5f9170cc7626c1f4584d0e6de17870e336bcc5b6e251e3ea2c7cd9633e1afe2f9aee5f9a7445d38218c20695cc7ba2a462b67ce39a060e6464133609

当我试图推导出发生了shared key一件奇怪的事情时,密钥最后有不同的位。

节点JS:

function _getSharedSecret(privateKey, publicKey, curveName = "secp521r1", encoding = "hex") {
    const private_0 = crypto.createECDH(curveName);
    private_0.setPrivateKey(privateKey, encoding);
    const _sharedSecret = private_0.computeSecret(publicKey, encoding);
    return _sharedSecret
};

网络加密 API

const sharedSecret = await window.crypto.subtle.deriveBits({
        name: "ECDH",
        namedCurve: "P-521",
        public: publicKey
    },
    privateKey, 
    521
);

结果:


//NodeJS: 
0089b99212c9348a6fd3aa78225a773a90ef45f57bbd10dc86d8e52fa26662c550b56d2368ee358ab240ceec10191b6cdd7d09bb0a8763ea48a487a5676ebdf7af[eb]

//WebCryptoAPI:

0089b99212c9348a6fd3aa78225a773a90ef45f57bbd10dc86d8e52fa26662c550b56d2368ee358ab240ceec10191b6cdd7d09bb0a8763ea48a487a5676ebdf7af[80]

这仅发生在曲线P-521/secp521r1 上,但不会发生在曲线P-256/secp256r1P-384/secp384r1 上

4

1 回答 1

2

检查第三个库(Python,密码学库)表明 NodeJS 结果是正确的(即以 结尾的那个0xEB)。

P-521 的 X 和 Y 坐标由 66 字节(521 位 = 65 字节 + 1 位)表示(s. here)。共享密钥是曲线点的 X 坐标(此处为 s. ),因此也有 66 字节 = 528 位。该值将在WebCrypto API实现中指定deriveBits()为共享密钥的长度。

如果您指定 521 位,则仅考虑最高位(设置为0xEB),其余位设置为0,从而产生值0x80

以下代码说明了这一点(请注意,该脚本不能在 Firefox 下运行,这可能是一个错误):

(async () => {
    await getSharedSecret(521);
    await getSharedSecret(528);
})();

async function getSharedSecret(bits) {
    var bobPrivateKeyJwk = {   
        kty: "EC",
        crv: "P-521",
        x:'ANejQuifUB4c2CJOJGPvGtBXybZL9FwdNifMHwbAVcgPdcIBPCe2PcmEtGfs_FICz5oSbvHxSH6Suaz7UquutwIu',
        y:'AfQlmRjKNPRCIUox0brTKfm-HGfOmK9mIfYi5EiHJk6FbujGZKUeVvJACMky7hy1UUwC0Duif2tqHNCqD46sJhJQ',
        d: "AJ0MgJ1pLIPH2NE1UgXdeOZ5Bm_ZwV0Szeo-EQOwQYc3ZSZDUeiTkIO4dtidQjMB2EhqlW2kVd29xKke9gr33SMl",
        ext: true,
    }
    var alicePublicKeyBuffer = typedArray('04000eefa90c3de22e79e6742f807806a603059d16afaa9f1bc69f420050aae100d0006e6510fe17a8f6767fe1e69bada039175ef5a375e30af4085e4315cf7527655f00ed9a39552a5f9170cc7626c1f4584d0e6de17870e336bcc5b6e251e3ea2c7cd9633e1afe2f9aee5f9a7445d38218c20695cc7ba2a462b67ce39a060e6464133609');    
    var privateKey = await window.crypto.subtle.importKey(
        "jwk", 
        bobPrivateKeyJwk,
        { name: "ECDH", namedCurve: "P-521" },
        true, 
        ["deriveKey", "deriveBits"] 
    );
    var publicKey = await window.crypto.subtle.importKey(
        "raw", 
        alicePublicKeyBuffer.buffer,
        { name: "ECDH", namedCurve: "P-521"},
        true, 
        [] 
    );
    var sharedSecret = await window.crypto.subtle.deriveBits(
        { name: "ECDH", namedCurve: "P-521", public: publicKey },
        privateKey, 
        bits 
    );
    console.log("Bob's shared secret:\n", buf2hex(sharedSecret).replace(/(.{48})/g,'$1\n'));
}; 

function typedArray(hex) {
    return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { // from: https://stackoverflow.com/a/43131635
        return parseInt(h, 16)
    }))
}

function buf2hex(buffer) {
    return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''); // from: https://stackoverflow.com/a/40031979/9014097 
}

对于曲线 P-256 和 P-384,问题不会出现,因为素数字段已经是 8 的倍数(分别为 32 字节和 48 字节)。

于 2021-05-04T08:36:34.137 回答