| 1 |
# encoding: binary |
| 2 |
# frozen_string_literal: true |
| 3 |
|
| 4 |
module RbNaCl |
| 5 |
module Signatures |
| 6 |
module Ed25519 |
| 7 |
# Private key for producing digital signatures using the Ed25519 algorithm. |
| 8 |
# Ed25519 provides a 128-bit security level, that is to say, all known attacks |
| 9 |
# take at least 2^128 operations, providing the same security level as |
| 10 |
# AES-128, NIST P-256, and RSA-3072. |
| 11 |
# |
| 12 |
# Signing keys are produced from a 32-byte (256-bit) random seed value. |
| 13 |
# This value can be passed into the SigningKey constructor as a String |
| 14 |
# whose bytesize is 32. |
| 15 |
# |
| 16 |
# The public VerifyKey can be computed from the private 32-byte seed value |
| 17 |
# as well, eliminating the need to store a "keypair". |
| 18 |
# |
| 19 |
# SigningKey produces 64-byte (512-bit) signatures. The signatures are |
| 20 |
# deterministic: signing the same message will always produce the same |
| 21 |
# signature. This prevents "entropy failure" seen in other signature |
| 22 |
# algorithms like DSA and ECDSA, where poor random number generators can |
| 23 |
# leak enough information to recover the private key. |
| 24 |
class SigningKey |
| 25 |
include KeyComparator |
| 26 |
include Serializable |
| 27 |
|
| 28 |
extend Sodium |
| 29 |
|
| 30 |
sodium_type :sign |
| 31 |
sodium_primitive :ed25519 |
| 32 |
|
| 33 |
sodium_function :sign_ed25519, |
| 34 |
:crypto_sign_ed25519, |
| 35 |
%i[pointer pointer pointer ulong_long pointer] |
| 36 |
|
| 37 |
sodium_function :sign_ed25519_seed_keypair, |
| 38 |
:crypto_sign_ed25519_seed_keypair, |
| 39 |
%i[pointer pointer pointer] |
| 40 |
|
| 41 |
sodium_function :to_private_key, |
| 42 |
:crypto_sign_ed25519_sk_to_curve25519, |
| 43 |
%i[pointer pointer] |
| 44 |
|
| 45 |
attr_reader :verify_key |
| 46 |
|
| 47 |
# Generate a random SigningKey |
| 48 |
# |
| 49 |
# @return [RbNaCl::SigningKey] Freshly-generated random SigningKey |
| 50 |
def self.generate |
| 51 |
new RbNaCl::Random.random_bytes(Ed25519::SEEDBYTES) |
| 52 |
end |
| 53 |
|
| 54 |
# Create a SigningKey from a seed value |
| 55 |
# |
| 56 |
# @param seed [String] Random 32-byte value (i.e. private key) |
| 57 |
# |
| 58 |
# @return [RbNaCl::SigningKey] Key which can sign messages |
| 59 |
def initialize(seed) |
| 60 |
seed = seed.to_s |
| 61 |
|
| 62 |
Util.check_length(seed, Ed25519::SEEDBYTES, "seed") |
| 63 |
|
| 64 |
pk = Util.zeros(Ed25519::VERIFYKEYBYTES) |
| 65 |
sk = Util.zeros(Ed25519::SIGNINGKEYBYTES) |
| 66 |
|
| 67 |
self.class.sign_ed25519_seed_keypair(pk, sk, seed) || raise(CryptoError, "Failed to generate a key pair") |
| 68 |
|
| 69 |
@seed = seed |
| 70 |
@signing_key = sk |
| 71 |
@verify_key = VerifyKey.new(pk) |
| 72 |
end |
| 73 |
|
| 74 |
# Sign a message using this key |
| 75 |
# |
| 76 |
# @param message [String] Message to be signed by this key |
| 77 |
# |
| 78 |
# @return [String] Signature as bytes |
| 79 |
def sign(message) |
| 80 |
sign_attached(message)[0, signature_bytes] |
| 81 |
end |
| 82 |
|
| 83 |
# Sign a message using this key, attaching the signature to the message |
| 84 |
# |
| 85 |
# @param message [String] Message to be signed by this key |
| 86 |
# |
| 87 |
# @return [String] Signature and the message as bytes |
| 88 |
def sign_attached(message) |
| 89 |
buffer = Util.prepend_zeros(signature_bytes, message) |
| 90 |
buffer_len = Util.zeros(FFI::Type::LONG_LONG.size) |
| 91 |
self.class.sign_ed25519(buffer, buffer_len, message, message.bytesize, @signing_key) |
| 92 |
buffer |
| 93 |
end |
| 94 |
|
| 95 |
# Return the raw seed value of this key |
| 96 |
# |
| 97 |
# @return [String] seed used to create this key |
| 98 |
def to_bytes |
| 99 |
@seed |
| 100 |
end |
| 101 |
|
| 102 |
# Return the raw 64 byte value of this key |
| 103 |
# |
| 104 |
# @return [String] The signature key bytes. Left half is 32-byte |
| 105 |
# curve25519 private scalar, right half is 32-byte group element |
| 106 |
def keypair_bytes |
| 107 |
@signing_key |
| 108 |
end |
| 109 |
|
| 110 |
# The crypto primitive this SigningKey class uses for signatures |
| 111 |
# |
| 112 |
# @return [Symbol] The primitive |
| 113 |
def primitive |
| 114 |
self.class.primitive |
| 115 |
end |
| 116 |
|
| 117 |
# The size of signatures generated by the SigningKey class |
| 118 |
# |
| 119 |
# @return [Integer] The number of bytes in a signature |
| 120 |
def self.signature_bytes |
| 121 |
Ed25519::SIGNATUREBYTES |
| 122 |
end |
| 123 |
|
| 124 |
# The size of signatures generated by the SigningKey instance |
| 125 |
# |
| 126 |
# @return [Integer] The number of bytes in a signature |
| 127 |
def signature_bytes |
| 128 |
Ed25519::SIGNATUREBYTES |
| 129 |
end |
| 130 |
|
| 131 |
# Return a new curve25519 (x25519) private key converted from this key |
| 132 |
# |
| 133 |
# it's recommeneded to read https://libsodium.gitbook.io/doc/advanced/ed25519-curve25519 |
| 134 |
# as it encourages using distinct keys for signing and for encryption |
| 135 |
# |
| 136 |
# @return [RbNaCl::PrivateKey] |
| 137 |
def to_curve25519_private_key |
| 138 |
buffer = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PrivateKey::BYTES) |
| 139 |
self.class.crypto_sign_ed25519_sk_to_curve25519(buffer, @signing_key) |
| 140 |
Boxes::Curve25519XSalsa20Poly1305::PrivateKey.new(buffer) |
| 141 |
end |
| 142 |
end |
| 143 |
end |
| 144 |
end |
| 145 |
end |