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