Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* ==================================================================== |
| 2 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in |
| 13 | * the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * |
| 16 | * 3. All advertising materials mentioning features or use of this |
| 17 | * software must display the following acknowledgment: |
| 18 | * "This product includes software developed by the OpenSSL Project |
| 19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 20 | * |
| 21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 22 | * endorse or promote products derived from this software without |
| 23 | * prior written permission. For written permission, please contact |
| 24 | * openssl-core@OpenSSL.org. |
| 25 | * |
| 26 | * 5. Products derived from this software may not be called "OpenSSL" |
| 27 | * nor may "OpenSSL" appear in their names without prior written |
| 28 | * permission of the OpenSSL Project. |
| 29 | * |
| 30 | * 6. Redistributions of any form whatsoever must retain the following |
| 31 | * acknowledgment: |
| 32 | * "This product includes software developed by the OpenSSL Project |
| 33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 34 | * |
| 35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 | * ==================================================================== |
| 48 | * |
| 49 | * This product includes cryptographic software written by Eric Young |
| 50 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 51 | * Hudson (tjh@cryptsoft.com). */ |
| 52 | |
| 53 | #ifndef OPENSSL_HEADER_ECDSA_H |
| 54 | #define OPENSSL_HEADER_ECDSA_H |
| 55 | |
| 56 | #include <openssl/base.h> |
| 57 | |
| 58 | #include <openssl/ec_key.h> |
| 59 | |
| 60 | #if defined(__cplusplus) |
| 61 | extern "C" { |
| 62 | #endif |
| 63 | |
| 64 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 65 | // ECDSA contains functions for signing and verifying with the Digital Signature |
| 66 | // Algorithm over elliptic curves. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 67 | |
| 68 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 69 | // Signing and verifying. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 70 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 71 | // ECDSA_sign signs |digest_len| bytes from |digest| with |key| and writes the |
| 72 | // resulting signature to |sig|, which must have |ECDSA_size(key)| bytes of |
| 73 | // space. On successful exit, |*sig_len| is set to the actual number of bytes |
| 74 | // written. The |type| argument should be zero. It returns one on success and |
| 75 | // zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 76 | OPENSSL_EXPORT int ECDSA_sign(int type, const uint8_t *digest, |
| 77 | size_t digest_len, uint8_t *sig, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 78 | unsigned int *sig_len, const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 79 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 80 | // ECDSA_verify verifies that |sig_len| bytes from |sig| constitute a valid |
| 81 | // signature by |key| of |digest|. (The |type| argument should be zero.) It |
| 82 | // returns one on success or zero if the signature is invalid or an error |
| 83 | // occurred. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 84 | OPENSSL_EXPORT int ECDSA_verify(int type, const uint8_t *digest, |
| 85 | size_t digest_len, const uint8_t *sig, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 86 | size_t sig_len, const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 87 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 88 | // ECDSA_size returns the maximum size of an ECDSA signature using |key|. It |
| 89 | // returns zero on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 90 | OPENSSL_EXPORT size_t ECDSA_size(const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 91 | |
| 92 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 93 | // Low-level signing and verification. |
| 94 | // |
| 95 | // Low-level functions handle signatures as |ECDSA_SIG| structures which allow |
| 96 | // the two values in an ECDSA signature to be handled separately. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 97 | |
| 98 | struct ecdsa_sig_st { |
| 99 | BIGNUM *r; |
| 100 | BIGNUM *s; |
| 101 | }; |
| 102 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 103 | // ECDSA_SIG_new returns a fresh |ECDSA_SIG| structure or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 104 | OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_new(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 105 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 106 | // ECDSA_SIG_free frees |sig| its member |BIGNUM|s. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 107 | OPENSSL_EXPORT void ECDSA_SIG_free(ECDSA_SIG *sig); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 108 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 109 | // ECDSA_do_sign signs |digest_len| bytes from |digest| with |key| and returns |
| 110 | // the resulting signature structure, or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 111 | OPENSSL_EXPORT ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 112 | size_t digest_len, const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 114 | // ECDSA_do_verify verifies that |sig| constitutes a valid signature by |key| |
| 115 | // of |digest|. It returns one on success or zero if the signature is invalid |
| 116 | // or on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 117 | OPENSSL_EXPORT int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 118 | const ECDSA_SIG *sig, const EC_KEY *key); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
| 120 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 121 | // Signing with precomputation. |
| 122 | // |
| 123 | // Parts of the ECDSA signature can be independent of the message to be signed |
| 124 | // thus it's possible to precompute them and reduce the signing latency. |
| 125 | // |
| 126 | // TODO(fork): remove support for this as it cannot support safe-randomness. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 127 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 128 | // ECDSA_sign_setup precomputes parts of an ECDSA signing operation. It sets |
| 129 | // |*kinv| and |*rp| to the precomputed values and uses the |ctx| argument, if |
| 130 | // not NULL. It returns one on success and zero otherwise. |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 131 | OPENSSL_EXPORT int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, |
| 132 | BIGNUM **kinv, BIGNUM **rp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 134 | // ECDSA_do_sign_ex is the same as |ECDSA_do_sign| but takes precomputed values |
| 135 | // as generated by |ECDSA_sign_setup|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 136 | OPENSSL_EXPORT ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, |
| 137 | size_t digest_len, |
| 138 | const BIGNUM *kinv, const BIGNUM *rp, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 139 | const EC_KEY *eckey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 141 | // ECDSA_sign_ex is the same as |ECDSA_sign| but takes precomputed values as |
| 142 | // generated by |ECDSA_sign_setup|. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 143 | OPENSSL_EXPORT int ECDSA_sign_ex(int type, const uint8_t *digest, |
| 144 | size_t digest_len, uint8_t *sig, |
| 145 | unsigned int *sig_len, const BIGNUM *kinv, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 146 | const BIGNUM *rp, const EC_KEY *eckey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
| 148 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 149 | // ASN.1 functions. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 151 | // ECDSA_SIG_parse parses a DER-encoded ECDSA-Sig-Value structure from |cbs| and |
| 152 | // advances |cbs|. It returns a newly-allocated |ECDSA_SIG| or NULL on error. |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 153 | OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_parse(CBS *cbs); |
| 154 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 155 | // ECDSA_SIG_from_bytes parses |in| as a DER-encoded ECDSA-Sig-Value structure. |
| 156 | // It returns a newly-allocated |ECDSA_SIG| structure or NULL on error. |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 157 | OPENSSL_EXPORT ECDSA_SIG *ECDSA_SIG_from_bytes(const uint8_t *in, |
| 158 | size_t in_len); |
| 159 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 160 | // ECDSA_SIG_marshal marshals |sig| as a DER-encoded ECDSA-Sig-Value and appends |
| 161 | // the result to |cbb|. It returns one on success and zero on error. |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 162 | OPENSSL_EXPORT int ECDSA_SIG_marshal(CBB *cbb, const ECDSA_SIG *sig); |
| 163 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 164 | // ECDSA_SIG_to_bytes marshals |sig| as a DER-encoded ECDSA-Sig-Value and, on |
| 165 | // success, sets |*out_bytes| to a newly allocated buffer containing the result |
| 166 | // and returns one. Otherwise, it returns zero. The result should be freed with |
| 167 | // |OPENSSL_free|. |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 168 | OPENSSL_EXPORT int ECDSA_SIG_to_bytes(uint8_t **out_bytes, size_t *out_len, |
| 169 | const ECDSA_SIG *sig); |
| 170 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 171 | // ECDSA_SIG_max_len returns the maximum length of a DER-encoded ECDSA-Sig-Value |
| 172 | // structure for a group whose order is represented in |order_len| bytes, or |
| 173 | // zero on overflow. |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 174 | OPENSSL_EXPORT size_t ECDSA_SIG_max_len(size_t order_len); |
| 175 | |
| 176 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 177 | // Deprecated functions. |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 178 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 179 | // d2i_ECDSA_SIG parses an ASN.1, DER-encoded, signature from |len| bytes at |
| 180 | // |*inp|. If |out| is not NULL then, on exit, a pointer to the result is in |
| 181 | // |*out|. Note that, even if |*out| is already non-NULL on entry, it will not |
| 182 | // be written to. Rather, a fresh |ECDSA_SIG| is allocated and the previous one |
| 183 | // is freed. On successful exit, |*inp| is advanced past the DER structure. It |
| 184 | // returns the result or NULL on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 185 | OPENSSL_EXPORT ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **out, const uint8_t **inp, |
| 186 | long len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 188 | // i2d_ECDSA_SIG marshals a signature from |sig| to an ASN.1, DER |
| 189 | // structure. If |outp| is not NULL then the result is written to |*outp| and |
| 190 | // |*outp| is advanced just past the output. It returns the number of bytes in |
| 191 | // the result, whether written or not, or a negative value on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 192 | OPENSSL_EXPORT int i2d_ECDSA_SIG(const ECDSA_SIG *sig, uint8_t **outp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | |
| 194 | |
| 195 | #if defined(__cplusplus) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 196 | } // extern C |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 197 | |
| 198 | extern "C++" { |
| 199 | |
| 200 | namespace bssl { |
| 201 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 202 | BORINGSSL_MAKE_DELETER(ECDSA_SIG, ECDSA_SIG_free) |
| 203 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 204 | } // namespace bssl |
| 205 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 206 | } // extern C++ |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 207 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | #endif |
| 209 | |
David Benjamin | 689be0f | 2015-02-11 15:55:26 -0500 | [diff] [blame] | 210 | #define ECDSA_R_BAD_SIGNATURE 100 |
| 211 | #define ECDSA_R_MISSING_PARAMETERS 101 |
| 212 | #define ECDSA_R_NEED_NEW_SETUP_VALUES 102 |
| 213 | #define ECDSA_R_NOT_IMPLEMENTED 103 |
| 214 | #define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED 104 |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 215 | #define ECDSA_R_ENCODE_ERROR 105 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 217 | #endif // OPENSSL_HEADER_ECDSA_H |