Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecp.h |
| 3 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 4 | * \brief This file contains ECP definitions and functions. |
| 5 | * |
| 6 | * The Elliptic Curve over P (ECP) is defined in <em>Standards for Efficient |
| 7 | * Cryptography Group (SECG): SEC1 Elliptic Curve Cryptography</em> and |
| 8 | * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites |
| 9 | * for Transport Layer Security (TLS)</em>. |
| 10 | * |
| 11 | * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP |
| 12 | * group types. |
| 13 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 14 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 15 | |
| 16 | /* |
| 17 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 18 | * SPDX-License-Identifier: Apache-2.0 |
| 19 | * |
| 20 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 21 | * not use this file except in compliance with the License. |
| 22 | * You may obtain a copy of the License at |
| 23 | * |
| 24 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | * |
| 26 | * Unless required by applicable law or agreed to in writing, software |
| 27 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 28 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | * See the License for the specific language governing permissions and |
| 30 | * limitations under the License. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 31 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 32 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 33 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #ifndef MBEDTLS_ECP_H |
| 36 | #define MBEDTLS_ECP_H |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 37 | |
Manuel Pégourié-Gonnard | bdc9676 | 2013-10-03 11:50:39 +0200 | [diff] [blame] | 38 | #include "bignum.h" |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 41 | * ECP error codes |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 42 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
| 44 | #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 45 | #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested curve not available. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */ |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 47 | #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 48 | #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 50 | #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 51 | #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */ |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 52 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 53 | #if !defined(MBEDTLS_ECP_ALT) |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 54 | /* |
| 55 | * default mbed TLS elliptic curve arithmetic implementation |
| 56 | * |
| 57 | * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an |
| 58 | * alternative implementation for the whole module and it will replace this |
| 59 | * one.) |
| 60 | */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 61 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 62 | #ifdef __cplusplus |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 66 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 67 | * Definition of domain parameter identifiers: curve, subgroup and generator. |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 68 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 69 | * \note Only curves over prime fields are supported. |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 70 | * |
| 71 | * \warning This library does not support validation of arbitrary domain |
| 72 | * parameters. Therefore, only well-known domain parameters from trusted |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 73 | * sources should be used. See mbedtls_ecp_group_load(). |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 74 | */ |
| 75 | typedef enum |
| 76 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 77 | MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */ |
| 78 | MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for 192-bit NIST curve. */ |
| 79 | MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for 224-bit NIST curve. */ |
| 80 | MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for 256-bit NIST curve. */ |
| 81 | MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for 384-bit NIST curve. */ |
| 82 | MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for 521-bit NIST curve. */ |
| 83 | MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */ |
| 84 | MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */ |
| 85 | MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */ |
| 86 | MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for a Curve25519 curve. */ |
| 87 | MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for a Curve448 curve. */ |
| 88 | MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */ |
| 89 | MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */ |
| 90 | MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | } mbedtls_ecp_group_id; |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 92 | |
| 93 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 94 | * The number of supported curves, plus one for none. |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 95 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 96 | * \note Montgomery curves are currently excluded. |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 97 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | #define MBEDTLS_ECP_DP_MAX 12 |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 101 | * Curve information, for use by other modules. |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 102 | */ |
| 103 | typedef struct |
| 104 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 105 | mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */ |
| 106 | uint16_t tls_id; /*!< The TLS NamedCurve identifier. */ |
| 107 | uint16_t bit_size; /*!< The size of the curve in bits. */ |
| 108 | const char *name; /*!< A human-friendly name. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | } mbedtls_ecp_curve_info; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 110 | |
| 111 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 112 | * \brief The ECP point structure, in jacobian coordinates. |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 113 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 114 | * \note All functions expect and return points satisfying |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 115 | * the following condition: \p Z == 0 or \p Z == 1. Other |
| 116 | * values of \p Z are used only by internal functions. |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 117 | * The point is zero, or "at infinity", if Z == 0. |
| 118 | * Otherwise, X and Y are its standard (affine) coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 119 | */ |
| 120 | typedef struct |
| 121 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 122 | mbedtls_mpi X; /*!< The X coordinate of the ECP point. */ |
| 123 | mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */ |
| 124 | mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 125 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | mbedtls_ecp_point; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 127 | |
| 128 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 129 | * \brief The ECP group structure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 130 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 131 | * We consider two types of curve equations: |
| 132 | * <ul><li>Short Weierstrass: y^2 = x^3 + \p A x + \p B mod P |
| 133 | * (SEC1 + RFC-4492)</li> |
| 134 | * <li>Montgomery: y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)</li></ul> |
| 135 | * In both cases, the generator (G) for a prime-order subgroup is fixed. |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 136 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 137 | * For Short Weierstrass, this subgroup is the whole curve, and its |
| 138 | * cardinal is denoted by \p N. Our code requires that \p N is an odd prime. |
Manuel Pégourié-Gonnard | dd75c31 | 2014-03-31 11:55:42 +0200 | [diff] [blame] | 139 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 140 | * \note For blinding, use odd in mbedtls_ecp_mul() and prime in |
| 141 | * mbedtls_ecdsa_sign(). |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 142 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 143 | * For Montgomery curves, we do not store \p A, but (A + 2) / 4, which is |
| 144 | * the quantity used in the formulas. Additionally, \p nbits is not the |
| 145 | * size of \p N but the required size for private keys. |
| 146 | * |
| 147 | * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm. |
| 148 | * Otherwise, it must point to a function that takes an \p mbedtls_mpi in the |
| 149 | * range of 0..2^(2*pbits)-1, and transforms it in-place in an integer of |
| 150 | * little more than \p pbits, so that the integer may be efficiently brought |
| 151 | * in the 0..P-1 range by a few additions or substractions. |
| 152 | * |
| 153 | * \return \c 0 on success |
| 154 | * \return Non-zero on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 155 | */ |
| 156 | typedef struct |
| 157 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 158 | mbedtls_ecp_group_id id; /*!< An internal group identifier. */ |
| 159 | mbedtls_mpi P; /*!< A prime modulus of the base field. */ |
| 160 | mbedtls_mpi A; /*!< \p A in the equation or <code>(A + 2) / 4</code>. */ |
| 161 | mbedtls_mpi B; /*!< \p B in the equation or unused. */ |
| 162 | mbedtls_ecp_point G; /*!< The generator of the (sub)group used. */ |
| 163 | mbedtls_mpi N; /*!< The order of \p G. */ |
| 164 | size_t pbits; /*!< The number of bits in \p P.*/ |
| 165 | size_t nbits; /*!< The number of bits in \p P, or the private |
| 166 | keys. */ |
| 167 | unsigned int h; /*!< \internal 1 if the constants are static. */ |
| 168 | int (*modp)(mbedtls_mpi *); /*!< The function for fast reduction mod P.*/ |
| 169 | int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */ |
| 170 | int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */ |
| 171 | void *t_data; /*!< Unused. */ |
| 172 | mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */ |
| 173 | size_t T_size; /*!< The number for pre-computed points. */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 174 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_ecp_group; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 176 | |
| 177 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 178 | * \brief The ECP key-pair structure. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 179 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 180 | * A generic key-pair that may be used for ECDSA and fixed ECDH, for example. |
Manuel Pégourié-Gonnard | 09162dd | 2013-08-14 18:16:50 +0200 | [diff] [blame] | 181 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 182 | * \note Members are deliberately in the same order as in the |
| 183 | * #mbedtls_ecdsa_context structure. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 184 | */ |
| 185 | typedef struct |
| 186 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 187 | mbedtls_ecp_group grp; /*!< The elliptic curve and base point. */ |
| 188 | mbedtls_mpi d; /*!< Our secret value. */ |
| 189 | mbedtls_ecp_point Q; /*!< Our public value. */ |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 190 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 191 | mbedtls_ecp_keypair; |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 192 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 193 | /** |
| 194 | * \name SECTION: Module settings |
| 195 | * |
| 196 | * The configuration options you can set for this module are in this section. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 197 | * Either change them in config.h, or define them using the compiler command line. |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 198 | * \{ |
| 199 | */ |
| 200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | #if !defined(MBEDTLS_ECP_MAX_BITS) |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 202 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 203 | * The maximum size of the groups, that is, of N and P. |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 204 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 205 | #define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */ |
Paul Bakker | e1b665e | 2013-12-11 16:02:58 +0100 | [diff] [blame] | 206 | #endif |
| 207 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) |
| 209 | #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | #if !defined(MBEDTLS_ECP_WINDOW_SIZE) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 212 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 213 | * Maximum "window" size used for point multiplication. |
| 214 | * Default: 6. |
| 215 | * Minimum value: 2. Maximum value: 7. |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 216 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 218 | * points used for point multiplication. This value is directly tied to EC |
| 219 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
| 220 | * by two (if large curves are in use). |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 221 | * |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 222 | * Reduction in size may reduce speed, but larger curves are impacted first. |
| 223 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
| 224 | * w-size: 6 5 4 3 2 |
| 225 | * 521 145 141 135 120 97 |
| 226 | * 384 214 209 198 177 146 |
| 227 | * 256 320 320 303 262 226 |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 228 | |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 229 | * 224 475 475 453 398 342 |
| 230 | * 192 640 640 633 587 476 |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 231 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 232 | #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | #endif /* MBEDTLS_ECP_WINDOW_SIZE */ |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) |
Manuel Pégourié-Gonnard | 9e4191c | 2013-12-30 18:41:16 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Trade memory for speed on fixed-point multiplication. |
| 238 | * |
| 239 | * This speeds up repeated multiplication of the generator (that is, the |
| 240 | * multiplication in ECDSA signatures, and half of the multiplications in |
| 241 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
| 242 | * |
| 243 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
| 244 | * |
| 245 | * Change this value to 0 to reduce peak memory usage. |
| 246 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 247 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 249 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 250 | /* \} name SECTION: Module settings */ |
| 251 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 252 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 253 | * Point formats, from RFC 4492's enum ECPointFormat |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 254 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 255 | #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */ |
| 256 | #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */ |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 258 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 259 | * Some other constants from RFC 4492 |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 260 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 261 | #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */ |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 262 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 263 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 264 | * \brief This function retrieves the information defined in |
| 265 | * mbedtls_ecp_curve_info()for all supported curves in order |
| 266 | * of preference. |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 267 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 268 | * \return A statically allocated array. The last entry is 0. |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 269 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void ); |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 271 | |
| 272 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 273 | * \brief This function retrieves the grp_id of all supported curves |
| 274 | * in order of preference. |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 275 | * |
| 276 | * \return A statically allocated array, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | * terminated with MBEDTLS_ECP_DP_NONE. |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 278 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void ); |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 280 | |
| 281 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 282 | * \brief This function retrieves curve information from an internal |
| 283 | * group identifier. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 284 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 285 | * \param grp_id An \c MBEDTLS_ECP_DP_XXX value. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 286 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 287 | * \return The associated curve information, or NULL. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 288 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 290 | |
| 291 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 292 | * \brief This function retrieves curve information from a TLS |
| 293 | * NamedCurve value. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 294 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 295 | * \param tls_id An \c MBEDTLS_ECP_DP_XXX value. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 296 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 297 | * \return The associated curve information, or NULL. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 298 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 300 | |
| 301 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 302 | * \brief This function retrieves curve information from a |
| 303 | * human-readable name. |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 304 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 305 | * \param name The human-readable name. |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 306 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 307 | * \return The associated curve information, or NULL. |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 308 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name ); |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 310 | |
| 311 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 312 | * \brief This function initializes a point as zero. |
| 313 | * |
| 314 | * \param pt The point to initialize. |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 315 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 317 | |
| 318 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 319 | * \brief This function initializes a group to something meaningless. |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 320 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 322 | |
| 323 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 324 | * \brief This function initializes a key pair as an invalid one. |
| 325 | * |
| 326 | * \param key The key pair to initialize. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 327 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 331 | * \brief This function frees the components of a point. |
| 332 | * |
| 333 | * \param pt The point to free. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 334 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 336 | |
| 337 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 338 | * \brief This function frees the components of an ECP group. |
| 339 | * \param grp The group to free. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 340 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 342 | |
| 343 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 344 | * \brief This function frees the components of a key pair. |
| 345 | * \param key The key pair to free. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 346 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 348 | |
| 349 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 350 | * \brief This function copies the contents of point \p Q into |
| 351 | * point \p P. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 352 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 353 | * \param P The destination point. |
| 354 | * \param Q The source point. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 355 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 356 | * \return \c 0 on success. |
| 357 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 358 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 360 | |
| 361 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 362 | * \brief This function copies the contents of group \p src into |
| 363 | * group \p dst. |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 364 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 365 | * \param dst The destination group. |
| 366 | * \param src The source group. |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 367 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 368 | * \return \c 0 on success. |
| 369 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails. |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 370 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 371 | int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ); |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 372 | |
| 373 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 374 | * \brief This function sets a point to zero. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 375 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 376 | * \param pt The point to set. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 377 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 378 | * \return \c 0 on success. |
| 379 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation fails. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 380 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 382 | |
| 383 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 384 | * \brief This function checks if a point is zero. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 385 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 386 | * \param pt The point to test. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 387 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 388 | * \return \c 1 if point is zero. |
| 389 | * \return \c 0 if point is non-zero. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 390 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 392 | |
| 393 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 394 | * \brief This function compares two points. |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 395 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 396 | * \note This assumes that the points are normalized. Otherwise, |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 397 | * they may compare as "not equal" even if they are. |
| 398 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 399 | * \param P The first point to compare. |
| 400 | * \param Q The second point to compare. |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 401 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 402 | * \return \c 0 if the points are equal. |
| 403 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal. |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 404 | */ |
| 405 | int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, |
| 406 | const mbedtls_ecp_point *Q ); |
| 407 | |
| 408 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 409 | * \brief This function imports a non-zero point from two ASCII |
| 410 | * strings. |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 411 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 412 | * \param P The destination point. |
| 413 | * \param radix The numeric base of the input. |
| 414 | * \param x The first affine coordinate, as a null-terminated string. |
| 415 | * \param y The second affine coordinate, as a null-terminated string. |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 416 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 417 | * \return \c 0 on success. |
| 418 | * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure. |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 419 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 421 | const char *x, const char *y ); |
| 422 | |
| 423 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 424 | * \brief This function exports a point into unsigned binary data. |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 425 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 426 | * \param grp The group to which the point should belong. |
| 427 | * \param P The point to export. |
| 428 | * \param format The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro. |
| 429 | * \param olen The length of the output. |
| 430 | * \param buf The output buffer. |
| 431 | * \param buflen The length of the output buffer. |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 432 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 433 | * \return \c 0 on success. |
| 434 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA |
| 435 | * or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 436 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 437 | int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 438 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 439 | unsigned char *buf, size_t buflen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 440 | |
| 441 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 442 | * \brief This function imports a point from unsigned binary data. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 443 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 444 | * \note This function does not check that the point actually |
| 445 | * belongs to the given group, see mbedtls_ecp_check_pubkey() |
| 446 | * for that. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 447 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 448 | * \param grp The group to which the point should belong. |
| 449 | * \param P The point to import. |
| 450 | * \param buf The input buffer. |
| 451 | * \param ilen The length of the input. |
| 452 | * |
| 453 | * \return \c 0 on success. |
| 454 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
| 455 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. |
| 456 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 457 | * is not implemented. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 458 | * |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 459 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 460 | int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 461 | const unsigned char *buf, size_t ilen ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 462 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 463 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 464 | * \brief This function imports a point from a TLS ECPoint record. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 465 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 466 | * \note On function return, \p buf is updated to point to immediately |
| 467 | * after the ECPoint. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 468 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 469 | * \param grp The ECP group used. |
| 470 | * \param pt The destination point. |
| 471 | * \param buf The address of the pointer to the start of input buffer. |
| 472 | * \param len The length of the buffer. |
Manuel Pégourié-Gonnard | 150c4f6 | 2014-11-21 09:14:52 +0100 | [diff] [blame] | 473 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 474 | * \return \c 0 on success. |
| 475 | * \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization failed. |
| 476 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 477 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 478 | int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 479 | const unsigned char **buf, size_t len ); |
| 480 | |
| 481 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 482 | * \brief This function exports a point as a TLS ECPoint record. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 483 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 484 | * \param grp The ECP group used. |
| 485 | * \param pt The point to export. |
| 486 | * \param format The export format. |
| 487 | * \param olen The length of data written. |
| 488 | * \param buf The Buffer to write to. |
| 489 | * \param blen The length of the Buffer. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 490 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 491 | * \return \c 0 on success. |
| 492 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or |
| 493 | * #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 494 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 496 | int format, size_t *olen, |
| 497 | unsigned char *buf, size_t blen ); |
| 498 | |
| 499 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 500 | * \brief This function sets a group using well-known domain parameters. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 501 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 502 | * \note The index should be a value of the NamedCurve enum, |
| 503 | * as defined in <em>RFC-4492: Elliptic Curve Cryptography |
| 504 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>, |
| 505 | * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 506 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 507 | * \param grp The destination group. |
| 508 | * \param id The index in the list of well-known domain parameters. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 509 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 510 | * \return \c 0 on success, |
| 511 | * \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization fails. |
| 512 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups. |
| 513 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 514 | */ |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 515 | int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 516 | |
| 517 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 518 | * \brief This function sets a group from a TLS ECParameters record. |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 519 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 520 | * \note \p buf is updated to point right after ECParameters on exit. |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 521 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 522 | * \param grp The destination group. |
| 523 | * \param buf The address of the pointer to the start of input buffer. |
| 524 | * \param len The length of the buffer. |
Manuel Pégourié-Gonnard | 150c4f6 | 2014-11-21 09:14:52 +0100 | [diff] [blame] | 525 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 526 | * \return \c 0 on success. |
| 527 | * \return An \c MBEDTLS_ERR_MPI_XXX error code if initialization fails. |
| 528 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 529 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len ); |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 531 | |
| 532 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 533 | * \brief This function writes the TLS ECParameters record for a group. |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 534 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 535 | * \param grp The ECP group used. |
| 536 | * \param olen The number of Bytes written. |
| 537 | * \param buf The buffer to write to. |
| 538 | * \param blen The length of the buffer. |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 539 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 540 | * \return \c 0 on success. |
| 541 | * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 542 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 543 | int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 544 | unsigned char *buf, size_t blen ); |
| 545 | |
| 546 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 547 | * \brief This function performs multiplication of a point by |
| 548 | * an integer: \p R = \p m * \p P. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 549 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 550 | * It is not thread-safe to use same group in multiple threads. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 551 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 552 | * \note To prevent timing attacks, this function |
| 553 | * executes the exact same sequence of base-field |
| 554 | * operations for any valid \p m. It avoids any if-branch or |
| 555 | * array index depending on the value of \p m. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 556 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 557 | * \note If \p f_rng is not NULL, it is used to randomize |
| 558 | * intermediate results to prevent potential timing attacks |
| 559 | * targeting these results. We recommend always providing |
| 560 | * a non-NULL \p f_rng. The overhead is negligible. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 561 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 562 | * \param grp The ECP group. |
| 563 | * \param R The destination point. |
| 564 | * \param m The integer by which to multiply. |
| 565 | * \param P The point to multiply. |
| 566 | * \param f_rng The RNG function. |
| 567 | * \param p_rng The RNG context. |
| 568 | * |
| 569 | * \return \c 0 on success. |
| 570 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid privkey, |
| 571 | * or \p P is not a valid pubkey. |
| 572 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 573 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 575 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | 09ceaf4 | 2013-11-20 23:06:14 +0100 | [diff] [blame] | 576 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 577 | |
| 578 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 579 | * \brief This function performs multiplication and addition of two |
| 580 | * points by integers: \p R = \p m * \p P + \p n * \p Q |
| 581 | |
| 582 | * It is not thread-safe to use same group in multiple threads. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 583 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 584 | * \note In contrast to mbedtls_ecp_mul(), this function does not |
| 585 | * guarantee a constant execution flow and timing. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 586 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 587 | * \param grp The ECP group. |
| 588 | * \param R The destination point. |
| 589 | * \param m The integer by which to multiply \p P. |
| 590 | * \param P The point to multiply by \p m. |
| 591 | * \param n The integer by which to multiply \p Q. |
| 592 | * \param Q The point to be multiplied by \p n. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 593 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 594 | * \return \c 0 on success. |
| 595 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not |
| 596 | * valid private keys, or \p P or \p Q are not valid public |
| 597 | * keys. |
| 598 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 599 | */ |
| 600 | int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 601 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 602 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q ); |
| 603 | |
| 604 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 605 | * \brief This function checks that a point is a valid public key |
| 606 | * on this curve. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 607 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 608 | * It only checks that the point is non-zero, has |
| 609 | * valid coordinates and lies on the curve. It does not verify |
| 610 | * that it is indeed a multiple of \p G. This additional |
| 611 | * check is computationally more expensive, is not required |
| 612 | * by standards, and should not be necessary if the group |
| 613 | * used has a small cofactor. In particular, it is useless for |
| 614 | * the NIST groups which all have a cofactor of 1. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 615 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 616 | * \note This function uses bare components rather than an |
| 617 | * mbedtls_ecp_keypair() structure, to ease use with other |
| 618 | * structures. For example, mbedtls_ecdh_context() or |
| 619 | * mbedtls_ecdsa_context(). |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 620 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 621 | * \param grp The curve or group the point should belong to. |
| 622 | * \param pt The point to check. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 623 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 624 | * \return \c 0 if the point is a valid public key. |
| 625 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 626 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 628 | |
| 629 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 630 | * \brief This function checks that an \p mbedtls_mpi is a valid private |
| 631 | * key for this curve. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 632 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 633 | * \note This function uses bare components rather than an |
| 634 | * mbedtls_ecp_keypair() structure to ease use with other |
| 635 | * structures such as mbedtls_ecdh_context() or |
| 636 | * mbedtls_ecdsa_context(). |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 637 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 638 | * \param grp The group used. |
| 639 | * \param d The integer to check. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 640 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 641 | * \return \c 0 if the point is a valid private key. |
| 642 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY otherwise. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 643 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 645 | |
| 646 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 647 | * \brief This function generates a keypair with a configurable base |
| 648 | * point. |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 649 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 650 | * \note This function uses bare components rather than an |
| 651 | * mbedtls_ecp_keypair() structure to ease use with other |
| 652 | * structures such as mbedtls_ecdh_context() or |
| 653 | * mbedtls_ecdsa_context(). |
| 654 | * |
| 655 | * \param grp The ECP group. |
| 656 | * \param G The chosen base point. |
| 657 | * \param d The destination MPI (secret part). |
| 658 | * \param Q The destination point (public part). |
| 659 | * \param f_rng The RNG function. |
| 660 | * \param p_rng The RNG context. |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 661 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 662 | * \return \c 0 on success. |
| 663 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 664 | * on failure. |
| 665 | */ |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 666 | int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, |
| 667 | const mbedtls_ecp_point *G, |
| 668 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 669 | int (*f_rng)(void *, unsigned char *, size_t), |
| 670 | void *p_rng ); |
| 671 | |
| 672 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 673 | * \brief This function generates a keypair. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 674 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 675 | * \note This function uses bare components rather than an |
| 676 | * mbedtls_ecp_keypair() structure to ease use with other |
| 677 | * structures such as mbedtls_ecdh_context() or |
| 678 | * mbedtls_ecdsa_context(). |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 679 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 680 | * \param grp The ECP group. |
| 681 | * \param d The destination MPI (secret part). |
| 682 | * \param Q The destination point (public part). |
| 683 | * \param f_rng The RNG function. |
| 684 | * \param p_rng The RNG context. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 685 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 686 | * \return \c 0 on success. |
| 687 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 688 | * on failure. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 689 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 691 | int (*f_rng)(void *, unsigned char *, size_t), |
| 692 | void *p_rng ); |
| 693 | |
| 694 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 695 | * \brief This function generates a key. |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 696 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 697 | * \param grp_id The ECP group identifier. |
| 698 | * \param key The destination key. |
| 699 | * \param f_rng The RNG function. |
| 700 | * \param p_rng The RNG context. |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 701 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 702 | * \return \c 0 on success. |
| 703 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 704 | * on failure. |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 705 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 707 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 708 | |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 709 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 710 | * \brief This function checks a public-private key pair. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 711 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 712 | * \param pub The keypair structure holding the public key. |
| 713 | * \param prv The keypair structure holding the private key. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 714 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 715 | * \note The both are keypairs, and may optionally hold the corresponding other key, but the public key passed in thee pub is checked against the private key passed in prv. |
| 716 | * |
| 717 | * \return \c 0 on success - the keys are valid and match. |
| 718 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or an \c |
| 719 | * MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX |
| 720 | * error code on failure. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 721 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | #if defined(MBEDTLS_SELF_TEST) |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 725 | |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 726 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 727 | * \brief The ECP checkup routine. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 728 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame^] | 729 | * \return \c 0 on success. |
| 730 | * \return \c 1 on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 731 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | int mbedtls_ecp_self_test( int verbose ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 733 | |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 734 | #endif /* MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 735 | |
| 736 | #ifdef __cplusplus |
| 737 | } |
| 738 | #endif |
| 739 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 740 | #else /* MBEDTLS_ECP_ALT */ |
| 741 | #include "ecp_alt.h" |
| 742 | #endif /* MBEDTLS_ECP_ALT */ |
| 743 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 744 | #endif /* ecp.h */ |