Class: Linzer::MLDSA::Key

Inherits:
Key
  • Object
show all
Defined in:
lib/linzer/ml_dsa.rb

Overview

An ML-DSA signing or verification key.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(material, params = {}) ⇒ Key

Returns a new instance of Key.



19
20
21
22
# File 'lib/linzer/ml_dsa.rb', line 19

def initialize(material, params = {})
  @algorithm = String(params.fetch(:algorithm))
  super
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



17
18
19
# File 'lib/linzer/ml_dsa.rb', line 17

def algorithm
  @algorithm
end

Instance Method Details

#sign(data) ⇒ String

Signs an RFC 9421 signature base with an empty FIPS 204 context.

Parameters:

  • data (String)

    Signature base bytes

Returns:

  • (String)

    Raw FIPS 204 signature bytes

Raises:



42
43
44
45
46
47
# File 'lib/linzer/ml_dsa.rb', line 42

def sign(data)
  validate_signing_key
  material.sign(data, context: "")
rescue MlDsa::Error => e
  raise SigningError, e.message, cause: e
end

#validate_signature_parameters(parameters) ⇒ true

Validates that the HTTP alg parameter matches this key's parameter set.

Parameters:

  • parameters (Hash)

    HTTP signature parameters

Returns:

  • (true)

    If alg is absent or matches this key

Raises:

  • (VerifyError)

    If alg selects another ML-DSA parameter set



29
30
31
32
33
34
35
# File 'lib/linzer/ml_dsa.rb', line 29

def validate_signature_parameters(parameters)
  supplied_algorithm = parameters["alg"] || parameters[:alg]
  return true if supplied_algorithm.nil? || supplied_algorithm == algorithm

  raise VerifyError,
    "Signature algorithm #{supplied_algorithm} does not match key algorithm #{algorithm}"
end

#verify(signature, data) ⇒ Boolean

Verifies an RFC 9421 signature base with an empty FIPS 204 context.

Parameters:

  • signature (String)

    Raw FIPS 204 signature bytes

  • data (String)

    Signature base bytes

Returns:

  • (Boolean)

    Whether the signature is valid

Raises:

  • (VerifyError)

    If public key material is unavailable



55
56
57
58
59
60
61
62
63
# File 'lib/linzer/ml_dsa.rb', line 55

def verify(signature, data)
  validate_verify_key
  return false unless signature.is_a?(String)
  return false unless signature.bytesize == parameter_set.signature_bytes

  verification_material.verify(data, signature, context: "")
rescue MlDsa::Error, ArgumentError, TypeError
  false
end