class COSE::Key::EC2

Constants

KTY_EC2
LABEL_Y

Attributes

y[R]

Public Class Methods

enforce_type(map) click to toggle source
# File lib/cose/key/ec2.rb, line 14
def self.enforce_type(map)
  if map[LABEL_KTY] != KTY_EC2
    raise "Not an EC2 key"
  end
end
from_pkey(pkey) click to toggle source
# File lib/cose/key/ec2.rb, line 20
def self.from_pkey(pkey)
  curve = Curve.by_pkey_name(pkey.group.curve_name) || raise("Unsupported EC curve #{pkey.group.curve_name}")

  case pkey
  when OpenSSL::PKey::EC::Point
    public_key = pkey
  when OpenSSL::PKey::EC
    public_key = pkey.public_key
    private_key = pkey.private_key
  else
    raise "Unsupported"
  end

  if public_key
    bytes = public_key.to_bn.to_s(2)[1..-1]

    coordinate_length = bytes.size / 2

    x = bytes[0..(coordinate_length - 1)]
    y = bytes[coordinate_length..-1]
  end

  if private_key
    d = private_key.to_s(2)
  end

  new(crv: curve.id, x: x, y: y, d: d)
end
keyword_arguments_for_initialize(map) click to toggle source
# File lib/cose/key/ec2.rb, line 90
def self.keyword_arguments_for_initialize(map)
  super.merge(y: map[LABEL_Y])
end
new(y: nil, **keyword_arguments) click to toggle source
Calls superclass method COSE::Key::CurveKey::new
# File lib/cose/key/ec2.rb, line 51
def initialize(y: nil, **keyword_arguments) # rubocop:disable Naming/MethodParameterName
  if (!y || !keyword_arguments[:x]) && !keyword_arguments[:d]
    raise ArgumentError, "Both x and y are required if d is missing"
  else
    super(**keyword_arguments)

    @y = y
  end
end

Public Instance Methods

curve() click to toggle source
# File lib/cose/key/ec2.rb, line 86
def curve
  Curve.find(crv)
end
map() click to toggle source
Calls superclass method COSE::Key::CurveKey#map
# File lib/cose/key/ec2.rb, line 61
def map
  super.merge(
    Base::LABEL_KTY => KTY_EC2,
    LABEL_Y => y,
  ).compact
end
to_pkey() click to toggle source
# File lib/cose/key/ec2.rb, line 68
def to_pkey
  if curve
    group = OpenSSL::PKey::EC::Group.new(curve.pkey_name)
    pkey = OpenSSL::PKey::EC.new(group)
    public_key_bn = OpenSSL::BN.new("\x04" + x + y, 2)
    public_key_point = OpenSSL::PKey::EC::Point.new(group, public_key_bn)
    pkey.public_key = public_key_point

    if d
      pkey.private_key = OpenSSL::BN.new(d, 2)
    end

    pkey
  else
    raise "Unsupported curve #{crv}"
  end
end