Skip to content

Ed25519

Header: #include <cryptopp/xed25519.h> | Namespace: CryptoPP Since: Crypto++ 8.0 (SignStream() and VerifyStream() since 8.1)

Ed25519 is the EdDSA signature scheme over the edwards25519 curve, specified in RFC 8032 and approved for digital signatures in FIPS 186-5. Keys are 32 bytes, signatures are 64 bytes, and signing is deterministic: it needs no random number generator once the key exists.

Quick Example

#include <cryptopp/xed25519.h>
#include <cryptopp/osrng.h>
#include <cryptopp/filters.h>
#include <cryptopp/cryptlib.h>  // NullRNG
#include <iostream>

int main() {
    using namespace CryptoPP;

    AutoSeededRandomPool rng;

    // Generate key pair
    ed25519::Signer signer(rng);
    ed25519::Verifier verifier(signer);

    // Sign message (Ed25519 is deterministic, no RNG needed after keygen)
    std::string message = "Hello, World!";
    std::string signature;

    StringSource(message, true,
        new SignerFilter(NullRNG(), signer,
            new StringSink(signature)
        )
    );

    // Verify signature
    bool valid = false;
    StringSource(signature + message, true,
        new SignatureVerificationFilter(verifier,
            new ArraySink((byte*)&valid, sizeof(valid))
        )
    );

    std::cout << "Signature valid: " << (valid ? "YES" : "NO") << std::endl;

    return valid ? 0 : 1;
}

Usage Guidelines

Do:

  • Use Ed25519 for digital signatures where you can choose the algorithm
  • Keep exported private keys in SecByteBlock
  • Verify a signature before acting on the signed data
  • Validate keys that come from files or other parties with Validate()

Avoid:

  • Using one signing key for several unrelated purposes
  • Using Ed25519 for key exchange (use X25519 instead)
  • Reading a very large input into memory to sign it. Use SignStream() / VerifyStream(), or sign a hash of the data and have the verifier do the same

Class: ed25519::Signer

Sign messages with an Ed25519 private key. ed25519::Signer is a typedef for ed25519Signer.

Constants

static const int SECRET_KEYLENGTH = 32;   // Private key (32 bytes)
static const int PUBLIC_KEYLENGTH = 32;   // Public key (32 bytes)
static const int SIGNATURE_LENGTH = 64;   // Signature (64 bytes)

Constructors

Constructor with RNG

ed25519::Signer(RandomNumberGenerator& rng);

Generate new signing key pair.

Example:

AutoSeededRandomPool rng;
ed25519::Signer signer(rng);

Constructor with Private Key

ed25519::Signer(const byte x[SECRET_KEYLENGTH]);
ed25519::Signer(const byte y[PUBLIC_KEYLENGTH], const byte x[SECRET_KEYLENGTH]);

Load an existing 32-byte private key. The first form derives the public key; the second takes a public key the caller already holds.

Example:

SecByteBlock privateKey(ed25519::Signer::SECRET_KEYLENGTH);
// ... load the 32 key bytes into privateKey ...
ed25519::Signer signer(privateKey);

Constructor with an Encoded Key

ed25519::Signer(BufferedTransformation& params);
ed25519::Signer(const PKCS8PrivateKey& key);

Load a private key from its DER encoding (the output of Save()), or copy one from another private-key object.

Methods

SignMessage()

size_t SignMessage(RandomNumberGenerator& rng,
                   const byte* message, size_t messageLen,
                   byte* signature) const;

Sign a message.

Parameters:

  • rng - Random number generator (not used; pass NullRNG())
  • message - Message to sign
  • messageLen - Message length
  • signature - Output buffer (64 bytes)

Returns: Signature length (always 64)

Example:

ed25519::Signer signer(rng);

std::string message = "Sign this";
byte signature[ed25519::Signer::SIGNATURE_LENGTH];

signer.SignMessage(NullRNG(),
    (const byte*)message.data(), message.size(),
    signature);

SignStream()

size_t SignStream(RandomNumberGenerator& rng, std::istream& stream,
                  byte* signature) const;

Sign the contents of a stream without loading it into memory. Ed25519 signing reads the message twice, so the stream must be seekable, for example a std::ifstream opened in binary mode, and its contents must not change between the two passes. Other signers in the library do not have this function.

Example:

std::ifstream file("release.tar.gz", std::ios::binary);
if (!file)
    throw std::runtime_error("cannot open release.tar.gz");

byte signature[ed25519::Signer::SIGNATURE_LENGTH];
signer.SignStream(NullRNG(), file, signature);

GetPrivateKey()

const PrivateKey& GetPrivateKey() const;
PrivateKey& AccessPrivateKey();

