Constify X509|X509_CRL|X509_REVOKED_get_ext
[openssl.git] / doc / crypto / ECDSA_SIG_new.pod
1 =pod
2
3 =head1 NAME
4
5 ECDSA_SIG_get0, ECDSA_SIG_set0,
6 ECDSA_SIG_new, ECDSA_SIG_free, i2d_ECDSA_SIG, d2i_ECDSA_SIG, ECDSA_size,
7 ECDSA_sign, ECDSA_do_sign, ECDSA_verify, ECDSA_do_verify, ECDSA_sign_setup,
8 ECDSA_sign_ex, ECDSA_do_sign_ex - low level elliptic curve digital signature
9 algorithm (ECDSA) functions
10
11 =head1 SYNOPSIS
12
13  #include <openssl/ecdsa.h>
14
15  ECDSA_SIG *ECDSA_SIG_new(void);
16  void ECDSA_SIG_free(ECDSA_SIG *sig);
17  void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
18  int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
19  int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
20  ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
21  int ECDSA_size(const EC_KEY *eckey);
22
23  int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
24                 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
25  ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
26                           EC_KEY *eckey);
27
28  int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
29                   const unsigned char *sig, int siglen, EC_KEY *eckey);
30  int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
31                      const ECDSA_SIG *sig, EC_KEY* eckey);
32
33  ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
34                              const BIGNUM *kinv, const BIGNUM *rp,
35                              EC_KEY *eckey);
36  int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
37  int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
38                    unsigned char *sig, unsigned int *siglen,
39                    const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
40
41 =head1 DESCRIPTION
42
43 Note: these functions provide a low level interface to ECDSA. Most
44 applications should use the higher level B<EVP> interface such as
45 L<EVP_DigestSignInit(3)> or L<EVP_DigestVerifyInit(3)> instead.
46
47 B<ECDSA_SIG> is an opaque structure consisting of two BIGNUMs for the
48 B<r> and B<s> value of an ECDSA signature (see X9.62 or FIPS 186-2).
49
50 ECDSA_SIG_new() allocates a new B<ECDSA_SIG> structure (note: this
51 function also allocates the BIGNUMs) and initializes it.
52
53 ECDSA_SIG_free() frees the B<ECDSA_SIG> structure B<sig>.
54
55 ECDSA_SIG_get0() returns internal pointers the B<r> and B<s> values contained
56 in B<sig>.
57
58 The B<r> and B<s> values can be set by calling ECDSA_SIG_set0() and passing the
59 new values for B<r> and B<s> as parameters to the function. Calling this
60 function transfers the memory management of the values to the ECDSA_SIG object,
61 and therefore the values that have been passed in should not be freed directly
62 after this function has been called.
63
64 i2d_ECDSA_SIG() creates the DER encoding of the ECDSA signature B<sig> and
65 writes the encoded signature to B<*pp> (note: if B<pp> is NULL i2d_ECDSA_SIG()
66 returns the expected length in bytes of the DER encoded signature).
67 i2d_ECDSA_SIG() returns the length of the DER encoded signature (or 0 on
68 error).
69
70 d2i_ECDSA_SIG() decodes a DER encoded ECDSA signature and returns the decoded
71 signature in a newly allocated B<ECDSA_SIG> structure. B<*sig> points to the
72 buffer containing the DER encoded signature of size B<len>.
73
74 ECDSA_size() returns the maximum length of a DER encoded ECDSA signature
75 created with the private EC key B<eckey>.
76
77 ECDSA_sign() computes a digital signature of the B<dgstlen> bytes hash value
78 B<dgst> using the private EC key B<eckey>. The DER encoded signatures is
79 stored in B<sig> and it's length is returned in B<sig_len>. Note: B<sig> must
80 point to ECDSA_size(eckey) bytes of memory. The parameter B<type> is currently
81 ignored. ECDSA_sign() is wrapper function for ECDSA_sign_ex() with B<kinv>
82 and B<rp> set to NULL.
83
84 ECDSA_do_sign() is similar to ECDSA_sign() except the signature is returned
85 as a newly allocated B<ECDSA_SIG> structure (or NULL on error). ECDSA_do_sign()
86 is a wrapper function for ECDSA_do_sign_ex() with B<kinv> and B<rp> set to
87 NULL.
88
89 ECDSA_verify() verifies that the signature in B<sig> of size B<siglen> is a
90 valid ECDSA signature of the hash value B<dgst> of size B<dgstlen> using the
91 public key B<eckey>.  The parameter B<type> is ignored.
92
93 ECDSA_do_verify() is similar to ECDSA_verify() except the signature is
94 presented in the form of a pointer to an B<ECDSA_SIG> structure.
95
96 The remaining functions utilise the internal B<kinv> and B<r> values used
97 during signature computation. Most applications will never need to call these
98 and some external ECDSA ENGINE implementations may not support them at all if
99 either B<kinv> or B<r> is not B<NULL>.
100
101 ECDSA_sign_setup() may be used to precompute parts of the signing operation.
102 B<eckey> is the private EC key and B<ctx> is a pointer to B<BN_CTX> structure
103 (or NULL). The precomputed values or returned in B<kinv> and B<rp> and can be
104 used in a later call to ECDSA_sign_ex() or ECDSA_do_sign_ex().
105
106 ECDSA_sign_ex() computes a digital signature of the B<dgstlen> bytes hash value
107 B<dgst> using the private EC key B<eckey> and the optional pre-computed values
108 B<kinv> and B<rp>. The DER encoded signatures is stored in B<sig> and it's
109 length is returned in B<sig_len>. Note: B<sig> must point to ECDSA_size(eckey)
110 bytes of memory. The parameter B<type> is ignored.
111
112 ECDSA_do_sign_ex() is similar to ECDSA_sign_ex() except the signature is
113 returned as a newly allocated B<ECDSA_SIG> structure (or NULL on error).
114
115 =head1 RETURN VALUES
116
117 ECDSA_SIG_set0() returns 1 on success or 0 on failure.
118
119 ECDSA_size() returns the maximum length signature or 0 on error.
120
121 ECDSA_sign(), ECDSA_sign_ex() and ECDSA_sign_setup() return 1 if successful
122 or 0 on error.
123
124 ECDSA_do_sign() and ECDSA_do_sign_ex() return a pointer to an allocated
125 B<ECDSA_SIG> structure or NULL on error.
126
127 ECDSA_verify() and ECDSA_do_verify() return 1 for a valid
128 signature, 0 for an invalid signature and -1 on error.
129 The error codes can be obtained by L<ERR_get_error(3)>.
130
131 =head1 EXAMPLES
132
133 Creating an ECDSA signature of a given SHA-256 hash value using the
134 named curve prime256v1 (aka P-256).
135
136 First step: create an EC_KEY object (note: this part is B<not> ECDSA
137 specific)
138
139  int        ret;
140  ECDSA_SIG *sig;
141  EC_KEY    *eckey;
142  eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
143  if (eckey == NULL) {
144     /* error */
145  }
146  if (EC_KEY_generate_key(eckey) == 0) {
147     /* error */
148  }
149
150 Second step: compute the ECDSA signature of a SHA-256 hash value
151 using ECDSA_do_sign():
152
153  sig = ECDSA_do_sign(digest, 32, eckey);
154  if (sig == NULL) {
155     /* error */
156  }
157
158 or using ECDSA_sign():
159
160  unsigned char *buffer, *pp;
161  int            buf_len;
162  buf_len = ECDSA_size(eckey);
163  buffer  = OPENSSL_malloc(buf_len);
164  pp = buffer;
165  if (ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey) == 0) {
166     /* error */
167  }
168
169 Third step: verify the created ECDSA signature using ECDSA_do_verify():
170
171  ret = ECDSA_do_verify(digest, 32, sig, eckey);
172
173 or using ECDSA_verify():
174
175  ret = ECDSA_verify(0, digest, 32, buffer, buf_len, eckey);
176
177 and finally evaluate the return value:
178
179  if (ret == 1) {
180     /* signature ok */
181  } else if (ret == 0) {
182     /* incorrect signature */
183  } else {
184     /* error */
185  }
186
187 =head1 CONFORMING TO
188
189 ANSI X9.62, US Federal Information Processing Standard FIPS 186-2
190 (Digital Signature Standard, DSS)
191
192 =head1 SEE ALSO
193
194 L<dsa(3)>,
195 L<EVP_DigestSignInit(3)>,
196 L<EVP_DigestVerifyInit(3)>
197
198 =head1 COPYRIGHT
199
200 Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
201
202 Licensed under the OpenSSL license (the "License").  You may not use
203 this file except in compliance with the License.  You can obtain a copy
204 in the file LICENSE in the source distribution or at
205 L<https://www.openssl.org/source/license.html>.
206
207 =cut