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