Return the signer’s key object, an ed25519PrivateKey. The key object can save itself and exposes the raw key bytes.

Example:

const ed25519PrivateKey& key =
    dynamic_cast<const ed25519PrivateKey&>(signer.GetPrivateKey());

// Raw 32-byte private key and public key
SecByteBlock privateKey(key.GetPrivateKeyBytePtr(),
                        ed25519PrivateKey::SECRET_KEYLENGTH);
SecByteBlock publicKey(key.GetPublicKeyBytePtr(),
                       ed25519PrivateKey::PUBLIC_KEYLENGTH);

// DER encoding (PKCS #8, unencrypted)
ByteQueue encoded;
key.Save(encoded);

Class: ed25519::Verifier

Verify Ed25519 signatures.

Constructors

Constructor with Public Key

ed25519::Verifier(const byte y[PUBLIC_KEYLENGTH]);

Load a 32-byte public key for verification. ed25519::Verifier is a typedef for ed25519Verifier.

Example:

byte publicKey[32];
// ... receive from sender ...
ed25519::Verifier verifier(publicKey);

Constructor from Signer

ed25519::Verifier(const ed25519::Signer& signer);

Extract public key from signer.

Example:

ed25519::Signer signer(rng);
ed25519::Verifier verifier(signer);

Constructor with an Encoded Key

ed25519::Verifier(BufferedTransformation& params);
ed25519::Verifier(const X509PublicKey& key);

Load a public key from its DER encoding (X.509 SubjectPublicKeyInfo), or copy one from another public-key object.

Methods

VerifyMessage()

bool VerifyMessage(const byte* message, size_t messageLen,
                   const byte* signature, size_t signatureLen) const;

Verify signature on message.

Parameters:

  • message - Message that was signed
  • messageLen - Message length
  • signature - Signature to verify (64 bytes)
  • signatureLen - Signature length (64)

Returns: true if valid, false if invalid. A signature that is not exactly 64 bytes is invalid.

Example:

ed25519::Verifier verifier(publicKey);

bool valid = verifier.VerifyMessage(
    (const byte*)message.data(), message.size(),
    signature, 64
);

if (!valid) {
    std::cerr << "Invalid signature!" << std::endl;
}

VerifyStream()

bool VerifyStream(std::istream& stream,
                  const byte* signature, size_t signatureLen) const;

Verify a signature over the contents of a stream. The counterpart of SignStream(); verification reads the stream once. A signature that is null or not exactly 64 bytes does not verify: the function returns false.

Example:

std::ifstream file("release.tar.gz", std::ios::binary);
if (!file)
    throw std::runtime_error("cannot open release.tar.gz");

// received holds the signature as it arrived (a std::string)
bool valid = received.size() == ed25519::Verifier::SIGNATURE_LENGTH &&
    verifier.VerifyStream(file, (const byte*)received.data(), received.size());

GetPublicKey()

const PublicKey& GetPublicKey() const;
PublicKey& AccessPublicKey();

Return the verifier’s key object, an ed25519PublicKey.

Example:

const ed25519PublicKey& key =
    dynamic_cast<const ed25519PublicKey&>(verifier.GetPublicKey());

// Raw 32-byte public key
SecByteBlock publicKey(key.GetPublicKeyBytePtr(),
                       ed25519PublicKey::PUBLIC_KEYLENGTH);

// DER encoding (X.509 SubjectPublicKeyInfo)
ByteQueue encoded;
key.Save(encoded);

Complete Example: Export and Reload Keys

#include <cryptopp/xed25519.h>
#include <cryptopp/osrng.h>
#include <cryptopp/queue.h>
#include <cryptopp/cryptlib.h>  // NullRNG
#include <iostream>
#include <string>

int main() {
    using namespace CryptoPP;

    AutoSeededRandomPool rng;
    ed25519::Signer signer(rng);

    // Export the raw keys
    const ed25519PrivateKey& key =
        dynamic_cast<const ed25519PrivateKey&>(signer.GetPrivateKey());
    SecByteBlock privateKey(key.GetPrivateKeyBytePtr(),
                            ed25519PrivateKey::SECRET_KEYLENGTH);
    SecByteBlock publicKey(key.GetPublicKeyBytePtr(),
                           ed25519PrivateKey::PUBLIC_KEYLENGTH);

    // Sign with a signer rebuilt from the private key bytes
    ed25519::Signer signer2(privateKey);
    std::string message = "Release 1.0";
    byte signature[ed25519::Signer::SIGNATURE_LENGTH];
    signer2.SignMessage(NullRNG(),
        (const byte*)message.data(), message.size(), signature);

    // Verify with a verifier built from the public key bytes
    ed25519::Verifier verifier(publicKey);
    bool valid = verifier.VerifyMessage(
        (const byte*)message.data(), message.size(),
        signature, sizeof(signature));

    std::cout << "Signature valid: " << (valid ? "YES" : "NO") << std::endl;

    return valid ? 0 : 1;
}

