Skip to content

Digital Signatures

Tony Arcieri edited this page Aug 1, 2019 · 22 revisions

NOTE: if all you need are Ed25519 digital signatures, consider the ed25519 gem instead which does not require libsodium as a dependency but still works on CRuby and JRuby

In the real world, signatures help uniquely identify people because everyone's signature is unique. Digital signatures work similarly in that they are unique to holders of a private key, but unlike real world signatures, digital signatures are unforgable.

Digital signatures allow you to publish a public key, then you can use your private signing key to sign messages. Others who have your public key can then use it to validate that your messages are actually authentic.

Code Example

Signer's perspective (RbNaCl::SigningKey):

# Generate a new random signing key
signing_key = RbNaCl::SigningKey.generate

# Serialize key to bytestring - load using RbNaCl::SigningKey.new(bytes)
signing_key.to_s

# Sign a message with the signing key
signature = signing_key.sign(message)

# Obtain the verify key for a given signing key
verify_key = signing_key.verify_key

# Convert the verify key to a string to send it to a third party
verify_key.to_s

Verifier's perspective (RbNaCl::VerifyKey):

# Create a VerifyKey object from a public key
verify_key = RbNaCl::VerifyKey.new(verify_key)

# Check the validity of a message's signature
# Will raise RbNaCl::BadSignatureError if the signature check fails
verify_key.verify(signature, message)

Algorithm features:

  • Small keys: Ed25519 keys are only 256-bits (32 bytes), making them small enough to easily copy and paste. Ed25519 also allows the public key to be derived from the private key, meaning that it doesn't need to be included in a serialized private key in cases you want both.
  • Small signatures: Ed25519 signatures are only 512-bits (64 bytes), one of the smallest signature sizes available.
  • Deterministic: Unlike (EC)DSA, Ed25519 does not rely on an entropy source when signing messages (which has lead to catastrophic private key compromises), but instead computes signature nonces from a combination of a hash of the signing key's "seed" and the message to be signed. This avoids using an entropy source for nonces, which can be a potential attack vector if the entropy source is not generating good random numbers. Even a single reused nonce can lead to a complete disclosure of the private key in these schemes, which Ed25519 avoids entirely by being deterministic instead of tied to an entropy source.
  • Collision Resistant: Hash-function collisions do not break this system. This adds a layer of defense against the possibility of weakness in the selected hash function.

Algorithm details:

Algorithm diagram (Ed25519):

Ed25519 Diagram

  • k: Ed25519 private key (passed into RbNaCl::SigningKey#new)
  • A: Ed25519 public key derived from k
  • M: message to be signed
  • R: a deterministic nonce value calculated from a combination of private key data RH and the message M
  • S: Ed25519 signature

RDoc