File: eddsa.rb

package info (click to toggle)
ruby-cose 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272 kB
  • sloc: ruby: 993; sh: 5; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 870 bytes parent folder | download
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
# frozen_string_literal: true

require "cose/algorithm/signature_algorithm"
require "cose/error"
require "cose/key/okp"
require "openssl"

module COSE
  module Algorithm
    class EdDSA < SignatureAlgorithm
      private

      def valid_key?(key)
        cose_key = to_cose_key(key)

        cose_key.is_a?(COSE::Key::OKP) && (!cose_key.alg || cose_key.alg == id)
      end

      def to_pkey(key)
        case key
        when COSE::Key::OKP
          key.to_pkey
        when OpenSSL::PKey::PKey
          key
        else
          raise(COSE::Error, "Incompatible key for algorithm")
        end
      end

      def valid_signature?(key, signature, verification_data)
        pkey = to_pkey(key)

        begin
          pkey.verify(nil, signature, verification_data)
        rescue OpenSSL::PKey::PKeyError
          false
        end
      end
    end
  end
end