Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file ecp.h |
| 3 | * |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 4 | * \brief This file provides an API for Elliptic Curves over GF(P) (ECP). |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 5 | * |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 6 | * The use of ECP in cryptography and TLS is defined in |
| 7 | * <em>Standards for Efficient Cryptography Group (SECG): SEC1 |
| 8 | * Elliptic Curve Cryptography</em> and |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 9 | * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites |
| 10 | * for Transport Layer Security (TLS)</em>. |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 11 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 12 | * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP |
| 13 | * group types. |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 14 | * |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 15 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 16 | |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 17 | /* |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 18 | * 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] | 19 | * SPDX-License-Identifier: Apache-2.0 |
| 20 | * |
| 21 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 22 | * not use this file except in compliance with the License. |
| 23 | * You may obtain a copy of the License at |
| 24 | * |
| 25 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | * |
| 27 | * Unless required by applicable law or agreed to in writing, software |
| 28 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 29 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 30 | * See the License for the specific language governing permissions and |
| 31 | * limitations under the License. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 32 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 33 | * 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] | 34 | */ |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #ifndef MBEDTLS_ECP_H |
| 37 | #define MBEDTLS_ECP_H |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 38 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 39 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 40 | #include "mbedtls/config.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 41 | #else |
| 42 | #include MBEDTLS_CONFIG_FILE |
| 43 | #endif |
| 44 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 45 | #include "mbedtls/bignum.h" |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 46 | |
| 47 | /* |
Manuel Pégourié-Gonnard | 7cfcea3 | 2012-11-05 10:06:12 +0100 | [diff] [blame] | 48 | * ECP error codes |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 49 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */ |
| 51 | #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */ |
Rose Zadik | d353405 | 2018-04-23 16:12:42 +0100 | [diff] [blame] | 52 | #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #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] | 54 | #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 55 | #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] | 56 | #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */ |
Gilles Peskine | 5114d3e | 2018-03-30 07:12:15 +0200 | [diff] [blame] | 57 | #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 58 | |
| 59 | /* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 60 | #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 62 | #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00 /**< Operation in progress, call again with the same parameters to continue. */ |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 63 | |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 64 | /* Flags indicating whether to include code that is specific to certain |
| 65 | * types of curves. These flags are for internal library use only. */ |
| 66 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \ |
| 67 | defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ |
| 68 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ |
| 69 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \ |
| 70 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \ |
| 71 | defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \ |
| 72 | defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \ |
| 73 | defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || \ |
| 74 | defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \ |
| 75 | defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \ |
| 76 | defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
| 77 | #define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED |
| 78 | #endif |
| 79 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \ |
| 80 | defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 81 | #define MBEDTLS_ECP_MONTGOMERY_ENABLED |
| 82 | #endif |
| 83 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 84 | #ifdef __cplusplus |
| 85 | extern "C" { |
| 86 | #endif |
| 87 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 88 | /** |
Rose Zadik | d76ac58 | 2018-04-23 06:29:34 +0100 | [diff] [blame] | 89 | * Domain-parameter identifiers: curve, subgroup, and generator. |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 90 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 91 | * \note Only curves over prime fields are supported. |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 92 | * |
| 93 | * \warning This library does not support validation of arbitrary domain |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 94 | * parameters. Therefore, only standardized domain parameters from trusted |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 95 | * sources should be used. See mbedtls_ecp_group_load(). |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 96 | */ |
Gilles Peskine | 963a207 | 2018-09-14 18:31:30 +0200 | [diff] [blame^] | 97 | /* Note: when adding a new curve: |
| 98 | * - Add it at the end of this enum, otherwise you'll break the ABI by |
| 99 | * changing the numerical value for existing curves. |
| 100 | * - Increment MBEDTLS_ECP_DP_MAX below. |
| 101 | * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to |
| 102 | * config.h. |
| 103 | * - List the curve as a dependency of MBEDTLS_ECP_C and |
| 104 | * MBEDTLS_ECDSA_C if supported in check_config.h. |
| 105 | * - Add the curve to the appropriate curve type macro |
| 106 | * MBEDTLS_ECP_yyy_ENABLED above. |
| 107 | * - Add the necessary definitions to ecp_curves.c. |
| 108 | * - Add the curve to the ecp_supported_curves array in ecp.c. |
| 109 | * - Add the curve to applicable profiles in x509_crt.c if applicable. |
| 110 | */ |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 111 | typedef enum |
| 112 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 113 | MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */ |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 114 | MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */ |
| 115 | MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */ |
| 116 | MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */ |
| 117 | MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */ |
| 118 | MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 119 | MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */ |
| 120 | MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */ |
| 121 | MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */ |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 122 | MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 123 | MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */ |
| 124 | MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */ |
| 125 | MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */ |
Jaeden Amero | fe0669f | 2018-04-27 17:43:32 +0100 | [diff] [blame] | 126 | MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | } mbedtls_ecp_group_id; |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 130 | * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE. |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 131 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 132 | * \note Montgomery curves are currently excluded. |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 133 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | #define MBEDTLS_ECP_DP_MAX 12 |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 135 | |
Janos Follath | df9295b | 2019-02-26 12:36:52 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Curve types |
| 138 | */ |
| 139 | typedef enum |
| 140 | { |
| 141 | MBEDTLS_ECP_TYPE_NONE = 0, |
| 142 | MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */ |
| 143 | MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */ |
| 144 | } mbedtls_ecp_curve_type; |
| 145 | |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 146 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 147 | * Curve information, for use by other modules. |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 148 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 149 | typedef struct mbedtls_ecp_curve_info |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 150 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 151 | mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */ |
| 152 | uint16_t tls_id; /*!< The TLS NamedCurve identifier. */ |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 153 | uint16_t bit_size; /*!< The curve size in bits. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 154 | const char *name; /*!< A human-friendly name. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | } mbedtls_ecp_curve_info; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 156 | |
| 157 | /** |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 158 | * \brief The ECP point structure, in Jacobian coordinates. |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 159 | * |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 160 | * \note All functions expect and return points satisfying |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 161 | * the following condition: <code>Z == 0</code> or |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 162 | * <code>Z == 1</code>. Other values of \p Z are |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 163 | * used only by internal functions. |
| 164 | * The point is zero, or "at infinity", if <code>Z == 0</code>. |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 165 | * Otherwise, \p X and \p Y are its standard (affine) |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 166 | * coordinates. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 167 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 168 | typedef struct mbedtls_ecp_point |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 169 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 170 | mbedtls_mpi X; /*!< The X coordinate of the ECP point. */ |
| 171 | mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */ |
| 172 | mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 173 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | mbedtls_ecp_point; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 175 | |
| 176 | #if !defined(MBEDTLS_ECP_ALT) |
| 177 | /* |
| 178 | * default mbed TLS elliptic curve arithmetic implementation |
| 179 | * |
| 180 | * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an |
| 181 | * alternative implementation for the whole module and it will replace this |
| 182 | * one.) |
| 183 | */ |
| 184 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 185 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 186 | * \brief The ECP group structure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 187 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 188 | * We consider two types of curve equations: |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 189 | * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code> |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 190 | * (SEC1 + RFC-4492)</li> |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 191 | * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519, |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 192 | * Curve448)</li></ul> |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 193 | * In both cases, the generator (\p G) for a prime-order subgroup is fixed. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 194 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 195 | * For Short Weierstrass, this subgroup is the whole curve, and its |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 196 | * cardinality is denoted by \p N. Our code requires that \p N is an |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 197 | * odd prime as mbedtls_ecp_mul() requires an odd number, and |
| 198 | * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 199 | * |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 200 | * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>, |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 201 | * which is the quantity used in the formulas. Additionally, \p nbits is |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 202 | * not the size of \p N but the required size for private keys. |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 203 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 204 | * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm. |
| 205 | * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the |
| 206 | * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer |
| 207 | * which is congruent mod \p P to the given MPI, and is close enough to \p pbits |
| 208 | * in size, so that it may be efficiently brought in the 0..P-1 range by a few |
| 209 | * additions or subtractions. Therefore, it is only an approximative modular |
| 210 | * reduction. It must return 0 on success and non-zero on failure. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 211 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 212 | * \note Alternative implementations must keep the group IDs distinct. If |
| 213 | * two group structures have the same ID, then they must be |
| 214 | * identical. |
| 215 | * |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 216 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 217 | typedef struct mbedtls_ecp_group |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 218 | { |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 219 | mbedtls_ecp_group_id id; /*!< An internal group identifier. */ |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 220 | mbedtls_mpi P; /*!< The prime modulus of the base field. */ |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 221 | mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For |
| 222 | Montgomery curves: <code>(A + 2) / 4</code>. */ |
| 223 | mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation. |
| 224 | For Montgomery curves: unused. */ |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 225 | mbedtls_ecp_point G; /*!< The generator of the subgroup used. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 226 | mbedtls_mpi N; /*!< The order of \p G. */ |
| 227 | size_t pbits; /*!< The number of bits in \p P.*/ |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 228 | size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P. |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 229 | For Montgomery curves: the number of bits in the |
Rose Zadik | d76ac58 | 2018-04-23 06:29:34 +0100 | [diff] [blame] | 230 | private keys. */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 231 | unsigned int h; /*!< \internal 1 if the constants are static. */ |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 232 | int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 233 | mod \p P (see above).*/ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 234 | int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */ |
| 235 | int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */ |
| 236 | void *t_data; /*!< Unused. */ |
| 237 | mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */ |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 238 | size_t T_size; /*!< The number of pre-computed points. */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 239 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | mbedtls_ecp_group; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 241 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 242 | /** |
| 243 | * \name SECTION: Module settings |
| 244 | * |
| 245 | * The configuration options you can set for this module are in this section. |
| 246 | * Either change them in config.h, or define them using the compiler command line. |
| 247 | * \{ |
| 248 | */ |
| 249 | |
| 250 | #if !defined(MBEDTLS_ECP_MAX_BITS) |
| 251 | /** |
| 252 | * The maximum size of the groups, that is, of \c N and \c P. |
| 253 | */ |
| 254 | #define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */ |
| 255 | #endif |
| 256 | |
| 257 | #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) |
| 258 | #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) |
| 259 | |
| 260 | #if !defined(MBEDTLS_ECP_WINDOW_SIZE) |
| 261 | /* |
| 262 | * Maximum "window" size used for point multiplication. |
| 263 | * Default: 6. |
| 264 | * Minimum value: 2. Maximum value: 7. |
| 265 | * |
| 266 | * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) |
| 267 | * points used for point multiplication. This value is directly tied to EC |
| 268 | * peak memory usage, so decreasing it by one should roughly cut memory usage |
| 269 | * by two (if large curves are in use). |
| 270 | * |
| 271 | * Reduction in size may reduce speed, but larger curves are impacted first. |
| 272 | * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): |
| 273 | * w-size: 6 5 4 3 2 |
| 274 | * 521 145 141 135 120 97 |
| 275 | * 384 214 209 198 177 146 |
| 276 | * 256 320 320 303 262 226 |
| 277 | * 224 475 475 453 398 342 |
| 278 | * 192 640 640 633 587 476 |
| 279 | */ |
| 280 | #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */ |
| 281 | #endif /* MBEDTLS_ECP_WINDOW_SIZE */ |
| 282 | |
| 283 | #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) |
| 284 | /* |
| 285 | * Trade memory for speed on fixed-point multiplication. |
| 286 | * |
| 287 | * This speeds up repeated multiplication of the generator (that is, the |
| 288 | * multiplication in ECDSA signatures, and half of the multiplications in |
| 289 | * ECDSA verification and ECDHE) by a factor roughly 3 to 4. |
| 290 | * |
| 291 | * The cost is increasing EC peak memory usage by a factor roughly 2. |
| 292 | * |
| 293 | * Change this value to 0 to reduce peak memory usage. |
| 294 | */ |
| 295 | #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ |
| 296 | #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ |
| 297 | |
| 298 | /* \} name SECTION: Module settings */ |
| 299 | |
| 300 | #else /* MBEDTLS_ECP_ALT */ |
| 301 | #include "ecp_alt.h" |
| 302 | #endif /* MBEDTLS_ECP_ALT */ |
| 303 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 304 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | b5a50e7 | 2017-04-20 16:06:13 +0200 | [diff] [blame] | 305 | |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 306 | /** |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 307 | * \brief Internal restart context for multiplication |
| 308 | * |
| 309 | * \note Opaque struct |
| 310 | */ |
| 311 | typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx; |
| 312 | |
| 313 | /** |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 314 | * \brief Internal restart context for ecp_muladd() |
| 315 | * |
| 316 | * \note Opaque struct |
| 317 | */ |
| 318 | typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx; |
| 319 | |
| 320 | /** |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 321 | * \brief General context for resuming ECC operations |
| 322 | */ |
| 323 | typedef struct |
| 324 | { |
Manuel Pégourié-Gonnard | 646393b | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 325 | unsigned ops_done; /*!< current ops count */ |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 326 | unsigned depth; /*!< call depth (0 = top-level) */ |
Manuel Pégourié-Gonnard | 646393b | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 327 | mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */ |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 328 | mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */ |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 329 | } mbedtls_ecp_restart_ctx; |
Manuel Pégourié-Gonnard | b5a50e7 | 2017-04-20 16:06:13 +0200 | [diff] [blame] | 330 | |
Manuel Pégourié-Gonnard | c751148 | 2017-04-20 16:31:00 +0200 | [diff] [blame] | 331 | /* |
| 332 | * Operation counts for restartable functions |
| 333 | */ |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 334 | #define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */ |
Manuel Pégourié-Gonnard | c751148 | 2017-04-20 16:31:00 +0200 | [diff] [blame] | 335 | #define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */ |
| 336 | #define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */ |
| 337 | #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */ |
| 338 | |
| 339 | /** |
| 340 | * \brief Internal; for restartable functions in other modules. |
| 341 | * Check and update basic ops budget. |
| 342 | * |
| 343 | * \param grp Group structure |
| 344 | * \param rs_ctx Restart context |
| 345 | * \param ops Number of basic ops to do |
| 346 | * |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 347 | * \return \c 0 if doing \p ops basic ops is still allowed, |
| 348 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise. |
Manuel Pégourié-Gonnard | c751148 | 2017-04-20 16:31:00 +0200 | [diff] [blame] | 349 | */ |
| 350 | int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp, |
| 351 | mbedtls_ecp_restart_ctx *rs_ctx, |
| 352 | unsigned ops ); |
| 353 | |
| 354 | /* Utility macro for checking and updating ops budget */ |
Hanno Becker | abdf67e | 2018-10-26 13:28:32 +0100 | [diff] [blame] | 355 | #define MBEDTLS_ECP_BUDGET( ops ) \ |
| 356 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \ |
| 357 | (unsigned) (ops) ) ); |
Manuel Pégourié-Gonnard | c751148 | 2017-04-20 16:31:00 +0200 | [diff] [blame] | 358 | |
Manuel Pégourié-Gonnard | b5a50e7 | 2017-04-20 16:06:13 +0200 | [diff] [blame] | 359 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 360 | |
Manuel Pégourié-Gonnard | c751148 | 2017-04-20 16:31:00 +0200 | [diff] [blame] | 361 | #define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */ |
| 362 | |
Manuel Pégourié-Gonnard | b5a50e7 | 2017-04-20 16:06:13 +0200 | [diff] [blame] | 363 | /* We want to declare restartable versions of existing functions anyway */ |
| 364 | typedef void mbedtls_ecp_restart_ctx; |
| 365 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 366 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 367 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 368 | /** |
Jaeden Amero | 8945343 | 2018-04-24 17:15:38 +0100 | [diff] [blame] | 369 | * \brief The ECP key-pair structure. |
Ron Eldor | 05d0e51 | 2018-04-16 17:40:04 +0300 | [diff] [blame] | 370 | * |
Jaeden Amero | 8945343 | 2018-04-24 17:15:38 +0100 | [diff] [blame] | 371 | * A generic key-pair that may be used for ECDSA and fixed ECDH, for example. |
Ron Eldor | 05d0e51 | 2018-04-16 17:40:04 +0300 | [diff] [blame] | 372 | * |
Jaeden Amero | 8945343 | 2018-04-24 17:15:38 +0100 | [diff] [blame] | 373 | * \note Members are deliberately in the same order as in the |
| 374 | * ::mbedtls_ecdsa_context structure. |
Ron Eldor | 05d0e51 | 2018-04-16 17:40:04 +0300 | [diff] [blame] | 375 | */ |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 376 | typedef struct mbedtls_ecp_keypair |
Ron Eldor | 05d0e51 | 2018-04-16 17:40:04 +0300 | [diff] [blame] | 377 | { |
| 378 | mbedtls_ecp_group grp; /*!< Elliptic curve and base point */ |
| 379 | mbedtls_mpi d; /*!< our secret value */ |
| 380 | mbedtls_ecp_point Q; /*!< our public value */ |
| 381 | } |
| 382 | mbedtls_ecp_keypair; |
| 383 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 384 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 385 | * Point formats, from RFC 4492's enum ECPointFormat |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 386 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 387 | #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */ |
| 388 | #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */ |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 389 | |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 390 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 391 | * Some other constants from RFC 4492 |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 392 | */ |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 393 | #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] | 394 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 395 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 396 | /** |
| 397 | * \brief Set the maximum number of basic operations done in a row. |
| 398 | * |
| 399 | * If more operations are needed to complete a computation, |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 400 | * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the |
Manuel Pégourié-Gonnard | 8467e68 | 2017-04-20 09:47:06 +0200 | [diff] [blame] | 401 | * function performing the computation. It is then the |
| 402 | * caller's responsibility to either call again with the same |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 403 | * parameters until it returns 0 or an error code; or to free |
Manuel Pégourié-Gonnard | 8467e68 | 2017-04-20 09:47:06 +0200 | [diff] [blame] | 404 | * the restart context if the operation is to be aborted. |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 405 | * |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 406 | * It is strictly required that all input parameters and the |
| 407 | * restart context be the same on successive calls for the |
| 408 | * same operation, but output parameters need not be the |
| 409 | * same; they must not be used until the function finally |
| 410 | * returns 0. |
| 411 | * |
Gilles Peskine | 1174db5 | 2020-02-26 19:52:06 +0100 | [diff] [blame] | 412 | * This only applies to functions whose documentation |
| 413 | * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or |
| 414 | * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the |
| 415 | * SSL module). For functions that accept a "restart context" |
| 416 | * argument, passing NULL disables restart and makes the |
| 417 | * function equivalent to the function with the same name |
Manuel Pégourié-Gonnard | 32df911 | 2018-10-15 13:29:21 +0200 | [diff] [blame] | 418 | * with \c _restartable removed. For functions in the ECDH |
Gilles Peskine | 1174db5 | 2020-02-26 19:52:06 +0100 | [diff] [blame] | 419 | * module, restart is disabled unless the function accepts |
| 420 | * an "ECDH context" argument and |
| 421 | * mbedtls_ecdh_enable_restart() was previously called on |
| 422 | * that context. For function in the SSL module, restart is |
| 423 | * only enabled for specific sides and key exchanges |
| 424 | * (currently only for clients and ECDHE-ECDSA). |
Manuel Pégourié-Gonnard | 8f28add | 2017-04-19 10:20:49 +0200 | [diff] [blame] | 425 | * |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 426 | * \param max_ops Maximum number of basic operations done in a row. |
| 427 | * Default: 0 (unlimited). |
| 428 | * Lower (non-zero) values mean ECC functions will block for |
| 429 | * a lesser maximum amount of time. |
| 430 | * |
Manuel Pégourié-Gonnard | e685449 | 2017-03-20 14:35:19 +0100 | [diff] [blame] | 431 | * \note A "basic operation" is defined as a rough equivalent of a |
| 432 | * multiplication in GF(p) for the NIST P-256 curve. |
| 433 | * As an indication, with default settings, a scalar |
| 434 | * multiplication (full run of \c mbedtls_ecp_mul()) is: |
| 435 | * - about 3300 basic operations for P-256 |
| 436 | * - about 9400 basic operations for P-384 |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 437 | * |
Manuel Pégourié-Gonnard | e58f65a | 2017-03-20 14:59:54 +0100 | [diff] [blame] | 438 | * \note Very low values are not always respected: sometimes |
Manuel Pégourié-Gonnard | 1c678e0 | 2017-03-20 13:39:39 +0100 | [diff] [blame] | 439 | * functions need to block for a minimum number of |
| 440 | * operations, and will do so even if max_ops is set to a |
| 441 | * lower value. That minimum depends on the curve size, and |
| 442 | * can be made lower by decreasing the value of |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 443 | * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the |
| 444 | * lowest effective value for various curves and values of |
| 445 | * that parameter (w for short): |
| 446 | * w=6 w=5 w=4 w=3 w=2 |
| 447 | * P-256 208 208 160 136 124 |
| 448 | * P-384 682 416 320 272 248 |
| 449 | * P-521 1364 832 640 544 496 |
Manuel Pégourié-Gonnard | e58f65a | 2017-03-20 14:59:54 +0100 | [diff] [blame] | 450 | * |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 451 | * \note This setting is currently ignored by Curve25519. |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 452 | */ |
| 453 | void mbedtls_ecp_set_max_ops( unsigned max_ops ); |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * \brief Check if restart is enabled (max_ops != 0) |
| 457 | * |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 458 | * \return \c 0 if \c max_ops == 0 (restart disabled) |
| 459 | * \return \c 1 otherwise (restart enabled) |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 460 | */ |
Manuel Pégourié-Gonnard | b843b15 | 2018-10-16 10:41:31 +0200 | [diff] [blame] | 461 | int mbedtls_ecp_restart_is_enabled( void ); |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 462 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 463 | |
Janos Follath | df9295b | 2019-02-26 12:36:52 +0000 | [diff] [blame] | 464 | /* |
| 465 | * Get the type of a curve |
| 466 | */ |
| 467 | mbedtls_ecp_curve_type mbedtls_ecp_get_type( const mbedtls_ecp_group *grp ); |
| 468 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 469 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 470 | * \brief This function retrieves the information defined in |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 471 | * mbedtls_ecp_curve_info() for all supported curves in order |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 472 | * of preference. |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 473 | * |
Gilles Peskine | d2085f5 | 2019-02-11 18:43:55 +0100 | [diff] [blame] | 474 | * \note This function returns information about all curves |
| 475 | * supported by the library. Some curves may not be |
| 476 | * supported for all algorithms. Call mbedtls_ecdh_can_do() |
| 477 | * or mbedtls_ecdsa_can_do() to check if a curve is |
| 478 | * supported for ECDH or ECDSA. |
| 479 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 480 | * \return A statically allocated array. The last entry is 0. |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 481 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void ); |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 483 | |
| 484 | /** |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 485 | * \brief This function retrieves the list of internal group |
| 486 | * identifiers of all supported curves in the order of |
| 487 | * preference. |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 488 | * |
Gilles Peskine | d2085f5 | 2019-02-11 18:43:55 +0100 | [diff] [blame] | 489 | * \note This function returns information about all curves |
| 490 | * supported by the library. Some curves may not be |
| 491 | * supported for all algorithms. Call mbedtls_ecdh_can_do() |
| 492 | * or mbedtls_ecdsa_can_do() to check if a curve is |
| 493 | * supported for ECDH or ECDSA. |
| 494 | * |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 495 | * \return A statically allocated array, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | * terminated with MBEDTLS_ECP_DP_NONE. |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 497 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | 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] | 499 | |
| 500 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 501 | * \brief This function retrieves curve information from an internal |
| 502 | * group identifier. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 503 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 504 | * \param grp_id An \c MBEDTLS_ECP_DP_XXX value. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 505 | * |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 506 | * \return The associated curve information on success. |
| 507 | * \return NULL on failure. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 508 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | 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] | 510 | |
| 511 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 512 | * \brief This function retrieves curve information from a TLS |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 513 | * NamedCurve value. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 514 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 515 | * \param tls_id An \c MBEDTLS_ECP_DP_XXX value. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 516 | * |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 517 | * \return The associated curve information on success. |
| 518 | * \return NULL on failure. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 519 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | 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] | 521 | |
| 522 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 523 | * \brief This function retrieves curve information from a |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 524 | * human-readable name. |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 525 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 526 | * \param name The human-readable name. |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 527 | * |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 528 | * \return The associated curve information on success. |
| 529 | * \return NULL on failure. |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 530 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | 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] | 532 | |
| 533 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 534 | * \brief This function initializes a point as zero. |
| 535 | * |
| 536 | * \param pt The point to initialize. |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 537 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 539 | |
| 540 | /** |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 541 | * \brief This function initializes an ECP group context |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 542 | * without loading any domain parameters. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 543 | * |
| 544 | * \note After this function is called, domain parameters |
| 545 | * for various ECP groups can be loaded through the |
Gilles Peskine | 13cf3ec | 2018-11-07 22:06:48 +0100 | [diff] [blame] | 546 | * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group() |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 547 | * functions. |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 548 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 550 | |
| 551 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 552 | * \brief This function initializes a key pair as an invalid one. |
| 553 | * |
| 554 | * \param key The key pair to initialize. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 555 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 557 | |
| 558 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 559 | * \brief This function frees the components of a point. |
| 560 | * |
| 561 | * \param pt The point to free. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 562 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 563 | void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 564 | |
| 565 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 566 | * \brief This function frees the components of an ECP group. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 567 | * |
| 568 | * \param grp The group to free. This may be \c NULL, in which |
| 569 | * case this function returns immediately. If it is not |
| 570 | * \c NULL, it must point to an initialized ECP group. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 571 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 573 | |
| 574 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 575 | * \brief This function frees the components of a key pair. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 576 | * |
| 577 | * \param key The key pair to free. This may be \c NULL, in which |
| 578 | * case this function returns immediately. If it is not |
| 579 | * \c NULL, it must point to an initialized ECP key pair. |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 580 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 582 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 583 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 584 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 585 | * \brief Initialize a restart context. |
| 586 | * |
| 587 | * \param ctx The restart context to initialize. This must |
| 588 | * not be \c NULL. |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 589 | */ |
| 590 | void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx ); |
| 591 | |
| 592 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 593 | * \brief Free the components of a restart context. |
| 594 | * |
| 595 | * \param ctx The restart context to free. This may be \c NULL, in which |
| 596 | * case this function returns immediately. If it is not |
| 597 | * \c NULL, it must point to an initialized restart context. |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 598 | */ |
| 599 | void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx ); |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 600 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 601 | |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 602 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 603 | * \brief This function copies the contents of point \p Q into |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 604 | * point \p P. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 605 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 606 | * \param P The destination point. This must be initialized. |
| 607 | * \param Q The source point. This must be initialized. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 608 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 609 | * \return \c 0 on success. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 610 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 611 | * \return Another negative error code for other kinds of failure. |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 612 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 613 | 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] | 614 | |
| 615 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 616 | * \brief This function copies the contents of group \p src into |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 617 | * group \p dst. |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 618 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 619 | * \param dst The destination group. This must be initialized. |
| 620 | * \param src The source group. This must be initialized. |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 621 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 622 | * \return \c 0 on success. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 623 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 624 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 625 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 626 | int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, |
| 627 | const mbedtls_ecp_group *src ); |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 628 | |
| 629 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 630 | * \brief This function sets a point to the point at infinity. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 631 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 632 | * \param pt The point to set. This must be initialized. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 633 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 634 | * \return \c 0 on success. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 635 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 636 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 637 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 639 | |
| 640 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 641 | * \brief This function checks if a point is the point at infinity. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 642 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 643 | * \param pt The point to test. This must be initialized. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 644 | * |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 645 | * \return \c 1 if the point is zero. |
| 646 | * \return \c 0 if the point is non-zero. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 647 | * \return A negative error code on failure. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 648 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 650 | |
| 651 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 652 | * \brief This function compares two points. |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 653 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 654 | * \note This assumes that the points are normalized. Otherwise, |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 655 | * they may compare as "not equal" even if they are. |
| 656 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 657 | * \param P The first point to compare. This must be initialized. |
| 658 | * \param Q The second point to compare. This must be initialized. |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 659 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 660 | * \return \c 0 if the points are equal. |
| 661 | * \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] | 662 | */ |
| 663 | int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, |
| 664 | const mbedtls_ecp_point *Q ); |
| 665 | |
| 666 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 667 | * \brief This function imports a non-zero point from two ASCII |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 668 | * strings. |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 669 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 670 | * \param P The destination point. This must be initialized. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 671 | * \param radix The numeric base of the input. |
| 672 | * \param x The first affine coordinate, as a null-terminated string. |
| 673 | * \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] | 674 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 675 | * \return \c 0 on success. |
| 676 | * \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] | 677 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 678 | 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] | 679 | const char *x, const char *y ); |
| 680 | |
| 681 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 682 | * \brief This function exports a point into unsigned binary data. |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 683 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 684 | * \param grp The group to which the point should belong. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 685 | * This must be initialized and have group parameters |
| 686 | * set, for example through mbedtls_ecp_group_load(). |
| 687 | * \param P The point to export. This must be initialized. |
| 688 | * \param format The point format. This must be either |
| 689 | * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED. |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 690 | * (For groups without these formats, this parameter is |
| 691 | * ignored. But it still has to be either of the above |
| 692 | * values.) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 693 | * \param olen The address at which to store the length of |
| 694 | * the output in Bytes. This must not be \c NULL. |
| 695 | * \param buf The output buffer. This must be a writable buffer |
| 696 | * of length \p buflen Bytes. |
| 697 | * \param buflen The length of the output buffer \p buf in Bytes. |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 698 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 699 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 700 | * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer |
| 701 | * is too small to hold the point. |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 702 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format |
| 703 | * or the export for the given group is not implemented. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 704 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 705 | */ |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 706 | int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, |
| 707 | const mbedtls_ecp_point *P, |
| 708 | int format, size_t *olen, |
| 709 | unsigned char *buf, size_t buflen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 710 | |
| 711 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 712 | * \brief This function imports a point from unsigned binary data. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 713 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 714 | * \note This function does not check that the point actually |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 715 | * belongs to the given group, see mbedtls_ecp_check_pubkey() |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 716 | * for that. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 717 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 718 | * \param grp The group to which the point should belong. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 719 | * This must be initialized and have group parameters |
| 720 | * set, for example through mbedtls_ecp_group_load(). |
| 721 | * \param P The destination context to import the point to. |
| 722 | * This must be initialized. |
| 723 | * \param buf The input buffer. This must be a readable buffer |
| 724 | * of length \p ilen Bytes. |
| 725 | * \param ilen The length of the input buffer \p buf in Bytes. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 726 | * |
| 727 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 728 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 729 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 730 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the |
| 731 | * given group is not implemented. |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 732 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 733 | int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, |
| 734 | mbedtls_ecp_point *P, |
| 735 | const unsigned char *buf, size_t ilen ); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 736 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 737 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 738 | * \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] | 739 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 740 | * \note On function return, \p *buf is updated to point immediately |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 741 | * after the ECPoint record. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 742 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 743 | * \param grp The ECP group to use. |
| 744 | * This must be initialized and have group parameters |
| 745 | * set, for example through mbedtls_ecp_group_load(). |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 746 | * \param pt The destination point. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 747 | * \param buf The address of the pointer to the start of the input buffer. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 748 | * \param len The length of the buffer. |
Manuel Pégourié-Gonnard | 150c4f6 | 2014-11-21 09:14:52 +0100 | [diff] [blame] | 749 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 750 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 751 | * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization |
| 752 | * failure. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 753 | * \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] | 754 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 755 | int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, |
| 756 | mbedtls_ecp_point *pt, |
| 757 | const unsigned char **buf, size_t len ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 758 | |
| 759 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 760 | * \brief This function exports a point as a TLS ECPoint record |
| 761 | * defined in RFC 4492, Section 5.4. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 762 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 763 | * \param grp The ECP group to use. |
| 764 | * This must be initialized and have group parameters |
| 765 | * set, for example through mbedtls_ecp_group_load(). |
| 766 | * \param pt The point to be exported. This must be initialized. |
| 767 | * \param format The point format to use. This must be either |
| 768 | * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED. |
| 769 | * \param olen The address at which to store the length in Bytes |
| 770 | * of the data written. |
| 771 | * \param buf The target buffer. This must be a writable buffer of |
| 772 | * length \p blen Bytes. |
| 773 | * \param blen The length of the target buffer \p buf in Bytes. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 774 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 775 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 776 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid. |
| 777 | * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer |
| 778 | * is too small to hold the exported point. |
| 779 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 780 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 781 | int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, |
| 782 | const mbedtls_ecp_point *pt, |
| 783 | int format, size_t *olen, |
| 784 | unsigned char *buf, size_t blen ); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 785 | |
| 786 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 787 | * \brief This function sets up an ECP group context |
| 788 | * from a standardized set of domain parameters. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 789 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 790 | * \note The index should be a value of the NamedCurve enum, |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 791 | * as defined in <em>RFC-4492: Elliptic Curve Cryptography |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 792 | * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>, |
| 793 | * 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] | 794 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 795 | * \param grp The group context to setup. This must be initialized. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 796 | * \param id The identifier of the domain parameter set to load. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 797 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 798 | * \return \c 0 on success. |
| 799 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't |
| 800 | * correspond to a known group. |
| 801 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 802 | */ |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 803 | 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] | 804 | |
| 805 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 806 | * \brief This function sets up an ECP group context from a TLS |
| 807 | * ECParameters record as defined in RFC 4492, Section 5.4. |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 808 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 809 | * \note The read pointer \p buf is updated to point right after |
| 810 | * the ECParameters record on exit. |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 811 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 812 | * \param grp The group context to setup. This must be initialized. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 813 | * \param buf The address of the pointer to the start of the input buffer. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 814 | * \param len The length of the input buffer \c *buf in Bytes. |
Manuel Pégourié-Gonnard | 150c4f6 | 2014-11-21 09:14:52 +0100 | [diff] [blame] | 815 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 816 | * \return \c 0 on success. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 817 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 818 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not |
| 819 | * recognized. |
| 820 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 821 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 822 | int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, |
| 823 | const unsigned char **buf, size_t len ); |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 824 | |
| 825 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 826 | * \brief This function extracts an elliptic curve group ID from a |
| 827 | * TLS ECParameters record as defined in RFC 4492, Section 5.4. |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 828 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 829 | * \note The read pointer \p buf is updated to point right after |
| 830 | * the ECParameters record on exit. |
| 831 | * |
| 832 | * \param grp The address at which to store the group id. |
| 833 | * This must not be \c NULL. |
| 834 | * \param buf The address of the pointer to the start of the input buffer. |
| 835 | * \param len The length of the input buffer \c *buf in Bytes. |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 836 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 837 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 838 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. |
| 839 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not |
| 840 | * recognized. |
| 841 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 842 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 843 | int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp, |
| 844 | const unsigned char **buf, |
| 845 | size_t len ); |
| 846 | /** |
| 847 | * \brief This function exports an elliptic curve as a TLS |
| 848 | * ECParameters record as defined in RFC 4492, Section 5.4. |
| 849 | * |
| 850 | * \param grp The ECP group to be exported. |
| 851 | * This must be initialized and have group parameters |
| 852 | * set, for example through mbedtls_ecp_group_load(). |
| 853 | * \param olen The address at which to store the number of Bytes written. |
| 854 | * This must not be \c NULL. |
| 855 | * \param buf The buffer to write to. This must be a writable buffer |
| 856 | * of length \p blen Bytes. |
| 857 | * \param blen The length of the output buffer \p buf in Bytes. |
| 858 | * |
| 859 | * \return \c 0 on success. |
| 860 | * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output |
| 861 | * buffer is too small to hold the exported group. |
| 862 | * \return Another negative error code on other kinds of failure. |
| 863 | */ |
| 864 | int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, |
| 865 | size_t *olen, |
| 866 | unsigned char *buf, size_t blen ); |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 867 | |
| 868 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 869 | * \brief This function performs a scalar multiplication of a point |
| 870 | * by an integer: \p R = \p m * \p P. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 871 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 872 | * 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] | 873 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 874 | * \note To prevent timing attacks, this function |
| 875 | * executes the exact same sequence of base-field |
| 876 | * operations for any valid \p m. It avoids any if-branch or |
| 877 | * array index depending on the value of \p m. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 878 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 879 | * \note If \p f_rng is not NULL, it is used to randomize |
| 880 | * intermediate results to prevent potential timing attacks |
| 881 | * targeting these results. We recommend always providing |
| 882 | * a non-NULL \p f_rng. The overhead is negligible. |
Manuel Pégourié-Gonnard | 71d5667 | 2020-06-04 10:20:12 +0200 | [diff] [blame] | 883 | * Note: unless #MBEDTLS_ECP_NO_INTERNAL_RNG is defined, when |
| 884 | * \p f_rng is NULL, an internal RNG (seeded from the value |
| 885 | * of \p m) will be used instead. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 886 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 887 | * \param grp The ECP group to use. |
| 888 | * This must be initialized and have group parameters |
| 889 | * set, for example through mbedtls_ecp_group_load(). |
| 890 | * \param R The point in which to store the result of the calculation. |
| 891 | * This must be initialized. |
| 892 | * \param m The integer by which to multiply. This must be initialized. |
| 893 | * \param P The point to multiply. This must be initialized. |
| 894 | * \param f_rng The RNG function. This may be \c NULL if randomization |
| 895 | * of intermediate results isn't desired (discouraged). |
| 896 | * \param p_rng The RNG context to be passed to \p p_rng. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 897 | * |
| 898 | * \return \c 0 on success. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 899 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private |
| 900 | * key, or \p P is not a valid public key. |
| 901 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 902 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 903 | */ |
| 904 | int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 905 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 906 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); |
| 907 | |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 908 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 909 | * \brief This function performs multiplication of a point by |
| 910 | * an integer: \p R = \p m * \p P in a restartable way. |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 911 | * |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 912 | * \see mbedtls_ecp_mul() |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 913 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 914 | * \note This function does the same as \c mbedtls_ecp_mul(), but |
| 915 | * it can return early and restart according to the limit set |
| 916 | * with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
| 917 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 918 | * \param grp The ECP group to use. |
| 919 | * This must be initialized and have group parameters |
| 920 | * set, for example through mbedtls_ecp_group_load(). |
| 921 | * \param R The point in which to store the result of the calculation. |
| 922 | * This must be initialized. |
| 923 | * \param m The integer by which to multiply. This must be initialized. |
| 924 | * \param P The point to multiply. This must be initialized. |
| 925 | * \param f_rng The RNG function. This may be \c NULL if randomization |
| 926 | * of intermediate results isn't desired (discouraged). |
| 927 | * \param p_rng The RNG context to be passed to \p p_rng. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 928 | * \param rs_ctx The restart context (NULL disables restart). |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 929 | * |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 930 | * \return \c 0 on success. |
| 931 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private |
| 932 | * key, or \p P is not a valid public key. |
| 933 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
| 934 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | 8467e68 | 2017-04-20 09:47:06 +0200 | [diff] [blame] | 935 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 936 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 937 | */ |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 938 | int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 939 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 940 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 941 | mbedtls_ecp_restart_ctx *rs_ctx ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 942 | |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 943 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 944 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 945 | * \brief This function performs multiplication and addition of two |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 946 | * points by integers: \p R = \p m * \p P + \p n * \p Q |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 947 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 948 | * 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] | 949 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 950 | * \note In contrast to mbedtls_ecp_mul(), this function does not |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 951 | * guarantee a constant execution flow and timing. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 952 | * |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 953 | * \note This function is only defined for short Weierstrass curves. |
| 954 | * It may not be included in builds without any short |
| 955 | * Weierstrass curve. |
| 956 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 957 | * \param grp The ECP group to use. |
| 958 | * This must be initialized and have group parameters |
| 959 | * set, for example through mbedtls_ecp_group_load(). |
| 960 | * \param R The point in which to store the result of the calculation. |
| 961 | * This must be initialized. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 962 | * \param m The integer by which to multiply \p P. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 963 | * This must be initialized. |
| 964 | * \param P The point to multiply by \p m. This must be initialized. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 965 | * \param n The integer by which to multiply \p Q. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 966 | * This must be initialized. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 967 | * \param Q The point to be multiplied by \p n. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 968 | * This must be initialized. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 969 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 970 | * \return \c 0 on success. |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 971 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not |
| 972 | * valid private keys, or \p P or \p Q are not valid public |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 973 | * keys. |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 974 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 975 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not |
| 976 | * designate a short Weierstrass curve. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 977 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 978 | */ |
| 979 | int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 980 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 981 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q ); |
| 982 | |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 983 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 984 | * \brief This function performs multiplication and addition of two |
| 985 | * points by integers: \p R = \p m * \p P + \p n * \p Q in a |
Manuel Pégourié-Gonnard | 12e4a8b | 2018-09-12 10:55:15 +0200 | [diff] [blame] | 986 | * restartable way. |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 987 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 988 | * \see \c mbedtls_ecp_muladd() |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 989 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 990 | * \note This function works the same as \c mbedtls_ecp_muladd(), |
| 991 | * but it can return early and restart according to the limit |
| 992 | * set with \c mbedtls_ecp_set_max_ops() to reduce blocking. |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 993 | * |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 994 | * \note This function is only defined for short Weierstrass curves. |
| 995 | * It may not be included in builds without any short |
| 996 | * Weierstrass curve. |
| 997 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 998 | * \param grp The ECP group to use. |
| 999 | * This must be initialized and have group parameters |
| 1000 | * set, for example through mbedtls_ecp_group_load(). |
| 1001 | * \param R The point in which to store the result of the calculation. |
| 1002 | * This must be initialized. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1003 | * \param m The integer by which to multiply \p P. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1004 | * This must be initialized. |
| 1005 | * \param P The point to multiply by \p m. This must be initialized. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1006 | * \param n The integer by which to multiply \p Q. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1007 | * This must be initialized. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1008 | * \param Q The point to be multiplied by \p n. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1009 | * This must be initialized. |
Manuel Pégourié-Gonnard | f0bbd7e | 2018-10-15 13:22:41 +0200 | [diff] [blame] | 1010 | * \param rs_ctx The restart context (NULL disables restart). |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1011 | * |
| 1012 | * \return \c 0 on success. |
| 1013 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not |
| 1014 | * valid private keys, or \p P or \p Q are not valid public |
| 1015 | * keys. |
| 1016 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 1017 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not |
| 1018 | * designate a short Weierstrass curve. |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1019 | * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 1020 | * operations was reached: see \c mbedtls_ecp_set_max_ops(). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1021 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 1022 | */ |
| 1023 | int mbedtls_ecp_muladd_restartable( |
| 1024 | mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 1025 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 1026 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q, |
| 1027 | mbedtls_ecp_restart_ctx *rs_ctx ); |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 1028 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 1029 | |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 1030 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1031 | * \brief This function checks that a point is a valid public key |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1032 | * on this curve. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1033 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1034 | * It only checks that the point is non-zero, has |
| 1035 | * valid coordinates and lies on the curve. It does not verify |
| 1036 | * that it is indeed a multiple of \p G. This additional |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1037 | * check is computationally more expensive, is not required |
| 1038 | * by standards, and should not be necessary if the group |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1039 | * used has a small cofactor. In particular, it is useless for |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1040 | * the NIST groups which all have a cofactor of 1. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1041 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1042 | * \note This function uses bare components rather than an |
| 1043 | * ::mbedtls_ecp_keypair structure, to ease use with other |
| 1044 | * structures, such as ::mbedtls_ecdh_context or |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1045 | * ::mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1046 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1047 | * \param grp The ECP group the point should belong to. |
| 1048 | * This must be initialized and have group parameters |
| 1049 | * set, for example through mbedtls_ecp_group_load(). |
| 1050 | * \param pt The point to check. This must be initialized. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1051 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1052 | * \return \c 0 if the point is a valid public key. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1053 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not |
| 1054 | * a valid public key for the given curve. |
| 1055 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1056 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1057 | int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, |
| 1058 | const mbedtls_ecp_point *pt ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1059 | |
| 1060 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1061 | * \brief This function checks that an \p mbedtls_mpi is a |
| 1062 | * valid private key for this curve. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1063 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1064 | * \note This function uses bare components rather than an |
| 1065 | * ::mbedtls_ecp_keypair structure to ease use with other |
| 1066 | * structures, such as ::mbedtls_ecdh_context or |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1067 | * ::mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1068 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1069 | * \param grp The ECP group the private key should belong to. |
| 1070 | * This must be initialized and have group parameters |
| 1071 | * set, for example through mbedtls_ecp_group_load(). |
| 1072 | * \param d The integer to check. This must be initialized. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1073 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1074 | * \return \c 0 if the point is a valid private key. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1075 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid |
| 1076 | * private key for the given curve. |
| 1077 | * \return Another negative error code on other kinds of failure. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1078 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1079 | int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, |
| 1080 | const mbedtls_mpi *d ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1081 | |
| 1082 | /** |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1083 | * \brief This function generates a private key. |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 1084 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1085 | * \param grp The ECP group to generate a private key for. |
| 1086 | * This must be initialized and have group parameters |
| 1087 | * set, for example through mbedtls_ecp_group_load(). |
| 1088 | * \param d The destination MPI (secret part). This must be initialized. |
| 1089 | * \param f_rng The RNG function. This must not be \c NULL. |
| 1090 | * \param p_rng The RNG parameter to be passed to \p f_rng. This may be |
| 1091 | * \c NULL if \p f_rng doesn't need a context argument. |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 1092 | * |
Manuel Pégourié-Gonnard | da19f4c | 2018-06-12 12:40:54 +0200 | [diff] [blame] | 1093 | * \return \c 0 on success. |
| 1094 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 1095 | * on failure. |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 1096 | */ |
| 1097 | int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp, |
| 1098 | mbedtls_mpi *d, |
| 1099 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1100 | void *p_rng ); |
| 1101 | |
| 1102 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1103 | * \brief This function generates a keypair with a configurable base |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1104 | * point. |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 1105 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1106 | * \note This function uses bare components rather than an |
| 1107 | * ::mbedtls_ecp_keypair structure to ease use with other |
| 1108 | * structures, such as ::mbedtls_ecdh_context or |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1109 | * ::mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 1110 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1111 | * \param grp The ECP group to generate a key pair for. |
| 1112 | * This must be initialized and have group parameters |
| 1113 | * set, for example through mbedtls_ecp_group_load(). |
| 1114 | * \param G The base point to use. This must be initialized |
| 1115 | * and belong to \p grp. It replaces the default base |
| 1116 | * point \c grp->G used by mbedtls_ecp_gen_keypair(). |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1117 | * \param d The destination MPI (secret part). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1118 | * This must be initialized. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1119 | * \param Q The destination point (public part). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1120 | * This must be initialized. |
| 1121 | * \param f_rng The RNG function. This must not be \c NULL. |
| 1122 | * \param p_rng The RNG context to be passed to \p f_rng. This may |
| 1123 | * be \c NULL if \p f_rng doesn't need a context argument. |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 1124 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1125 | * \return \c 0 on success. |
| 1126 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 1127 | * on failure. |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 1128 | */ |
| 1129 | int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1130 | const mbedtls_ecp_point *G, |
| 1131 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 1132 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1133 | void *p_rng ); |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 1134 | |
| 1135 | /** |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 1136 | * \brief This function generates an ECP keypair. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1137 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1138 | * \note This function uses bare components rather than an |
| 1139 | * ::mbedtls_ecp_keypair structure to ease use with other |
| 1140 | * structures, such as ::mbedtls_ecdh_context or |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1141 | * ::mbedtls_ecdsa_context. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1142 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1143 | * \param grp The ECP group to generate a key pair for. |
| 1144 | * This must be initialized and have group parameters |
| 1145 | * set, for example through mbedtls_ecp_group_load(). |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1146 | * \param d The destination MPI (secret part). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1147 | * This must be initialized. |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1148 | * \param Q The destination point (public part). |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1149 | * This must be initialized. |
| 1150 | * \param f_rng The RNG function. This must not be \c NULL. |
| 1151 | * \param p_rng The RNG context to be passed to \p f_rng. This may |
| 1152 | * be \c NULL if \p f_rng doesn't need a context argument. |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1153 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1154 | * \return \c 0 on success. |
| 1155 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 1156 | * on failure. |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1157 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1158 | int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, |
| 1159 | mbedtls_ecp_point *Q, |
| 1160 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1161 | void *p_rng ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1162 | |
| 1163 | /** |
Rose Zadik | f56cb34 | 2018-04-19 12:49:10 +0100 | [diff] [blame] | 1164 | * \brief This function generates an ECP key. |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1165 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1166 | * \param grp_id The ECP group identifier. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1167 | * \param key The destination key. This must be initialized. |
| 1168 | * \param f_rng The RNG function to use. This must not be \c NULL. |
| 1169 | * \param p_rng The RNG context to be passed to \p f_rng. This may |
| 1170 | * be \c NULL if \p f_rng doesn't need a context argument. |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1171 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1172 | * \return \c 0 on success. |
| 1173 | * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code |
| 1174 | * on failure. |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1175 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1177 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1178 | void *p_rng ); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1179 | |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1180 | /** |
Janos Follath | b65853c | 2019-02-25 16:33:28 +0000 | [diff] [blame] | 1181 | * \brief This function reads an elliptic curve private key. |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1182 | * |
| 1183 | * \param grp_id The ECP group identifier. |
| 1184 | * \param key The destination key. |
| 1185 | * \param buf The the buffer containing the binary representation of the |
| 1186 | * key. (Big endian integer for Weierstrass curves, byte |
| 1187 | * string for Montgomery curves.) |
| 1188 | * \param buflen The length of the buffer in bytes. |
| 1189 | * |
| 1190 | * \return \c 0 on success. |
Janos Follath | b65853c | 2019-02-25 16:33:28 +0000 | [diff] [blame] | 1191 | * \return #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is |
| 1192 | * invalid. |
| 1193 | * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 1194 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for |
| 1195 | * the group is not implemented. |
Janos Follath | b65853c | 2019-02-25 16:33:28 +0000 | [diff] [blame] | 1196 | * \return Another negative error code on different kinds of failure. |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1197 | */ |
| 1198 | int mbedtls_ecp_read_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
| 1199 | const unsigned char *buf, size_t buflen ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1200 | |
| 1201 | /** |
| 1202 | * \brief This function exports an elliptic curve private key. |
| 1203 | * |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1204 | * \param key The private key. |
Steven Cooreman | c9b7f78 | 2020-06-11 17:00:36 +0200 | [diff] [blame] | 1205 | * \param buf The output buffer for containing the binary representation |
| 1206 | * of the key. (Big endian integer for Weierstrass curves, byte |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1207 | * string for Montgomery curves.) |
| 1208 | * \param buflen The total length of the buffer in bytes. |
| 1209 | * |
| 1210 | * \return \c 0 on success. |
Steven Cooreman | c9b7f78 | 2020-06-11 17:00:36 +0200 | [diff] [blame] | 1211 | * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key |
| 1212 | representation is larger than the available space in \p buf. |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1213 | * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for |
| 1214 | * the group is not implemented. |
| 1215 | * \return Another negative error code on different kinds of failure. |
| 1216 | */ |
Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 1217 | int mbedtls_ecp_write_key( mbedtls_ecp_keypair *key, |
Steven Cooreman | c9b7f78 | 2020-06-11 17:00:36 +0200 | [diff] [blame] | 1218 | unsigned char *buf, size_t buflen ); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 1219 | |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 1220 | /** |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1221 | * \brief This function checks that the keypair objects |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1222 | * \p pub and \p prv have the same group and the |
| 1223 | * same public point, and that the private key in |
| 1224 | * \p prv is consistent with the public key. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1225 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1226 | * \param pub The keypair structure holding the public key. This |
| 1227 | * must be initialized. If it contains a private key, that |
| 1228 | * part is ignored. |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1229 | * \param prv The keypair structure holding the full keypair. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1230 | * This must be initialized. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1231 | * |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1232 | * \return \c 0 on success, meaning that the keys are valid and match. |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1233 | * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match. |
Rose Zadik | a7a6155 | 2018-04-24 13:14:01 +0100 | [diff] [blame] | 1234 | * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX |
Rose Zadik | b2e111a | 2018-04-20 10:13:48 +0100 | [diff] [blame] | 1235 | * error code on calculation failure. |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1236 | */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1237 | int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, |
| 1238 | const mbedtls_ecp_keypair *prv ); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 1239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1240 | #if defined(MBEDTLS_SELF_TEST) |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1241 | |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 1242 | /** |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1243 | * \brief The ECP checkup routine. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1244 | * |
Rose Zadik | d3c9bfc | 2018-04-17 10:56:55 +0100 | [diff] [blame] | 1245 | * \return \c 0 on success. |
| 1246 | * \return \c 1 on failure. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1247 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1248 | int mbedtls_ecp_self_test( int verbose ); |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1249 | |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 1250 | #endif /* MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1251 | |
| 1252 | #ifdef __cplusplus |
| 1253 | } |
| 1254 | #endif |
| 1255 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1256 | #endif /* ecp.h */ |