| 1 |
# encoding: binary |
| 2 |
# frozen_string_literal: true |
| 3 |
|
| 4 |
module RbNaCl |
| 5 |
module Signatures |
| 6 |
module Ed25519 |
| 7 |
# The public key counterpart to an Ed25519 SigningKey for producing digital |
| 8 |
# signatures. Like the name says, VerifyKeys can be used to verify that a |
| 9 |
# given digital signature is authentic. |
| 10 |
# |
| 11 |
# For more information on the Ed25519 digital signature system, please see |
| 12 |
# the SigningKey documentation. |
| 13 |
class VerifyKey |
| 14 |
include KeyComparator |
| 15 |
include Serializable |
| 16 |
|
| 17 |
extend Sodium |
| 18 |
|
| 19 |
sodium_type :sign |
| 20 |
sodium_primitive :ed25519 |
| 21 |
|
| 22 |
sodium_function :sign_ed25519_open, |
| 23 |
:crypto_sign_ed25519_open, |
| 24 |
%i[pointer pointer pointer ulong_long pointer] |
| 25 |
|
| 26 |
sodium_function :to_public_key, |
| 27 |
:crypto_sign_ed25519_pk_to_curve25519, |
| 28 |
%i[pointer pointer] |
| 29 |
|
| 30 |
# Create a new VerifyKey object from a public key. |
| 31 |
# |
| 32 |
# @param key [String] Ed25519 public key |
| 33 |
# |
| 34 |
# @return [RbNaCl::VerifyKey] Key which can verify messages |
| 35 |
def initialize(key) |
| 36 |
@key = key.to_str |
| 37 |
Util.check_length(@key, Ed25519::VERIFYKEYBYTES, "key") |
| 38 |
end |
| 39 |
|
| 40 |
# Verify a signature for a given message |
| 41 |
# |
| 42 |
# Raises if the signature is invalid. |
| 43 |
# |
| 44 |
# @param signature [String] Alleged signature to be checked |
| 45 |
# @param message [String] Message to be authenticated |
| 46 |
# |
| 47 |
# @raise [BadSignatureError] if the signature check fails |
| 48 |
# @raise [LengthError] if the signature is of the wrong length |
| 49 |
# |
| 50 |
# @return [Boolean] was the signature authentic? |
| 51 |
def verify(signature, message) |
| 52 |
signature = signature.to_str |
| 53 |
Util.check_length(signature, signature_bytes, "signature") |
| 54 |
verify_attached(signature + message) |
| 55 |
end |
| 56 |
|
| 57 |
# Verify a signature for a given signed message |
| 58 |
# |
| 59 |
# Raises if the signature is invalid. |
| 60 |
# |
| 61 |
# @param signed_message [String] Message combined with signature to be authenticated |
| 62 |
# |
| 63 |
# @raise [BadSignatureError] if the signature check fails |
| 64 |
# |
| 65 |
# @return [Boolean] was the signature authentic? |
| 66 |
def verify_attached(signed_message) |
| 67 |
raise LengthError, "Signed message can not be nil" if signed_message.nil? |
| 68 |
raise LengthError, "Signed message can not be shorter than a signature" if signed_message.bytesize <= signature_bytes |
| 69 |
|
| 70 |
buffer = Util.zeros(signed_message.bytesize) |
| 71 |
buffer_len = Util.zeros(FFI::Type::LONG_LONG.size) |
| 72 |
|
| 73 |
success = self.class.sign_ed25519_open(buffer, buffer_len, signed_message, signed_message.bytesize, @key) |
| 74 |
raise(BadSignatureError, "signature was forged/corrupt") unless success |
| 75 |
|
| 76 |
true |
| 77 |
end |
| 78 |
|
| 79 |
# Return the raw key in byte format |
| 80 |
# |
| 81 |
# @return [String] raw key as bytes |
| 82 |
def to_bytes |
| 83 |
@key |
| 84 |
end |
| 85 |
|
| 86 |
# The crypto primitive this VerifyKey class uses for signatures |
| 87 |
# |
| 88 |
# @return [Symbol] The primitive |
| 89 |
def primitive |
| 90 |
self.class.primitive |
| 91 |
end |
| 92 |
|
| 93 |
# The size of signatures verified by the VerifyKey class |
| 94 |
# |
| 95 |
# @return [Integer] The number of bytes in a signature |
| 96 |
def self.signature_bytes |
| 97 |
Ed25519::SIGNATUREBYTES |
| 98 |
end |
| 99 |
|
| 100 |
# The size of signatures verified by the VerifyKey instance |
| 101 |
# |
| 102 |
# @return [Integer] The number of bytes in a signature |
| 103 |
def signature_bytes |
| 104 |
Ed25519::SIGNATUREBYTES |
| 105 |
end |
| 106 |
|
| 107 |
# Return a new curve25519 (x25519) public key converted from this key |
| 108 |
# |
| 109 |
# it's recommeneded to read https://libsodium.gitbook.io/doc/advanced/ed25519-curve25519 |
| 110 |
# as it encourages using distinct keys for signing and for encryption |
| 111 |
# |
| 112 |
# @return [RbNaCl::PublicKey] |
| 113 |
def to_curve25519_public_key |
| 114 |
buffer = Util.zeros(Boxes::Curve25519XSalsa20Poly1305::PublicKey::BYTES) |
| 115 |
self.class.crypto_sign_ed25519_pk_to_curve25519(buffer, @key) |
| 116 |
Boxes::Curve25519XSalsa20Poly1305::PublicKey.new(buffer) |
| 117 |
end |
| 118 |
end |
| 119 |
end |
| 120 |
end |
| 121 |
end |