Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WncControllerK64F
Fork of WNCInterface by
mbedtls/mbedtls/ecdsa.h@12:0071cb144c7a, 2016-11-01 (annotated)
- Committer:
- JMF
- Date:
- Tue Nov 01 14:22:56 2016 +0000
- Revision:
- 12:0071cb144c7a
Adding mbedtls files
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JMF | 12:0071cb144c7a | 1 | /** |
| JMF | 12:0071cb144c7a | 2 | * \file ecdsa.h |
| JMF | 12:0071cb144c7a | 3 | * |
| JMF | 12:0071cb144c7a | 4 | * \brief Elliptic curve DSA |
| JMF | 12:0071cb144c7a | 5 | * |
| JMF | 12:0071cb144c7a | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
| JMF | 12:0071cb144c7a | 7 | * SPDX-License-Identifier: Apache-2.0 |
| JMF | 12:0071cb144c7a | 8 | * |
| JMF | 12:0071cb144c7a | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| JMF | 12:0071cb144c7a | 10 | * not use this file except in compliance with the License. |
| JMF | 12:0071cb144c7a | 11 | * You may obtain a copy of the License at |
| JMF | 12:0071cb144c7a | 12 | * |
| JMF | 12:0071cb144c7a | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| JMF | 12:0071cb144c7a | 14 | * |
| JMF | 12:0071cb144c7a | 15 | * Unless required by applicable law or agreed to in writing, software |
| JMF | 12:0071cb144c7a | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| JMF | 12:0071cb144c7a | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| JMF | 12:0071cb144c7a | 18 | * See the License for the specific language governing permissions and |
| JMF | 12:0071cb144c7a | 19 | * limitations under the License. |
| JMF | 12:0071cb144c7a | 20 | * |
| JMF | 12:0071cb144c7a | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| JMF | 12:0071cb144c7a | 22 | */ |
| JMF | 12:0071cb144c7a | 23 | #ifndef MBEDTLS_ECDSA_H |
| JMF | 12:0071cb144c7a | 24 | #define MBEDTLS_ECDSA_H |
| JMF | 12:0071cb144c7a | 25 | |
| JMF | 12:0071cb144c7a | 26 | #include "ecp.h" |
| JMF | 12:0071cb144c7a | 27 | #include "md.h" |
| JMF | 12:0071cb144c7a | 28 | |
| JMF | 12:0071cb144c7a | 29 | /* |
| JMF | 12:0071cb144c7a | 30 | * RFC 4492 page 20: |
| JMF | 12:0071cb144c7a | 31 | * |
| JMF | 12:0071cb144c7a | 32 | * Ecdsa-Sig-Value ::= SEQUENCE { |
| JMF | 12:0071cb144c7a | 33 | * r INTEGER, |
| JMF | 12:0071cb144c7a | 34 | * s INTEGER |
| JMF | 12:0071cb144c7a | 35 | * } |
| JMF | 12:0071cb144c7a | 36 | * |
| JMF | 12:0071cb144c7a | 37 | * Size is at most |
| JMF | 12:0071cb144c7a | 38 | * 1 (tag) + 1 (len) + 1 (initial 0) + ECP_MAX_BYTES for each of r and s, |
| JMF | 12:0071cb144c7a | 39 | * twice that + 1 (tag) + 2 (len) for the sequence |
| JMF | 12:0071cb144c7a | 40 | * (assuming ECP_MAX_BYTES is less than 126 for r and s, |
| JMF | 12:0071cb144c7a | 41 | * and less than 124 (total len <= 255) for the sequence) |
| JMF | 12:0071cb144c7a | 42 | */ |
| JMF | 12:0071cb144c7a | 43 | #if MBEDTLS_ECP_MAX_BYTES > 124 |
| JMF | 12:0071cb144c7a | 44 | #error "MBEDTLS_ECP_MAX_BYTES bigger than expected, please fix MBEDTLS_ECDSA_MAX_LEN" |
| JMF | 12:0071cb144c7a | 45 | #endif |
| JMF | 12:0071cb144c7a | 46 | /** Maximum size of an ECDSA signature in bytes */ |
| JMF | 12:0071cb144c7a | 47 | #define MBEDTLS_ECDSA_MAX_LEN ( 3 + 2 * ( 3 + MBEDTLS_ECP_MAX_BYTES ) ) |
| JMF | 12:0071cb144c7a | 48 | |
| JMF | 12:0071cb144c7a | 49 | /** |
| JMF | 12:0071cb144c7a | 50 | * \brief ECDSA context structure |
| JMF | 12:0071cb144c7a | 51 | */ |
| JMF | 12:0071cb144c7a | 52 | typedef mbedtls_ecp_keypair mbedtls_ecdsa_context; |
| JMF | 12:0071cb144c7a | 53 | |
| JMF | 12:0071cb144c7a | 54 | #ifdef __cplusplus |
| JMF | 12:0071cb144c7a | 55 | extern "C" { |
| JMF | 12:0071cb144c7a | 56 | #endif |
| JMF | 12:0071cb144c7a | 57 | |
| JMF | 12:0071cb144c7a | 58 | /** |
| JMF | 12:0071cb144c7a | 59 | * \brief Compute ECDSA signature of a previously hashed message |
| JMF | 12:0071cb144c7a | 60 | * |
| JMF | 12:0071cb144c7a | 61 | * \note The deterministic version is usually prefered. |
| JMF | 12:0071cb144c7a | 62 | * |
| JMF | 12:0071cb144c7a | 63 | * \param grp ECP group |
| JMF | 12:0071cb144c7a | 64 | * \param r First output integer |
| JMF | 12:0071cb144c7a | 65 | * \param s Second output integer |
| JMF | 12:0071cb144c7a | 66 | * \param d Private signing key |
| JMF | 12:0071cb144c7a | 67 | * \param buf Message hash |
| JMF | 12:0071cb144c7a | 68 | * \param blen Length of buf |
| JMF | 12:0071cb144c7a | 69 | * \param f_rng RNG function |
| JMF | 12:0071cb144c7a | 70 | * \param p_rng RNG parameter |
| JMF | 12:0071cb144c7a | 71 | * |
| JMF | 12:0071cb144c7a | 72 | * \return 0 if successful, |
| JMF | 12:0071cb144c7a | 73 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
| JMF | 12:0071cb144c7a | 74 | */ |
| JMF | 12:0071cb144c7a | 75 | int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| JMF | 12:0071cb144c7a | 76 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| JMF | 12:0071cb144c7a | 77 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| JMF | 12:0071cb144c7a | 78 | |
| JMF | 12:0071cb144c7a | 79 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| JMF | 12:0071cb144c7a | 80 | /** |
| JMF | 12:0071cb144c7a | 81 | * \brief Compute ECDSA signature of a previously hashed message, |
| JMF | 12:0071cb144c7a | 82 | * deterministic version (RFC 6979). |
| JMF | 12:0071cb144c7a | 83 | * |
| JMF | 12:0071cb144c7a | 84 | * \param grp ECP group |
| JMF | 12:0071cb144c7a | 85 | * \param r First output integer |
| JMF | 12:0071cb144c7a | 86 | * \param s Second output integer |
| JMF | 12:0071cb144c7a | 87 | * \param d Private signing key |
| JMF | 12:0071cb144c7a | 88 | * \param buf Message hash |
| JMF | 12:0071cb144c7a | 89 | * \param blen Length of buf |
| JMF | 12:0071cb144c7a | 90 | * \param md_alg MD algorithm used to hash the message |
| JMF | 12:0071cb144c7a | 91 | * |
| JMF | 12:0071cb144c7a | 92 | * \return 0 if successful, |
| JMF | 12:0071cb144c7a | 93 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
| JMF | 12:0071cb144c7a | 94 | */ |
| JMF | 12:0071cb144c7a | 95 | int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, |
| JMF | 12:0071cb144c7a | 96 | const mbedtls_mpi *d, const unsigned char *buf, size_t blen, |
| JMF | 12:0071cb144c7a | 97 | mbedtls_md_type_t md_alg ); |
| JMF | 12:0071cb144c7a | 98 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
| JMF | 12:0071cb144c7a | 99 | |
| JMF | 12:0071cb144c7a | 100 | /** |
| JMF | 12:0071cb144c7a | 101 | * \brief Verify ECDSA signature of a previously hashed message |
| JMF | 12:0071cb144c7a | 102 | * |
| JMF | 12:0071cb144c7a | 103 | * \param grp ECP group |
| JMF | 12:0071cb144c7a | 104 | * \param buf Message hash |
| JMF | 12:0071cb144c7a | 105 | * \param blen Length of buf |
| JMF | 12:0071cb144c7a | 106 | * \param Q Public key to use for verification |
| JMF | 12:0071cb144c7a | 107 | * \param r First integer of the signature |
| JMF | 12:0071cb144c7a | 108 | * \param s Second integer of the signature |
| JMF | 12:0071cb144c7a | 109 | * |
| JMF | 12:0071cb144c7a | 110 | * \return 0 if successful, |
| JMF | 12:0071cb144c7a | 111 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid |
| JMF | 12:0071cb144c7a | 112 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
| JMF | 12:0071cb144c7a | 113 | */ |
| JMF | 12:0071cb144c7a | 114 | int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, |
| JMF | 12:0071cb144c7a | 115 | const unsigned char *buf, size_t blen, |
| JMF | 12:0071cb144c7a | 116 | const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s); |
| JMF | 12:0071cb144c7a | 117 | |
| JMF | 12:0071cb144c7a | 118 | /** |
| JMF | 12:0071cb144c7a | 119 | * \brief Compute ECDSA signature and write it to buffer, |
| JMF | 12:0071cb144c7a | 120 | * serialized as defined in RFC 4492 page 20. |
| JMF | 12:0071cb144c7a | 121 | * (Not thread-safe to use same context in multiple threads) |
| JMF | 12:0071cb144c7a | 122 | * |
| JMF | 12:0071cb144c7a | 123 | * \note The deterministice version (RFC 6979) is used if |
| JMF | 12:0071cb144c7a | 124 | * MBEDTLS_ECDSA_DETERMINISTIC is defined. |
| JMF | 12:0071cb144c7a | 125 | * |
| JMF | 12:0071cb144c7a | 126 | * \param ctx ECDSA context |
| JMF | 12:0071cb144c7a | 127 | * \param md_alg Algorithm that was used to hash the message |
| JMF | 12:0071cb144c7a | 128 | * \param hash Message hash |
| JMF | 12:0071cb144c7a | 129 | * \param hlen Length of hash |
| JMF | 12:0071cb144c7a | 130 | * \param sig Buffer that will hold the signature |
| JMF | 12:0071cb144c7a | 131 | * \param slen Length of the signature written |
| JMF | 12:0071cb144c7a | 132 | * \param f_rng RNG function |
| JMF | 12:0071cb144c7a | 133 | * \param p_rng RNG parameter |
| JMF | 12:0071cb144c7a | 134 | * |
| JMF | 12:0071cb144c7a | 135 | * \note The "sig" buffer must be at least as large as twice the |
| JMF | 12:0071cb144c7a | 136 | * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit |
| JMF | 12:0071cb144c7a | 137 | * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe. |
| JMF | 12:0071cb144c7a | 138 | * |
| JMF | 12:0071cb144c7a | 139 | * \return 0 if successful, |
| JMF | 12:0071cb144c7a | 140 | * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or |
| JMF | 12:0071cb144c7a | 141 | * MBEDTLS_ERR_ASN1_XXX error code |
| JMF | 12:0071cb144c7a | 142 | */ |
| JMF | 12:0071cb144c7a | 143 | int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, |
| JMF | 12:0071cb144c7a | 144 | const unsigned char *hash, size_t hlen, |
| JMF | 12:0071cb144c7a | 145 | unsigned char *sig, size_t *slen, |
| JMF | 12:0071cb144c7a | 146 | int (*f_rng)(void *, unsigned char *, size_t), |
| JMF | 12:0071cb144c7a | 147 | void *p_rng ); |
| JMF | 12:0071cb144c7a | 148 | |
| JMF | 12:0071cb144c7a | 149 | #if defined(MBEDTLS_ECDSA_DETERMINISTIC) |
| JMF | 12:0071cb144c7a | 150 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| JMF | 12:0071cb144c7a | 151 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| JMF | 12:0071cb144c7a | 152 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| JMF | 12:0071cb144c7a | 153 | #else |
| JMF | 12:0071cb144c7a | 154 | #define MBEDTLS_DEPRECATED |
| JMF | 12:0071cb144c7a | 155 | #endif |
| JMF | 12:0071cb144c7a | 156 | /** |
| JMF | 12:0071cb144c7a | 157 | * \brief Compute ECDSA signature and write it to buffer, |
| JMF | 12:0071cb144c7a | 158 | * serialized as defined in RFC 4492 page 20. |
| JMF | 12:0071cb144c7a | 159 | * Deterministic version, RFC 6979. |
| JMF | 12:0071cb144c7a | 160 | * (Not thread-safe to use same context in multiple threads) |
| JMF | 12:0071cb144c7a | 161 | * |
| JMF | 12:0071cb144c7a | 162 | * \deprecated Superseded by mbedtls_ecdsa_write_signature() in 2.0.0 |
| JMF | 12:0071cb144c7a | 163 | * |
| JMF | 12:0071cb144c7a | 164 | * \param ctx ECDSA context |
| JMF | 12:0071cb144c7a | 165 | * \param hash Message hash |
| JMF | 12:0071cb144c7a | 166 | * \param hlen Length of hash |
| JMF | 12:0071cb144c7a | 167 | * \param sig Buffer that will hold the signature |
| JMF | 12:0071cb144c7a | 168 | * \param slen Length of the signature written |
| JMF | 12:0071cb144c7a | 169 | * \param md_alg MD algorithm used to hash the message |
| JMF | 12:0071cb144c7a | 170 | * |
| JMF | 12:0071cb144c7a | 171 | * \note The "sig" buffer must be at least as large as twice the |
| JMF | 12:0071cb144c7a | 172 | * size of the curve used, plus 9 (eg. 73 bytes if a 256-bit |
| JMF | 12:0071cb144c7a | 173 | * curve is used). MBEDTLS_ECDSA_MAX_LEN is always safe. |
| JMF | 12:0071cb144c7a | 174 | * |
| JMF | 12:0071cb144c7a | 175 | * \return 0 if successful, |
| JMF | 12:0071cb144c7a | 176 | * or a MBEDTLS_ERR_ECP_XXX, MBEDTLS_ERR_MPI_XXX or |
| JMF | 12:0071cb144c7a | 177 | * MBEDTLS_ERR_ASN1_XXX error code |
| JMF | 12:0071cb144c7a | 178 | */ |
| JMF | 12:0071cb144c7a | 179 | int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, |
| JMF | 12:0071cb144c7a | 180 | const unsigned char *hash, size_t hlen, |
| JMF | 12:0071cb144c7a | 181 | unsigned char *sig, size_t *slen, |
| JMF | 12:0071cb144c7a | 182 | mbedtls_md_type_t md_alg ) MBEDTLS_DEPRECATED; |
| JMF | 12:0071cb144c7a | 183 | #undef MBEDTLS_DEPRECATED |
| JMF | 12:0071cb144c7a | 184 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
| JMF | 12:0071cb144c7a | 185 | #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ |
| JMF | 12:0071cb144c7a | 186 | |
| JMF | 12:0071cb144c7a | 187 | /** |
| JMF | 12:0071cb144c7a | 188 | * \brief Read and verify an ECDSA signature |
| JMF | 12:0071cb144c7a | 189 | * |
| JMF | 12:0071cb144c7a | 190 | * \param ctx ECDSA context |
| JMF | 12:0071cb144c7a | 191 | * \param hash Message hash |
| JMF | 12:0071cb144c7a | 192 | * \param hlen Size of hash |
| JMF | 12:0071cb144c7a | 193 | * \param sig Signature to read and verify |
| JMF | 12:0071cb144c7a | 194 | * \param slen Size of sig |
| JMF | 12:0071cb144c7a | 195 | * |
| JMF | 12:0071cb144c7a | 196 | * \return 0 if successful, |
| JMF | 12:0071cb144c7a | 197 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid, |
| JMF | 12:0071cb144c7a | 198 | * MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH if the signature is |
| JMF | 12:0071cb144c7a | 199 | * valid but its actual length is less than siglen, |
| JMF | 12:0071cb144c7a | 200 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX error code |
| JMF | 12:0071cb144c7a | 201 | */ |
| JMF | 12:0071cb144c7a | 202 | int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, |
| JMF | 12:0071cb144c7a | 203 | const unsigned char *hash, size_t hlen, |
| JMF | 12:0071cb144c7a | 204 | const unsigned char *sig, size_t slen ); |
| JMF | 12:0071cb144c7a | 205 | |
| JMF | 12:0071cb144c7a | 206 | /** |
| JMF | 12:0071cb144c7a | 207 | * \brief Generate an ECDSA keypair on the given curve |
| JMF | 12:0071cb144c7a | 208 | * |
| JMF | 12:0071cb144c7a | 209 | * \param ctx ECDSA context in which the keypair should be stored |
| JMF | 12:0071cb144c7a | 210 | * \param gid Group (elliptic curve) to use. One of the various |
| JMF | 12:0071cb144c7a | 211 | * MBEDTLS_ECP_DP_XXX macros depending on configuration. |
| JMF | 12:0071cb144c7a | 212 | * \param f_rng RNG function |
| JMF | 12:0071cb144c7a | 213 | * \param p_rng RNG parameter |
| JMF | 12:0071cb144c7a | 214 | * |
| JMF | 12:0071cb144c7a | 215 | * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code. |
| JMF | 12:0071cb144c7a | 216 | */ |
| JMF | 12:0071cb144c7a | 217 | int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, |
| JMF | 12:0071cb144c7a | 218 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| JMF | 12:0071cb144c7a | 219 | |
| JMF | 12:0071cb144c7a | 220 | /** |
| JMF | 12:0071cb144c7a | 221 | * \brief Set an ECDSA context from an EC key pair |
| JMF | 12:0071cb144c7a | 222 | * |
| JMF | 12:0071cb144c7a | 223 | * \param ctx ECDSA context to set |
| JMF | 12:0071cb144c7a | 224 | * \param key EC key to use |
| JMF | 12:0071cb144c7a | 225 | * |
| JMF | 12:0071cb144c7a | 226 | * \return 0 on success, or a MBEDTLS_ERR_ECP_XXX code. |
| JMF | 12:0071cb144c7a | 227 | */ |
| JMF | 12:0071cb144c7a | 228 | int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ); |
| JMF | 12:0071cb144c7a | 229 | |
| JMF | 12:0071cb144c7a | 230 | /** |
| JMF | 12:0071cb144c7a | 231 | * \brief Initialize context |
| JMF | 12:0071cb144c7a | 232 | * |
| JMF | 12:0071cb144c7a | 233 | * \param ctx Context to initialize |
| JMF | 12:0071cb144c7a | 234 | */ |
| JMF | 12:0071cb144c7a | 235 | void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); |
| JMF | 12:0071cb144c7a | 236 | |
| JMF | 12:0071cb144c7a | 237 | /** |
| JMF | 12:0071cb144c7a | 238 | * \brief Free context |
| JMF | 12:0071cb144c7a | 239 | * |
| JMF | 12:0071cb144c7a | 240 | * \param ctx Context to free |
| JMF | 12:0071cb144c7a | 241 | */ |
| JMF | 12:0071cb144c7a | 242 | void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); |
| JMF | 12:0071cb144c7a | 243 | |
| JMF | 12:0071cb144c7a | 244 | #ifdef __cplusplus |
| JMF | 12:0071cb144c7a | 245 | } |
| JMF | 12:0071cb144c7a | 246 | #endif |
| JMF | 12:0071cb144c7a | 247 | |
| JMF | 12:0071cb144c7a | 248 | #endif /* ecdsa.h */ |
