Skip to content

The signature and verification of SM2 return incorrect results in crypto.createSign and crypto.createVerify #53761

Description

@xicilion

Version

v22.0.0

Platform

Darwin air.lan 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112 arm64

Subsystem

crypto

What steps will reproduce the bug?

When using crypto.createSign to sign SM2, I got an incorrect signature. The same issue occurs with crypto.createVerify.

constcrypto=require('crypto');constdata="AABB";varkeys={privateKey: crypto.createPrivateKey(`-----BEGIN PRIVATE KEY-----MIGHAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBG0wawIBAQQgbjCNHopgvyGVfLaPPamI9E9lf6jXT+xm1Pns1t/xQTihRANCAATV+I7HUGF2gC+miVl3JfjpoZaU2hrZQqHwKUNtIDE/uxxWNLBbYKaiLOWrbYA8skrWQWl3RkbXW4ZI28afRw9g-----END PRIVATE KEY-----`),publicKey: crypto.createPublicKey(`-----BEGIN PUBLIC KEY-----MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE1fiOx1BhdoAvpolZdyX46aGWlNoa2UKh8ClDbSAxP7scVjSwW2Cmoizlq22APLJK1kFpd0ZG11uGSNvGn0cPYA==-----END PUBLIC KEY-----`)};varsig=crypto.createSign('sm3').update(data).sign(keys.privateKey);// var sig = crypto.sign('sm3', data, keys.privateKey);varok=crypto.verify('sm3',data,keys.publicKey,sig);console.log("verify: ",ok);

How often does it reproduce? Is there a required condition?

always.

What is the expected behavior? Why is that the expected behavior?

verify: true

What do you see instead?

verify: false

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions