QSC Post Quantum Cryptographic Library 1.3.0.0 (C1)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
ecdsap256base.h File Reference

Internal API for NIST P-256 (secp256r1) ECDSA key exchange operations. More...

#include "qsccommon.h"

Go to the source code of this file.

Macros

#define EC_NISTP256_SEED_SIZE   32U
 Seed and derived scalar byte length.
#define EC_NISTP256_PUBLICKEY_SIZE   64U
 Public key byte length (X || Y, each 32 bytes big-endian)
#define EC_NISTP256_PRIVATEKEY_SIZE   96U
 Private key byte length (seed[32] || pubkey[64])
#define EC_NISTP256_SIGNATURE_SIZE   64U
 Signature byte length (r[32] || s[32], big-endian)
#define EC_NISTP256_SHAREDSECRET_SIZE   32U
 Shared secret byte length derived from the affine X coordinate.

Functions

int32_t qsc_p256_publickey_from_privatekey (uint8_t *publickey, const uint8_t *privatekey)
 Derive a P-256 public key from a raw private scalar.
int32_t qsc_p256_keypair (uint8_t *publickey, uint8_t *privatekey, const uint8_t *seed)
 Generate a P-256 public/private key pair from a 32-byte seed.
int32_t qsc_p256_sign (uint8_t *signedmsg, size_t *smsglen, const uint8_t *message, size_t msglen, const uint8_t *privatekey)
 Sign a message using a P-256 private key.
int32_t qsc_p256_sign_scalar (uint8_t *signedmsg, size_t *smsglen, const uint8_t *message, size_t msglen, const uint8_t *privatekey)
 Sign a message using a P-256 private key (RFC 6979 deterministic k using scalar d).
bool qsc_p256_verify (uint8_t *message, size_t *msglen, const uint8_t *signedmsg, size_t smsglen, const uint8_t *publickey)
 Verify a P-256 signed message and recover the message bytes.

Detailed Description

Internal API for NIST P-256 (secp256r1) ECDSA key exchange operations.

cond

This header defines the internal functions for NIST P-256 (secp256r1) ECDSA operations, including key pair generation from a seed, deterministic message signing using RFC 6979, and signature verification. The implementation uses Jacobian projective coordinates for elliptic curve point arithmetic, Solinas reduction for the field prime p, and Barrett reduction for the group order n.

Key and signature encoding is big-endian and compatible with X9.62/SEC 1 conventions:

  • Public key: 64 bytes (32-byte X || 32-byte Y, uncompressed, no 0x04 prefix)
  • Private key: 32-byte seed || 64-byte public key = 96 bytes
  • Signature: 32-byte r || 32-byte s

Function Documentation

◆ qsc_p256_keypair()

int32_t qsc_p256_keypair ( uint8_t * publickey,
uint8_t * privatekey,
const uint8_t * seed )

Generate a P-256 public/private key pair from a 32-byte seed.

Derives a private scalar from the seed via SHA-256, clamps it into [1, n-1], computes Q = d*G using the P-256 base point, and stores both keys. The private key layout is seed[32] || Qx[32] || Qy[32].

Parameters
publickey[uint8_t*] Output public key (64 bytes: Qx || Qy, big-endian).
privatekey[uint8_t*] Output private key (96 bytes: seed || Qx || Qy).
seed[const uint8_t*] 32-byte random seed.
Returns
[int32_t] Returns 0 on success, or a negative error code on failure.

◆ qsc_p256_publickey_from_privatekey()

int32_t qsc_p256_publickey_from_privatekey ( uint8_t * publickey,
const uint8_t * privatekey )

Derive a P-256 public key from a raw private scalar.

This function derives the affine public point Q = dG from a 32-byte big-endian private scalar and serializes the result as the raw public-key form Qx || Qy.

The private scalar must be in the range [1, n - 1], where n is the order of the P-256 base point.

Parameters
publickey[uint8_t*] Output buffer receiving the 64-byte public key.
privatekey[const uint8_t*] Input 32-byte private scalar.
Returns
[int32_t] Returns 0 on success, or a negative error code on failure.

◆ qsc_p256_sign()

int32_t qsc_p256_sign ( uint8_t * signedmsg,
size_t * smsglen,
const uint8_t * message,
size_t msglen,
const uint8_t * privatekey )

Sign a message using a P-256 private key.

Produces a 64-byte signature (r || s) prepended to the message in the signedmsg buffer. The nonce k is derived deterministically from the private key and message hash per RFC 6979 using HMAC-SHA256, eliminating the need for a random number generator at signing time.

Parameters
signedmsg[uint8_t*] Output signed-message buffer (msglen + 64 bytes).
smsglen[size_t*] Set to msglen + EC_NISTP256_SIGNATURE_SIZE on success, 0 on failure.
message[const uint8_t*] Message to sign.
msglen[size_t] Message length in bytes.
privatekey[const uint8_t*] 96-byte private key (seed || pubkey).
Returns
[int32_t] 0 on success, -1 on failure.

◆ qsc_p256_sign_scalar()

int32_t qsc_p256_sign_scalar ( uint8_t * signedmsg,
size_t * smsglen,
const uint8_t * message,
size_t msglen,
const uint8_t * privatekey )

Sign a message using a P-256 private key (RFC 6979 deterministic k using scalar d).

Produces a 64-byte signature (r || s) prepended to the message in the signedmsg buffer. The nonce k is derived deterministically from the private key and message hash per RFC 6979 using HMAC-SHA256, eliminating the need for a random number generator at signing time.

Parameters
signedmsg[uint8_t*] Output signed-message buffer (msglen + 64 bytes).
smsglen[size_t*] Set to msglen + EC_NISTP256_SIGNATURE_SIZE on success, 0 on failure.
message[const uint8_t*] Message to sign.
msglen[size_t] Message length in bytes.
privatekey[const uint8_t*] 96-byte private key (seed || pubkey).
Returns
[int32_t] 0 on success, -1 on failure.

◆ qsc_p256_verify()

bool qsc_p256_verify ( uint8_t * message,
size_t * msglen,
const uint8_t * signedmsg,
size_t smsglen,
const uint8_t * publickey )

Verify a P-256 signed message and recover the message bytes.

Verifies the 64-byte (r || s) signature prepended to signedmsg against the 64-byte public key. On success the message bytes are copied into message and msglen is set. On failure message is zeroed and msglen is set to 0.

Parameters
message[uint8_t*] Output message buffer (at least smsglen - 64 bytes).
msglen[size_t*] Set to the recovered message length on success.
signedmsg[const uint8_t*] Signed-message buffer (signature || message).
smsglen[size_t] Total signed-message length.
publickey[const uint8_t*] 64-byte public key (Qx || Qy, big-endian).
Returns
[int32_t] 0 on success, -1 on failure.