Make DSA_SIG and ECDSA_SIG getters const.
[openssl.git] / doc / crypto / ECDSA_SIG_new.pod
1 =pod
2
3 =head1 NAME
4
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
9
10 =head1 SYNOPSIS
11
12  #include <openssl/ecdsa.h>
13
14  ECDSA_SIG *ECDSA_SIG_new(void);
15  void ECDSA_SIG_free(ECDSA_SIG *sig);
16  void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
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);
21
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,
25                           EC_KEY *eckey);
26
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);
31
32  ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
33                              const BIGNUM *kinv, const BIGNUM *rp,
34                              EC_KEY *eckey);
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);
39
40 =head1 DESCRIPTION
41
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.
45
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).
48
49 ECDSA_SIG_new() allocates a new B<ECDSA_SIG> structure (note: this
50 function also allocates the BIGNUMs) and initializes it.
51
52 ECDSA_SIG_free() frees the B<ECDSA_SIG> structure B<sig>.
53
54 ECDSA_SIG_get0() returns internal pointers the B<r> and B<s> values contained
55 in B<sig>.
56
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.
62
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
67 error).
68
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>.
72
73 ECDSA_size() returns the maximum length of a DER encoded ECDSA signature
74 created with the private EC key B<eckey>.
75
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.
82
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
86 NULL.
87
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.
91
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.
94
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>.
99
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().
104
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.
110
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).
113
114 =head1 RETURN VALUES
115
116 ECDSA_SIG_set0() returns 1 on success or 0 on failure.
117
118 ECDSA_size() returns the maximum length signature or 0 on error.
119
120 ECDSA_sign(), ECDSA_sign_ex() and ECDSA_sign_setup() return 1 if successful
121 or 0 on error.
122
123 ECDSA_do_sign() and ECDSA_do_sign_ex() return a pointer to an allocated
124 B<ECDSA_SIG> structure or NULL on error.
125
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)>.
129
130 =head1 EXAMPLES
131
132 Creating an ECDSA signature of a given SHA-256 hash value using the
133 named curve prime256v1 (aka P-256).
134
135 First step: create an EC_KEY object (note: this part is B<not> ECDSA
136 specific)
137
138  int        ret;
139  ECDSA_SIG *sig;
140  EC_KEY    *eckey;
141  eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
142  if (eckey == NULL) {
143     /* error */
144  }
145  if (EC_KEY_generate_key(eckey) == 0) {
146     /* error */
147  }
148
149 Second step: compute the ECDSA signature of a SHA-256 hash value
150 using ECDSA_do_sign():
151
152  sig = ECDSA_do_sign(digest, 32, eckey);
153  if (sig == NULL) {
154     /* error */
155  }
156
157 or using ECDSA_sign():
158
159  unsigned char *buffer, *pp;
160  int            buf_len;
161  buf_len = ECDSA_size(eckey);
162  buffer  = OPENSSL_malloc(buf_len);
163  pp = buffer;
164  if (ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey) == 0) {
165     /* error */
166  }
167
168 Third step: verify the created ECDSA signature using ECDSA_do_verify():
169
170  ret = ECDSA_do_verify(digest, 32, sig, eckey);
171
172 or using ECDSA_verify():
173
174  ret = ECDSA_verify(0, digest, 32, buffer, buf_len, eckey);
175
176 and finally evaluate the return value:
177
178  if (ret == 1) {
179     /* signature ok */
180  } else if (ret == 0) {
181     /* incorrect signature */
182  } else {
183     /* error */
184  }
185
186 =head1 CONFORMING TO
187
188 ANSI X9.62, US Federal Information Processing Standard FIPS 186-2
189 (Digital Signature Standard, DSS)
190
191 =head1 SEE ALSO
192
193 L<dsa(3)>,
194 L<EVP_DigestSignInit(3)>,
195 L<EVP_DigestVerifyInit(3)>
196
197 =head1 COPYRIGHT
198
199 Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
200
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>.
205
206 =cut