luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

P256.h (2549B)


      1 /*
      2  * P256.h - NIST P-256 (secp256r1) Elliptic Curve
      3  *
      4  * Curve: y^2 = x^3 - 3x + b (mod p)
      5  * p = 2^256 - 2^224 + 2^192 + 2^96 - 1
      6  *
      7  * Used for:
      8  * - ECDH (Elliptic Curve Diffie-Hellman)
      9  * - ECDSA (Elliptic Curve Digital Signature Algorithm)
     10  *
     11  * Standards: FIPS 186-4, SEC 2, RFC 5480
     12  */
     13 
     14 #ifndef P256_H
     15 #define P256_H
     16 
     17 #include <stdint.h>
     18 #include <stddef.h>
     19 
     20 #ifdef __cplusplus
     21 extern "C" {
     22 #endif
     23 
     24 #define P256_SCALAR_SIZE 32
     25 #define P256_POINT_SIZE 65  /* Uncompressed: 0x04 || x || y */
     26 
     27 /* ECDH: Elliptic Curve Diffie-Hellman on P-256 */
     28 
     29 /**
     30  * Generate P-256 ECDH keypair
     31  * @param public_key Output: 65-byte public key (uncompressed point)
     32  * @param private_key Output: 32-byte private key (scalar)
     33  */
     34 void p256_ecdh_keypair(uint8_t public_key[65], uint8_t private_key[32]);
     35 
     36 /**
     37  * Derive public key from private key
     38  * @param public_key Output: 65-byte public key
     39  * @param private_key Input: 32-byte private key
     40  */
     41 void p256_ecdh_public_key(uint8_t public_key[65], const uint8_t private_key[32]);
     42 
     43 /**
     44  * Compute ECDH shared secret
     45  * @param shared_secret Output: 32-byte shared secret (x-coordinate)
     46  * @param my_private_key My 32-byte private key
     47  * @param their_public_key Their 65-byte public key
     48  * @return 0 on success, -1 on error
     49  */
     50 int p256_ecdh_shared_secret(uint8_t shared_secret[32],
     51                              const uint8_t my_private_key[32],
     52                              const uint8_t their_public_key[65]);
     53 
     54 /* ECDSA: Elliptic Curve Digital Signature Algorithm on P-256 */
     55 
     56 /**
     57  * Generate P-256 ECDSA keypair
     58  * @param public_key Output: 65-byte public key
     59  * @param private_key Output: 32-byte private key
     60  */
     61 void p256_ecdsa_keypair(uint8_t public_key[65], uint8_t private_key[32]);
     62 
     63 /**
     64  * Sign message hash with ECDSA
     65  * @param signature Output: 64-byte signature (r || s)
     66  * @param message_hash Input: 32-byte message hash (SHA-256)
     67  * @param private_key Input: 32-byte private key
     68  * @return 0 on success, -1 on error
     69  */
     70 int p256_ecdsa_sign(uint8_t signature[64],
     71                     const uint8_t message_hash[32],
     72                     const uint8_t private_key[32]);
     73 
     74 /**
     75  * Verify ECDSA signature
     76  * @param signature Input: 64-byte signature (r || s)
     77  * @param message_hash Input: 32-byte message hash
     78  * @param public_key Input: 65-byte public key
     79  * @return 0 if valid, -1 if invalid
     80  */
     81 int p256_ecdsa_verify(const uint8_t signature[64],
     82                       const uint8_t message_hash[32],
     83                       const uint8_t public_key[65]);
     84 
     85 #ifdef __cplusplus
     86 }
     87 #endif
     88 
     89 #endif /* P256_H */