silk.crypto.p384.

Key agreement (ECDH) and ECDSA signatures on the NIST P-384 curve (secp384r1).

When to use#

Use P384 for an ECDH key exchange where a protocol needs P-384, such as a TLS 1.3 key share secp384r1, and to sign with ECDSA, as in ES384 JSON Web Tokens or Node.js crypto.sign. Use EcdsaP384Sha384 to verify a DER signature with SHA-384, for example in a certificate chain, and EcdsaP384 to verify with another hash or in the fixed 96-byte form.

Details#

Public keys are 97 bytes in uncompressed SEC1 form: 0x04, then the X and Y coordinates, each 48 bytes big-endian. P384.compressPublicKey and P384.decompressPublicKey convert to and from the 49-byte compressed form. Secrets are 48 bytes big-endian. P384.agree returns the X coordinate of the shared point and consumes the key. P384.sign returns r and s, each 48 bytes, with the deterministic nonce of RFC 6979. EcdsaP384.toDer and EcdsaP384.fromDer convert signatures to and from DER. Nothing allocates.

Errors#

  • P384Error for a secret or a peer public key that is not valid.

  • P384SignatureError for a key or signature that is not valid, or that does not match.

Gotchas#

A valid signature proves only that the holder of the private key signed the message. It does not prove that you can trust the key. To check a certificate chain, use the silk.tls.trust_* modules. The key exchange does not authenticate the peer. Pass the shared secret through silk.crypto.hkdf before you use it as a key. The secret is not erased when it is dropped.

Examples#

Verify a signature#

silk
import silk.crypto.p384 { EcdsaP384Sha384, P384SignatureError }
import silk.core.result { Result }

pub fn main() -> i32 {
  // A public key and a signature of "hello" that another program made.
  let publicKey: [u8; 97] = [
    0x04,
    0xe8,
    0x6b,
    0x99,
    0x5e,
    0x35,
    0x0e,
    0x4d,
    0x31,
    0x2a,
    0x95,
    0xa6,
    0xb7,
    0xd5,
    0xd8,
    0xcd,
    0xcd,
    0xb4,
    0x68,
    0x2f,
    0x58,
    0x88,
    0x00,
    0xa1,
    0xcd,
    0x6c,
    0x97,
    0x2e,
    0x1d,
    0xcb,
    0x2a,
    0x5a,
    0xc3,
    0x8d,
    0x90,
    0x04,
    0x69,
    0x51,
    0xb0,
    0x3c,
    0xcb,
    0x3b,
    0x17,
    0x9a,
    0x89,
    0xfd,
    0xa8,
    0x8b,
    0x1b,
    0x48,
    0x5b,
    0x9f,
    0x2c,
    0x1f,
    0x44,
    0xb0,
    0xdd,
    0x41,
    0xce,
    0x6e,
    0x3c,
    0x3a,
    0x69,
    0x0b,
    0xbf,
    0xaa,
    0xbe,
    0xc1,
    0xe1,
    0xff,
    0x4a,
    0xef,
    0xb2,
    0x46,
    0x1f,
    0xdd,
    0xa6,
    0x62,
    0x9b,
    0xec,
    0xbd,
    0x43,
    0xcf,
    0x74,
    0xc1,
    0x3d,
    0x8c,
    0x1a,
    0x16,
    0x25,
    0x61,
    0x22,
    0x43,
    0xf0,
    0x04,
    0x61,
    0x48
  ]
  let signature: [u8; 104] = [
    0x30,
    0x66,
    0x02,
    0x31,
    0x00,
    0xb2,
    0x11,
    0x52,
    0x9f,
    0xfe,
    0x5e,
    0xf2,
    0x0c,
    0x08,
    0x48,
    0xbc,
    0x09,
    0xf5,
    0x2d,
    0x3a,
    0xb4,
    0xb8,
    0x79,
    0x2e,
    0x6a,
    0xe4,
    0x2d,
    0xb3,
    0x33,
    0xea,
    0x25,
    0xed,
    0x22,
    0x8e,
    0x3d,
    0x73,
    0x35,
    0x9d,
    0x59,
    0xac,
    0xaa,
    0x65,
    0xe0,
    0x98,
    0x57,
    0xd1,
    0x34,
    0x4f,
    0x37,
    0x9f,
    0x7f,
    0x6c,
    0xd3,
    0x02,
    0x31,
    0x00,
    0xd6,
    0x17,
    0x87,
    0xa8,
    0x8f,
    0xf1,
    0xdd,
    0x7f,
    0xbf,
    0x45,
    0x5d,
    0x3a,
    0x2b,
    0xf0,
    0x16,
    0x49,
    0xed,
    0xe6,
    0x4b,
    0xc2,
    0x3e,
    0xbe,
    0x12,
    0x6c,
    0x61,
    0x01,
    0xd2,
    0xea,
    0x73,
    0xc4,
    0x70,
    0xd0,
    0x22,
    0x30,
    0x6f,
    0x42,
    0x32,
    0xeb,
    0x6f,
    0x7b,
    0x84,
    0x9a,
    0x61,
    0x66,
    0x56,
    0xa6,
    0x50,
    0xd8
  ]
  match EcdsaP384Sha384.verify(&publicKey, b"hello", &signature) {
    Result<(), P384SignatureError>.Success {..} => {}
    Result<(), P384SignatureError>.Failure {..} => {
      return 1
    }
  }

  // The same signature does not match another message.
  return match EcdsaP384Sha384.verify(&publicKey, b"hellp", &signature) {
    Result<(), P384SignatureError>.Failure { error: P384SignatureError.AuthenticationFailed } => 0
    _ => 1
  }
}

