Pico HSM signs data with an ECDSA private key kept on the device. The examples show how to create and verify those signatures.
RSA and ECDSA are two common digital-signature algorithms. RSA signs data with a private key, and the matching public key verifies the signature. ECDSA uses elliptic-curve keys and follows the same sign-and-verify process; it can also make signing and verification faster and more efficient.
The PKCS#1 v1.5 variant is the original RSA signature scheme and accepts raw input or a precomputed SHA digest. PSS is considered more secure and robust, and accepts either raw or prehashed input. The X.509 variant prepends an ASN.1 structure containing the required parameters.
ECDSA has a single signature mechanism and takes a precomputed SHA digest as input.
Preparation
First, create a file containing the data to sign. This example uses data:
$ echo "This is a test string. Be safe, be secure." > data
Use OpenSSL to create and verify the signature. It needs the public key in DER or PEM format. This example uses the ECC key with ID 2:
$ pkcs11-tool --read-object --pin 648219 --id 2 --type pubkey > 2.der
$ openssl ec -inform DER -outform PEM -in 2.der -pubin > 2.pub
The --id parameter selects private key 2. The first command retrieves the public key associated with private key 2 and saves it to 2.der.
The second command converts the public key from DER to PEM format.
Install the sc-hsm-embedded driver using the build instructions on its project page, then create this alias:
$ alias sc-tool=pkcs11-tool --module /path/to/libsc-hsm-pkcs11.so
ECDSA
ECDSA normally signs a hashed message. pkcs11-tool limits the digest length to the curve size in bytes. With a 192-bit curve, it supports SHA-1 digests (20 bytes < 24 bytes); SHA-256 requires a curve of at least 256 bits. The sc-hsm-embedded driver and sc-tool do not have this length limit and can sign arbitrary data.
To sign the data:
$ pkcs11-tool --id 2 --sign --pin 648219 --mechanism ECDSA -i data.sha1 -o data.sig --signature-format openssl
Using slot 0 with a present token (0x0)
Using signature algorithm ECDSA
To verify the signature:
$ openssl pkeyutl -verify -pubin -inkey 2.pub -in data.sha1 -sigfile data.sig
Signature Verified Successfully
To sign raw data, use sc-tool of sc-hsm-embedded driver instead of pkcs11-tool.
SHA1-ECDSA
This example uses the ECC key selected with --id 2. The signing process is similar to RSA.
To sign the data:
$ pkcs11-tool --id 2 --sign --pin 648219 --mechanism ECDSA-SHA1 -i data -o data.sig --signature-format openssl
Using slot 0 with a present token (0x0)
Using signature algorithm ECDSA-SHA256
The signature is verified with the hash:
$ openssl pkeyutl -verify -pubin -inkey 2.pub -in data.sha1 -sigfile data.sig
Signature Verified Successfully
To use another digest, replace the SHA1 flag with SHA224, SHA256, SHA384, or SHA512. Update the digest file data.sha1 to match.
