1ecdsa(3) OpenSSL ecdsa(3)
2
3
4
6 ecdsa - Elliptic Curve Digital Signature Algorithm
7
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
51 The ECDSA_SIG structure consists of two BIGNUMs for the r and s value
52 of a ECDSA signature (see X9.62 or FIPS 186-2).
53
54 struct
55 {
56 BIGNUM *r;
57 BIGNUM *s;
58 } ECDSA_SIG;
59
60 ECDSA_SIG_new() allocates a new ECDSA_SIG structure (note: this func‐
61 tion also allocates the BIGNUMs) and initialize it.
62
63 ECDSA_SIG_free() frees the ECDSA_SIG structure sig.
64
65 i2d_ECDSA_SIG() creates the DER encoding of the ECDSA signature sig and
66 writes the encoded signature to *pp (note: if pp is NULL i2d_ECDSA_SIG
67 returns the expected length in bytes of the DER encoded signature).
68 i2d_ECDSA_SIG returns the length of the DER encoded signature (or 0 on
69 error).
70
71 d2i_ECDSA_SIG() decodes a DER encoded ECDSA signature and returns the
72 decoded signature in a newly allocated ECDSA_SIG structure. *sig
73 points to the buffer containing the DER encoded signature of size len.
74
75 ECDSA_size() returns the maximum length of a DER encoded ECDSA signa‐
76 ture created with the private EC key eckey.
77
78 ECDSA_sign_setup() may be used to precompute parts of the signing oper‐
79 ation. eckey is the private EC key and ctx is a pointer to BN_CTX
80 structure (or NULL). The precomputed values or returned in kinv and rp
81 and can be used in a later call to ECDSA_sign_ex or ECDSA_do_sign_ex.
82
83 ECDSA_sign() is wrapper function for ECDSA_sign_ex with kinv and rp set
84 to NULL.
85
86 ECDSA_sign_ex() computes a digital signature of the dgstlen bytes hash
87 value dgst using the private EC key eckey and the optional pre-computed
88 values kinv and rp. The DER encoded signatures is stored in sig and
89 it's length is returned in sig_len. Note: sig must point to ECDSA_size
90 bytes of memory. The parameter type is ignored.
91
92 ECDSA_verify() verifies that the signature in sig of size siglen is a
93 valid ECDSA signature of the hash value value dgst of size dgstlen
94 using the public key eckey. The parameter type is ignored.
95
96 ECDSA_do_sign() is wrapper function for ECDSA_do_sign_ex with kinv and
97 rp set to NULL.
98
99 ECDSA_do_sign_ex() computes a digital signature of the dgst_len bytes
100 hash value dgst using the private key eckey and the optional pre-com‐
101 puted values kinv and rp. The signature is returned in a newly allo‐
102 cated ECDSA_SIG structure (or NULL on error).
103
104 ECDSA_do_verify() verifies that the signature sig is a valid ECDSA sig‐
105 nature of the hash value dgst of size dgst_len using the public key
106 eckey.
107
109 ECDSA_size() returns the maximum length signature or 0 on error.
110
111 ECDSA_sign_setup() and ECDSA_sign() return 1 if successful or -1 on
112 error.
113
114 ECDSA_verify() and ECDSA_do_verify() return 1 for a valid signature, 0
115 for an invalid signature and -1 on error. The error codes can be
116 obtained by ERR_get_error(3).
117
119 Creating a ECDSA signature of given SHA-1 hash value using the named
120 curve secp192k1.
121
122 First step: create a EC_KEY object (note: this part is not ECDSA spe‐
123 cific)
124
125 int ret;
126 ECDSA_SIG *sig;
127 EC_KEY *eckey = EC_KEY_new();
128 if (eckey == NULL)
129 {
130 /* error */
131 }
132 key->group = EC_GROUP_new_by_nid(NID_secp192k1);
133 if (key->group == NULL)
134 {
135 /* error */
136 }
137 if (!EC_KEY_generate_key(eckey))
138 {
139 /* error */
140 }
141
142 Second step: compute the ECDSA signature of a SHA-1 hash value using
143 ECDSA_do_sign
144
145 sig = ECDSA_do_sign(digest, 20, eckey);
146 if (sig == NULL)
147 {
148 /* error */
149 }
150
151 or using ECDSA_sign
152
153 unsigned char *buffer, *pp;
154 int buf_len;
155 buf_len = ECDSA_size(eckey);
156 buffer = OPENSSL_malloc(buf_len);
157 pp = buffer;
158 if (!ECDSA_sign(0, dgst, dgstlen, pp, &buf_len, eckey);
159 {
160 /* error */
161 }
162
163 Third step: verify the created ECDSA signature using ECDSA_do_verify
164
165 ret = ECDSA_do_verify(digest, 20, sig, eckey);
166
167 or using ECDSA_verify
168
169 ret = ECDSA_verify(0, digest, 20, buffer, buf_len, eckey);
170
171 and finally evaluate the return value:
172
173 if (ret == -1)
174 {
175 /* error */
176 }
177 else if (ret == 0)
178 {
179 /* incorrect signature */
180 }
181 else /* ret == 1 */
182 {
183 /* signature ok */
184 }
185
187 ANSI X9.62, US Federal Information Processing Standard FIPS 186-2 (Dig‐
188 ital Signature Standard, DSS)
189
191 dsa(3), rsa(3)
192
194 The ecdsa implementation was first introduced in OpenSSL 0.9.8
195
197 Nils Larsch for the OpenSSL project (http://www.openssl.org).
198
199
200
2010.9.8b 2005-05-19 ecdsa(3)