常量SM2 密钥交换协议
标准参考:
这是一个安全的密钥协商协议,允许两方在不安全的通道上协商出共享密钥。
协议流程:
安全特性:
密钥交换参数
密钥交换结果,包含临时公钥、共享密钥和可选的确认哈希值
// 双方先生成长期密钥对和本次会话的临时密钥对。
const keyPairA = generateKeyPair();
const keyPairB = generateKeyPair();
const tempKeyPairA = generateKeyPair();
const tempKeyPairB = generateKeyPair();
const resultA = keyExchange({
privateKey: keyPairA.privateKey,
publicKey: keyPairA.publicKey,
peerPublicKey: keyPairB.publicKey,
tempPrivateKey: tempKeyPairA.privateKey,
peerTempPublicKey: tempKeyPairB.publicKey,
isInitiator: true
});
const resultB = keyExchange({
privateKey: keyPairB.privateKey,
publicKey: keyPairB.publicKey,
peerPublicKey: keyPairA.publicKey,
tempPrivateKey: tempKeyPairB.privateKey,
peerTempPublicKey: tempKeyPairA.publicKey,
isInitiator: false
});
if (resultA.sharedKey !== resultB.sharedKey) {
throw new Error('SM2 密钥交换结果不一致');
}
执行 SM2 密钥交换;参与方角色、用户标识和派生长度见
SM2KeyExchangeParams。