Sign and verify#

silk
import silk.crypto.p384 { EcdsaP384, P384, P384Error, P384SignatureError }
import silk.crypto.sha2 { Sha256 }
import silk.core.result { Result }

pub fn main() -> i32 {
  // Real programs use `P384.generate()` with a secure `Random` provider.
  let mut secret: [u8; 48] = [0; 48]
  secret[47] = 7
  let key = match P384.fromBytes(&secret) {
    Result<P384, P384Error>.Success { value } => move value
    Result<P384, P384Error>.Failure {..} => {
      return 1
    }
  }
  let publicKey = key.publicKey()
  let signature = key.sign<Sha256>(b"hello")
  match EcdsaP384.verify<Sha256>(&publicKey, b"hello", &signature) {
    Result<(), P384SignatureError>.Success {..} => {}
    Result<(), P384SignatureError>.Failure {..} => {
      return 1
    }
  }

  // The deterministic nonce gives the same signature again.
  let again = key.sign<Sha256>(b"hello")
  if again[0] != signature[0] || again[95] != signature[95] {
    return 1
  }

  // The DER form, as in X.509 and TLS, has at most 104 bytes.
  let mut der: [u8; 104] = [0; 104]
  let length = EcdsaP384.toDer(&signature, &mut der)
  if der[0] != 0x30 || length > 104 {
    return 1
  }
  return 0
}

Import as P384 with import silk.crypto.p384 { P384 }.

Public declarations: 5.

P384Error#

silk
pub enum P384Error

The reason that a P-384 secret or public key is not valid. It holds no secret bytes.

InvalidLength#

silk
InvalidLength

A secret is not 48 bytes, or a public key is not 97 bytes.

InvalidScalar#

silk
InvalidScalar

The secret is 0, or not less than the order of the curve.

InvalidEncoding#

silk
InvalidEncoding

The public key does not start with 0x04. Compressed keys are not supported.

InvalidPoint#

silk
InvalidPoint

A coordinate is not less than the field prime, or the point is not on the curve.

Implementation Debug for P384Error#

silk
impl Debug for P384Error

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut Writer

Writes the name of the variant, such as InvalidLength.

P384#

silk
pub struct P384

A P-384 secret, for key agreement and for ECDSA signatures. It is not Copy.

When to use#

Use it for an ECDH key exchange where a protocol needs P-384, such as a TLS 1.3 key share secp384r1, or to sign with ECDSA.

Details#

P384.agree consumes it, so make a new one for each exchange. P384.sign does not. Public keys are 97 bytes in uncompressed SEC1 form: 0x04, then the X and Y coordinates, each 48 bytes big-endian. Secrets are 48 bytes big-endian. Nothing allocates.

Gotchas#

The key exchange does not authenticate the peer. Pass the shared secret through silk.crypto.hkdf before you use it as a key. The secret is not erased when it is dropped.

Implementation Copy for P384#

silk
impl Copy for P384

Implementation P384#

silk
impl P384

Associated function validatePublicKey#

silk
pub fn validatePublicKey(publicKey: &[u8]) -> Result<(), P384Error>

Checks that publicKey is a valid uncompressed P-384 public key.

It must be 97 bytes: 0x04, then X and Y, each 48 bytes big-endian and less than the field prime, and the point must be on the curve.

Errors#

  • InvalidLength, InvalidEncoding, or InvalidPoint.

Associated function fromBytes#

