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