Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support signatures in DER format #68

Open
fhunleth opened this issue Feb 14, 2022 · 0 comments
Open

Support signatures in DER format #68

fhunleth opened this issue Feb 14, 2022 · 0 comments

Comments

@fhunleth
Copy link
Collaborator

Currently signatures are returned as raw bits. It's also common to need DER-encoded signatures. These are a few bytes longer, and it can be confusing since other APIs often assume one or the other without going into the details. It would be nice to have better documentation in this area and a helper function to return the DER-encoded version.

Here's some code from https://gist.githubusercontent.com/voltone/d3c0bb3ee821703f52d439e00262cb88/raw/e90117c60dceb07f3a674cbf5102ef6f2722e0c8/ecdsa_signature.ex:

defmodule ECDSASignature do
  require Record

  Record.defrecord(
    :ecdsa_signature,
    :"ECDSA-Sig-Value",
    Record.extract(:"ECDSA-Sig-Value", from_lib: "public_key/include/OTP-PUB-KEY.hrl")
  )

  def new(r, s) when is_integer(r) and is_integer(s) do
    ecdsa_signature(r: r, s: s)
  end

  def new(raw) when is_binary(raw) do
    size = raw |> byte_size() |> div(2)
    <<r::size(size)-unit(8), s::size(size)-unit(8)>> = raw
    new(r, s)
  end

  # Export to DER binary format, for use with :public_key.verify/4
  def to_der(ecdsa_signature() = signature) do
    :public_key.der_encode(:"ECDSA-Sig-Value", signature)
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant