Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/ecdsa.h

raw
1 // Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_ECDSA_H
5 #define OPENSSL_HEADER_ECDSA_H
6
7 #include <openssl/base.h>
8
9 #include <openssl/ec_key.h>
10
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14
15
16 // ECDSA contains functions for signing and verifying with the Digital Signature
17 // Algorithm over elliptic curves.
18
19
20 // Signing and verifying.
21
22 // ECDSA_sign signs |digest_len| bytes from |digest| with |key| and writes the
23 // resulting signature to |sig|, which must have |ECDSA_size(key)| bytes of
24 // space. On successful exit, |*sig_len| is set to the actual number of bytes
25 // written. The |type| argument should be zero. It returns one on success and
26 // zero otherwise.
27 //
28 // WARNING: |digest| must be the output of some hash function on the data to be
29 // signed. Passing unhashed inputs will not result in a secure signature scheme.
30 OPENSSL_EXPORT int ECDSA_sign(int type, const uint8_t *digest,
31 size_t digest_len, uint8_t *sig,
32 unsigned int *sig_len, const EC_KEY *key);
33
34 // ECDSA_verify verifies that |sig_len| bytes from |sig| constitute a valid
35 // signature by |key| of |digest|. (The |type| argument should be zero.) It
36 // returns one on success or zero if the signature is invalid or an error
37 // occurred.
38 //
39 // WARNING: |digest| must be the output of some hash function on the data to be
40 // verified. Passing unhashed inputs will not result in a secure signature
41 // scheme.
42 OPENSSL_EXPORT int ECDSA_verify(int type, const uint8_t *digest,
43 size_t digest_len, const uint8_t *sig,
44 size_t sig_len, const EC_KEY *key);
45
46 // ECDSA_size returns the maximum size of an ECDSA signature using |key|. It
47 // returns zero if |key| is NULL or if it doesn't have a group set.
48 OPENSSL_EXPORT size_t ECDSA_size(const EC_KEY *key);
49
50
51 // Low-level signing and verification.
52 //
53 // Low-level functions handle signatures as |ECDSA_SIG| structures which allow
54 // the two values in an ECDSA signature to be handled separately.
55
56 struct ecdsa_sig_st {
57 BIGNUM *r;
58 BIGNUM *s;
59 };
60
61 // ECDSA_SIG_new returns a fresh |ECDSA_SIG| structure or NULL on error.
62 OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_new(void);
63
64 // ECDSA_SIG_free frees |sig| its member |BIGNUM|s.
65 OPENSSL_EXPORT void ECDSA_SIG_free(ECDSA_SIG *sig);
66
67 // ECDSA_SIG_get0_r returns the r component of |sig|.
68 OPENSSL_EXPORT const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
69
70 // ECDSA_SIG_get0_s returns the s component of |sig|.
71 OPENSSL_EXPORT const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
72
73 // ECDSA_SIG_get0 sets |*out_r| and |*out_s|, if non-NULL, to the two
74 // components of |sig|.
75 OPENSSL_EXPORT void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r,
76 const BIGNUM **out_s);
77
78 // ECDSA_SIG_set0 sets |sig|'s components to |r| and |s|, neither of which may
79 // be NULL. On success, it takes ownership of each argument and returns one.
80 // Otherwise, it returns zero.
81 OPENSSL_EXPORT int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
82
83 // ECDSA_do_sign signs |digest_len| bytes from |digest| with |key| and returns
84 // the resulting signature structure, or NULL on error.
85 //
86 // WARNING: |digest| must be the output of some hash function on the data to be
87 // signed. Passing unhashed inputs will not result in a secure signature scheme.
88 OPENSSL_EXPORT ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest,
89 size_t digest_len, const EC_KEY *key);
90
91 // ECDSA_do_verify verifies that |sig| constitutes a valid signature by |key|
92 // of |digest|. It returns one on success or zero if the signature is invalid
93 // or on error.
94 //
95 // We distinguish between a "mismatched" signature error and "bad" signature
96 // error because of JCA expectations. Specifically:
97 // * Error |ECDSA_R_BAD_SIGNATURE| is set if step (1) of SEC 1 v2 4.1.4 returns
98 // "invalid".
99 // * Error |ECDSA_R_MISMATCHED_SIGNATURE| is set if steps (5) and (8) of SEC 1
100 // v2 4.1.4 returns "invalid".
101 //
102 // WARNING: |digest| must be the output of some hash function on the data to be
103 // verified. Passing unhashed inputs will not result in a secure signature
104 // scheme.
105 OPENSSL_EXPORT int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
106 const ECDSA_SIG *sig, const EC_KEY *key);
107
108
109 // ASN.1 functions.
110
111 // ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from |cbs| and
112 // advances |cbs|. It returns a newly-allocated |ECDSA_SIG| or NULL on error.
113 OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_parse(CBS *cbs);
114
115 // ECDSA_SIG_from_bytes parses |in| as a DER-encoded ECDSA-Sig-Value structure.
116 // It returns a newly-allocated |ECDSA_SIG| structure or NULL on error.
117 OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_from_bytes(const uint8_t *in,
118 size_t in_len);
119
120 // ECDSA_SIG_marshal marshals |sig| as a DER-encoded ECDSA-Sig-Value and appends
121 // the result to |cbb|. It returns one on success and zero on error.
122 OPENSSL_EXPORT int ECDSA_SIG_marshal(CBB *cbb, const ECDSA_SIG *sig);
123
124 // ECDSA_SIG_to_bytes marshals |sig| as a DER-encoded ECDSA-Sig-Value and, on
125 // success, sets |*out_bytes| to a newly allocated buffer containing the result
126 // and returns one. Otherwise, it returns zero. The result should be freed with
127 // |OPENSSL_free|.
128 OPENSSL_EXPORT int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len,
129 const ECDSA_SIG *sig);
130
131 // ECDSA_SIG_max_len returns the maximum length of a DER-encoded ECDSA-Sig-Value
132 // structure for a group whose order is represented in |order_len| bytes, or
133 // zero on overflow.
134 OPENSSL_EXPORT size_t ECDSA_SIG_max_len(size_t order_len);
135
136
137 // Testing-only functions.
138
139 // ECDSA_sign_with_nonce_and_leak_private_key_for_testing behaves like
140 // |ECDSA_do_sign| but uses |nonce| for the ECDSA nonce 'k', instead of a random
141 // value. |nonce| is interpreted as a big-endian integer. It must be reduced
142 // modulo the group order and padded with zeros up to |BN_num_bytes(order)|
143 // bytes.
144 //
145 // WARNING: This function is only exported for testing purposes, when using test
146 // vectors or fuzzing strategies. It must not be used outside tests and may leak
147 // any private keys it is used with.
148 OPENSSL_EXPORT ECDSA_SIG *
149 ECDSA_sign_with_nonce_and_leak_private_key_for_testing(const uint8_t *digest,
150 size_t digest_len,
151 const EC_KEY *eckey,
152 const uint8_t *nonce,
153 size_t nonce_len);
154
155
156 // Deprecated functions.
157
158 // d2i_ECDSA_SIG parses aa DER-encoded ECDSA-Sig-Value structure from |len|
159 // bytes at |*inp|, as described in |d2i_SAMPLE|.
160 //
161 // Use |ECDSA_SIG_parse| instead.
162 OPENSSL_EXPORT ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **out, const uint8_t **inp,
163 long len);
164
165 // i2d_ECDSA_SIG marshals |sig| as a DER-encoded ECDSA-Sig-Value, as described
166 // in |i2d_SAMPLE|.
167 //
168 // Use |ECDSA_SIG_marshal| instead.
169 OPENSSL_EXPORT int i2d_ECDSA_SIG(const ECDSA_SIG *sig, uint8_t **outp);
170
171
172 #if defined(__cplusplus)
173 } // extern C
174
175 extern "C++" {
176
177 BSSL_NAMESPACE_BEGIN
178
179 BORINGSSL_MAKE_DELETER(ECDSA_SIG, ECDSA_SIG_free)
180
181 BSSL_NAMESPACE_END
182
183 } // extern C++
184
185 #endif
186
187 #define ECDSA_R_BAD_SIGNATURE 100
188 #define ECDSA_R_MISSING_PARAMETERS 101
189 #define ECDSA_R_NEED_NEW_SETUP_VALUES 102
190 #define ECDSA_R_NOT_IMPLEMENTED 103
191 #define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED 104
192 #define ECDSA_R_ENCODE_ERROR 105
193 #define ECDSA_R_MISMATCHED_SIGNATURE 205
194 #define ECDSA_R_TOO_MANY_ITERATIONS 106
195
196 #endif // OPENSSL_HEADER_ECDSA_H