Sizes

AlgorithmSignaturePublic key
Ed2551964 bytes32 bytes
ECDSA P-25664 bytes (fixed-width r and s)65 bytes uncompressed, 33 compressed
RSA-2048256 bytes256-byte modulus plus exponent

Security

Security Properties

  • Security level: about 128 bits
  • Private key: 256 bits (32 bytes)
  • Public key: 256 bits (32 bytes)
  • Signature: 512 bits (64 bytes, R || S)
  • Deterministic: Signing the same message with the same key always yields the same signature
  • Standard: Ed25519 as specified in RFC 8032 (“pure” EdDSA over edwards25519). The Ed25519ph and Ed25519ctx variants are not provided.

Security Notes & Best Practices

  • Large data

    • For very large inputs, either:
      • use ed25519::Signer::SignStream() on a seekable stream and ed25519::Verifier::VerifyStream(), or
      • sign a hash of the data (for example a SHA-256 digest); the verifier must then hash the data the same way.
    • Both avoid holding the whole input in memory. Ed25519 signing makes two passes over the message; verification makes one.
  • Key validation

    • When loading keys that weren’t generated locally (files, configuration, another party), call Validate(rng, level) on the key object before use. Level 2 and above rejects public keys of small order. The verifier does not depend on that call: VerifyMessage() and VerifyStream() return false for a small-order public key.
  • Key separation

    • Use distinct keys for distinct roles (for example one key for software releases and another for authentication).
    • This limits the damage from a compromise and avoids cross-protocol problems from reusing one key everywhere.
  • Key handling

    • Treat Ed25519 private keys as long-term, high-value secrets:
      • keep exported key bytes in SecByteBlock,
      • restrict file and backup access,
      • rotate keys if they’ve been exposed in untrusted environments.
  • No key exchange / encryption

    • Ed25519 is a signature scheme only. Don’t use it directly for key agreement or encryption.
    • Use X25519 for key exchange and a symmetric AEAD (AES-GCM or ChaCha20-Poly1305) for confidentiality.

Test Vector (RFC 8032 Section 7.1, Test 1)

#include <cryptopp/xed25519.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include <cryptopp/cryptlib.h>  // NullRNG
#include <iostream>
#include <string>

int main() {
    using namespace CryptoPP;

    const byte secretKey[32] = {
        0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60,
        0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4,
        0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19,
        0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60
    };

    // The message is empty
    ed25519::Signer signer(secretKey);
    byte signature[ed25519::Signer::SIGNATURE_LENGTH];
    signer.SignMessage(NullRNG(), NULLPTR, 0, signature);

    std::string hex;
    StringSource(signature, sizeof(signature), true,
        new HexEncoder(new StringSink(hex), false));

    const std::string expected =
        "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155"
        "5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b";

    std::cout << hex << std::endl;
    std::cout << (hex == expected ? "MATCH" : "MISMATCH") << std::endl;

    return hex == expected ? 0 : 1;
}

Thread Safety

Not thread-safe. Use separate signer and verifier objects in each thread.

When to Use Ed25519

Use Ed25519 for:

  1. Digital signatures - Documents, software releases, firmware
  2. Authentication - Signing challenges or requests with a long-term key
  3. Protocols that specify it - For example SSH, TLS 1.3 and OpenPGP

Don’t use Ed25519 for:

  1. Key exchange - Use X25519 instead
  2. Encryption - A signature does not hide the message
  3. Peers that only support RSA or ECDSA - Use the algorithm they require

Ed25519 vs RSA vs ECDSA

FeatureEd25519RSA-2048ECDSA P-256
Public key32 bytes256-byte modulus65 bytes uncompressed
Signature64 bytes256 bytes64 bytes
Security strengthAbout 128 bits112 bits128 bits
Signing randomnessNone (deterministic)PSS uses a random saltPer-signature nonce, random or derived (RFC 6979)

Exceptions

  • BERDecodeErr - A DER-encoded key could not be parsed

A signature that does not verify is reported by the return value (false), not by an exception.

See Also

References

  1. RFC 8032: Edwards-Curve Digital Signature Algorithm (EdDSA)
  2. FIPS 186-5: Digital Signature Standard (DSS)
  3. RFC 8410: Algorithm Identifiers for Ed25519, Ed448, X25519, and X448 for Use in the Internet X.509 Public Key Infrastructure
  4. High-speed high-security signatures (Bernstein, Duif, Lange, Schwabe, Yang)