xref: /freebsd/crypto/openssl/doc/man3/ECDSA_SIG_new.pod (revision a90b9d0159070121c221b966469c3e36d912bf82)
1=pod
2
3=head1 NAME
4
5ECDSA_SIG_new, ECDSA_SIG_free,
6ECDSA_SIG_get0, ECDSA_SIG_get0_r, ECDSA_SIG_get0_s, ECDSA_SIG_set0
7- Functions for creating, destroying and manipulating ECDSA_SIG objects
8
9=head1 SYNOPSIS
10
11 #include <openssl/ecdsa.h>
12
13 ECDSA_SIG *ECDSA_SIG_new(void);
14 void ECDSA_SIG_free(ECDSA_SIG *sig);
15 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
16 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
17 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
18 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
19
20=head1 DESCRIPTION
21
22B<ECDSA_SIG> is an opaque structure consisting of two BIGNUMs for the
23I<r> and I<s> value of an Elliptic Curve Digital Signature Algorithm (ECDSA) signature
24(see FIPS186-4 or X9.62).
25The B<ECDSA_SIG> object was mainly used by the deprecated low level functions described in
26L<ECDSA_sign(3)>, it is still required in order to be able to set or get the values of
27I<r> and I<s> into or from a signature. This is mainly used for testing purposes as shown
28in the L</EXAMPLES>.
29
30ECDSA_SIG_new() allocates an empty B<ECDSA_SIG> structure.
31Note: before OpenSSL 1.1.0, the I<r> and I<s> components were initialised.
32
33ECDSA_SIG_free() frees the B<ECDSA_SIG> structure I<sig>.
34
35ECDSA_SIG_get0() returns internal pointers the I<r> and I<s> values contained
36in I<sig> and stores them in I<*pr> and I<*ps>, respectively.
37The pointer I<pr> or I<ps> can be NULL, in which case the corresponding value
38is not returned.
39
40The values I<r>, I<s> can also be retrieved separately by the corresponding
41function ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s(), respectively.
42
43Non-NULL I<r> and I<s> values can be set on the I<sig> by calling
44ECDSA_SIG_set0(). Calling this function transfers the memory management of the
45values to the B<ECDSA_SIG> object, and therefore the values that have been
46passed in should not be freed by the caller.
47
48See L<i2d_ECDSA_SIG(3)> and L<d2i_ECDSA_SIG(3)> for information about encoding
49and decoding ECDSA signatures to/from DER.
50
51=head1 RETURN VALUES
52
53ECDSA_SIG_new() returns NULL if the allocation fails.
54
55ECDSA_SIG_set0() returns 1 on success or 0 on failure.
56
57ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s() return the corresponding value,
58or NULL if it is unset.
59
60=head1 EXAMPLES
61
62Extract signature I<r> and I<s> values from a ECDSA I<signature>
63of size I<signaturelen>:
64
65 ECDSA_SIG *obj;
66 const BIGNUM *r, *s;
67
68 /* Load a signature into the ECDSA_SIG object */
69 obj = d2i_ECDSA_SIG(NULL, &signature, signaturelen);
70 if (obj == NULL)
71     /* error */
72
73 r = ECDSA_SIG_get0_r(obj);
74 s = ECDSA_SIG_get0_s(obj);
75 if (r == NULL || s == NULL)
76     /* error */
77
78 /* Use BN_bn2binpad() here to convert to r and s into byte arrays */
79
80 /*
81  * Do not try to access I<r> or I<s> after calling ECDSA_SIG_free(),
82  * as they are both freed by this call.
83  */
84 ECDSA_SIG_free(obj);
85
86Convert I<r> and I<s> byte arrays into an ECDSA_SIG I<signature> of
87size I<signaturelen>:
88
89 ECDSA_SIG *obj = NULL;
90 unsigned char *signature = NULL;
91 size_t signaturelen;
92 BIGNUM *rbn = NULL, *sbn = NULL;
93
94 obj = ECDSA_SIG_new();
95 if (obj == NULL)
96     /* error */
97 rbn = BN_bin2bn(r, rlen, NULL);
98 sbn = BN_bin2bn(s, slen, NULL);
99 if (rbn == NULL || sbn == NULL)
100     /* error */
101
102 if (!ECDSA_SIG_set0(obj, rbn, sbn))
103     /* error */
104 /* Set these to NULL since they are now owned by obj */
105 rbn = sbn = NULL;
106
107 signaturelen = i2d_ECDSA_SIG(obj, &signature);
108 if (signaturelen <= 0)
109     /* error */
110
111 /*
112  * This signature could now be passed to L<EVP_DigestVerify(3)>
113  * or L<EVP_DigestVerifyFinal(3)>
114  */
115
116 BN_free(rbn);
117 BN_free(sbn);
118 OPENSSL_free(signature);
119 ECDSA_SIG_free(obj);
120
121=head1 CONFORMING TO
122
123ANSI X9.62,
124US Federal Information Processing Standard FIPS186-4
125(Digital Signature Standard, DSS)
126
127=head1 SEE ALSO
128
129L<EC_KEY_new(3)>,
130L<EVP_DigestSignInit(3)>,
131L<EVP_DigestVerifyInit(3)>,
132L<EVP_PKEY_sign(3)>
133L<i2d_ECDSA_SIG(3)>,
134L<d2i_ECDSA_SIG(3)>,
135L<ECDSA_sign(3)>
136
137=head1 COPYRIGHT
138
139Copyright 2004-2022 The OpenSSL Project Authors. All Rights Reserved.
140
141Licensed under the Apache License 2.0 (the "License").  You may not use
142this file except in compliance with the License.  You can obtain a copy
143in the file LICENSE in the source distribution or at
144L<https://www.openssl.org/source/license.html>.
145
146=cut
147