Skip to content
Portada » Blog » Create RSA Signatures with Pico HSM

Create RSA Signatures with Pico HSM

Pico HSM signs data with a private key kept on the device. The examples use its supported RSA mechanisms and show how to verify each signature.

RSA-PKCS

RSA-X-509

SHA1-RSA-PKCS

SHA256-RSA-PKCS

SHA224-RSA-PKCS

SHA384-RSA-PKCS

SHA512-RSA-PKCS

RSA-PKCS-PSS

SHA1-RSA-PKCS-PSS

SHA256-RSA-PKCS-PSS

SHA224-RSA-PKCS-PSS

SHA384-RSA-PKCS-PSS

SHA512-RSA-PKCS-PSS

SHA1-ECDSA

SHA224-ECDSA

SHA256-ECDSA

SHA384-ECDSA

SHA512-ECDSA

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 RSA key with ID 1:

$ pkcs11-tool --read-object --pin 648219 --id 1 --type pubkey > 1.der
$ openssl rsa -inform DER -outform PEM -in 1.der -pubin > 1.pub

The --id parameter selects private key 1. The first command retrieves the public key associated with private key 1 and saves it to 1.der.

The second command converts the public key from DER to PEM format.

RSA-PKCS

This algorithm is used to sign raw data.

To sign the data:

$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism RSA-PKCS -i data -o data.sig

The private key stays on Pico HSM. Use --id to tell Pico HSM which key to use when signing the data file.

After the command, the signature is saved in the data.sig file. To verify the signature:

$ openssl pkeyutl -verify -pubin -inkey 1.pub -in data -sigfile data.sig
Signature Verified Successfully

Caution: RSA-PKCS signs raw data, so the full input is sent to Pico HSM over USB. Large inputs may break the connection. Use this mechanism only for small inputs; for larger data, choose a variant that computes the digest outside the device.

SHA1-RSA-PKCS

This algorithm is used to sign digests computed outside. It supports SHA1, SHA224, SHA256, SHA384 and SHA512.

First, we generate a file with the digest:

openssl dgst -sha1 -binary -out data.sha1 data

To sign the data:

$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism SHA1-RSA-PKCS -i data -o data.sig

To verify the signature:

$ openssl pkeyutl -verify -in data.sha1 -sigfile data.sig -pubin -inkey 1.pub -pkeyopt digest:sha1
Signature Verified Successfully

RSA-X-509

This algorithm is used for signing raw data. In this algorithm, the data must be padded with a length equal to the size of private key (128, 256, 512 bytes for RSA-1024, RSA-2048 and RSA-4096, respectively).

The original data file is 29 bytes. For an RSA-2048 key, add 227 bytes of padding:

$ cp data data_pad
$ dd if=/dev/zero bs=1 count=227 >> data_pad

To sign the data:

$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism RSA-X-509 -i data_pad -o data.sig

To verify the signature:

$ openssl rsautl -verify -inkey 1.pub -in data.sig -pubin -raw
This is a test string. Be safe, be secure.

RSA-PKCS-PSS

RSA-PKCS-PSS adds a salt to randomize the signature. Pico HSM uses the maximum salt length, equal to the hash length, and takes the hash as input.

To sign the data:

$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism RSA-PKCS-PSS -i data.sha1 -o data.sig

To verify the signature:

$ openssl pkeyutl -verify -in data.sha1 -sigfile data.sig -pubin -inkey 1.pub -pkeyopt rsa_padding_mode:pss -pkeyopt rsa_pss_saltlen:-1 -pkeyopt digest:sha1
Signature Verified Successfully

SHA1-RSA-PKCS-PSS

This algorithm takes the file as the input and sends its hash for signing with the random salt.

To sign the data:

$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism SHA1-RSA-PKCS-PSS -i data -o data.sig

To verify the signature:

$ openssl pkeyutl -verify -in data.sha1 -sigfile data.sig -pubin -inkey 1.pub -pkeyopt rsa_padding_mode:pss -pkeyopt rsa_pss_saltlen:-1
Signature Verified Successfully