xref: /openssl/doc/man3/ECDSA_SIG_new.pod (revision 7ed6de99)
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>.
34If the argument is NULL, nothing is done.
35
36ECDSA_SIG_get0() returns internal pointers the I<r> and I<s> values contained
37in I<sig> and stores them in I<*pr> and I<*ps>, respectively.
38The pointer I<pr> or I<ps> can be NULL, in which case the corresponding value
39is not returned.
40
41The values I<r>, I<s> can also be retrieved separately by the corresponding
42function ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s(), respectively.
43
44Non-NULL I<r> and I<s> values can be set on the I<sig> by calling
45ECDSA_SIG_set0(). Calling this function transfers the memory management of the
46values to the B<ECDSA_SIG> object, and therefore the values that have been
47passed in should not be freed by the caller.
48
49See L<i2d_ECDSA_SIG(3)> and L<d2i_ECDSA_SIG(3)> for information about encoding
50and decoding ECDSA signatures to/from DER.
51
52=head1 RETURN VALUES
53
54ECDSA_SIG_new() returns NULL if the allocation fails.
55
56ECDSA_SIG_set0() returns 1 on success or 0 on failure.
57
58ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s() return the corresponding value,
59or NULL if it is unset.
60
61=head1 EXAMPLES
62
63Extract signature I<r> and I<s> values from a ECDSA I<signature>
64of size I<signaturelen>:
65
66 ECDSA_SIG *obj;
67 const BIGNUM *r, *s;
68
69 /* Load a signature into the ECDSA_SIG object */
70 obj = d2i_ECDSA_SIG(NULL, &signature, signaturelen);
71 if (obj == NULL)
72     /* error */
73
74 r = ECDSA_SIG_get0_r(obj);
75 s = ECDSA_SIG_get0_s(obj);
76 if (r == NULL || s == NULL)
77     /* error */
78
79 /* Use BN_bn2binpad() here to convert to r and s into byte arrays */
80
81 /*
82  * Do not try to access I<r> or I<s> after calling ECDSA_SIG_free(),
83  * as they are both freed by this call.
84  */
85 ECDSA_SIG_free(obj);
86
87Convert I<r> and I<s> byte arrays into an ECDSA_SIG I<signature> of
88size I<signaturelen>:
89
90 ECDSA_SIG *obj = NULL;
91 unsigned char *signature = NULL;
92 size_t signaturelen;
93 BIGNUM *rbn = NULL, *sbn = NULL;
94
95 obj = ECDSA_SIG_new();
96 if (obj == NULL)
97     /* error */
98 rbn = BN_bin2bn(r, rlen, NULL);
99 sbn = BN_bin2bn(s, slen, NULL);
100 if (rbn == NULL || sbn == NULL)
101     /* error */
102
103 if (!ECDSA_SIG_set0(obj, rbn, sbn))
104     /* error */
105 /* Set these to NULL since they are now owned by obj */
106 rbn = sbn = NULL;
107
108 signaturelen = i2d_ECDSA_SIG(obj, &signature);
109 if (signaturelen <= 0)
110     /* error */
111
112 /*
113  * This signature could now be passed to L<EVP_DigestVerify(3)>
114  * or L<EVP_DigestVerifyFinal(3)>
115  */
116
117 BN_free(rbn);
118 BN_free(sbn);
119 OPENSSL_free(signature);
120 ECDSA_SIG_free(obj);
121
122=head1 CONFORMING TO
123
124ANSI X9.62,
125US Federal Information Processing Standard FIPS186-4
126(Digital Signature Standard, DSS)
127
128=head1 SEE ALSO
129
130L<EC_KEY_new(3)>,
131L<EVP_DigestSignInit(3)>,
132L<EVP_DigestVerifyInit(3)>,
133L<EVP_PKEY_sign(3)>
134L<i2d_ECDSA_SIG(3)>,
135L<d2i_ECDSA_SIG(3)>,
136L<ECDSA_sign(3)>
137
138=head1 COPYRIGHT
139
140Copyright 2004-2024 The OpenSSL Project Authors. All Rights Reserved.
141
142Licensed under the Apache License 2.0 (the "License").  You may not use
143this file except in compliance with the License.  You can obtain a copy
144in the file LICENSE in the source distribution or at
145L<https://www.openssl.org/source/license.html>.
146
147=cut
148