beetlebug▾
/

ztcl::crypto verify

Checks the signature of a message.

ztcl::crypto verify ?-encoding der|raw? ALGORITHM PUBLICKEY MESSAGE SIGNATURE

Description

Gives 1 when SIGNATURE is a valid signature of MESSAGE by the key PUBLICKEY, and 0 otherwise. ALGORITHM is ed25519 (RFC 8032) or ecdsa-p256-sha256 (ECDSA on the curve P-256 with SHA-256 of the message). An Ed25519 key is 32 bytes. A P-256 key is a SEC 1 point: 65 bytes uncompressed, or 33 bytes compressed. A signature of the wrong length or form gives 0. A key of the wrong form is an error.

Arguments

ArgumentTypeDefaultDescription
ALGORITHMstringrequireded25519 or ecdsa-p256-sha256.
PUBLICKEYbytesrequiredThe public key, as raw bytes.
MESSAGEbytesrequiredThe signed bytes.
SIGNATUREbytesrequiredThe signature: 64 bytes for Ed25519; DER or raw for ECDSA.

Options

OptionTypeDefaultDescription
-encodingstringderThe form of an ECDSA signature: der (the default, as X.509 and WebAuthn give it) or raw (the 32 bytes of r, then the 32 bytes of s).

-encoding FORM

The form of an ECDSA signature: der (the default, as X.509 and WebAuthn give it) or raw (the 32 bytes of r, then the 32 bytes of s).

The option applies to ecdsa-p256-sha256. An Ed25519 signature is always the 64 raw bytes. A signature in the other form gives 0.

% set pub [binary decode hex 0460fed4ba255a9d31c961eb74c6356d68c049b8923b61fa6ce669622e60f29fb67903fe1008b8bc99a41ae9e95628bc64f2f1b20c2d7e9f5177a3c294d4462299]
> set rs [binary decode hex efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8]
> list [ztcl::crypto verify -encoding raw ecdsa-p256-sha256 $pub sample $rs] [ztcl::crypto verify ecdsa-p256-sha256 $pub sample $rs]
1 0
% ztcl::crypto verify -encoding pem ecdsa-p256-sha256 a b c
bad encoding "pem": must be der or raw
-errorcode TCL LOOKUP INDEX value

Returns

1 or 0.

Examples

Each example of this page runs after package require ztcl::crypto.

% set pub [binary decode hex d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a]
> set sig [binary decode hex e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b]
> list [ztcl::crypto verify ed25519 $pub {} $sig] [ztcl::crypto verify ed25519 $pub x $sig]
1 0

% set pub [binary decode hex 0460fed4ba255a9d31c961eb74c6356d68c049b8923b61fa6ce669622e60f29fb67903fe1008b8bc99a41ae9e95628bc64f2f1b20c2d7e9f5177a3c294d4462299]
> set rs [binary decode hex efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8]
> ztcl::crypto verify -encoding raw ecdsa-p256-sha256 $pub sample $rs
1

Errors

% ztcl::crypto verify rsa a b c
bad algorithm "rsa": must be ed25519 or ecdsa-p256-sha256
-errorcode ZTCL CRYPTO ALG

% ztcl::crypto verify ed25519 abc {} {}
an Ed25519 public key is 32 bytes
-errorcode ZTCL CRYPTO KEY

% ztcl::crypto verify ed25519 a b
wrong # args: should be "ztcl::crypto verify ?-encoding der|raw? algorithm publickey message signature"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.10
Compute cost
One compute unit for each 4,096 bytes of the message, the data, and the key, and 64 more units for a signature command.

See also