silk
pub fn fromBytes(bytes: &[u8]) -> Result<P384, P384Error>

Returns the secret for the 48 big-endian bytes bytes.

Errors#

  • InvalidLength if bytes is not 48 bytes.

  • InvalidScalar if the value is 0, or not less than the order of the curve.

Associated function generate#

silk
pub effect fn generate() -> P384 ? &mut Random

Returns a new random secret, with bytes from the ambient Random.

It draws 48 bytes again until the value is valid, so the result is uniform. The Random provider must be cryptographically secure. If it fails, the program stops.

Method publicKey#

silk
pub fn publicKey(self: &Self) -> [u8; 97]

Returns the 97-byte uncompressed public key to send to the peer. It does not consume the secret.

Method agree#

silk
pub fn agree(self: Self, peer: &[u8]) -> Result<[u8; 48], P384Error>

Returns the 48-byte shared secret for the public key peer, and consumes the secret.

The result is the big-endian X coordinate of the shared point.

Errors#

  • InvalidLength, InvalidEncoding, or InvalidPoint if peer is not a valid uncompressed public key. The secret is consumed also when it fails.

Gotchas#

Pass the result through silk.crypto.hkdf before you use it as a key.

Method toBytes#

silk
pub fn toBytes(self: &Self) -> [u8; 48]

Returns the 48 big-endian bytes of the secret, the input of P384.fromBytes.

Gotchas#

The bytes are the private key. Keep them secret.

Method sign#

silk
pub fn sign<H: Digest>(self: &Self, message: &[u8]) -> [u8; 96]

Returns the ECDSA signature of message, hashed with H, as 96 bytes: r, then s, each 48 bytes big-endian.

The nonce is deterministic (RFC 6979) with HMAC over H, so the same key, hash, and message always give the same signature. EcdsaP384.toDer converts the result to DER.

Gotchas#

The program traps if message is longer than H allows.

Method signDigest#

silk
pub fn signDigest<H: Digest>(self: &Self, digest: &[u8]) -> [u8; 96]

Returns the ECDSA signature of a message whose digest is digest, as 96 bytes: r, then s, each 48 bytes big-endian.

H is the hash for the RFC 6979 nonce, normally the hash that made digest. A digest longer than 48 bytes is cut to its first 48 bytes, as ECDSA requires.

Associated function compressPublicKey#

silk
pub fn compressPublicKey(publicKey: &[u8]) -> Result<[u8; 49], P384Error>

Returns the 49-byte compressed form of the uncompressed public key publicKey: 0x02 or 0x03 for the parity of Y, then X.

Errors#

  • InvalidLength, InvalidEncoding, or InvalidPoint if publicKey is not a valid uncompressed public key.

Associated function decompressPublicKey#

silk
pub fn decompressPublicKey(publicKey: &[u8]) -> Result<[u8; 97], P384Error>

Returns the 97-byte uncompressed form of the compressed public key publicKey.

Errors#

  • InvalidLength if publicKey is not 49 bytes.

  • InvalidEncoding if it does not start with 0x02 or 0x03.

  • InvalidPoint if X is not less than the field prime, or no point has that X.

P384SignatureError#

silk
pub enum P384SignatureError

The reason that an ECDSA P-384 verification failed.

InvalidKey#

silk
InvalidKey

The public key is not a valid uncompressed P-384 key.

InvalidEncoding#

silk
InvalidEncoding

The signature is not strict DER, or r or s is not between 1 and the order of the curve.

UnsupportedAlgorithm#

silk
UnsupportedAlgorithm

A certificate names a key or signature algorithm other than ECDSA P-384 with SHA-384.

InvalidParameters#

silk
InvalidParameters

A certificate has wrong algorithm parameters, or a BIT STRING with unused bits.

AuthenticationFailed#

silk
AuthenticationFailed

The inputs are valid, but the signature does not match the message and the key.

Implementation Debug for P384SignatureError#

silk
impl Debug for P384SignatureError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut Writer

Writes the name of the variant, such as InvalidKey.

EcdsaP384Sha384#

silk
pub struct EcdsaP384Sha384

Functions that verify ECDSA signatures on P-384 with SHA-384.

When to use#

Use it to verify a P-384 signature, for example in a certificate chain or a JSON Web Token with ES384. To sign, use P384.sign and EcdsaP384.toDer.

Details#

Public keys are 97 bytes in uncompressed SEC1 form: 0x04, then the X and Y coordinates, each 48 bytes big-endian. Signatures are in DER form. Nothing allocates.

Gotchas#

