Module: Net::SSH::Authentication::ED25519

Defined in:
lib/net/ssh/authentication/ed25519.rb

Defined Under Namespace

Classes: OpenSSHPrivateKeyLoader, PrivKey, PubKey, UnsupportedError

Constant Summary collapse

ALGORITHM =
"ED25519"
KEY_BYTES =
32
PRIVATE_KEY_BYTES =
64

Class Method Summary collapse

Class Method Details

.ensure_supported!Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/net/ssh/authentication/ed25519.rb', line 15

def self.ensure_supported!
  unless OpenSSL::PKey.respond_to?(:new_raw_public_key) && OpenSSL::PKey.respond_to?(:new_raw_private_key)
    raise UnsupportedError, "OpenSSL::PKey raw public/private key APIs are unavailable"
  end

  key = new_private_key("\x00" * KEY_BYTES)
  signature = sign(key, "")
  public_key = new_public_key(key.raw_public_key)
  raise UnsupportedError, "OpenSSL Ed25519 signature verification failed" unless verify(public_key, signature, "")
rescue OpenSSL::PKey::PKeyError => e
  raise UnsupportedError, e.message
end

.new_private_key(key) ⇒ Object



33
34
35
36
# File 'lib/net/ssh/authentication/ed25519.rb', line 33

def self.new_private_key(key)
  validate_key_bytes!(key, "private key", KEY_BYTES)
  OpenSSL::PKey.new_raw_private_key(ALGORITHM, binary_string(key))
end

.new_public_key(key) ⇒ Object



28
29
30
31
# File 'lib/net/ssh/authentication/ed25519.rb', line 28

def self.new_public_key(key)
  validate_key_bytes!(key, "public key", KEY_BYTES)
  OpenSSL::PKey.new_raw_public_key(ALGORITHM, binary_string(key))
end

.sign(key, data) ⇒ Object



38
39
40
# File 'lib/net/ssh/authentication/ed25519.rb', line 38

def self.sign(key, data)
  key.sign(nil, data)
end

.validate_key_bytes!(key, label, expected_bytes) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
# File 'lib/net/ssh/authentication/ed25519.rb', line 46

def self.validate_key_bytes!(key, label, expected_bytes)
  raise ArgumentError, "invalid Ed25519 #{label}" unless key.respond_to?(:bytesize) && key.bytesize == expected_bytes
end

.verify(key, signature, data) ⇒ Object



42
43
44
# File 'lib/net/ssh/authentication/ed25519.rb', line 42

def self.verify(key, signature, data)
  key.verify(nil, signature, data)
end