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