MatterEx.Crypto.Certificate (matter_ex v0.3.0)

Copy Markdown View Source

ECDSA P-256 signing/verification for Matter CASE authentication.

Wraps Erlang's :crypto and :public_key modules.

Summary

Functions

Build a minimal PKCS#10 Certificate Signing Request (CSR) in DER format.

Convert a DER-encoded ECDSA signature to raw P1363 format (r || s).

Compute ECDH shared secret (P-256 x-coordinate, 32 bytes).

Generate a new P-256 keypair.

Extract the EC public key from a PKCS#10 CSR DER.

Convert a raw P1363 signature (r || s, 64 bytes) to DER format.

Build a minimal self-signed X.509 DER certificate.

Sign a message with ECDSA-SHA256 over P-256.

Sign a message with ECDSA-SHA256 over P-256.

Verify an ECDSA-SHA256 signature over P-256 (DER-encoded signature).

Verify an ECDSA-SHA256 signature in raw P1363 format (r || s, 64 bytes).

Functions

build_csr(pub, priv)

@spec build_csr(binary(), binary()) :: binary()

Build a minimal PKCS#10 Certificate Signing Request (CSR) in DER format.

Used during commissioning when chip-tool sends CSRRequest. The CSR contains the EC public key and is signed with the private key.

der_signature_to_raw(arg)

@spec der_signature_to_raw(binary()) :: binary()

Convert a DER-encoded ECDSA signature to raw P1363 format (r || s).

For P-256, the output is always exactly 64 bytes.

ecdh(peer_public_key, my_private_key)

@spec ecdh(binary(), binary()) :: binary()

Compute ECDH shared secret (P-256 x-coordinate, 32 bytes).

generate_keypair()

@spec generate_keypair() :: {binary(), binary()}

Generate a new P-256 keypair.

Returns {public_key, private_key} where:

  • public_key is a 65-byte SEC1 uncompressed point (0x04 || x || y)
  • private_key is a 32-byte scalar

pubkey_from_csr(csr_der)

@spec pubkey_from_csr(binary()) :: binary()

Extract the EC public key from a PKCS#10 CSR DER.

Returns the 65-byte uncompressed SEC1 point (0x04 || x || y).

raw_signature_to_der(arg)

@spec raw_signature_to_der(binary()) :: binary()

Convert a raw P1363 signature (r || s, 64 bytes) to DER format.

self_signed_der(pub, priv, cn)

@spec self_signed_der(binary(), binary(), String.t()) :: binary()

Build a minimal self-signed X.509 DER certificate.

Used during commissioning when chip-tool requests PAI/DAC certificates. With --bypass-attestation-verifier, chip-tool won't validate the content.

sign(message, private_key)

@spec sign(binary(), binary()) :: binary()

Sign a message with ECDSA-SHA256 over P-256.

Returns the DER-encoded signature.

sign_raw(message, private_key)

@spec sign_raw(binary(), binary()) :: binary()

Sign a message with ECDSA-SHA256 over P-256.

Returns the raw P1363 format signature (r || s, 64 bytes). Matter uses this format for attestation and NOCSR signatures.

verify(message, signature, public_key)

@spec verify(binary(), binary(), binary()) :: boolean()

Verify an ECDSA-SHA256 signature over P-256 (DER-encoded signature).

verify_raw(message, signature, public_key)

@spec verify_raw(binary(), binary(), binary()) :: boolean()

Verify an ECDSA-SHA256 signature in raw P1363 format (r || s, 64 bytes).

Matter CASE uses raw format. Converts to DER for Erlang's crypto module.