Class: COSE::Key::EC2

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

Constant Summary collapse

LABEL_Y =
-3
KTY_EC2 =
2
ZERO_BYTE =
"\0".b

Constants inherited from CurveKey

CurveKey::LABEL_CRV, CurveKey::LABEL_D, CurveKey::LABEL_X

Constants inherited from Base

Base::LABEL_ALG, Base::LABEL_BASE_IV, Base::LABEL_KEY_OPS, Base::LABEL_KID, Base::LABEL_KTY

Instance Attribute Summary collapse

Attributes inherited from CurveKey

#crv, #d, #x

Attributes inherited from Base

#alg, #base_iv, #key_ops, #kid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

deserialize, from_map, #serialize

Constructor Details

#initialize(y: nil, **keyword_arguments) ⇒ EC2

rubocop:disable Naming/MethodParameterName



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

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

Instance Attribute Details

#yObject (readonly)

Returns the value of attribute y.



51
52
53
# File 'lib/cose/key/ec2.rb', line 51

def y
  @y
end

Class Method Details

.enforce_type(map) ⇒ Object



16
17
18
19
20
# File 'lib/cose/key/ec2.rb', line 16

def self.enforce_type(map)
  if map[LABEL_KTY] != KTY_EC2
    raise "Not an EC2 key"
  end
end

.from_pkey(pkey) ⇒ Object



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
# File 'lib/cose/key/ec2.rb', line 22

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) ⇒ Object



115
116
117
# File 'lib/cose/key/ec2.rb', line 115

def self.keyword_arguments_for_initialize(map)
  super.merge(y: map[LABEL_Y])
end

Instance Method Details

#curveObject



111
112
113
# File 'lib/cose/key/ec2.rb', line 111

def curve
  Curve.find(crv)
end

#mapObject



63
64
65
66
67
68
# File 'lib/cose/key/ec2.rb', line 63

def map
  super.merge(
    Base::LABEL_KTY => KTY_EC2,
    LABEL_Y => y,
  ).compact
end

#pad_coordinate(group, coordinate) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/cose/key/ec2.rb', line 119

def pad_coordinate(group, coordinate)
  coordinate_length = (group.degree + 7) / 8
  padding_required = coordinate_length - coordinate.length
  return coordinate if padding_required <= 0

  (ZERO_BYTE * padding_required) + coordinate
end

#to_pkeyObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cose/key/ec2.rb', line 70

def to_pkey
  if curve
    group = OpenSSL::PKey::EC::Group.new(curve.pkey_name)
    public_key_bn = OpenSSL::BN.new("\x04" + pad_coordinate(group, x) + pad_coordinate(group, y), 2)
    public_key_point = OpenSSL::PKey::EC::Point.new(group, public_key_bn)

    # RFC5480 SubjectPublicKeyInfo
    asn1 = OpenSSL::ASN1::Sequence(
      [
        OpenSSL::ASN1::Sequence(
          [
            OpenSSL::ASN1::ObjectId("id-ecPublicKey"),
            OpenSSL::ASN1::ObjectId(curve.pkey_name),
          ]
        ),
        OpenSSL::ASN1::BitString(public_key_point.to_octet_string(:uncompressed))
      ]
    )

    if d
      # RFC5915 ECPrivateKey
      asn1 = OpenSSL::ASN1::Sequence(
        [
          OpenSSL::ASN1::Integer.new(1),
          # Not properly padded but OpenSSL doesn't mind
          OpenSSL::ASN1::OctetString(OpenSSL::BN.new(d, 2).to_s(2)),
          OpenSSL::ASN1::ObjectId(curve.pkey_name, 0, :EXPLICIT),
          OpenSSL::ASN1::BitString(public_key_point.to_octet_string(:uncompressed), 1, :EXPLICIT),
        ]
      )

      der = asn1.to_der
      return OpenSSL::PKey::EC.new(der)
    end

    OpenSSL::PKey::EC.new(asn1.to_der)
  else
    raise "Unsupported curve #{crv}"
  end
end