1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimECDSA_SIG_get0, ECDSA_SIG_get0_r, ECDSA_SIG_get0_s, ECDSA_SIG_set0, 6e71b7053SJung-uk KimECDSA_SIG_new, ECDSA_SIG_free, i2d_ECDSA_SIG, d2i_ECDSA_SIG, ECDSA_size, 7e71b7053SJung-uk KimECDSA_sign, ECDSA_do_sign, ECDSA_verify, ECDSA_do_verify, ECDSA_sign_setup, 8e71b7053SJung-uk KimECDSA_sign_ex, ECDSA_do_sign_ex - low level elliptic curve digital signature 9e71b7053SJung-uk Kimalgorithm (ECDSA) functions 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim=head1 SYNOPSIS 12e71b7053SJung-uk Kim 13e71b7053SJung-uk Kim #include <openssl/ecdsa.h> 14e71b7053SJung-uk Kim 15e71b7053SJung-uk Kim ECDSA_SIG *ECDSA_SIG_new(void); 16e71b7053SJung-uk Kim void ECDSA_SIG_free(ECDSA_SIG *sig); 17e71b7053SJung-uk Kim void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps); 18e71b7053SJung-uk Kim const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig); 19e71b7053SJung-uk Kim const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig); 20e71b7053SJung-uk Kim int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s); 21e71b7053SJung-uk Kim int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp); 22e71b7053SJung-uk Kim ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len); 23e71b7053SJung-uk Kim int ECDSA_size(const EC_KEY *eckey); 24e71b7053SJung-uk Kim 25e71b7053SJung-uk Kim int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 26e71b7053SJung-uk Kim unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); 27e71b7053SJung-uk Kim ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len, 28e71b7053SJung-uk Kim EC_KEY *eckey); 29e71b7053SJung-uk Kim 30e71b7053SJung-uk Kim int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 31e71b7053SJung-uk Kim const unsigned char *sig, int siglen, EC_KEY *eckey); 32e71b7053SJung-uk Kim int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, 33e71b7053SJung-uk Kim const ECDSA_SIG *sig, EC_KEY* eckey); 34e71b7053SJung-uk Kim 35e71b7053SJung-uk Kim ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, 36e71b7053SJung-uk Kim const BIGNUM *kinv, const BIGNUM *rp, 37e71b7053SJung-uk Kim EC_KEY *eckey); 38e71b7053SJung-uk Kim int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp); 39e71b7053SJung-uk Kim int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, 40e71b7053SJung-uk Kim unsigned char *sig, unsigned int *siglen, 41e71b7053SJung-uk Kim const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); 42e71b7053SJung-uk Kim 43e71b7053SJung-uk Kim=head1 DESCRIPTION 44e71b7053SJung-uk Kim 45e71b7053SJung-uk KimNote: these functions provide a low level interface to ECDSA. Most 46e71b7053SJung-uk Kimapplications should use the higher level B<EVP> interface such as 47e71b7053SJung-uk KimL<EVP_DigestSignInit(3)> or L<EVP_DigestVerifyInit(3)> instead. 48e71b7053SJung-uk Kim 49e71b7053SJung-uk KimB<ECDSA_SIG> is an opaque structure consisting of two BIGNUMs for the 50e71b7053SJung-uk KimB<r> and B<s> value of an ECDSA signature (see X9.62 or FIPS 186-2). 51e71b7053SJung-uk Kim 52e71b7053SJung-uk KimECDSA_SIG_new() allocates an empty B<ECDSA_SIG> structure. Note: before 53e71b7053SJung-uk KimOpenSSL 1.1.0 the: the B<r> and B<s> components were initialised. 54e71b7053SJung-uk Kim 55e71b7053SJung-uk KimECDSA_SIG_free() frees the B<ECDSA_SIG> structure B<sig>. 56e71b7053SJung-uk Kim 57e71b7053SJung-uk KimECDSA_SIG_get0() returns internal pointers the B<r> and B<s> values contained 58e71b7053SJung-uk Kimin B<sig> and stores them in B<*pr> and B<*ps>, respectively. 59e71b7053SJung-uk KimThe pointer B<pr> or B<ps> can be NULL, in which case the corresponding value 60e71b7053SJung-uk Kimis not returned. 61e71b7053SJung-uk Kim 62e71b7053SJung-uk KimThe values B<r>, B<s> can also be retrieved separately by the corresponding 63e71b7053SJung-uk Kimfunction ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s(), respectively. 64e71b7053SJung-uk Kim 65e71b7053SJung-uk KimThe B<r> and B<s> values can be set by calling ECDSA_SIG_set0() and passing the 66e71b7053SJung-uk Kimnew values for B<r> and B<s> as parameters to the function. Calling this 67e71b7053SJung-uk Kimfunction transfers the memory management of the values to the ECDSA_SIG object, 68e71b7053SJung-uk Kimand therefore the values that have been passed in should not be freed directly 69e71b7053SJung-uk Kimafter this function has been called. 70e71b7053SJung-uk Kim 71e71b7053SJung-uk Kimi2d_ECDSA_SIG() creates the DER encoding of the ECDSA signature B<sig> and 72e71b7053SJung-uk Kimwrites the encoded signature to B<*pp> (note: if B<pp> is NULL i2d_ECDSA_SIG() 73e71b7053SJung-uk Kimreturns the expected length in bytes of the DER encoded signature). 74e71b7053SJung-uk Kimi2d_ECDSA_SIG() returns the length of the DER encoded signature (or 0 on 75e71b7053SJung-uk Kimerror). 76e71b7053SJung-uk Kim 77e71b7053SJung-uk Kimd2i_ECDSA_SIG() decodes a DER encoded ECDSA signature and returns the decoded 78e71b7053SJung-uk Kimsignature in a newly allocated B<ECDSA_SIG> structure. B<*sig> points to the 79e71b7053SJung-uk Kimbuffer containing the DER encoded signature of size B<len>. 80e71b7053SJung-uk Kim 81e71b7053SJung-uk KimECDSA_size() returns the maximum length of a DER encoded ECDSA signature 82e71b7053SJung-uk Kimcreated with the private EC key B<eckey>. 83e71b7053SJung-uk Kim 84e71b7053SJung-uk KimECDSA_sign() computes a digital signature of the B<dgstlen> bytes hash value 85e71b7053SJung-uk KimB<dgst> using the private EC key B<eckey>. The DER encoded signatures is 86e71b7053SJung-uk Kimstored in B<sig> and its length is returned in B<sig_len>. Note: B<sig> must 87e71b7053SJung-uk Kimpoint to ECDSA_size(eckey) bytes of memory. The parameter B<type> is currently 88e71b7053SJung-uk Kimignored. ECDSA_sign() is wrapper function for ECDSA_sign_ex() with B<kinv> 89e71b7053SJung-uk Kimand B<rp> set to NULL. 90e71b7053SJung-uk Kim 91e71b7053SJung-uk KimECDSA_do_sign() is similar to ECDSA_sign() except the signature is returned 92e71b7053SJung-uk Kimas a newly allocated B<ECDSA_SIG> structure (or NULL on error). ECDSA_do_sign() 93e71b7053SJung-uk Kimis a wrapper function for ECDSA_do_sign_ex() with B<kinv> and B<rp> set to 94e71b7053SJung-uk KimNULL. 95e71b7053SJung-uk Kim 96e71b7053SJung-uk KimECDSA_verify() verifies that the signature in B<sig> of size B<siglen> is a 97e71b7053SJung-uk Kimvalid ECDSA signature of the hash value B<dgst> of size B<dgstlen> using the 98e71b7053SJung-uk Kimpublic key B<eckey>. The parameter B<type> is ignored. 99e71b7053SJung-uk Kim 100e71b7053SJung-uk KimECDSA_do_verify() is similar to ECDSA_verify() except the signature is 101e71b7053SJung-uk Kimpresented in the form of a pointer to an B<ECDSA_SIG> structure. 102e71b7053SJung-uk Kim 103e71b7053SJung-uk KimThe remaining functions utilise the internal B<kinv> and B<r> values used 104e71b7053SJung-uk Kimduring signature computation. Most applications will never need to call these 105e71b7053SJung-uk Kimand some external ECDSA ENGINE implementations may not support them at all if 106e71b7053SJung-uk Kimeither B<kinv> or B<r> is not B<NULL>. 107e71b7053SJung-uk Kim 108e71b7053SJung-uk KimECDSA_sign_setup() may be used to precompute parts of the signing operation. 109e71b7053SJung-uk KimB<eckey> is the private EC key and B<ctx> is a pointer to B<BN_CTX> structure 110e71b7053SJung-uk Kim(or NULL). The precomputed values or returned in B<kinv> and B<rp> and can be 111e71b7053SJung-uk Kimused in a later call to ECDSA_sign_ex() or ECDSA_do_sign_ex(). 112e71b7053SJung-uk Kim 113e71b7053SJung-uk KimECDSA_sign_ex() computes a digital signature of the B<dgstlen> bytes hash value 114e71b7053SJung-uk KimB<dgst> using the private EC key B<eckey> and the optional pre-computed values 115e71b7053SJung-uk KimB<kinv> and B<rp>. The DER encoded signature is stored in B<sig> and its 116e71b7053SJung-uk Kimlength is returned in B<sig_len>. Note: B<sig> must point to ECDSA_size(eckey) 117e71b7053SJung-uk Kimbytes of memory. The parameter B<type> is ignored. 118e71b7053SJung-uk Kim 119e71b7053SJung-uk KimECDSA_do_sign_ex() is similar to ECDSA_sign_ex() except the signature is 120e71b7053SJung-uk Kimreturned as a newly allocated B<ECDSA_SIG> structure (or NULL on error). 121e71b7053SJung-uk Kim 122e71b7053SJung-uk Kim=head1 RETURN VALUES 123e71b7053SJung-uk Kim 124e71b7053SJung-uk KimECDSA_SIG_new() returns NULL if the allocation fails. 125e71b7053SJung-uk Kim 126e71b7053SJung-uk KimECDSA_SIG_set0() returns 1 on success or 0 on failure. 127e71b7053SJung-uk Kim 128e71b7053SJung-uk KimECDSA_SIG_get0_r() and ECDSA_SIG_get0_s() return the corresponding value, 129e71b7053SJung-uk Kimor NULL if it is unset. 130e71b7053SJung-uk Kim 131e71b7053SJung-uk KimECDSA_size() returns the maximum length signature or 0 on error. 132e71b7053SJung-uk Kim 133e71b7053SJung-uk KimECDSA_sign(), ECDSA_sign_ex() and ECDSA_sign_setup() return 1 if successful 134e71b7053SJung-uk Kimor 0 on error. 135e71b7053SJung-uk Kim 136e71b7053SJung-uk KimECDSA_do_sign() and ECDSA_do_sign_ex() return a pointer to an allocated 137e71b7053SJung-uk KimB<ECDSA_SIG> structure or NULL on error. 138e71b7053SJung-uk Kim 139e71b7053SJung-uk KimECDSA_verify() and ECDSA_do_verify() return 1 for a valid 140e71b7053SJung-uk Kimsignature, 0 for an invalid signature and -1 on error. 141e71b7053SJung-uk KimThe error codes can be obtained by L<ERR_get_error(3)>. 142e71b7053SJung-uk Kim 143e71b7053SJung-uk Kim=head1 EXAMPLES 144e71b7053SJung-uk Kim 145e71b7053SJung-uk KimCreating an ECDSA signature of a given SHA-256 hash value using the 146e71b7053SJung-uk Kimnamed curve prime256v1 (aka P-256). 147e71b7053SJung-uk Kim 148e71b7053SJung-uk KimFirst step: create an EC_KEY object (note: this part is B<not> ECDSA 149e71b7053SJung-uk Kimspecific) 150e71b7053SJung-uk Kim 151e71b7053SJung-uk Kim int ret; 152e71b7053SJung-uk Kim ECDSA_SIG *sig; 153e71b7053SJung-uk Kim EC_KEY *eckey; 154e71b7053SJung-uk Kim 155e71b7053SJung-uk Kim eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); 156e71b7053SJung-uk Kim if (eckey == NULL) 157e71b7053SJung-uk Kim /* error */ 158e71b7053SJung-uk Kim if (EC_KEY_generate_key(eckey) == 0) 159e71b7053SJung-uk Kim /* error */ 160e71b7053SJung-uk Kim 161e71b7053SJung-uk KimSecond step: compute the ECDSA signature of a SHA-256 hash value 162e71b7053SJung-uk Kimusing ECDSA_do_sign(): 163e71b7053SJung-uk Kim 164e71b7053SJung-uk Kim sig = ECDSA_do_sign(digest, 32, eckey); 165e71b7053SJung-uk Kim if (sig == NULL) 166e71b7053SJung-uk Kim /* error */ 167e71b7053SJung-uk Kim 168e71b7053SJung-uk Kimor using ECDSA_sign(): 169e71b7053SJung-uk Kim 170e71b7053SJung-uk Kim unsigned char *buffer, *pp; 171e71b7053SJung-uk Kim int buf_len; 172e71b7053SJung-uk Kim 173e71b7053SJung-uk Kim buf_len = ECDSA_size(eckey); 174e71b7053SJung-uk Kim buffer = OPENSSL_malloc(buf_len); 175e71b7053SJung-uk Kim pp = buffer; 176e71b7053SJung-uk Kim if (ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey) == 0) 177e71b7053SJung-uk Kim /* error */ 178e71b7053SJung-uk Kim 179e71b7053SJung-uk KimThird step: verify the created ECDSA signature using ECDSA_do_verify(): 180e71b7053SJung-uk Kim 181e71b7053SJung-uk Kim ret = ECDSA_do_verify(digest, 32, sig, eckey); 182e71b7053SJung-uk Kim 183e71b7053SJung-uk Kimor using ECDSA_verify(): 184e71b7053SJung-uk Kim 185e71b7053SJung-uk Kim ret = ECDSA_verify(0, digest, 32, buffer, buf_len, eckey); 186e71b7053SJung-uk Kim 187e71b7053SJung-uk Kimand finally evaluate the return value: 188e71b7053SJung-uk Kim 189e71b7053SJung-uk Kim if (ret == 1) 190e71b7053SJung-uk Kim /* signature ok */ 191e71b7053SJung-uk Kim else if (ret == 0) 192e71b7053SJung-uk Kim /* incorrect signature */ 193e71b7053SJung-uk Kim else 194e71b7053SJung-uk Kim /* error */ 195e71b7053SJung-uk Kim 196e71b7053SJung-uk Kim=head1 CONFORMING TO 197e71b7053SJung-uk Kim 198e71b7053SJung-uk KimANSI X9.62, US Federal Information Processing Standard FIPS 186-2 199e71b7053SJung-uk Kim(Digital Signature Standard, DSS) 200e71b7053SJung-uk Kim 201e71b7053SJung-uk Kim=head1 SEE ALSO 202e71b7053SJung-uk Kim 203*610a21fdSJung-uk KimL<EC_KEY_new(3)>, 204e71b7053SJung-uk KimL<EVP_DigestSignInit(3)>, 205e71b7053SJung-uk KimL<EVP_DigestVerifyInit(3)> 206e71b7053SJung-uk Kim 207e71b7053SJung-uk Kim=head1 COPYRIGHT 208e71b7053SJung-uk Kim 209*610a21fdSJung-uk KimCopyright 2004-2019 The OpenSSL Project Authors. All Rights Reserved. 210e71b7053SJung-uk Kim 211e71b7053SJung-uk KimLicensed under the OpenSSL license (the "License"). You may not use 212e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 213e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 214e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 215e71b7053SJung-uk Kim 216e71b7053SJung-uk Kim=cut 217