Skip to content
Portada » Blog » ECDH Key Derivation with Pico HSM

ECDH Key Derivation with Pico HSM

Pico HSM can decrypt data with a symmetric ECDH key. The steps below show the supported mechanism and how to derive the shared key.

RSA-PKCS

RSA-X-509

RSA-PKCS-OAEP

ECDH-DERIVE

With public-key encryption, anyone can encrypt data with the recipient’s public key, but only the matching private key can decrypt it.

RSA supports several encryption mechanisms. PKCS is the basic RSA operation, while X.509 prepends an ASN.1 structure containing the required parameters. RSA-PKCS-OAEP is the newer variant and is considered the most secure and robust of the three.

Elliptic-curve keys do not encrypt data directly. ECDH derives a shared symmetric key from one party’s private key and the other party’s public key. Both sides can then use that shared key to encrypt and decrypt data.

Preparation

First, prepare the data file for the example. This uses data:

$ echo "This is a test string. Be safe, be secure." > data

The following OpenSSL commands prepare the public key used in the examples. The key has 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.

To use sc-tool, 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

ECDH-DERIVE

Elliptic-curve keys do not encrypt data directly. ECDH derives a shared symmetric key from the local private key and the peer’s public key, so the key itself is never transmitted.

First, create Bob’s keypair and export his public key:

$ openssl ecparam -genkey -name prime192v1 > bob.pem
$ openssl ec -in bob.pem -pubout -outform DER > bob.der

Give Bob’s public key to Pico HSM to derive the shared key:

$ pkcs11-tool --pin 648219 --id 2 --derive -i bob.der -o mine-bob.der

Bob derives the same shared key with his private key and our public key:

$ openssl pkeyutl -derive -out bob-mine.der -inkey bob.pem -peerkey 2.pub

Finally, compare the two shared keys:

$ cmp bob-mine.der mine-bob.der

No output means the two keys match.

You can inspect both keys as well:

$ xxd -p bob-mine.der
9874558aefa9d92cc051e5da6d1753987e5314925d6d78bf
$ xxd -p mine-bob.der
9874558aefa9d92cc051e5da6d1753987e5314925d6d78bf