Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecdsa.h |
| 3 | * |
| 4 | * \brief This file contains ECDSA definitions and functions. |
| 5 | * |
| 6 | * The Elliptic Curve Digital Signature Algorithm (ECDSA) is defined in |
| 7 | * <em>Standards for Efficient Cryptography Group (SECG): |
| 8 | * SEC1 Elliptic Curve Cryptography</em>. |
| 9 | * The use of ECDSA for TLS is defined in <em>RFC-4492: Elliptic Curve |
| 10 | * Cryptography (ECC) Cipher Suites for Transport Layer Security (TLS)</em>. |
| 11 | * |
| 12 | */ |
| 13 | /* |
| 14 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 15 | * SPDX-License-Identifier: Apache-2.0 |
| 16 | * |
| 17 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 18 | * not use this file except in compliance with the License. |
| 19 | * You may obtain a copy of the License at |
| 20 | * |
| 21 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | * |
| 23 | * Unless required by applicable law or agreed to in writing, software |
| 24 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | * See the License for the specific language governing permissions and |
| 27 | * limitations under the License. |
| 28 | * |
| 29 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 30 | */ |
| 31 | |
| 32 | #ifndef MBEDCRYPTO_ECDSA_H |
| 33 | #define MBEDCRYPTO_ECDSA_H |
| 34 | |
| 35 | #include "ecp.h" |
| 36 | #include "md.h" |
| 37 | |
| 38 | /* |
| 39 | * RFC-4492 page 20: |
| 40 | * |
| 41 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| 42 | * r INTEGER, |
| 43 | * s INTEGER |
| 44 | * } |
| 45 | * |
| 46 | * Size is at most |
| 47 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| 48 | * twice that + 1 (tag) + 2 (len) for the sequence |
| 49 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| 50 | * and less than 124 (total len <= 255) for the sequence) |
| 51 | */ |
| 52 | #if MBEDCRYPTO_ECP_MAX_BYTES > 124 |
| 53 | #error "MBEDCRYPTO_ECP_MAX_BYTES bigger than expected, please fix MBEDCRYPTO_ECDSA_MAX_LEN" |
| 54 | #endif |
| 55 | /** The maximal size of an ECDSA signature in Bytes. */ |
| 56 | #define MBEDCRYPTO_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDCRYPTO_ECP_MAX_BYTES ) ) |
| 57 | |
| 58 | /** |
| 59 | * \brief The ECDSA context structure. |
| 60 | */ |
| 61 | typedef mbedcrypto_ecp_keypair mbedcrypto_ecdsa_context; |
| 62 | |
| 63 | #ifdef __cplusplus |
| 64 | extern "C" { |
| 65 | #endif |
| 66 | |
| 67 | /** |
| 68 | * \brief This function computes the ECDSA signature of a |
| 69 | * previously-hashed message. |
| 70 | * |
| 71 | * \note The deterministic version is usually preferred. |
| 72 | * |
| 73 | * \note If the bitlength of the message hash is larger than the |
| 74 | * bitlength of the group order, then the hash is truncated |
| 75 | * as defined in <em>Standards for Efficient Cryptography Group |
| 76 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 77 | * 4.1.3, step 5. |
| 78 | * |
| 79 | * \see ecp.h |
| 80 | * |
| 81 | * \param grp The ECP group. |
| 82 | * \param r The first output integer. |
| 83 | * \param s The second output integer. |
| 84 | * \param d The private signing key. |
| 85 | * \param buf The message hash. |
| 86 | * \param blen The length of \p buf. |
| 87 | * \param f_rng The RNG function. |
| 88 | * \param p_rng The RNG context. |
| 89 | * |
| 90 | * \return \c 0 on success. |
| 91 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX |
| 92 | * or \c MBEDCRYPTO_MPI_XXX error code on failure. |
| 93 | */ |
| 94 | int mbedcrypto_ecdsa_sign( mbedcrypto_ecp_group *grp, mbedcrypto_mpi *r, mbedcrypto_mpi *s, |
| 95 | const mbedcrypto_mpi *d, const unsigned char *buf, size_t blen, |
| 96 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 97 | |
| 98 | #if defined(MBEDCRYPTO_ECDSA_DETERMINISTIC) |
| 99 | /** |
| 100 | * \brief This function computes the ECDSA signature of a |
| 101 | * previously-hashed message, deterministic version. |
| 102 | * |
| 103 | * For more information, see <em>RFC-6979: Deterministic |
| 104 | * Usage of the Digital Signature Algorithm (DSA) and Elliptic |
| 105 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
| 106 | * |
| 107 | * \note If the bitlength of the message hash is larger than the |
| 108 | * bitlength of the group order, then the hash is truncated as |
| 109 | * defined in <em>Standards for Efficient Cryptography Group |
| 110 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 111 | * 4.1.3, step 5. |
| 112 | * |
| 113 | * \see ecp.h |
| 114 | * |
| 115 | * \param grp The ECP group. |
| 116 | * \param r The first output integer. |
| 117 | * \param s The second output integer. |
| 118 | * \param d The private signing key. |
| 119 | * \param buf The message hash. |
| 120 | * \param blen The length of \p buf. |
| 121 | * \param md_alg The MD algorithm used to hash the message. |
| 122 | * |
| 123 | * \return \c 0 on success. |
| 124 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or \c MBEDCRYPTO_MPI_XXX |
| 125 | * error code on failure. |
| 126 | */ |
| 127 | int mbedcrypto_ecdsa_sign_det( mbedcrypto_ecp_group *grp, mbedcrypto_mpi *r, mbedcrypto_mpi *s, |
| 128 | const mbedcrypto_mpi *d, const unsigned char *buf, size_t blen, |
| 129 | mbedcrypto_md_type_t md_alg ); |
| 130 | #endif /* MBEDCRYPTO_ECDSA_DETERMINISTIC */ |
| 131 | |
| 132 | /** |
| 133 | * \brief This function verifies the ECDSA signature of a |
| 134 | * previously-hashed message. |
| 135 | * |
| 136 | * \note If the bitlength of the message hash is larger than the |
| 137 | * bitlength of the group order, then the hash is truncated as |
| 138 | * defined in <em>Standards for Efficient Cryptography Group |
| 139 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 140 | * 4.1.4, step 3. |
| 141 | * |
| 142 | * \see ecp.h |
| 143 | * |
| 144 | * \param grp The ECP group. |
| 145 | * \param buf The message hash. |
| 146 | * \param blen The length of \p buf. |
| 147 | * \param Q The public key to use for verification. |
| 148 | * \param r The first integer of the signature. |
| 149 | * \param s The second integer of the signature. |
| 150 | * |
| 151 | * \return \c 0 on success. |
| 152 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if the signature |
| 153 | * is invalid. |
| 154 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or \c MBEDCRYPTO_MPI_XXX |
| 155 | * error code on failure for any other reason. |
| 156 | */ |
| 157 | int mbedcrypto_ecdsa_verify( mbedcrypto_ecp_group *grp, |
| 158 | const unsigned char *buf, size_t blen, |
| 159 | const mbedcrypto_ecp_point *Q, const mbedcrypto_mpi *r, const mbedcrypto_mpi *s); |
| 160 | |
| 161 | /** |
| 162 | * \brief This function computes the ECDSA signature and writes it |
| 163 | * to a buffer, serialized as defined in <em>RFC-4492: |
| 164 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 165 | * Transport Layer Security (TLS)</em>. |
| 166 | * |
| 167 | * \warning It is not thread-safe to use the same context in |
| 168 | * multiple threads. |
| 169 | * |
| 170 | * \note The deterministic version is used if |
| 171 | * #MBEDCRYPTO_ECDSA_DETERMINISTIC is defined. For more |
| 172 | * information, see <em>RFC-6979: Deterministic Usage |
| 173 | * of the Digital Signature Algorithm (DSA) and Elliptic |
| 174 | * Curve Digital Signature Algorithm (ECDSA)</em>. |
| 175 | * |
| 176 | * \note The \p sig buffer must be at least twice as large as the |
| 177 | * size of the curve used, plus 9. For example, 73 Bytes if |
| 178 | * a 256-bit curve is used. A buffer length of |
| 179 | * #MBEDCRYPTO_ECDSA_MAX_LEN is always safe. |
| 180 | * |
| 181 | * \note If the bitlength of the message hash is larger than the |
| 182 | * bitlength of the group order, then the hash is truncated as |
| 183 | * defined in <em>Standards for Efficient Cryptography Group |
| 184 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 185 | * 4.1.3, step 5. |
| 186 | * |
| 187 | * \see ecp.h |
| 188 | * |
| 189 | * \param ctx The ECDSA context. |
| 190 | * \param md_alg The message digest that was used to hash the message. |
| 191 | * \param hash The message hash. |
| 192 | * \param hlen The length of the hash. |
| 193 | * \param sig The buffer that holds the signature. |
| 194 | * \param slen The length of the signature written. |
| 195 | * \param f_rng The RNG function. |
| 196 | * \param p_rng The RNG context. |
| 197 | * |
| 198 | * \return \c 0 on success. |
| 199 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX, \c MBEDCRYPTO_ERR_MPI_XXX or |
| 200 | * \c MBEDCRYPTO_ERR_ASN1_XXX error code on failure. |
| 201 | */ |
| 202 | int mbedcrypto_ecdsa_write_signature( mbedcrypto_ecdsa_context *ctx, mbedcrypto_md_type_t md_alg, |
| 203 | const unsigned char *hash, size_t hlen, |
| 204 | unsigned char *sig, size_t *slen, |
| 205 | int (*f_rng)(void *, unsigned char *, size_t), |
| 206 | void *p_rng ); |
| 207 | |
| 208 | #if defined(MBEDCRYPTO_ECDSA_DETERMINISTIC) |
| 209 | #if ! defined(MBEDCRYPTO_DEPRECATED_REMOVED) |
| 210 | #if defined(MBEDCRYPTO_DEPRECATED_WARNING) |
| 211 | #define MBEDCRYPTO_DEPRECATED __attribute__((deprecated)) |
| 212 | #else |
| 213 | #define MBEDCRYPTO_DEPRECATED |
| 214 | #endif |
| 215 | /** |
| 216 | * \brief This function computes an ECDSA signature and writes |
| 217 | * it to a buffer, serialized as defined in <em>RFC-4492: |
| 218 | * Elliptic Curve Cryptography (ECC) Cipher Suites for |
| 219 | * Transport Layer Security (TLS)</em>. |
| 220 | * |
| 221 | * The deterministic version is defined in <em>RFC-6979: |
| 222 | * Deterministic Usage of the Digital Signature Algorithm (DSA) |
| 223 | * and Elliptic Curve Digital Signature Algorithm (ECDSA)</em>. |
| 224 | * |
| 225 | * \warning It is not thread-safe to use the same context in |
| 226 | * multiple threads. |
| 227 | * |
| 228 | * \note The \p sig buffer must be at least twice as large as the |
| 229 | * size of the curve used, plus 9. For example, 73 Bytes if a |
| 230 | * 256-bit curve is used. A buffer length of |
| 231 | * #MBEDCRYPTO_ECDSA_MAX_LEN is always safe. |
| 232 | * |
| 233 | * \note If the bitlength of the message hash is larger than the |
| 234 | * bitlength of the group order, then the hash is truncated as |
| 235 | * defined in <em>Standards for Efficient Cryptography Group |
| 236 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 237 | * 4.1.3, step 5. |
| 238 | * |
| 239 | * \see ecp.h |
| 240 | * |
| 241 | * \deprecated Superseded by mbedcrypto_ecdsa_write_signature() in |
| 242 | * Mbed Crypto version 2.0 and later. |
| 243 | * |
| 244 | * \param ctx The ECDSA context. |
| 245 | * \param hash The message hash. |
| 246 | * \param hlen The length of the hash. |
| 247 | * \param sig The buffer that holds the signature. |
| 248 | * \param slen The length of the signature written. |
| 249 | * \param md_alg The MD algorithm used to hash the message. |
| 250 | * |
| 251 | * \return \c 0 on success. |
| 252 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX, \c MBEDCRYPTO_ERR_MPI_XXX or |
| 253 | * \c MBEDCRYPTO_ERR_ASN1_XXX error code on failure. |
| 254 | */ |
| 255 | int mbedcrypto_ecdsa_write_signature_det( mbedcrypto_ecdsa_context *ctx, |
| 256 | const unsigned char *hash, size_t hlen, |
| 257 | unsigned char *sig, size_t *slen, |
| 258 | mbedcrypto_md_type_t md_alg ) MBEDCRYPTO_DEPRECATED; |
| 259 | #undef MBEDCRYPTO_DEPRECATED |
| 260 | #endif /* MBEDCRYPTO_DEPRECATED_REMOVED */ |
| 261 | #endif /* MBEDCRYPTO_ECDSA_DETERMINISTIC */ |
| 262 | |
| 263 | /** |
| 264 | * \brief This function reads and verifies an ECDSA signature. |
| 265 | * |
| 266 | * \note If the bitlength of the message hash is larger than the |
| 267 | * bitlength of the group order, then the hash is truncated as |
| 268 | * defined in <em>Standards for Efficient Cryptography Group |
| 269 | * (SECG): SEC1 Elliptic Curve Cryptography</em>, section |
| 270 | * 4.1.4, step 3. |
| 271 | * |
| 272 | * \see ecp.h |
| 273 | * |
| 274 | * \param ctx The ECDSA context. |
| 275 | * \param hash The message hash. |
| 276 | * \param hlen The size of the hash. |
| 277 | * \param sig The signature to read and verify. |
| 278 | * \param slen The size of \p sig. |
| 279 | * |
| 280 | * \return \c 0 on success. |
| 281 | * \return #MBEDCRYPTO_ERR_ECP_BAD_INPUT_DATA if signature is invalid. |
| 282 | * \return #MBEDCRYPTO_ERR_ECP_SIG_LEN_MISMATCH if there is a valid |
| 283 | * signature in \p sig, but its length is less than \p siglen. |
| 284 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX or \c MBEDCRYPTO_ERR_MPI_XXX |
| 285 | * error code on failure for any other reason. |
| 286 | */ |
| 287 | int mbedcrypto_ecdsa_read_signature( mbedcrypto_ecdsa_context *ctx, |
| 288 | const unsigned char *hash, size_t hlen, |
| 289 | const unsigned char *sig, size_t slen ); |
| 290 | |
| 291 | /** |
| 292 | * \brief This function generates an ECDSA keypair on the given curve. |
| 293 | * |
| 294 | * \see ecp.h |
| 295 | * |
| 296 | * \param ctx The ECDSA context to store the keypair in. |
| 297 | * \param gid The elliptic curve to use. One of the various |
| 298 | * \c MBEDCRYPTO_ECP_DP_XXX macros depending on configuration. |
| 299 | * \param f_rng The RNG function. |
| 300 | * \param p_rng The RNG context. |
| 301 | * |
| 302 | * \return \c 0 on success. |
| 303 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX code on failure. |
| 304 | */ |
| 305 | int mbedcrypto_ecdsa_genkey( mbedcrypto_ecdsa_context *ctx, mbedcrypto_ecp_group_id gid, |
| 306 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 307 | |
| 308 | /** |
| 309 | * \brief This function sets an ECDSA context from an EC key pair. |
| 310 | * |
| 311 | * \see ecp.h |
| 312 | * |
| 313 | * \param ctx The ECDSA context to set. |
| 314 | * \param key The EC key to use. |
| 315 | * |
| 316 | * \return \c 0 on success. |
| 317 | * \return An \c MBEDCRYPTO_ERR_ECP_XXX code on failure. |
| 318 | */ |
| 319 | int mbedcrypto_ecdsa_from_keypair( mbedcrypto_ecdsa_context *ctx, const mbedcrypto_ecp_keypair *key ); |
| 320 | |
| 321 | /** |
| 322 | * \brief This function initializes an ECDSA context. |
| 323 | * |
| 324 | * \param ctx The ECDSA context to initialize. |
| 325 | */ |
| 326 | void mbedcrypto_ecdsa_init( mbedcrypto_ecdsa_context *ctx ); |
| 327 | |
| 328 | /** |
| 329 | * \brief This function frees an ECDSA context. |
| 330 | * |
| 331 | * \param ctx The ECDSA context to free. |
| 332 | */ |
| 333 | void mbedcrypto_ecdsa_free( mbedcrypto_ecdsa_context *ctx ); |
| 334 | |
| 335 | #ifdef __cplusplus |
| 336 | } |
| 337 | #endif |
| 338 | |
| 339 | #endif /* ecdsa.h */ |