1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
# frozen_string_literal: true
require "cose/algorithm/signature_algorithm"
require "cose/error"
require "cose/key/curve"
require "cose/key/ec2"
require "openssl"
require "openssl/signature_algorithm/ecdsa"
module COSE
module Algorithm
class ECDSA < SignatureAlgorithm
attr_reader :hash_function, :curve
def initialize(*args, hash_function:, curve_name:)
super(*args)
@hash_function = hash_function
@curve = COSE::Key::Curve.by_name(curve_name) || raise("Couldn't find curve with name='#{curve_name}'")
end
private
def valid_key?(key)
cose_key = to_cose_key(key)
cose_key.is_a?(COSE::Key::EC2) && (!cose_key.alg || cose_key.alg == id)
end
def signature_algorithm_class
OpenSSL::SignatureAlgorithm::ECDSA
end
def signature_algorithm_parameters
if curve
super.merge(curve: curve.pkey_name)
else
super
end
end
def to_pkey(key)
case key
when COSE::Key::EC2
key.to_pkey
when OpenSSL::PKey::EC
key
else
raise(COSE::Error, "Incompatible key for algorithm")
end
end
end
end
end
|