mbed TLS Build
include/mbedtls/ecp.h@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /** |
markrad | 0:cdf462088d13 | 2 | * \file ecp.h |
markrad | 0:cdf462088d13 | 3 | * |
markrad | 0:cdf462088d13 | 4 | * \brief Elliptic curves over GF(p) |
markrad | 0:cdf462088d13 | 5 | * |
markrad | 0:cdf462088d13 | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
markrad | 0:cdf462088d13 | 7 | * SPDX-License-Identifier: Apache-2.0 |
markrad | 0:cdf462088d13 | 8 | * |
markrad | 0:cdf462088d13 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
markrad | 0:cdf462088d13 | 10 | * not use this file except in compliance with the License. |
markrad | 0:cdf462088d13 | 11 | * You may obtain a copy of the License at |
markrad | 0:cdf462088d13 | 12 | * |
markrad | 0:cdf462088d13 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
markrad | 0:cdf462088d13 | 14 | * |
markrad | 0:cdf462088d13 | 15 | * Unless required by applicable law or agreed to in writing, software |
markrad | 0:cdf462088d13 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
markrad | 0:cdf462088d13 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
markrad | 0:cdf462088d13 | 18 | * See the License for the specific language governing permissions and |
markrad | 0:cdf462088d13 | 19 | * limitations under the License. |
markrad | 0:cdf462088d13 | 20 | * |
markrad | 0:cdf462088d13 | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
markrad | 0:cdf462088d13 | 22 | */ |
markrad | 0:cdf462088d13 | 23 | #ifndef MBEDTLS_ECP_H |
markrad | 0:cdf462088d13 | 24 | #define MBEDTLS_ECP_H |
markrad | 0:cdf462088d13 | 25 | |
markrad | 0:cdf462088d13 | 26 | #include "bignum.h" |
markrad | 0:cdf462088d13 | 27 | |
markrad | 0:cdf462088d13 | 28 | /* |
markrad | 0:cdf462088d13 | 29 | * ECP error codes |
markrad | 0:cdf462088d13 | 30 | */ |
markrad | 0:cdf462088d13 | 31 | #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
markrad | 0:cdf462088d13 | 32 | #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
markrad | 0:cdf462088d13 | 33 | #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */ |
markrad | 0:cdf462088d13 | 34 | #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */ |
markrad | 0:cdf462088d13 | 35 | #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
markrad | 0:cdf462088d13 | 36 | #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */ |
markrad | 0:cdf462088d13 | 37 | #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
markrad | 0:cdf462088d13 | 38 | #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */ |
markrad | 0:cdf462088d13 | 39 | |
markrad | 0:cdf462088d13 | 40 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 41 | extern "C" { |
markrad | 0:cdf462088d13 | 42 | #endif |
markrad | 0:cdf462088d13 | 43 | |
markrad | 0:cdf462088d13 | 44 | /** |
markrad | 0:cdf462088d13 | 45 | * Domain parameters (curve, subgroup and generator) identifiers. |
markrad | 0:cdf462088d13 | 46 | * |
markrad | 0:cdf462088d13 | 47 | * Only curves over prime fields are supported. |
markrad | 0:cdf462088d13 | 48 | * |
markrad | 0:cdf462088d13 | 49 | * \warning This library does not support validation of arbitrary domain |
markrad | 0:cdf462088d13 | 50 | * parameters. Therefore, only well-known domain parameters from trusted |
markrad | 0:cdf462088d13 | 51 | * sources should be used. See mbedtls_ecp_group_load(). |
markrad | 0:cdf462088d13 | 52 | */ |
markrad | 0:cdf462088d13 | 53 | typedef enum |
markrad | 0:cdf462088d13 | 54 | { |
markrad | 0:cdf462088d13 | 55 | MBEDTLS_ECP_DP_NONE = 0, |
markrad | 0:cdf462088d13 | 56 | MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */ |
markrad | 0:cdf462088d13 | 57 | MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */ |
markrad | 0:cdf462088d13 | 58 | MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */ |
markrad | 0:cdf462088d13 | 59 | MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */ |
markrad | 0:cdf462088d13 | 60 | MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */ |
markrad | 0:cdf462088d13 | 61 | MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */ |
markrad | 0:cdf462088d13 | 62 | MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */ |
markrad | 0:cdf462088d13 | 63 | MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */ |
markrad | 0:cdf462088d13 | 64 | MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */ |
markrad | 0:cdf462088d13 | 65 | MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */ |
markrad | 0:cdf462088d13 | 66 | MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */ |
markrad | 0:cdf462088d13 | 67 | MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */ |
markrad | 0:cdf462088d13 | 68 | } mbedtls_ecp_group_id; |
markrad | 0:cdf462088d13 | 69 | |
markrad | 0:cdf462088d13 | 70 | /** |
markrad | 0:cdf462088d13 | 71 | * Number of supported curves (plus one for NONE). |
markrad | 0:cdf462088d13 | 72 | * |
markrad | 0:cdf462088d13 | 73 | * (Montgomery curves excluded for now.) |
markrad | 0:cdf462088d13 | 74 | */ |
markrad | 0:cdf462088d13 | 75 | #define MBEDTLS_ECP_DP_MAX 12 |
markrad | 0:cdf462088d13 | 76 | |
markrad | 0:cdf462088d13 | 77 | /** |
markrad | 0:cdf462088d13 | 78 | * Curve information for use by other modules |
markrad | 0:cdf462088d13 | 79 | */ |
markrad | 0:cdf462088d13 | 80 | typedef struct |
markrad | 0:cdf462088d13 | 81 | { |
markrad | 0:cdf462088d13 | 82 | mbedtls_ecp_group_id grp_id; /*!< Internal identifier */ |
markrad | 0:cdf462088d13 | 83 | uint16_t tls_id; /*!< TLS NamedCurve identifier */ |
markrad | 0:cdf462088d13 | 84 | uint16_t bit_size; /*!< Curve size in bits */ |
markrad | 0:cdf462088d13 | 85 | const char *name; /*!< Human-friendly name */ |
markrad | 0:cdf462088d13 | 86 | } mbedtls_ecp_curve_info; |
markrad | 0:cdf462088d13 | 87 | |
markrad | 0:cdf462088d13 | 88 | /** |
markrad | 0:cdf462088d13 | 89 | * \brief ECP point structure (jacobian coordinates) |
markrad | 0:cdf462088d13 | 90 | * |
markrad | 0:cdf462088d13 | 91 | * \note All functions expect and return points satisfying |
markrad | 0:cdf462088d13 | 92 | * the following condition: Z == 0 or Z == 1. (Other |
markrad | 0:cdf462088d13 | 93 | * values of Z are used by internal functions only.) |
markrad | 0:cdf462088d13 | 94 | * The point is zero, or "at infinity", if Z == 0. |
markrad | 0:cdf462088d13 | 95 | * Otherwise, X and Y are its standard (affine) coordinates. |
markrad | 0:cdf462088d13 | 96 | */ |
markrad | 0:cdf462088d13 | 97 | typedef struct |
markrad | 0:cdf462088d13 | 98 | { |
markrad | 0:cdf462088d13 | 99 | mbedtls_mpi X; /*!< the point's X coordinate */ |
markrad | 0:cdf462088d13 | 100 | mbedtls_mpi Y; /*!< the point's Y coordinate */ |
markrad | 0:cdf462088d13 | 101 | mbedtls_mpi Z; /*!< the point's Z coordinate */ |
markrad | 0:cdf462088d13 | 102 | } |
markrad | 0:cdf462088d13 | 103 | mbedtls_ecp_point; |
markrad | 0:cdf462088d13 | 104 | |
markrad | 0:cdf462088d13 | 105 | /** |
markrad | 0:cdf462088d13 | 106 | * \brief ECP group structure |
markrad | 0:cdf462088d13 | 107 | * |
markrad | 0:cdf462088d13 | 108 | * We consider two types of curves equations: |
markrad | 0:cdf462088d13 | 109 | * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492) |
markrad | 0:cdf462088d13 | 110 | * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft) |
markrad | 0:cdf462088d13 | 111 | * In both cases, a generator G for a prime-order subgroup is fixed. In the |
markrad | 0:cdf462088d13 | 112 | * short weierstrass, this subgroup is actually the whole curve, and its |
markrad | 0:cdf462088d13 | 113 | * cardinal is denoted by N. |
markrad | 0:cdf462088d13 | 114 | * |
markrad | 0:cdf462088d13 | 115 | * In the case of Short Weierstrass curves, our code requires that N is an odd |
markrad | 0:cdf462088d13 | 116 | * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.) |
markrad | 0:cdf462088d13 | 117 | * |
markrad | 0:cdf462088d13 | 118 | * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is |
markrad | 0:cdf462088d13 | 119 | * the quantity actually used in the formulas. Also, nbits is not the size of N |
markrad | 0:cdf462088d13 | 120 | * but the required size for private keys. |
markrad | 0:cdf462088d13 | 121 | * |
markrad | 0:cdf462088d13 | 122 | * If modp is NULL, reduction modulo P is done using a generic algorithm. |
markrad | 0:cdf462088d13 | 123 | * Otherwise, it must point to a function that takes an mbedtls_mpi in the range |
markrad | 0:cdf462088d13 | 124 | * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more |
markrad | 0:cdf462088d13 | 125 | * than pbits, so that the integer may be efficiently brought in the 0..P-1 |
markrad | 0:cdf462088d13 | 126 | * range by a few additions or substractions. It must return 0 on success and |
markrad | 0:cdf462088d13 | 127 | * non-zero on failure. |
markrad | 0:cdf462088d13 | 128 | */ |
markrad | 0:cdf462088d13 | 129 | typedef struct |
markrad | 0:cdf462088d13 | 130 | { |
markrad | 0:cdf462088d13 | 131 | mbedtls_ecp_group_id id; /*!< internal group identifier */ |
markrad | 0:cdf462088d13 | 132 | mbedtls_mpi P; /*!< prime modulus of the base field */ |
markrad | 0:cdf462088d13 | 133 | mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */ |
markrad | 0:cdf462088d13 | 134 | mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */ |
markrad | 0:cdf462088d13 | 135 | mbedtls_ecp_point G; /*!< generator of the (sub)group used */ |
markrad | 0:cdf462088d13 | 136 | mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */ |
markrad | 0:cdf462088d13 | 137 | size_t pbits; /*!< number of bits in P */ |
markrad | 0:cdf462088d13 | 138 | size_t nbits; /*!< number of bits in 1. P, or 2. private keys */ |
markrad | 0:cdf462088d13 | 139 | unsigned int h; /*!< internal: 1 if the constants are static */ |
markrad | 0:cdf462088d13 | 140 | int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */ |
markrad | 0:cdf462088d13 | 141 | int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */ |
markrad | 0:cdf462088d13 | 142 | int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */ |
markrad | 0:cdf462088d13 | 143 | void *t_data; /*!< unused */ |
markrad | 0:cdf462088d13 | 144 | mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */ |
markrad | 0:cdf462088d13 | 145 | size_t T_size; /*!< number for pre-computed points */ |
markrad | 0:cdf462088d13 | 146 | } |
markrad | 0:cdf462088d13 | 147 | mbedtls_ecp_group; |
markrad | 0:cdf462088d13 | 148 | |
markrad | 0:cdf462088d13 | 149 | /** |
markrad | 0:cdf462088d13 | 150 | * \brief ECP key pair structure |
markrad | 0:cdf462088d13 | 151 | * |
markrad | 0:cdf462088d13 | 152 | * A generic key pair that could be used for ECDSA, fixed ECDH, etc. |
markrad | 0:cdf462088d13 | 153 | * |
markrad | 0:cdf462088d13 | 154 | * \note Members purposefully in the same order as struc mbedtls_ecdsa_context. |
markrad | 0:cdf462088d13 | 155 | */ |
markrad | 0:cdf462088d13 | 156 | typedef struct |
markrad | 0:cdf462088d13 | 157 | { |
markrad | 0:cdf462088d13 | 158 | mbedtls_ecp_group grp; /*!< Elliptic curve and base point */ |
markrad | 0:cdf462088d13 | 159 | mbedtls_mpi d; /*!< our secret value */ |
markrad | 0:cdf462088d13 | 160 | mbedtls_ecp_point Q; /*!< our public value */ |
markrad | 0:cdf462088d13 | 161 | } |
markrad | 0:cdf462088d13 | 162 | mbedtls_ecp_keypair; |
markrad | 0:cdf462088d13 | 163 | |
markrad | 0:cdf462088d13 | 164 | /** |
markrad | 0:cdf462088d13 | 165 | * \name SECTION: Module settings |
markrad | 0:cdf462088d13 | 166 | * |
markrad | 0:cdf462088d13 | 167 | * The configuration options you can set for this module are in this section. |
markrad | 0:cdf462088d13 | 168 | * Either change them in config.h or define them on the compiler command line. |
markrad | 0:cdf462088d13 | 169 | * \{ |
markrad | 0:cdf462088d13 | 170 | */ |
markrad | 0:cdf462088d13 | 171 | |
markrad | 0:cdf462088d13 | 172 | #if !defined(MBEDTLS_ECP_MAX_BITS) |
markrad | 0:cdf462088d13 | 173 | /** |
markrad | 0:cdf462088d13 | 174 | * Maximum size of the groups (that is, of N and P) |
markrad | 0:cdf462088d13 | 175 | */ |
markrad | 0:cdf462088d13 | 176 | #define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */ |
markrad | 0:cdf462088d13 | 177 | #endif |
markrad | 0:cdf462088d13 | 178 | |
markrad | 0:cdf462088d13 | 179 | #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) |
markrad | 0:cdf462088d13 | 180 | #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) |
markrad | 0:cdf462088d13 | 181 | |
markrad | 0:cdf462088d13 | 182 | #if !defined(MBEDTLS_ECP_WINDOW_SIZE) |
markrad | 0:cdf462088d13 | 183 | /* |
markrad | 0:cdf462088d13 | 184 | * Maximum "window" size used for point multiplication. |
markrad | 0:cdf462088d13 | 185 | * Default: 6. |
markrad | 0:cdf462088d13 | 186 | * Minimum value: 2. Maximum value: 7. |
markrad | 0:cdf462088d13 | 187 | * |
markrad | 0:cdf462088d13 | 188 | * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) |
markrad | 0:cdf462088d13 | 189 | * points used for point multiplication. This value is directly tied to EC |
markrad | 0:cdf462088d13 | 190 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
markrad | 0:cdf462088d13 | 191 | * by two (if large curves are in use). |
markrad | 0:cdf462088d13 | 192 | * |
markrad | 0:cdf462088d13 | 193 | * Reduction in size may reduce speed, but larger curves are impacted first. |
markrad | 0:cdf462088d13 | 194 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
markrad | 0:cdf462088d13 | 195 | * w-size: 6 5 4 3 2 |
markrad | 0:cdf462088d13 | 196 | * 521 145 141 135 120 97 |
markrad | 0:cdf462088d13 | 197 | * 384 214 209 198 177 146 |
markrad | 0:cdf462088d13 | 198 | * 256 320 320 303 262 226 |
markrad | 0:cdf462088d13 | 199 | |
markrad | 0:cdf462088d13 | 200 | * 224 475 475 453 398 342 |
markrad | 0:cdf462088d13 | 201 | * 192 640 640 633 587 476 |
markrad | 0:cdf462088d13 | 202 | */ |
markrad | 0:cdf462088d13 | 203 | #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */ |
markrad | 0:cdf462088d13 | 204 | #endif /* MBEDTLS_ECP_WINDOW_SIZE */ |
markrad | 0:cdf462088d13 | 205 | |
markrad | 0:cdf462088d13 | 206 | #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) |
markrad | 0:cdf462088d13 | 207 | /* |
markrad | 0:cdf462088d13 | 208 | * Trade memory for speed on fixed-point multiplication. |
markrad | 0:cdf462088d13 | 209 | * |
markrad | 0:cdf462088d13 | 210 | * This speeds up repeated multiplication of the generator (that is, the |
markrad | 0:cdf462088d13 | 211 | * multiplication in ECDSA signatures, and half of the multiplications in |
markrad | 0:cdf462088d13 | 212 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
markrad | 0:cdf462088d13 | 213 | * |
markrad | 0:cdf462088d13 | 214 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
markrad | 0:cdf462088d13 | 215 | * |
markrad | 0:cdf462088d13 | 216 | * Change this value to 0 to reduce peak memory usage. |
markrad | 0:cdf462088d13 | 217 | */ |
markrad | 0:cdf462088d13 | 218 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */ |
markrad | 0:cdf462088d13 | 219 | #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ |
markrad | 0:cdf462088d13 | 220 | |
markrad | 0:cdf462088d13 | 221 | /* \} name SECTION: Module settings */ |
markrad | 0:cdf462088d13 | 222 | |
markrad | 0:cdf462088d13 | 223 | /* |
markrad | 0:cdf462088d13 | 224 | * Point formats, from RFC 4492's enum ECPointFormat |
markrad | 0:cdf462088d13 | 225 | */ |
markrad | 0:cdf462088d13 | 226 | #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */ |
markrad | 0:cdf462088d13 | 227 | #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */ |
markrad | 0:cdf462088d13 | 228 | |
markrad | 0:cdf462088d13 | 229 | /* |
markrad | 0:cdf462088d13 | 230 | * Some other constants from RFC 4492 |
markrad | 0:cdf462088d13 | 231 | */ |
markrad | 0:cdf462088d13 | 232 | #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */ |
markrad | 0:cdf462088d13 | 233 | |
markrad | 0:cdf462088d13 | 234 | /** |
markrad | 0:cdf462088d13 | 235 | * \brief Get the list of supported curves in order of preferrence |
markrad | 0:cdf462088d13 | 236 | * (full information) |
markrad | 0:cdf462088d13 | 237 | * |
markrad | 0:cdf462088d13 | 238 | * \return A statically allocated array, the last entry is 0. |
markrad | 0:cdf462088d13 | 239 | */ |
markrad | 0:cdf462088d13 | 240 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void ); |
markrad | 0:cdf462088d13 | 241 | |
markrad | 0:cdf462088d13 | 242 | /** |
markrad | 0:cdf462088d13 | 243 | * \brief Get the list of supported curves in order of preferrence |
markrad | 0:cdf462088d13 | 244 | * (grp_id only) |
markrad | 0:cdf462088d13 | 245 | * |
markrad | 0:cdf462088d13 | 246 | * \return A statically allocated array, |
markrad | 0:cdf462088d13 | 247 | * terminated with MBEDTLS_ECP_DP_NONE. |
markrad | 0:cdf462088d13 | 248 | */ |
markrad | 0:cdf462088d13 | 249 | const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void ); |
markrad | 0:cdf462088d13 | 250 | |
markrad | 0:cdf462088d13 | 251 | /** |
markrad | 0:cdf462088d13 | 252 | * \brief Get curve information from an internal group identifier |
markrad | 0:cdf462088d13 | 253 | * |
markrad | 0:cdf462088d13 | 254 | * \param grp_id A MBEDTLS_ECP_DP_XXX value |
markrad | 0:cdf462088d13 | 255 | * |
markrad | 0:cdf462088d13 | 256 | * \return The associated curve information or NULL |
markrad | 0:cdf462088d13 | 257 | */ |
markrad | 0:cdf462088d13 | 258 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id ); |
markrad | 0:cdf462088d13 | 259 | |
markrad | 0:cdf462088d13 | 260 | /** |
markrad | 0:cdf462088d13 | 261 | * \brief Get curve information from a TLS NamedCurve value |
markrad | 0:cdf462088d13 | 262 | * |
markrad | 0:cdf462088d13 | 263 | * \param tls_id A MBEDTLS_ECP_DP_XXX value |
markrad | 0:cdf462088d13 | 264 | * |
markrad | 0:cdf462088d13 | 265 | * \return The associated curve information or NULL |
markrad | 0:cdf462088d13 | 266 | */ |
markrad | 0:cdf462088d13 | 267 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id ); |
markrad | 0:cdf462088d13 | 268 | |
markrad | 0:cdf462088d13 | 269 | /** |
markrad | 0:cdf462088d13 | 270 | * \brief Get curve information from a human-readable name |
markrad | 0:cdf462088d13 | 271 | * |
markrad | 0:cdf462088d13 | 272 | * \param name The name |
markrad | 0:cdf462088d13 | 273 | * |
markrad | 0:cdf462088d13 | 274 | * \return The associated curve information or NULL |
markrad | 0:cdf462088d13 | 275 | */ |
markrad | 0:cdf462088d13 | 276 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name ); |
markrad | 0:cdf462088d13 | 277 | |
markrad | 0:cdf462088d13 | 278 | /** |
markrad | 0:cdf462088d13 | 279 | * \brief Initialize a point (as zero) |
markrad | 0:cdf462088d13 | 280 | */ |
markrad | 0:cdf462088d13 | 281 | void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ); |
markrad | 0:cdf462088d13 | 282 | |
markrad | 0:cdf462088d13 | 283 | /** |
markrad | 0:cdf462088d13 | 284 | * \brief Initialize a group (to something meaningless) |
markrad | 0:cdf462088d13 | 285 | */ |
markrad | 0:cdf462088d13 | 286 | void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ); |
markrad | 0:cdf462088d13 | 287 | |
markrad | 0:cdf462088d13 | 288 | /** |
markrad | 0:cdf462088d13 | 289 | * \brief Initialize a key pair (as an invalid one) |
markrad | 0:cdf462088d13 | 290 | */ |
markrad | 0:cdf462088d13 | 291 | void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ); |
markrad | 0:cdf462088d13 | 292 | |
markrad | 0:cdf462088d13 | 293 | /** |
markrad | 0:cdf462088d13 | 294 | * \brief Free the components of a point |
markrad | 0:cdf462088d13 | 295 | */ |
markrad | 0:cdf462088d13 | 296 | void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ); |
markrad | 0:cdf462088d13 | 297 | |
markrad | 0:cdf462088d13 | 298 | /** |
markrad | 0:cdf462088d13 | 299 | * \brief Free the components of an ECP group |
markrad | 0:cdf462088d13 | 300 | */ |
markrad | 0:cdf462088d13 | 301 | void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ); |
markrad | 0:cdf462088d13 | 302 | |
markrad | 0:cdf462088d13 | 303 | /** |
markrad | 0:cdf462088d13 | 304 | * \brief Free the components of a key pair |
markrad | 0:cdf462088d13 | 305 | */ |
markrad | 0:cdf462088d13 | 306 | void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ); |
markrad | 0:cdf462088d13 | 307 | |
markrad | 0:cdf462088d13 | 308 | /** |
markrad | 0:cdf462088d13 | 309 | * \brief Copy the contents of point Q into P |
markrad | 0:cdf462088d13 | 310 | * |
markrad | 0:cdf462088d13 | 311 | * \param P Destination point |
markrad | 0:cdf462088d13 | 312 | * \param Q Source point |
markrad | 0:cdf462088d13 | 313 | * |
markrad | 0:cdf462088d13 | 314 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 315 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
markrad | 0:cdf462088d13 | 316 | */ |
markrad | 0:cdf462088d13 | 317 | int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ); |
markrad | 0:cdf462088d13 | 318 | |
markrad | 0:cdf462088d13 | 319 | /** |
markrad | 0:cdf462088d13 | 320 | * \brief Copy the contents of a group object |
markrad | 0:cdf462088d13 | 321 | * |
markrad | 0:cdf462088d13 | 322 | * \param dst Destination group |
markrad | 0:cdf462088d13 | 323 | * \param src Source group |
markrad | 0:cdf462088d13 | 324 | * |
markrad | 0:cdf462088d13 | 325 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 326 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
markrad | 0:cdf462088d13 | 327 | */ |
markrad | 0:cdf462088d13 | 328 | int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ); |
markrad | 0:cdf462088d13 | 329 | |
markrad | 0:cdf462088d13 | 330 | /** |
markrad | 0:cdf462088d13 | 331 | * \brief Set a point to zero |
markrad | 0:cdf462088d13 | 332 | * |
markrad | 0:cdf462088d13 | 333 | * \param pt Destination point |
markrad | 0:cdf462088d13 | 334 | * |
markrad | 0:cdf462088d13 | 335 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 336 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
markrad | 0:cdf462088d13 | 337 | */ |
markrad | 0:cdf462088d13 | 338 | int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ); |
markrad | 0:cdf462088d13 | 339 | |
markrad | 0:cdf462088d13 | 340 | /** |
markrad | 0:cdf462088d13 | 341 | * \brief Tell if a point is zero |
markrad | 0:cdf462088d13 | 342 | * |
markrad | 0:cdf462088d13 | 343 | * \param pt Point to test |
markrad | 0:cdf462088d13 | 344 | * |
markrad | 0:cdf462088d13 | 345 | * \return 1 if point is zero, 0 otherwise |
markrad | 0:cdf462088d13 | 346 | */ |
markrad | 0:cdf462088d13 | 347 | int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); |
markrad | 0:cdf462088d13 | 348 | |
markrad | 0:cdf462088d13 | 349 | /** |
markrad | 0:cdf462088d13 | 350 | * \brief Compare two points |
markrad | 0:cdf462088d13 | 351 | * |
markrad | 0:cdf462088d13 | 352 | * \note This assumes the points are normalized. Otherwise, |
markrad | 0:cdf462088d13 | 353 | * they may compare as "not equal" even if they are. |
markrad | 0:cdf462088d13 | 354 | * |
markrad | 0:cdf462088d13 | 355 | * \param P First point to compare |
markrad | 0:cdf462088d13 | 356 | * \param Q Second point to compare |
markrad | 0:cdf462088d13 | 357 | * |
markrad | 0:cdf462088d13 | 358 | * \return 0 if the points are equal, |
markrad | 0:cdf462088d13 | 359 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise |
markrad | 0:cdf462088d13 | 360 | */ |
markrad | 0:cdf462088d13 | 361 | int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, |
markrad | 0:cdf462088d13 | 362 | const mbedtls_ecp_point *Q ); |
markrad | 0:cdf462088d13 | 363 | |
markrad | 0:cdf462088d13 | 364 | /** |
markrad | 0:cdf462088d13 | 365 | * \brief Import a non-zero point from two ASCII strings |
markrad | 0:cdf462088d13 | 366 | * |
markrad | 0:cdf462088d13 | 367 | * \param P Destination point |
markrad | 0:cdf462088d13 | 368 | * \param radix Input numeric base |
markrad | 0:cdf462088d13 | 369 | * \param x First affine coordinate as a null-terminated string |
markrad | 0:cdf462088d13 | 370 | * \param y Second affine coordinate as a null-terminated string |
markrad | 0:cdf462088d13 | 371 | * |
markrad | 0:cdf462088d13 | 372 | * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code |
markrad | 0:cdf462088d13 | 373 | */ |
markrad | 0:cdf462088d13 | 374 | int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, |
markrad | 0:cdf462088d13 | 375 | const char *x, const char *y ); |
markrad | 0:cdf462088d13 | 376 | |
markrad | 0:cdf462088d13 | 377 | /** |
markrad | 0:cdf462088d13 | 378 | * \brief Export a point into unsigned binary data |
markrad | 0:cdf462088d13 | 379 | * |
markrad | 0:cdf462088d13 | 380 | * \param grp Group to which the point should belong |
markrad | 0:cdf462088d13 | 381 | * \param P Point to export |
markrad | 0:cdf462088d13 | 382 | * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro |
markrad | 0:cdf462088d13 | 383 | * \param olen Length of the actual output |
markrad | 0:cdf462088d13 | 384 | * \param buf Output buffer |
markrad | 0:cdf462088d13 | 385 | * \param buflen Length of the output buffer |
markrad | 0:cdf462088d13 | 386 | * |
markrad | 0:cdf462088d13 | 387 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 388 | * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA |
markrad | 0:cdf462088d13 | 389 | * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL |
markrad | 0:cdf462088d13 | 390 | */ |
markrad | 0:cdf462088d13 | 391 | int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, |
markrad | 0:cdf462088d13 | 392 | int format, size_t *olen, |
markrad | 0:cdf462088d13 | 393 | unsigned char *buf, size_t buflen ); |
markrad | 0:cdf462088d13 | 394 | |
markrad | 0:cdf462088d13 | 395 | /** |
markrad | 0:cdf462088d13 | 396 | * \brief Import a point from unsigned binary data |
markrad | 0:cdf462088d13 | 397 | * |
markrad | 0:cdf462088d13 | 398 | * \param grp Group to which the point should belong |
markrad | 0:cdf462088d13 | 399 | * \param P Point to import |
markrad | 0:cdf462088d13 | 400 | * \param buf Input buffer |
markrad | 0:cdf462088d13 | 401 | * \param ilen Actual length of input |
markrad | 0:cdf462088d13 | 402 | * |
markrad | 0:cdf462088d13 | 403 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 404 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid, |
markrad | 0:cdf462088d13 | 405 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
markrad | 0:cdf462088d13 | 406 | * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
markrad | 0:cdf462088d13 | 407 | * is not implemented. |
markrad | 0:cdf462088d13 | 408 | * |
markrad | 0:cdf462088d13 | 409 | * \note This function does NOT check that the point actually |
markrad | 0:cdf462088d13 | 410 | * belongs to the given group, see mbedtls_ecp_check_pubkey() for |
markrad | 0:cdf462088d13 | 411 | * that. |
markrad | 0:cdf462088d13 | 412 | */ |
markrad | 0:cdf462088d13 | 413 | int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, |
markrad | 0:cdf462088d13 | 414 | const unsigned char *buf, size_t ilen ); |
markrad | 0:cdf462088d13 | 415 | |
markrad | 0:cdf462088d13 | 416 | /** |
markrad | 0:cdf462088d13 | 417 | * \brief Import a point from a TLS ECPoint record |
markrad | 0:cdf462088d13 | 418 | * |
markrad | 0:cdf462088d13 | 419 | * \param grp ECP group used |
markrad | 0:cdf462088d13 | 420 | * \param pt Destination point |
markrad | 0:cdf462088d13 | 421 | * \param buf $(Start of input buffer) |
markrad | 0:cdf462088d13 | 422 | * \param len Buffer length |
markrad | 0:cdf462088d13 | 423 | * |
markrad | 0:cdf462088d13 | 424 | * \note buf is updated to point right after the ECPoint on exit |
markrad | 0:cdf462088d13 | 425 | * |
markrad | 0:cdf462088d13 | 426 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 427 | * MBEDTLS_ERR_MPI_XXX if initialization failed |
markrad | 0:cdf462088d13 | 428 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid |
markrad | 0:cdf462088d13 | 429 | */ |
markrad | 0:cdf462088d13 | 430 | int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
markrad | 0:cdf462088d13 | 431 | const unsigned char **buf, size_t len ); |
markrad | 0:cdf462088d13 | 432 | |
markrad | 0:cdf462088d13 | 433 | /** |
markrad | 0:cdf462088d13 | 434 | * \brief Export a point as a TLS ECPoint record |
markrad | 0:cdf462088d13 | 435 | * |
markrad | 0:cdf462088d13 | 436 | * \param grp ECP group used |
markrad | 0:cdf462088d13 | 437 | * \param pt Point to export |
markrad | 0:cdf462088d13 | 438 | * \param format Export format |
markrad | 0:cdf462088d13 | 439 | * \param olen length of data written |
markrad | 0:cdf462088d13 | 440 | * \param buf Buffer to write to |
markrad | 0:cdf462088d13 | 441 | * \param blen Buffer length |
markrad | 0:cdf462088d13 | 442 | * |
markrad | 0:cdf462088d13 | 443 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 444 | * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA |
markrad | 0:cdf462088d13 | 445 | * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL |
markrad | 0:cdf462088d13 | 446 | */ |
markrad | 0:cdf462088d13 | 447 | int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, |
markrad | 0:cdf462088d13 | 448 | int format, size_t *olen, |
markrad | 0:cdf462088d13 | 449 | unsigned char *buf, size_t blen ); |
markrad | 0:cdf462088d13 | 450 | |
markrad | 0:cdf462088d13 | 451 | /** |
markrad | 0:cdf462088d13 | 452 | * \brief Set a group using well-known domain parameters |
markrad | 0:cdf462088d13 | 453 | * |
markrad | 0:cdf462088d13 | 454 | * \param grp Destination group |
markrad | 0:cdf462088d13 | 455 | * \param index Index in the list of well-known domain parameters |
markrad | 0:cdf462088d13 | 456 | * |
markrad | 0:cdf462088d13 | 457 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 458 | * MBEDTLS_ERR_MPI_XXX if initialization failed |
markrad | 0:cdf462088d13 | 459 | * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups |
markrad | 0:cdf462088d13 | 460 | * |
markrad | 0:cdf462088d13 | 461 | * \note Index should be a value of RFC 4492's enum NamedCurve, |
markrad | 0:cdf462088d13 | 462 | * usually in the form of a MBEDTLS_ECP_DP_XXX macro. |
markrad | 0:cdf462088d13 | 463 | */ |
markrad | 0:cdf462088d13 | 464 | int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index ); |
markrad | 0:cdf462088d13 | 465 | |
markrad | 0:cdf462088d13 | 466 | /** |
markrad | 0:cdf462088d13 | 467 | * \brief Set a group from a TLS ECParameters record |
markrad | 0:cdf462088d13 | 468 | * |
markrad | 0:cdf462088d13 | 469 | * \param grp Destination group |
markrad | 0:cdf462088d13 | 470 | * \param buf &(Start of input buffer) |
markrad | 0:cdf462088d13 | 471 | * \param len Buffer length |
markrad | 0:cdf462088d13 | 472 | * |
markrad | 0:cdf462088d13 | 473 | * \note buf is updated to point right after ECParameters on exit |
markrad | 0:cdf462088d13 | 474 | * |
markrad | 0:cdf462088d13 | 475 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 476 | * MBEDTLS_ERR_MPI_XXX if initialization failed |
markrad | 0:cdf462088d13 | 477 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid |
markrad | 0:cdf462088d13 | 478 | */ |
markrad | 0:cdf462088d13 | 479 | int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len ); |
markrad | 0:cdf462088d13 | 480 | |
markrad | 0:cdf462088d13 | 481 | /** |
markrad | 0:cdf462088d13 | 482 | * \brief Write the TLS ECParameters record for a group |
markrad | 0:cdf462088d13 | 483 | * |
markrad | 0:cdf462088d13 | 484 | * \param grp ECP group used |
markrad | 0:cdf462088d13 | 485 | * \param olen Number of bytes actually written |
markrad | 0:cdf462088d13 | 486 | * \param buf Buffer to write to |
markrad | 0:cdf462088d13 | 487 | * \param blen Buffer length |
markrad | 0:cdf462088d13 | 488 | * |
markrad | 0:cdf462088d13 | 489 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 490 | * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL |
markrad | 0:cdf462088d13 | 491 | */ |
markrad | 0:cdf462088d13 | 492 | int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, |
markrad | 0:cdf462088d13 | 493 | unsigned char *buf, size_t blen ); |
markrad | 0:cdf462088d13 | 494 | |
markrad | 0:cdf462088d13 | 495 | /** |
markrad | 0:cdf462088d13 | 496 | * \brief Multiplication by an integer: R = m * P |
markrad | 0:cdf462088d13 | 497 | * (Not thread-safe to use same group in multiple threads) |
markrad | 0:cdf462088d13 | 498 | * |
markrad | 0:cdf462088d13 | 499 | * \note In order to prevent timing attacks, this function |
markrad | 0:cdf462088d13 | 500 | * executes the exact same sequence of (base field) |
markrad | 0:cdf462088d13 | 501 | * operations for any valid m. It avoids any if-branch or |
markrad | 0:cdf462088d13 | 502 | * array index depending on the value of m. |
markrad | 0:cdf462088d13 | 503 | * |
markrad | 0:cdf462088d13 | 504 | * \note If f_rng is not NULL, it is used to randomize intermediate |
markrad | 0:cdf462088d13 | 505 | * results in order to prevent potential timing attacks |
markrad | 0:cdf462088d13 | 506 | * targeting these results. It is recommended to always |
markrad | 0:cdf462088d13 | 507 | * provide a non-NULL f_rng (the overhead is negligible). |
markrad | 0:cdf462088d13 | 508 | * |
markrad | 0:cdf462088d13 | 509 | * \param grp ECP group |
markrad | 0:cdf462088d13 | 510 | * \param R Destination point |
markrad | 0:cdf462088d13 | 511 | * \param m Integer by which to multiply |
markrad | 0:cdf462088d13 | 512 | * \param P Point to multiply |
markrad | 0:cdf462088d13 | 513 | * \param f_rng RNG function (see notes) |
markrad | 0:cdf462088d13 | 514 | * \param p_rng RNG parameter |
markrad | 0:cdf462088d13 | 515 | * |
markrad | 0:cdf462088d13 | 516 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 517 | * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey |
markrad | 0:cdf462088d13 | 518 | * or P is not a valid pubkey, |
markrad | 0:cdf462088d13 | 519 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
markrad | 0:cdf462088d13 | 520 | */ |
markrad | 0:cdf462088d13 | 521 | int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
markrad | 0:cdf462088d13 | 522 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
markrad | 0:cdf462088d13 | 523 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
markrad | 0:cdf462088d13 | 524 | |
markrad | 0:cdf462088d13 | 525 | /** |
markrad | 0:cdf462088d13 | 526 | * \brief Multiplication and addition of two points by integers: |
markrad | 0:cdf462088d13 | 527 | * R = m * P + n * Q |
markrad | 0:cdf462088d13 | 528 | * (Not thread-safe to use same group in multiple threads) |
markrad | 0:cdf462088d13 | 529 | * |
markrad | 0:cdf462088d13 | 530 | * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee |
markrad | 0:cdf462088d13 | 531 | * a constant execution flow and timing. |
markrad | 0:cdf462088d13 | 532 | * |
markrad | 0:cdf462088d13 | 533 | * \param grp ECP group |
markrad | 0:cdf462088d13 | 534 | * \param R Destination point |
markrad | 0:cdf462088d13 | 535 | * \param m Integer by which to multiply P |
markrad | 0:cdf462088d13 | 536 | * \param P Point to multiply by m |
markrad | 0:cdf462088d13 | 537 | * \param n Integer by which to multiply Q |
markrad | 0:cdf462088d13 | 538 | * \param Q Point to be multiplied by n |
markrad | 0:cdf462088d13 | 539 | * |
markrad | 0:cdf462088d13 | 540 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 541 | * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey |
markrad | 0:cdf462088d13 | 542 | * or P or Q is not a valid pubkey, |
markrad | 0:cdf462088d13 | 543 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
markrad | 0:cdf462088d13 | 544 | */ |
markrad | 0:cdf462088d13 | 545 | int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
markrad | 0:cdf462088d13 | 546 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
markrad | 0:cdf462088d13 | 547 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q ); |
markrad | 0:cdf462088d13 | 548 | |
markrad | 0:cdf462088d13 | 549 | /** |
markrad | 0:cdf462088d13 | 550 | * \brief Check that a point is a valid public key on this curve |
markrad | 0:cdf462088d13 | 551 | * |
markrad | 0:cdf462088d13 | 552 | * \param grp Curve/group the point should belong to |
markrad | 0:cdf462088d13 | 553 | * \param pt Point to check |
markrad | 0:cdf462088d13 | 554 | * |
markrad | 0:cdf462088d13 | 555 | * \return 0 if point is a valid public key, |
markrad | 0:cdf462088d13 | 556 | * MBEDTLS_ERR_ECP_INVALID_KEY otherwise. |
markrad | 0:cdf462088d13 | 557 | * |
markrad | 0:cdf462088d13 | 558 | * \note This function only checks the point is non-zero, has valid |
markrad | 0:cdf462088d13 | 559 | * coordinates and lies on the curve, but not that it is |
markrad | 0:cdf462088d13 | 560 | * indeed a multiple of G. This is additional check is more |
markrad | 0:cdf462088d13 | 561 | * expensive, isn't required by standards, and shouldn't be |
markrad | 0:cdf462088d13 | 562 | * necessary if the group used has a small cofactor. In |
markrad | 0:cdf462088d13 | 563 | * particular, it is useless for the NIST groups which all |
markrad | 0:cdf462088d13 | 564 | * have a cofactor of 1. |
markrad | 0:cdf462088d13 | 565 | * |
markrad | 0:cdf462088d13 | 566 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
markrad | 0:cdf462088d13 | 567 | * in order to ease use with other structures such as |
markrad | 0:cdf462088d13 | 568 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
markrad | 0:cdf462088d13 | 569 | */ |
markrad | 0:cdf462088d13 | 570 | int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ); |
markrad | 0:cdf462088d13 | 571 | |
markrad | 0:cdf462088d13 | 572 | /** |
markrad | 0:cdf462088d13 | 573 | * \brief Check that an mbedtls_mpi is a valid private key for this curve |
markrad | 0:cdf462088d13 | 574 | * |
markrad | 0:cdf462088d13 | 575 | * \param grp Group used |
markrad | 0:cdf462088d13 | 576 | * \param d Integer to check |
markrad | 0:cdf462088d13 | 577 | * |
markrad | 0:cdf462088d13 | 578 | * \return 0 if point is a valid private key, |
markrad | 0:cdf462088d13 | 579 | * MBEDTLS_ERR_ECP_INVALID_KEY otherwise. |
markrad | 0:cdf462088d13 | 580 | * |
markrad | 0:cdf462088d13 | 581 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
markrad | 0:cdf462088d13 | 582 | * in order to ease use with other structures such as |
markrad | 0:cdf462088d13 | 583 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
markrad | 0:cdf462088d13 | 584 | */ |
markrad | 0:cdf462088d13 | 585 | int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ); |
markrad | 0:cdf462088d13 | 586 | |
markrad | 0:cdf462088d13 | 587 | /** |
markrad | 0:cdf462088d13 | 588 | * \brief Generate a keypair with configurable base point |
markrad | 0:cdf462088d13 | 589 | * |
markrad | 0:cdf462088d13 | 590 | * \param grp ECP group |
markrad | 0:cdf462088d13 | 591 | * \param G Chosen base point |
markrad | 0:cdf462088d13 | 592 | * \param d Destination MPI (secret part) |
markrad | 0:cdf462088d13 | 593 | * \param Q Destination point (public part) |
markrad | 0:cdf462088d13 | 594 | * \param f_rng RNG function |
markrad | 0:cdf462088d13 | 595 | * \param p_rng RNG parameter |
markrad | 0:cdf462088d13 | 596 | * |
markrad | 0:cdf462088d13 | 597 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 598 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
markrad | 0:cdf462088d13 | 599 | * |
markrad | 0:cdf462088d13 | 600 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
markrad | 0:cdf462088d13 | 601 | * in order to ease use with other structures such as |
markrad | 0:cdf462088d13 | 602 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
markrad | 0:cdf462088d13 | 603 | */ |
markrad | 0:cdf462088d13 | 604 | int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, |
markrad | 0:cdf462088d13 | 605 | const mbedtls_ecp_point *G, |
markrad | 0:cdf462088d13 | 606 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
markrad | 0:cdf462088d13 | 607 | int (*f_rng)(void *, unsigned char *, size_t), |
markrad | 0:cdf462088d13 | 608 | void *p_rng ); |
markrad | 0:cdf462088d13 | 609 | |
markrad | 0:cdf462088d13 | 610 | /** |
markrad | 0:cdf462088d13 | 611 | * \brief Generate a keypair |
markrad | 0:cdf462088d13 | 612 | * |
markrad | 0:cdf462088d13 | 613 | * \param grp ECP group |
markrad | 0:cdf462088d13 | 614 | * \param d Destination MPI (secret part) |
markrad | 0:cdf462088d13 | 615 | * \param Q Destination point (public part) |
markrad | 0:cdf462088d13 | 616 | * \param f_rng RNG function |
markrad | 0:cdf462088d13 | 617 | * \param p_rng RNG parameter |
markrad | 0:cdf462088d13 | 618 | * |
markrad | 0:cdf462088d13 | 619 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 620 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
markrad | 0:cdf462088d13 | 621 | * |
markrad | 0:cdf462088d13 | 622 | * \note Uses bare components rather than an mbedtls_ecp_keypair structure |
markrad | 0:cdf462088d13 | 623 | * in order to ease use with other structures such as |
markrad | 0:cdf462088d13 | 624 | * mbedtls_ecdh_context of mbedtls_ecdsa_context. |
markrad | 0:cdf462088d13 | 625 | */ |
markrad | 0:cdf462088d13 | 626 | int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
markrad | 0:cdf462088d13 | 627 | int (*f_rng)(void *, unsigned char *, size_t), |
markrad | 0:cdf462088d13 | 628 | void *p_rng ); |
markrad | 0:cdf462088d13 | 629 | |
markrad | 0:cdf462088d13 | 630 | /** |
markrad | 0:cdf462088d13 | 631 | * \brief Generate a keypair |
markrad | 0:cdf462088d13 | 632 | * |
markrad | 0:cdf462088d13 | 633 | * \param grp_id ECP group identifier |
markrad | 0:cdf462088d13 | 634 | * \param key Destination keypair |
markrad | 0:cdf462088d13 | 635 | * \param f_rng RNG function |
markrad | 0:cdf462088d13 | 636 | * \param p_rng RNG parameter |
markrad | 0:cdf462088d13 | 637 | * |
markrad | 0:cdf462088d13 | 638 | * \return 0 if successful, |
markrad | 0:cdf462088d13 | 639 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code |
markrad | 0:cdf462088d13 | 640 | */ |
markrad | 0:cdf462088d13 | 641 | int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
markrad | 0:cdf462088d13 | 642 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
markrad | 0:cdf462088d13 | 643 | |
markrad | 0:cdf462088d13 | 644 | /** |
markrad | 0:cdf462088d13 | 645 | * \brief Check a public-private key pair |
markrad | 0:cdf462088d13 | 646 | * |
markrad | 0:cdf462088d13 | 647 | * \param pub Keypair structure holding a public key |
markrad | 0:cdf462088d13 | 648 | * \param prv Keypair structure holding a private (plus public) key |
markrad | 0:cdf462088d13 | 649 | * |
markrad | 0:cdf462088d13 | 650 | * \return 0 if successful (keys are valid and match), or |
markrad | 0:cdf462088d13 | 651 | * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or |
markrad | 0:cdf462088d13 | 652 | * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code. |
markrad | 0:cdf462088d13 | 653 | */ |
markrad | 0:cdf462088d13 | 654 | int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ); |
markrad | 0:cdf462088d13 | 655 | |
markrad | 0:cdf462088d13 | 656 | #if defined(MBEDTLS_SELF_TEST) |
markrad | 0:cdf462088d13 | 657 | /** |
markrad | 0:cdf462088d13 | 658 | * \brief Checkup routine |
markrad | 0:cdf462088d13 | 659 | * |
markrad | 0:cdf462088d13 | 660 | * \return 0 if successful, or 1 if a test failed |
markrad | 0:cdf462088d13 | 661 | */ |
markrad | 0:cdf462088d13 | 662 | int mbedtls_ecp_self_test( int verbose ); |
markrad | 0:cdf462088d13 | 663 | #endif |
markrad | 0:cdf462088d13 | 664 | |
markrad | 0:cdf462088d13 | 665 | #ifdef __cplusplus |
markrad | 0:cdf462088d13 | 666 | } |
markrad | 0:cdf462088d13 | 667 | #endif |
markrad | 0:cdf462088d13 | 668 | |
markrad | 0:cdf462088d13 | 669 | #endif /* ecp.h */ |