A valid signature proves only that the holder of the private key signed the message. It does not prove that you can trust the key. To check a certificate chain, use the silk.tls.trust_* modules.

Implementation EcdsaP384Sha384#

silk
impl EcdsaP384Sha384

Associated function validatePublicKey#

silk
pub fn validatePublicKey(publicKey: &[u8]) -> Result<(), P384SignatureError>

Checks that publicKey is a valid uncompressed P-384 public key.

It must be 97 bytes: 0x04, then X and Y, each 48 bytes big-endian and less than the field prime, and the point must be on the curve.

Errors#

  • InvalidKey if it is not valid.

Associated function verify#

silk
pub fn verify(publicKey: &[u8], message: &[u8], signature: &[u8]) -> Result<
    (),
    P384SignatureError,
  >

Checks that signature is a valid signature of message by publicKey.

publicKey is 97 bytes, uncompressed. signature is DER. The function hashes message with SHA-384, so give the message, not its digest. Both high and low s values are accepted.

Errors#

  • InvalidKey or InvalidEncoding for input that is not valid.

  • AuthenticationFailed if the signature does not match.

Associated function verifyCertificate#

silk
pub fn verifyCertificate<'key, 'sig>(
    keyAlgorithm: &AlgorithmView<'key>,
    publicKey: &BitStringView<'key>,
    message: &[u8],
    signatureAlgorithm: &AlgorithmView<'sig>,
    signature: &BitStringView<'sig>,
  ) -> Result<(), P384SignatureError>

Verifies a certificate signature from the parts that silk.tls.certificate reads.

message is the signed part of the certificate (the TBSCertificate DER). The key algorithm must be id-ecPublicKey with the curve secp384r1, and the signature algorithm must be ecdsa-with-SHA384 with no parameters.

Errors#

  • UnsupportedAlgorithm or InvalidParameters if the algorithms do not match.

  • InvalidKey or InvalidEncoding if the key or the signature is not valid.

  • AuthenticationFailed if the signature does not match.

Gotchas#

It checks only this one signature. It does not check the chain, the dates, or the names.

EcdsaP384#

silk
pub struct EcdsaP384

ECDSA on P-384 with any hash, and conversion between the two signature forms.

When to use#

Use it to verify a signature made with a hash other than SHA-256, or a signature in the fixed 96-byte form (r, then s), as in JSON Web Tokens and WebCrypto. To sign, use P384.sign. For SHA-384 with DER signatures, as in TLS and X.509, EcdsaP384Sha384 is shorter.

Details#

Public keys are 97 bytes in uncompressed SEC1 form. A fixed-form signature is 96 bytes. A DER signature is a SEQUENCE of the two INTEGERs, at most 104 bytes. Nothing allocates.

Gotchas#

A valid signature proves only that the holder of the private key signed the message. It does not prove that you can trust the key.

Implementation EcdsaP384#

silk
impl EcdsaP384

Associated function verify#

silk
pub fn verify<H: Digest>(publicKey: &[u8], message: &[u8], signature: &[u8]) -> Result<
    (),
    P384SignatureError,
  >

Checks that signature, in the fixed 96-byte form, is a valid signature of message, hashed with H, by publicKey.

Both high and low s values are accepted.

Errors#

  • InvalidKey if publicKey is not a valid uncompressed key.

  • InvalidEncoding if signature is not 96 bytes, or r or s is not between 1 and the order of the curve.

  • AuthenticationFailed if the signature does not match.

Gotchas#

The program traps if message is longer than H allows.

Associated function verifyDigest#

silk
pub fn verifyDigest(publicKey: &[u8], digest: &[u8], signature: &[u8]) -> Result<
    (),
    P384SignatureError,
  >

Checks that signature, in the fixed 96-byte form, is a valid signature by publicKey of a message whose digest is digest.

A digest longer than 48 bytes is cut to its first 48 bytes, as ECDSA requires.

Errors#

Associated function fromDer#

silk
pub fn fromDer(signature: &[u8]) -> Result<[u8; 96], P384SignatureError>

Returns the fixed 96-byte form of the DER signature signature.

Errors#

  • InvalidEncoding if signature is not strict DER, or r or s is not between 1 and the order of the curve.

Associated function toDer#

silk
pub fn toDer(signature: &[u8], output: &mut [u8]) -> usize

Writes the DER form of the fixed 96-byte signature signature to the start of output, and returns its length, at most 104 bytes.

It does not check the values of r and s. The bytes of output after the DER do not change.

Gotchas#

The program traps if signature is not 96 bytes, or output is shorter than the DER.

Category: Cryptography.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page