Crypt::PK::Ed25519#

📦 max

Ed25519 signatures (RFC 8032): keys, signing and verification.

Generate a key or load one - raw bytes, DER, PEM (PKCS#8, encrypted too, or an X.509 certificate), OpenSSH, an ssh-ed25519 public key line, or a JWK - then sign_message and verify_message. The _ctx and _ph forms are Ed25519ctx and Ed25519ph, with a context string of up to 255 bytes. Keys export as raw bytes, DER, PEM and JWK. Failures croak with FATAL: and the reason, as CryptX’s do.

The signatures are the ones in the system’s libcrypto 3, reached through the built-in Net::SSLeay; the same key and message give the same signature as any other Ed25519.

This is a native implementation of Crypt::PK::Ed25519 from CryptX 0.091: its CryptX_PK_Ed25519.xs.inc and its Perl methods. The rest of CryptX is not part of pperl.

Synopsis#

use Crypt::PK::Ed25519;

my $key = Crypt::PK::Ed25519->new->generate_key;
my $sig = $key->sign_message("message");

my $pub = Crypt::PK::Ed25519->new(\$key->export_key_pem('public'));
print $pub->verify_message($sig, "message") ? "ok\n" : "bad\n";

Differences from upstream#

  • An OpenSSH private key protected by a passphrase croaks FATAL: pem_decode_openssh failed: Invalid cipher specified.: its bcrypt key derivation is not available. Unprotected OpenSSH keys load.

  • Ed25519ctx with an empty context, which RFC 8032 advises against, is refused: sign_message_ctx($msg, '') croaks and verify_message_ctx returns 0. CryptX signs and verifies it.

  • Where CryptX would fail, pperl does better: a 64-byte private key whose second half is not its public key signs as its first half’s key, where CryptX makes a signature no key verifies. (t/81-xs-native/Crypt-PK-Ed25519/150-or-better.t)

Functions#

Keys#

generate_key#

$key->generate_key: a new random key pair; returns the object.

is_private#

$key->is_private: 1 for a private key, 0 for a public one, undef before a key is loaded.

new#

Crypt::PK::Ed25519->new([$key [, $password]]): a new object, with the key import_key reads from $key when one is given.

Import#

import_key_raw#

$key->import_key_raw($bytes, 'private' | 'public'): the 32 raw key bytes; returns the object.

import_key#

$key->import_key($key [, $password]): the key $key holds - a file name, a reference to the key’s text, or a hash of kty/crv/d/x (JWK) or of curve/priv/pub (key2hash). Text may be PEM, OpenSSH, an ssh-ed25519 line, JSON or DER; raw bytes need import_key_raw. Returns the object.

Export#

key2hash#

$key->key2hash: { curve => 'ed25519', pub => hex, priv => hex or undef }, or undef before a key is loaded.

export_key_der#

$key->export_key_der('private' | 'public'): the key as DER, PKCS#8 or SubjectPublicKeyInfo.

export_key_raw#

$key->export_key_raw('private' | 'public'): the 32 key bytes.

export_key_pem#

$key->export_key_pem('private' | 'public' [, $password [, $cipher]]): the key as PEM; a private key encrypted when a password is given, with $cipher (AES-256-CBC by default; AES, CAMELLIA, DES, DES-EDE3 or SEED in CBC, CFB, ECB or OFB).

export_key_jwk#

$key->export_key_jwk('private' | 'public' [, $wanthash]): the key as a JWK, canonical JSON or ($wanthash) the hash; d only for a private export of a private key. Nothing before a key is loaded.

Signatures#

sign_message#

$key->sign_message($message): the 64-byte Ed25519 signature; private keys only.

verify_message#

$key->verify_message($signature, $message): 1 when the signature is the key’s over the message, else 0.

sign_message_ctx#

$key->sign_message_ctx($message, $context): an Ed25519ctx signature.

verify_message_ctx#

$key->verify_message_ctx($signature, $message, $context): the Ed25519ctx verification.

sign_message_ph#

$key->sign_message_ph($message [, $context]): an Ed25519ph signature, over the message’s SHA-512.

verify_message_ph#

$key->verify_message_ph($signature, $message [, $context]): the Ed25519ph verification.