5 ECDSA_SIG_new, ECDSA_SIG_free, i2d_ECDSA_SIG, d2i_ECDSA_SIG, ECDSA_size,
6 ECDSA_sign, ECDSA_do_sign, ECDSA_verify, ECDSA_do_verify, ECDSA_sign_setup,
7 ECDSA_sign_ex, ECDSA_do_sign_ex - low level elliptic curve digital signature
8 algorithm (ECDSA) functions
12 #include <openssl/ecdsa.h>
14 ECDSA_SIG *ECDSA_SIG_new(void);
15 void ECDSA_SIG_free(ECDSA_SIG *sig);
16 void ECDSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, const ECDSA_SIG *sig);
17 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
18 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
19 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
20 int ECDSA_size(const EC_KEY *eckey);
22 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
23 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
24 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
27 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
28 const unsigned char *sig, int siglen, EC_KEY *eckey);
29 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
30 const ECDSA_SIG *sig, EC_KEY* eckey);
32 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
33 const BIGNUM *kinv, const BIGNUM *rp,
35 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
36 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
37 unsigned char *sig, unsigned int *siglen,
38 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
42 Note: these functions provide a low level interface to ECDSA. Most
43 applications should use the higher level B<EVP> interface such as
44 L<EVP_DigestSignInit(3)> or L<EVP_DigestVerifyInit(3)> instead.
46 B<ECDSA_SIG> is an opaque structure consisting of two BIGNUMs for the
47 B<r> and B<s> value of an ECDSA signature (see X9.62 or FIPS 186-2).
49 ECDSA_SIG_new() allocates a new B<ECDSA_SIG> structure (note: this
50 function also allocates the BIGNUMs) and initializes it.
52 ECDSA_SIG_free() frees the B<ECDSA_SIG> structure B<sig>.
54 ECDSA_SIG_get0() returns internal pointers the B<r> and B<s> values contained
55 in B<sig>. The values can then be examined or initialised.
57 The B<r> and B<s> values can be set by calling ECDSA_SIG_set0() and passing the
58 new values for B<r> and B<s> as parameters to the function. Calling this
59 function transfers the memory management of the values to the ECDSA_SIG object,
60 and therefore the values that have been passed in should not be freed directly
61 after this function has been called.
63 i2d_ECDSA_SIG() creates the DER encoding of the ECDSA signature B<sig> and
64 writes the encoded signature to B<*pp> (note: if B<pp> is NULL i2d_ECDSA_SIG()
65 returns the expected length in bytes of the DER encoded signature).
66 i2d_ECDSA_SIG() returns the length of the DER encoded signature (or 0 on
69 d2i_ECDSA_SIG() decodes a DER encoded ECDSA signature and returns the decoded
70 signature in a newly allocated B<ECDSA_SIG> structure. B<*sig> points to the
71 buffer containing the DER encoded signature of size B<len>.
73 ECDSA_size() returns the maximum length of a DER encoded ECDSA signature
74 created with the private EC key B<eckey>.
76 ECDSA_sign() computes a digital signature of the B<dgstlen> bytes hash value
77 B<dgst> using the private EC key B<eckey>. The DER encoded signatures is
78 stored in B<sig> and it's length is returned in B<sig_len>. Note: B<sig> must
79 point to ECDSA_size(eckey) bytes of memory. The parameter B<type> is currently
80 ignored. ECDSA_sign() is wrapper function for ECDSA_sign_ex() with B<kinv>
81 and B<rp> set to NULL.
83 ECDSA_do_sign() is similar to ECDSA_sign() except the signature is returned
84 as a newly allocated B<ECDSA_SIG> structure (or NULL on error). ECDSA_do_sign()
85 is a wrapper function for ECDSA_do_sign_ex() with B<kinv> and B<rp> set to
88 ECDSA_verify() verifies that the signature in B<sig> of size B<siglen> is a
89 valid ECDSA signature of the hash value B<dgst> of size B<dgstlen> using the
90 public key B<eckey>. The parameter B<type> is ignored.
92 ECDSA_do_verify() is similar to ECDSA_verify() except the signature is
93 presented in the form of a pointer to an B<ECDSA_SIG> structure.
95 The remaining functions utilise the internal B<kinv> and B<r> values used
96 during signature computation. Most applications will never need to call these
97 and some external ECDSA ENGINE implementations may not support them at all if
98 either B<kinv> or B<r> is not B<NULL>.
100 ECDSA_sign_setup() may be used to precompute parts of the signing operation.
101 B<eckey> is the private EC key and B<ctx> is a pointer to B<BN_CTX> structure
102 (or NULL). The precomputed values or returned in B<kinv> and B<rp> and can be
103 used in a later call to ECDSA_sign_ex() or ECDSA_do_sign_ex().
105 ECDSA_sign_ex() computes a digital signature of the B<dgstlen> bytes hash value
106 B<dgst> using the private EC key B<eckey> and the optional pre-computed values
107 B<kinv> and B<rp>. The DER encoded signatures is stored in B<sig> and it's
108 length is returned in B<sig_len>. Note: B<sig> must point to ECDSA_size(eckey)
109 bytes of memory. The parameter B<type> is ignored.
111 ECDSA_do_sign_ex() is similar to ECDSA_sign_ex() except the signature is
112 returned as a newly allocated B<ECDSA_SIG> structure (or NULL on error).
116 ECDSA_SIG_set0() returns 1 on success or 0 on failure.
118 ECDSA_size() returns the maximum length signature or 0 on error.
120 ECDSA_sign(), ECDSA_sign_ex() and ECDSA_sign_setup() return 1 if successful
123 ECDSA_do_sign() and ECDSA_do_sign_ex() return a pointer to an allocated
124 B<ECDSA_SIG> structure or NULL on error.
126 ECDSA_verify() and ECDSA_do_verify() return 1 for a valid
127 signature, 0 for an invalid signature and -1 on error.
128 The error codes can be obtained by L<ERR_get_error(3)>.
132 Creating an ECDSA signature of a given SHA-256 hash value using the
133 named curve prime256v1 (aka P-256).
135 First step: create an EC_KEY object (note: this part is B<not> ECDSA
141 eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
145 if (EC_KEY_generate_key(eckey) == 0) {
149 Second step: compute the ECDSA signature of a SHA-256 hash value
150 using ECDSA_do_sign():
152 sig = ECDSA_do_sign(digest, 32, eckey);
157 or using ECDSA_sign():
159 unsigned char *buffer, *pp;
161 buf_len = ECDSA_size(eckey);
162 buffer = OPENSSL_malloc(buf_len);
164 if (ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey) == 0) {
168 Third step: verify the created ECDSA signature using ECDSA_do_verify():
170 ret = ECDSA_do_verify(digest, 32, sig, eckey);
172 or using ECDSA_verify():
174 ret = ECDSA_verify(0, digest, 32, buffer, buf_len, eckey);
176 and finally evaluate the return value:
180 } else if (ret == 0) {
181 /* incorrect signature */
188 ANSI X9.62, US Federal Information Processing Standard FIPS 186-2
189 (Digital Signature Standard, DSS)
194 L<EVP_DigestSignInit(3)>,
195 L<EVP_DigestVerifyInit(3)>
199 Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
201 Licensed under the OpenSSL license (the "License"). You may not use
202 this file except in compliance with the License. You can obtain a copy
203 in the file LICENSE in the source distribution or at
204 L<https://www.openssl.org/source/license.html>.