Verifying signature with Cloud KMS - node.js

122 views
Skip to first unread message

Andre Meirelles

unread,
Jul 20, 2021, 10:41:51 AM7/20/21
to Google Cloud Developers

I'm trying to verify a signature generated with Google's cloud KMS, but I keep getting invalid responses. 


Here's how I'm testing it:


    const versionName = client.cryptoKeyVersionPath(

          projectId,

          locationId,

          keyRingId,

          keyId,

          versionId

        )

    

        const [publicKey] = await client.getPublicKey({

          name: versionName,

        })

    

        const valueToSign = 'hola, que tal'

    

        const digest = crypto.createHash('sha256').update(valueToSign).digest()

    

        const [signResponse] = await client.asymmetricSign({

          name: versionName,

          digest: {

            sha256: digest,

          },

        })

    

        const valid = crypto.createVerify('sha256').update(digest).verify(publicKey.pem, signResponse.signature)

    

        if (!valid) return console.log('INVALID SIGNATURE')

    

        console.log('SIGNATURE IS VALID!')

    

    // output: INVALID SIGNATURE


This code will always log 'INVALID SIGNATURE' **unless** I use the original message instead of its hash:


    const valid = crypto.createVerify('sha256').update(valueToSign).verify(publicKey.pem, signResponse.signature) // true


But using a local private key, I'm able to sign messages and verify them using their hashes:


    const valueToSign = 'hola, the tal'

    const msgHash = crypto.createHash("sha256").update(valueToSign).digest('base64');

    

    const signer = crypto.createSign('sha256');

    signer.update(msgHash);

    const signature = signer.sign(pk, 'base64');

    

    const verifier = crypto.createVerify('sha256');

    verifier.update(msgHash);

    const valid = verifier.verify(pubKey, signature, 'base64');

    console.log(valid) // true


Why is it? Is there something different about kms signatures?

Wilfred L. (Cloud Platform Support)

unread,
Jul 21, 2021, 8:37:18 PM7/21/21
to Google Cloud Developers
Hello, are you following this article? [1] or this [2]. I am seeing there are some difference from what you posted

Reply all
Reply to author
Forward
0 new messages