Class: COSE::Key::EC2

Inherits:
COSE::Key show all
Defined in:
lib/cose/key/ec2.rb

Constant Summary collapse

CRV =
-1
X =
-2
Y =
-3
D =
-4
ALGS =
{
  ES256: -7,
  ES384: -35,
  ES512: -36
}
CRVS =
{
  P256: 1,
  P384: 2,
  P521: 3
}

Constants inherited from COSE::Key

ALG, BASE_IV, KID, KTY, KTY_EC2, KTY_OKP, KTY_RSA, KTY_SYMMETRIC, OPS

Instance Attribute Summary collapse

Attributes inherited from COSE::Key

#alg, #base_iv, #kid, #kty, #ops, #raw

Instance Method Summary collapse

Methods inherited from COSE::Key

decode, detect

Constructor Details

#initialize(attrs = {}) ⇒ EC2



22
23
24
25
26
27
28
# File 'lib/cose/key/ec2.rb', line 22

def initialize(attrs = {})
  super
  self.crv = attrs[CRV]
  self.x = attrs[X]
  self.y = attrs[Y]
  self.d = attrs[D]
end

Instance Attribute Details

#crvObject

Returns the value of attribute crv.



20
21
22
# File 'lib/cose/key/ec2.rb', line 20

def crv
  @crv
end

#dObject

Returns the value of attribute d.



20
21
22
# File 'lib/cose/key/ec2.rb', line 20

def d
  @d
end

#xObject

Returns the value of attribute x.



20
21
22
# File 'lib/cose/key/ec2.rb', line 20

def x
  @x
end

#yObject

Returns the value of attribute y.



20
21
22
# File 'lib/cose/key/ec2.rb', line 20

def y
  @y
end

Instance Method Details

#alg_keyObject



30
31
32
33
# File 'lib/cose/key/ec2.rb', line 30

def alg_key
  ALGS.invert[alg] or
  raise UknownAlgorithm, 'Unknown Algorithm'
end

#crv_keyObject



35
36
37
38
# File 'lib/cose/key/ec2.rb', line 35

def crv_key
  CRVS.invert[crv] or
  raise UknownAlgorithm, 'Unknown Curve'
end

#crv_nameObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/cose/key/ec2.rb', line 40

def crv_name
  case crv_key
  when :P256
    'prime256v1'
  when :P384
    'secp384r1'
  when :P521
    'secp521r1'
  end
end

#digestObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/cose/key/ec2.rb', line 51

def digest
  case alg_key
  when :ES256
    OpenSSL::Digest::SHA256
  when :ES384
    OpenSSL::Digest::SHA384
  when :ES512
    OpenSSL::Digest::SHA512
  end.new
end

#to_keyObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cose/key/ec2.rb', line 62

def to_key
  point = OpenSSL::PKey::EC::Point.new(
    OpenSSL::PKey::EC::Group.new(crv_name),
    OpenSSL::BN.new(['04' + x.unpack('H*').first + y.unpack('H*').first].pack('H*'), 2)
  )

  # Public key
  data_sequence = OpenSSL::ASN1::Sequence([
    OpenSSL::ASN1::Sequence([
      OpenSSL::ASN1::ObjectId("id-ecPublicKey"),
      OpenSSL::ASN1::ObjectId(crv_name)
    ]),
    OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed))
  ])

  if d
    # Private key
    data_sequence = OpenSSL::ASN1::Sequence([
      OpenSSL::ASN1::Integer(1),
      OpenSSL::ASN1::OctetString(OpenSSL::BN.new(d, 2).to_s(2)),
      OpenSSL::ASN1::ObjectId(crv_name, 0, :EXPLICIT),
      OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed), 1, :EXPLICIT)
    ])
  end

  OpenSSL::PKey::EC.new(data_sequence.to_der)
end

#verify(signature, signature_base_string) ⇒ Object



90
91
92
93
# File 'lib/cose/key/ec2.rb', line 90

def verify(signature, signature_base_string)
  public_key = to_key
  public_key.verify digest, signature, signature_base_string
end