update docs (recent constification)
[openssl.git] / doc / crypto / ecdsa.pod
1 =pod
2
3 =head1 NAME
4
5 ecdsa - Elliptic Curve Digital Signature Algorithm
6
7 =head1 SYNOPSIS
8
9  #include <openssl/ecdsa.h>
10
11  ECDSA_SIG*     ECDSA_SIG_new(void);
12  void           ECDSA_SIG_free(ECDSA_SIG *sig);
13  int            i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
14  ECDSA_SIG*     d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, 
15                 long len);
16
17  ECDSA_DATA*    ECDSA_DATA_new(void);
18  ECDSA_DATA*    ECDSA_DATA_new_method(ENGINE *eng);
19  void           ECDSA_DATA_free(ECDSA_DATA *data);
20  ECDSA_DATA*    ecdsa_check(EC_KEY *eckey);
21
22  ECDSA_SIG*     ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
23                         EC_KEY *eckey);
24  int            ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
25                         ECDSA_SIG *sig, EC_KEY* eckey);
26  int            ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx,
27                         BIGNUM **kinv, BIGNUM **rp);
28  int            ECDSA_sign(int type, const unsigned char *dgst,
29                         int dgstlen, unsigned char *sig,
30                         unsigned int *siglen, EC_KEY *eckey);
31  int            ECDSA_verify(int type, const unsigned char *dgst,
32                         int dgstlen, const unsigned char *sig,
33                         int siglen, EC_KEY *eckey);
34  int            ECDSA_size(const EC_KEY *eckey);
35
36  const ECDSA_METHOD*    ECDSA_OpenSSL(void);
37  void           ECDSA_set_default_method(const ECDSA_METHOD *meth);
38  const ECDSA_METHOD*    ECDSA_get_default_method(void);
39  int            ECDSA_set_method(EC_KEY *eckey,const ECDSA_METHOD *meth);
40
41  int            ECDSA_get_ex_new_index(long argl, void *argp,
42                         CRYPTO_EX_new *new_func,
43                         CRYPTO_EX_dup *dup_func,
44                         CRYPTO_EX_free *free_func);
45  int            ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
46  void*          ECDSA_get_ex_data(EC_KEY *d, int idx);
47
48 =head1 DESCRIPTION
49
50 The B<ECDSA_SIG> structure consists of two BIGNUMs for the
51 r and s value of a ECDSA signature (see X9.62 or FIPS 186-2).
52
53  struct
54         {
55         BIGNUM *r;
56         BIGNUM *s;
57  } ECDSA_SIG;
58
59 ECDSA_SIG_new() allocates a new B<ECDSA_SIG> structure (note: this
60 function also allocates the BIGNUMs) and initialize it.
61
62 ECDSA_SIG_free() frees the B<ECDSA_SIG> structure B<sig>.
63
64 i2d_ECDSA_SIG() creates the DER encoding of the ECDSA signature
65 B<sig> and writes the encoded signature to B<*pp> (note: if B<pp>
66 is NULL B<i2d_ECDSA_SIG> returns the expected length in bytes of 
67 the DER encoded signature). B<i2d_ECDSA_SIG> returns the length
68 of the DER encoded signature (or 0 on error).
69
70 d2i_ECDSA_SIG() decodes a DER encoded ECDSA signature and returns
71 the decoded signature in a newly allocated B<ECDSA_SIG> structure.
72 B<*sig> points to the buffer containing the DER encoded signature
73 of size B<len>.
74
75 The B<ECDSA_DATA> structure extends the B<EC_KEY_METH_DATA>
76 structure with ECDSA specific data.
77
78  struct
79         {
80         /* EC_KEY_METH_DATA part */
81         int  (*init)(EC_KEY *);
82         void (*finish)(EC_KEY *);
83         /* method (ECDSA) specific part */
84         BIGNUM  *kinv;  /* signing pre-calc */
85         BIGNUM  *r;     /* signing pre-calc */
86         ...
87         } 
88  ECDSA_DATA;
89
90 B<kinv> and B<r> are used to store precomputed values (see
91 B<ECDSA_sign_setup>). 
92
93 ECDSA_DATA_new() returns a newly allocated and initialized
94 B<ECDSA_DATA> structure (or NULL on error). 
95
96 ECDSA_DATA_free() frees the B<ECDSA_DATA> structure B<data>.
97
98 ecdsa_check() returns the pointer to the B<ECDSA_DATA>
99 structure in B<EC_KEY-E<gt>meth_data> (if B<EC_KEY-E<gt>meth_data>
100 is not a pointer to a B<ECDSA_DATA> structure then the old
101 data is freed and a new B<ECDSA_DATA> structure is allocated
102 using B<ECDSA_DATA_new>).
103
104 ECDSA_size() returns the maximum length of a DER encoded
105 ECDSA signature created with the private EC key B<eckey>.
106
107 ECDSA_sign_setup() may be used to precompute parts of the
108 signing operation. B<eckey> is the private EC key and B<ctx>
109 is a pointer to B<BN_CTX> structure (or NULL). The precomputed
110 values or returned in B<kinv> and B<rp> and can be used in a
111 later call to B<ECDSA_sign> or B<ECDSA_do_sign> when placed in
112 B<ECDSA_DATA-E<gt>kinv> and B<ECDSA_DATA-E<gt>r>.
113
114 ECDSA_sign() computes a digital signature of the B<dgstlen> bytes
115 hash value B<dgst> using the private EC key B<eckey> and places
116 the DER encoding of the created signature in B<sig>. The length
117 of the created signature is returned in B<sig_len>. Note: B<sig>
118 must point to B<ECDSA_size> bytes of memory. The parameter B<type>
119 is ignored.
120
121 ECDSA_verify() verifies that the signature in B<sig> of size
122 B<siglen> is a valid ECDSA signature of the hash value
123 value B<dgst> of size B<dgstlen> using the public key B<eckey>.
124 The parameter B<type> is ignored.
125
126 ECDSA_do_sign() computes a digital signature of the B<dgst_len>
127 bytes hash value B<dgst> using the private key B<eckey> and 
128 returns the signature in a newly allocated B<ECDSA_SIG> structure
129 (or NULL on error).
130
131 ECDSA_do_verify() verifies that the signature B<sig> is a valid
132 ECDSA signature of the hash value B<dgst> of size B<dgst_len>
133 using the public key B<eckey>.
134
135 =head1 RETURN VALUES
136
137 ECDSA_size() returns the maximum length signature or 0 on error.
138
139 ECDSA_sign_setup() and ECDSA_sign() return 1 if successful or -1
140 on error.
141
142 ECDSA_verify() and ECDSA_do_verify() return 1 for a valid
143 signature, 0 for an invalid signature and -1 on error.
144 The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>.
145
146 =head1 EXAMPLES
147
148 Creating a ECDSA signature of given SHA-1 hash value using the
149 named curve secp192k1.
150
151 First step: create a EC_KEY object (note: this part is B<not> ECDSA
152 specific)
153
154  int        ret;
155  ECDSA_SIG *sig;
156  EC_KEY    *eckey = EC_KEY_new();
157  if (eckey == NULL)
158         {
159         /* error */
160         }
161  key->group = EC_GROUP_new_by_nid(NID_secp192k1);
162  if (key->group == NULL)
163         {
164         /* error */
165         }
166  if (!EC_KEY_generate_key(eckey))
167         {
168         /* error */
169         }
170
171 Second step: compute the ECDSA signature of a SHA-1 hash value 
172 using B<ECDSA_do_sign> 
173
174  sig = ECDSA_do_sign(digest, 20, eckey);
175  if (sig == NULL)
176         {
177         /* error */
178         }
179
180 or using B<ECDSA_sign>
181
182  unsigned char *buffer, *pp;
183  int            buf_len;
184  buf_len = ECDSA_size(eckey);
185  buffer  = OPENSSL_malloc(buf_len);
186  pp = buffer;
187  if (!ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey);
188         {
189         /* error */
190         }
191
192 Third step: verify the created ECDSA signature using B<ECDSA_do_verify>
193
194  ret = ECDSA_do_verify(digest, 20, sig, eckey);
195
196 or using B<ECDSA_verify>
197
198  ret = ECDSA_verify(0, digest, 20, buffer, buf_len, eckey);
199
200 and finally evaluate the return value:
201
202  if (ret == -1)
203         {
204         /* error */
205         }
206  else if (ret == 0)
207         {
208         /* incorrect signature */
209         }
210  else   /* ret == 1 */
211         {
212         /* signature ok */
213         }
214
215 =head1 CONFORMING TO
216
217 ANSI X9.62, US Federal Information Processing Standard FIPS 186-2
218 (Digital Signature Standard, DSS)
219
220 =head1 SEE ALSO
221
222 L<dsa(3)|dsa(3)>, L<rsa(3)|rsa(3)>
223
224 =head1 HISTORY
225
226 The ecdsa implementation was first introduced in OpenSSL 0.9.8
227
228 =head1 AUTHOR
229
230 Nils Larsch for the OpenSSL project (http://www.openssl.org).
231
232 =cut