nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
mbd_os/features/mbedtls/src/ecp.c@7:3a65ef12ba31, 2016-11-04 (annotated)
- Committer:
- nitsshukla
- Date:
- Fri Nov 04 12:06:04 2016 +0000
- Revision:
- 7:3a65ef12ba31
- Parent:
- 1:55a6170b404f
kghj;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* |
nexpaq | 1:55a6170b404f | 2 | * Elliptic curves over GF(p): generic functions |
nexpaq | 1:55a6170b404f | 3 | * |
nexpaq | 1:55a6170b404f | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
nexpaq | 1:55a6170b404f | 5 | * SPDX-License-Identifier: Apache-2.0 |
nexpaq | 1:55a6170b404f | 6 | * |
nexpaq | 1:55a6170b404f | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
nexpaq | 1:55a6170b404f | 8 | * not use this file except in compliance with the License. |
nexpaq | 1:55a6170b404f | 9 | * You may obtain a copy of the License at |
nexpaq | 1:55a6170b404f | 10 | * |
nexpaq | 1:55a6170b404f | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 1:55a6170b404f | 12 | * |
nexpaq | 1:55a6170b404f | 13 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 1:55a6170b404f | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
nexpaq | 1:55a6170b404f | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 1:55a6170b404f | 16 | * See the License for the specific language governing permissions and |
nexpaq | 1:55a6170b404f | 17 | * limitations under the License. |
nexpaq | 1:55a6170b404f | 18 | * |
nexpaq | 1:55a6170b404f | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
nexpaq | 1:55a6170b404f | 20 | */ |
nexpaq | 1:55a6170b404f | 21 | |
nexpaq | 1:55a6170b404f | 22 | /* |
nexpaq | 1:55a6170b404f | 23 | * References: |
nexpaq | 1:55a6170b404f | 24 | * |
nexpaq | 1:55a6170b404f | 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
nexpaq | 1:55a6170b404f | 26 | * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone |
nexpaq | 1:55a6170b404f | 27 | * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf |
nexpaq | 1:55a6170b404f | 28 | * RFC 4492 for the related TLS structures and constants |
nexpaq | 1:55a6170b404f | 29 | * |
nexpaq | 1:55a6170b404f | 30 | * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf |
nexpaq | 1:55a6170b404f | 31 | * |
nexpaq | 1:55a6170b404f | 32 | * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis |
nexpaq | 1:55a6170b404f | 33 | * for elliptic curve cryptosystems. In : Cryptographic Hardware and |
nexpaq | 1:55a6170b404f | 34 | * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. |
nexpaq | 1:55a6170b404f | 35 | * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25> |
nexpaq | 1:55a6170b404f | 36 | * |
nexpaq | 1:55a6170b404f | 37 | * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to |
nexpaq | 1:55a6170b404f | 38 | * render ECC resistant against Side Channel Attacks. IACR Cryptology |
nexpaq | 1:55a6170b404f | 39 | * ePrint Archive, 2004, vol. 2004, p. 342. |
nexpaq | 1:55a6170b404f | 40 | * <http://eprint.iacr.org/2004/342.pdf> |
nexpaq | 1:55a6170b404f | 41 | */ |
nexpaq | 1:55a6170b404f | 42 | |
nexpaq | 1:55a6170b404f | 43 | #if !defined(MBEDTLS_CONFIG_FILE) |
nexpaq | 1:55a6170b404f | 44 | #include "mbedtls/config.h" |
nexpaq | 1:55a6170b404f | 45 | #else |
nexpaq | 1:55a6170b404f | 46 | #include MBEDTLS_CONFIG_FILE |
nexpaq | 1:55a6170b404f | 47 | #endif |
nexpaq | 1:55a6170b404f | 48 | |
nexpaq | 1:55a6170b404f | 49 | #if defined(MBEDTLS_ECP_C) |
nexpaq | 1:55a6170b404f | 50 | |
nexpaq | 1:55a6170b404f | 51 | #include "mbedtls/ecp.h" |
nexpaq | 1:55a6170b404f | 52 | |
nexpaq | 1:55a6170b404f | 53 | #include <string.h> |
nexpaq | 1:55a6170b404f | 54 | |
nexpaq | 1:55a6170b404f | 55 | #if defined(MBEDTLS_PLATFORM_C) |
nexpaq | 1:55a6170b404f | 56 | #include "mbedtls/platform.h" |
nexpaq | 1:55a6170b404f | 57 | #else |
nexpaq | 1:55a6170b404f | 58 | #include <stdlib.h> |
nexpaq | 1:55a6170b404f | 59 | #include <stdio.h> |
nexpaq | 1:55a6170b404f | 60 | #define mbedtls_printf printf |
nexpaq | 1:55a6170b404f | 61 | #define mbedtls_calloc calloc |
nexpaq | 1:55a6170b404f | 62 | #define mbedtls_free free |
nexpaq | 1:55a6170b404f | 63 | #endif |
nexpaq | 1:55a6170b404f | 64 | |
nexpaq | 1:55a6170b404f | 65 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
nexpaq | 1:55a6170b404f | 66 | !defined(inline) && !defined(__cplusplus) |
nexpaq | 1:55a6170b404f | 67 | #define inline __inline |
nexpaq | 1:55a6170b404f | 68 | #endif |
nexpaq | 1:55a6170b404f | 69 | |
nexpaq | 1:55a6170b404f | 70 | /* Implementation that should never be optimized out by the compiler */ |
nexpaq | 1:55a6170b404f | 71 | static void mbedtls_zeroize( void *v, size_t n ) { |
nexpaq | 1:55a6170b404f | 72 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
nexpaq | 1:55a6170b404f | 73 | } |
nexpaq | 1:55a6170b404f | 74 | |
nexpaq | 1:55a6170b404f | 75 | #if defined(MBEDTLS_SELF_TEST) |
nexpaq | 1:55a6170b404f | 76 | /* |
nexpaq | 1:55a6170b404f | 77 | * Counts of point addition and doubling, and field multiplications. |
nexpaq | 1:55a6170b404f | 78 | * Used to test resistance of point multiplication to simple timing attacks. |
nexpaq | 1:55a6170b404f | 79 | */ |
nexpaq | 1:55a6170b404f | 80 | static unsigned long add_count, dbl_count, mul_count; |
nexpaq | 1:55a6170b404f | 81 | #endif |
nexpaq | 1:55a6170b404f | 82 | |
nexpaq | 1:55a6170b404f | 83 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 84 | defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 85 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 86 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 87 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 88 | defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 89 | defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 90 | defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 91 | defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 92 | defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \ |
nexpaq | 1:55a6170b404f | 93 | defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
nexpaq | 1:55a6170b404f | 94 | #define ECP_SHORTWEIERSTRASS |
nexpaq | 1:55a6170b404f | 95 | #endif |
nexpaq | 1:55a6170b404f | 96 | |
nexpaq | 1:55a6170b404f | 97 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
nexpaq | 1:55a6170b404f | 98 | #define ECP_MONTGOMERY |
nexpaq | 1:55a6170b404f | 99 | #endif |
nexpaq | 1:55a6170b404f | 100 | |
nexpaq | 1:55a6170b404f | 101 | /* |
nexpaq | 1:55a6170b404f | 102 | * Curve types: internal for now, might be exposed later |
nexpaq | 1:55a6170b404f | 103 | */ |
nexpaq | 1:55a6170b404f | 104 | typedef enum |
nexpaq | 1:55a6170b404f | 105 | { |
nexpaq | 1:55a6170b404f | 106 | ECP_TYPE_NONE = 0, |
nexpaq | 1:55a6170b404f | 107 | ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */ |
nexpaq | 1:55a6170b404f | 108 | ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */ |
nexpaq | 1:55a6170b404f | 109 | } ecp_curve_type; |
nexpaq | 1:55a6170b404f | 110 | |
nexpaq | 1:55a6170b404f | 111 | /* |
nexpaq | 1:55a6170b404f | 112 | * List of supported curves: |
nexpaq | 1:55a6170b404f | 113 | * - internal ID |
nexpaq | 1:55a6170b404f | 114 | * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2) |
nexpaq | 1:55a6170b404f | 115 | * - size in bits |
nexpaq | 1:55a6170b404f | 116 | * - readable name |
nexpaq | 1:55a6170b404f | 117 | * |
nexpaq | 1:55a6170b404f | 118 | * Curves are listed in order: largest curves first, and for a given size, |
nexpaq | 1:55a6170b404f | 119 | * fastest curves first. This provides the default order for the SSL module. |
nexpaq | 1:55a6170b404f | 120 | * |
nexpaq | 1:55a6170b404f | 121 | * Reminder: update profiles in x509_crt.c when adding a new curves! |
nexpaq | 1:55a6170b404f | 122 | */ |
nexpaq | 1:55a6170b404f | 123 | static const mbedtls_ecp_curve_info ecp_supported_curves[] = |
nexpaq | 1:55a6170b404f | 124 | { |
nexpaq | 1:55a6170b404f | 125 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
nexpaq | 1:55a6170b404f | 126 | { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" }, |
nexpaq | 1:55a6170b404f | 127 | #endif |
nexpaq | 1:55a6170b404f | 128 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
nexpaq | 1:55a6170b404f | 129 | { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" }, |
nexpaq | 1:55a6170b404f | 130 | #endif |
nexpaq | 1:55a6170b404f | 131 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
nexpaq | 1:55a6170b404f | 132 | { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" }, |
nexpaq | 1:55a6170b404f | 133 | #endif |
nexpaq | 1:55a6170b404f | 134 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
nexpaq | 1:55a6170b404f | 135 | { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" }, |
nexpaq | 1:55a6170b404f | 136 | #endif |
nexpaq | 1:55a6170b404f | 137 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
nexpaq | 1:55a6170b404f | 138 | { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" }, |
nexpaq | 1:55a6170b404f | 139 | #endif |
nexpaq | 1:55a6170b404f | 140 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
nexpaq | 1:55a6170b404f | 141 | { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" }, |
nexpaq | 1:55a6170b404f | 142 | #endif |
nexpaq | 1:55a6170b404f | 143 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
nexpaq | 1:55a6170b404f | 144 | { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" }, |
nexpaq | 1:55a6170b404f | 145 | #endif |
nexpaq | 1:55a6170b404f | 146 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
nexpaq | 1:55a6170b404f | 147 | { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" }, |
nexpaq | 1:55a6170b404f | 148 | #endif |
nexpaq | 1:55a6170b404f | 149 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
nexpaq | 1:55a6170b404f | 150 | { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" }, |
nexpaq | 1:55a6170b404f | 151 | #endif |
nexpaq | 1:55a6170b404f | 152 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
nexpaq | 1:55a6170b404f | 153 | { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" }, |
nexpaq | 1:55a6170b404f | 154 | #endif |
nexpaq | 1:55a6170b404f | 155 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
nexpaq | 1:55a6170b404f | 156 | { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" }, |
nexpaq | 1:55a6170b404f | 157 | #endif |
nexpaq | 1:55a6170b404f | 158 | { MBEDTLS_ECP_DP_NONE, 0, 0, NULL }, |
nexpaq | 1:55a6170b404f | 159 | }; |
nexpaq | 1:55a6170b404f | 160 | |
nexpaq | 1:55a6170b404f | 161 | #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \ |
nexpaq | 1:55a6170b404f | 162 | sizeof( ecp_supported_curves[0] ) |
nexpaq | 1:55a6170b404f | 163 | |
nexpaq | 1:55a6170b404f | 164 | static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES]; |
nexpaq | 1:55a6170b404f | 165 | |
nexpaq | 1:55a6170b404f | 166 | /* |
nexpaq | 1:55a6170b404f | 167 | * List of supported curves and associated info |
nexpaq | 1:55a6170b404f | 168 | */ |
nexpaq | 1:55a6170b404f | 169 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void ) |
nexpaq | 1:55a6170b404f | 170 | { |
nexpaq | 1:55a6170b404f | 171 | return( ecp_supported_curves ); |
nexpaq | 1:55a6170b404f | 172 | } |
nexpaq | 1:55a6170b404f | 173 | |
nexpaq | 1:55a6170b404f | 174 | /* |
nexpaq | 1:55a6170b404f | 175 | * List of supported curves, group ID only |
nexpaq | 1:55a6170b404f | 176 | */ |
nexpaq | 1:55a6170b404f | 177 | const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void ) |
nexpaq | 1:55a6170b404f | 178 | { |
nexpaq | 1:55a6170b404f | 179 | static int init_done = 0; |
nexpaq | 1:55a6170b404f | 180 | |
nexpaq | 1:55a6170b404f | 181 | if( ! init_done ) |
nexpaq | 1:55a6170b404f | 182 | { |
nexpaq | 1:55a6170b404f | 183 | size_t i = 0; |
nexpaq | 1:55a6170b404f | 184 | const mbedtls_ecp_curve_info *curve_info; |
nexpaq | 1:55a6170b404f | 185 | |
nexpaq | 1:55a6170b404f | 186 | for( curve_info = mbedtls_ecp_curve_list(); |
nexpaq | 1:55a6170b404f | 187 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
nexpaq | 1:55a6170b404f | 188 | curve_info++ ) |
nexpaq | 1:55a6170b404f | 189 | { |
nexpaq | 1:55a6170b404f | 190 | ecp_supported_grp_id[i++] = curve_info->grp_id; |
nexpaq | 1:55a6170b404f | 191 | } |
nexpaq | 1:55a6170b404f | 192 | ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE; |
nexpaq | 1:55a6170b404f | 193 | |
nexpaq | 1:55a6170b404f | 194 | init_done = 1; |
nexpaq | 1:55a6170b404f | 195 | } |
nexpaq | 1:55a6170b404f | 196 | |
nexpaq | 1:55a6170b404f | 197 | return( ecp_supported_grp_id ); |
nexpaq | 1:55a6170b404f | 198 | } |
nexpaq | 1:55a6170b404f | 199 | |
nexpaq | 1:55a6170b404f | 200 | /* |
nexpaq | 1:55a6170b404f | 201 | * Get the curve info for the internal identifier |
nexpaq | 1:55a6170b404f | 202 | */ |
nexpaq | 1:55a6170b404f | 203 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id ) |
nexpaq | 1:55a6170b404f | 204 | { |
nexpaq | 1:55a6170b404f | 205 | const mbedtls_ecp_curve_info *curve_info; |
nexpaq | 1:55a6170b404f | 206 | |
nexpaq | 1:55a6170b404f | 207 | for( curve_info = mbedtls_ecp_curve_list(); |
nexpaq | 1:55a6170b404f | 208 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
nexpaq | 1:55a6170b404f | 209 | curve_info++ ) |
nexpaq | 1:55a6170b404f | 210 | { |
nexpaq | 1:55a6170b404f | 211 | if( curve_info->grp_id == grp_id ) |
nexpaq | 1:55a6170b404f | 212 | return( curve_info ); |
nexpaq | 1:55a6170b404f | 213 | } |
nexpaq | 1:55a6170b404f | 214 | |
nexpaq | 1:55a6170b404f | 215 | return( NULL ); |
nexpaq | 1:55a6170b404f | 216 | } |
nexpaq | 1:55a6170b404f | 217 | |
nexpaq | 1:55a6170b404f | 218 | /* |
nexpaq | 1:55a6170b404f | 219 | * Get the curve info from the TLS identifier |
nexpaq | 1:55a6170b404f | 220 | */ |
nexpaq | 1:55a6170b404f | 221 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id ) |
nexpaq | 1:55a6170b404f | 222 | { |
nexpaq | 1:55a6170b404f | 223 | const mbedtls_ecp_curve_info *curve_info; |
nexpaq | 1:55a6170b404f | 224 | |
nexpaq | 1:55a6170b404f | 225 | for( curve_info = mbedtls_ecp_curve_list(); |
nexpaq | 1:55a6170b404f | 226 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
nexpaq | 1:55a6170b404f | 227 | curve_info++ ) |
nexpaq | 1:55a6170b404f | 228 | { |
nexpaq | 1:55a6170b404f | 229 | if( curve_info->tls_id == tls_id ) |
nexpaq | 1:55a6170b404f | 230 | return( curve_info ); |
nexpaq | 1:55a6170b404f | 231 | } |
nexpaq | 1:55a6170b404f | 232 | |
nexpaq | 1:55a6170b404f | 233 | return( NULL ); |
nexpaq | 1:55a6170b404f | 234 | } |
nexpaq | 1:55a6170b404f | 235 | |
nexpaq | 1:55a6170b404f | 236 | /* |
nexpaq | 1:55a6170b404f | 237 | * Get the curve info from the name |
nexpaq | 1:55a6170b404f | 238 | */ |
nexpaq | 1:55a6170b404f | 239 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name ) |
nexpaq | 1:55a6170b404f | 240 | { |
nexpaq | 1:55a6170b404f | 241 | const mbedtls_ecp_curve_info *curve_info; |
nexpaq | 1:55a6170b404f | 242 | |
nexpaq | 1:55a6170b404f | 243 | for( curve_info = mbedtls_ecp_curve_list(); |
nexpaq | 1:55a6170b404f | 244 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
nexpaq | 1:55a6170b404f | 245 | curve_info++ ) |
nexpaq | 1:55a6170b404f | 246 | { |
nexpaq | 1:55a6170b404f | 247 | if( strcmp( curve_info->name, name ) == 0 ) |
nexpaq | 1:55a6170b404f | 248 | return( curve_info ); |
nexpaq | 1:55a6170b404f | 249 | } |
nexpaq | 1:55a6170b404f | 250 | |
nexpaq | 1:55a6170b404f | 251 | return( NULL ); |
nexpaq | 1:55a6170b404f | 252 | } |
nexpaq | 1:55a6170b404f | 253 | |
nexpaq | 1:55a6170b404f | 254 | /* |
nexpaq | 1:55a6170b404f | 255 | * Get the type of a curve |
nexpaq | 1:55a6170b404f | 256 | */ |
nexpaq | 1:55a6170b404f | 257 | static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp ) |
nexpaq | 1:55a6170b404f | 258 | { |
nexpaq | 1:55a6170b404f | 259 | if( grp->G.X.p == NULL ) |
nexpaq | 1:55a6170b404f | 260 | return( ECP_TYPE_NONE ); |
nexpaq | 1:55a6170b404f | 261 | |
nexpaq | 1:55a6170b404f | 262 | if( grp->G.Y.p == NULL ) |
nexpaq | 1:55a6170b404f | 263 | return( ECP_TYPE_MONTGOMERY ); |
nexpaq | 1:55a6170b404f | 264 | else |
nexpaq | 1:55a6170b404f | 265 | return( ECP_TYPE_SHORT_WEIERSTRASS ); |
nexpaq | 1:55a6170b404f | 266 | } |
nexpaq | 1:55a6170b404f | 267 | |
nexpaq | 1:55a6170b404f | 268 | /* |
nexpaq | 1:55a6170b404f | 269 | * Initialize (the components of) a point |
nexpaq | 1:55a6170b404f | 270 | */ |
nexpaq | 1:55a6170b404f | 271 | void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 272 | { |
nexpaq | 1:55a6170b404f | 273 | if( pt == NULL ) |
nexpaq | 1:55a6170b404f | 274 | return; |
nexpaq | 1:55a6170b404f | 275 | |
nexpaq | 1:55a6170b404f | 276 | mbedtls_mpi_init( &pt->X ); |
nexpaq | 1:55a6170b404f | 277 | mbedtls_mpi_init( &pt->Y ); |
nexpaq | 1:55a6170b404f | 278 | mbedtls_mpi_init( &pt->Z ); |
nexpaq | 1:55a6170b404f | 279 | } |
nexpaq | 1:55a6170b404f | 280 | |
nexpaq | 1:55a6170b404f | 281 | /* |
nexpaq | 1:55a6170b404f | 282 | * Initialize (the components of) a group |
nexpaq | 1:55a6170b404f | 283 | */ |
nexpaq | 1:55a6170b404f | 284 | void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ) |
nexpaq | 1:55a6170b404f | 285 | { |
nexpaq | 1:55a6170b404f | 286 | if( grp == NULL ) |
nexpaq | 1:55a6170b404f | 287 | return; |
nexpaq | 1:55a6170b404f | 288 | |
nexpaq | 1:55a6170b404f | 289 | memset( grp, 0, sizeof( mbedtls_ecp_group ) ); |
nexpaq | 1:55a6170b404f | 290 | } |
nexpaq | 1:55a6170b404f | 291 | |
nexpaq | 1:55a6170b404f | 292 | /* |
nexpaq | 1:55a6170b404f | 293 | * Initialize (the components of) a key pair |
nexpaq | 1:55a6170b404f | 294 | */ |
nexpaq | 1:55a6170b404f | 295 | void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ) |
nexpaq | 1:55a6170b404f | 296 | { |
nexpaq | 1:55a6170b404f | 297 | if( key == NULL ) |
nexpaq | 1:55a6170b404f | 298 | return; |
nexpaq | 1:55a6170b404f | 299 | |
nexpaq | 1:55a6170b404f | 300 | mbedtls_ecp_group_init( &key->grp ); |
nexpaq | 1:55a6170b404f | 301 | mbedtls_mpi_init( &key->d ); |
nexpaq | 1:55a6170b404f | 302 | mbedtls_ecp_point_init( &key->Q ); |
nexpaq | 1:55a6170b404f | 303 | } |
nexpaq | 1:55a6170b404f | 304 | |
nexpaq | 1:55a6170b404f | 305 | /* |
nexpaq | 1:55a6170b404f | 306 | * Unallocate (the components of) a point |
nexpaq | 1:55a6170b404f | 307 | */ |
nexpaq | 1:55a6170b404f | 308 | void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 309 | { |
nexpaq | 1:55a6170b404f | 310 | if( pt == NULL ) |
nexpaq | 1:55a6170b404f | 311 | return; |
nexpaq | 1:55a6170b404f | 312 | |
nexpaq | 1:55a6170b404f | 313 | mbedtls_mpi_free( &( pt->X ) ); |
nexpaq | 1:55a6170b404f | 314 | mbedtls_mpi_free( &( pt->Y ) ); |
nexpaq | 1:55a6170b404f | 315 | mbedtls_mpi_free( &( pt->Z ) ); |
nexpaq | 1:55a6170b404f | 316 | } |
nexpaq | 1:55a6170b404f | 317 | |
nexpaq | 1:55a6170b404f | 318 | /* |
nexpaq | 1:55a6170b404f | 319 | * Unallocate (the components of) a group |
nexpaq | 1:55a6170b404f | 320 | */ |
nexpaq | 1:55a6170b404f | 321 | void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ) |
nexpaq | 1:55a6170b404f | 322 | { |
nexpaq | 1:55a6170b404f | 323 | size_t i; |
nexpaq | 1:55a6170b404f | 324 | |
nexpaq | 1:55a6170b404f | 325 | if( grp == NULL ) |
nexpaq | 1:55a6170b404f | 326 | return; |
nexpaq | 1:55a6170b404f | 327 | |
nexpaq | 1:55a6170b404f | 328 | if( grp->h != 1 ) |
nexpaq | 1:55a6170b404f | 329 | { |
nexpaq | 1:55a6170b404f | 330 | mbedtls_mpi_free( &grp->P ); |
nexpaq | 1:55a6170b404f | 331 | mbedtls_mpi_free( &grp->A ); |
nexpaq | 1:55a6170b404f | 332 | mbedtls_mpi_free( &grp->B ); |
nexpaq | 1:55a6170b404f | 333 | mbedtls_ecp_point_free( &grp->G ); |
nexpaq | 1:55a6170b404f | 334 | mbedtls_mpi_free( &grp->N ); |
nexpaq | 1:55a6170b404f | 335 | } |
nexpaq | 1:55a6170b404f | 336 | |
nexpaq | 1:55a6170b404f | 337 | if( grp->T != NULL ) |
nexpaq | 1:55a6170b404f | 338 | { |
nexpaq | 1:55a6170b404f | 339 | for( i = 0; i < grp->T_size; i++ ) |
nexpaq | 1:55a6170b404f | 340 | mbedtls_ecp_point_free( &grp->T[i] ); |
nexpaq | 1:55a6170b404f | 341 | mbedtls_free( grp->T ); |
nexpaq | 1:55a6170b404f | 342 | } |
nexpaq | 1:55a6170b404f | 343 | |
nexpaq | 1:55a6170b404f | 344 | mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) ); |
nexpaq | 1:55a6170b404f | 345 | } |
nexpaq | 1:55a6170b404f | 346 | |
nexpaq | 1:55a6170b404f | 347 | /* |
nexpaq | 1:55a6170b404f | 348 | * Unallocate (the components of) a key pair |
nexpaq | 1:55a6170b404f | 349 | */ |
nexpaq | 1:55a6170b404f | 350 | void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ) |
nexpaq | 1:55a6170b404f | 351 | { |
nexpaq | 1:55a6170b404f | 352 | if( key == NULL ) |
nexpaq | 1:55a6170b404f | 353 | return; |
nexpaq | 1:55a6170b404f | 354 | |
nexpaq | 1:55a6170b404f | 355 | mbedtls_ecp_group_free( &key->grp ); |
nexpaq | 1:55a6170b404f | 356 | mbedtls_mpi_free( &key->d ); |
nexpaq | 1:55a6170b404f | 357 | mbedtls_ecp_point_free( &key->Q ); |
nexpaq | 1:55a6170b404f | 358 | } |
nexpaq | 1:55a6170b404f | 359 | |
nexpaq | 1:55a6170b404f | 360 | /* |
nexpaq | 1:55a6170b404f | 361 | * Copy the contents of a point |
nexpaq | 1:55a6170b404f | 362 | */ |
nexpaq | 1:55a6170b404f | 363 | int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ) |
nexpaq | 1:55a6170b404f | 364 | { |
nexpaq | 1:55a6170b404f | 365 | int ret; |
nexpaq | 1:55a6170b404f | 366 | |
nexpaq | 1:55a6170b404f | 367 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) ); |
nexpaq | 1:55a6170b404f | 368 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) ); |
nexpaq | 1:55a6170b404f | 369 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) ); |
nexpaq | 1:55a6170b404f | 370 | |
nexpaq | 1:55a6170b404f | 371 | cleanup: |
nexpaq | 1:55a6170b404f | 372 | return( ret ); |
nexpaq | 1:55a6170b404f | 373 | } |
nexpaq | 1:55a6170b404f | 374 | |
nexpaq | 1:55a6170b404f | 375 | /* |
nexpaq | 1:55a6170b404f | 376 | * Copy the contents of a group object |
nexpaq | 1:55a6170b404f | 377 | */ |
nexpaq | 1:55a6170b404f | 378 | int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ) |
nexpaq | 1:55a6170b404f | 379 | { |
nexpaq | 1:55a6170b404f | 380 | return mbedtls_ecp_group_load( dst, src->id ); |
nexpaq | 1:55a6170b404f | 381 | } |
nexpaq | 1:55a6170b404f | 382 | |
nexpaq | 1:55a6170b404f | 383 | /* |
nexpaq | 1:55a6170b404f | 384 | * Set point to zero |
nexpaq | 1:55a6170b404f | 385 | */ |
nexpaq | 1:55a6170b404f | 386 | int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 387 | { |
nexpaq | 1:55a6170b404f | 388 | int ret; |
nexpaq | 1:55a6170b404f | 389 | |
nexpaq | 1:55a6170b404f | 390 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) ); |
nexpaq | 1:55a6170b404f | 391 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) ); |
nexpaq | 1:55a6170b404f | 392 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) ); |
nexpaq | 1:55a6170b404f | 393 | |
nexpaq | 1:55a6170b404f | 394 | cleanup: |
nexpaq | 1:55a6170b404f | 395 | return( ret ); |
nexpaq | 1:55a6170b404f | 396 | } |
nexpaq | 1:55a6170b404f | 397 | |
nexpaq | 1:55a6170b404f | 398 | /* |
nexpaq | 1:55a6170b404f | 399 | * Tell if a point is zero |
nexpaq | 1:55a6170b404f | 400 | */ |
nexpaq | 1:55a6170b404f | 401 | int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 402 | { |
nexpaq | 1:55a6170b404f | 403 | return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 ); |
nexpaq | 1:55a6170b404f | 404 | } |
nexpaq | 1:55a6170b404f | 405 | |
nexpaq | 1:55a6170b404f | 406 | /* |
nexpaq | 1:55a6170b404f | 407 | * Compare two points lazyly |
nexpaq | 1:55a6170b404f | 408 | */ |
nexpaq | 1:55a6170b404f | 409 | int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 410 | const mbedtls_ecp_point *Q ) |
nexpaq | 1:55a6170b404f | 411 | { |
nexpaq | 1:55a6170b404f | 412 | if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 && |
nexpaq | 1:55a6170b404f | 413 | mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 && |
nexpaq | 1:55a6170b404f | 414 | mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 ) |
nexpaq | 1:55a6170b404f | 415 | { |
nexpaq | 1:55a6170b404f | 416 | return( 0 ); |
nexpaq | 1:55a6170b404f | 417 | } |
nexpaq | 1:55a6170b404f | 418 | |
nexpaq | 1:55a6170b404f | 419 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 420 | } |
nexpaq | 1:55a6170b404f | 421 | |
nexpaq | 1:55a6170b404f | 422 | /* |
nexpaq | 1:55a6170b404f | 423 | * Import a non-zero point from ASCII strings |
nexpaq | 1:55a6170b404f | 424 | */ |
nexpaq | 1:55a6170b404f | 425 | int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, |
nexpaq | 1:55a6170b404f | 426 | const char *x, const char *y ) |
nexpaq | 1:55a6170b404f | 427 | { |
nexpaq | 1:55a6170b404f | 428 | int ret; |
nexpaq | 1:55a6170b404f | 429 | |
nexpaq | 1:55a6170b404f | 430 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) ); |
nexpaq | 1:55a6170b404f | 431 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) ); |
nexpaq | 1:55a6170b404f | 432 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) ); |
nexpaq | 1:55a6170b404f | 433 | |
nexpaq | 1:55a6170b404f | 434 | cleanup: |
nexpaq | 1:55a6170b404f | 435 | return( ret ); |
nexpaq | 1:55a6170b404f | 436 | } |
nexpaq | 1:55a6170b404f | 437 | |
nexpaq | 1:55a6170b404f | 438 | /* |
nexpaq | 1:55a6170b404f | 439 | * Export a point into unsigned binary data (SEC1 2.3.3) |
nexpaq | 1:55a6170b404f | 440 | */ |
nexpaq | 1:55a6170b404f | 441 | int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 442 | int format, size_t *olen, |
nexpaq | 1:55a6170b404f | 443 | unsigned char *buf, size_t buflen ) |
nexpaq | 1:55a6170b404f | 444 | { |
nexpaq | 1:55a6170b404f | 445 | int ret = 0; |
nexpaq | 1:55a6170b404f | 446 | size_t plen; |
nexpaq | 1:55a6170b404f | 447 | |
nexpaq | 1:55a6170b404f | 448 | if( format != MBEDTLS_ECP_PF_UNCOMPRESSED && |
nexpaq | 1:55a6170b404f | 449 | format != MBEDTLS_ECP_PF_COMPRESSED ) |
nexpaq | 1:55a6170b404f | 450 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 451 | |
nexpaq | 1:55a6170b404f | 452 | /* |
nexpaq | 1:55a6170b404f | 453 | * Common case: P == 0 |
nexpaq | 1:55a6170b404f | 454 | */ |
nexpaq | 1:55a6170b404f | 455 | if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 ) |
nexpaq | 1:55a6170b404f | 456 | { |
nexpaq | 1:55a6170b404f | 457 | if( buflen < 1 ) |
nexpaq | 1:55a6170b404f | 458 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
nexpaq | 1:55a6170b404f | 459 | |
nexpaq | 1:55a6170b404f | 460 | buf[0] = 0x00; |
nexpaq | 1:55a6170b404f | 461 | *olen = 1; |
nexpaq | 1:55a6170b404f | 462 | |
nexpaq | 1:55a6170b404f | 463 | return( 0 ); |
nexpaq | 1:55a6170b404f | 464 | } |
nexpaq | 1:55a6170b404f | 465 | |
nexpaq | 1:55a6170b404f | 466 | plen = mbedtls_mpi_size( &grp->P ); |
nexpaq | 1:55a6170b404f | 467 | |
nexpaq | 1:55a6170b404f | 468 | if( format == MBEDTLS_ECP_PF_UNCOMPRESSED ) |
nexpaq | 1:55a6170b404f | 469 | { |
nexpaq | 1:55a6170b404f | 470 | *olen = 2 * plen + 1; |
nexpaq | 1:55a6170b404f | 471 | |
nexpaq | 1:55a6170b404f | 472 | if( buflen < *olen ) |
nexpaq | 1:55a6170b404f | 473 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
nexpaq | 1:55a6170b404f | 474 | |
nexpaq | 1:55a6170b404f | 475 | buf[0] = 0x04; |
nexpaq | 1:55a6170b404f | 476 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) ); |
nexpaq | 1:55a6170b404f | 477 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) ); |
nexpaq | 1:55a6170b404f | 478 | } |
nexpaq | 1:55a6170b404f | 479 | else if( format == MBEDTLS_ECP_PF_COMPRESSED ) |
nexpaq | 1:55a6170b404f | 480 | { |
nexpaq | 1:55a6170b404f | 481 | *olen = plen + 1; |
nexpaq | 1:55a6170b404f | 482 | |
nexpaq | 1:55a6170b404f | 483 | if( buflen < *olen ) |
nexpaq | 1:55a6170b404f | 484 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
nexpaq | 1:55a6170b404f | 485 | |
nexpaq | 1:55a6170b404f | 486 | buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 ); |
nexpaq | 1:55a6170b404f | 487 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) ); |
nexpaq | 1:55a6170b404f | 488 | } |
nexpaq | 1:55a6170b404f | 489 | |
nexpaq | 1:55a6170b404f | 490 | cleanup: |
nexpaq | 1:55a6170b404f | 491 | return( ret ); |
nexpaq | 1:55a6170b404f | 492 | } |
nexpaq | 1:55a6170b404f | 493 | |
nexpaq | 1:55a6170b404f | 494 | /* |
nexpaq | 1:55a6170b404f | 495 | * Import a point from unsigned binary data (SEC1 2.3.4) |
nexpaq | 1:55a6170b404f | 496 | */ |
nexpaq | 1:55a6170b404f | 497 | int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
nexpaq | 1:55a6170b404f | 498 | const unsigned char *buf, size_t ilen ) |
nexpaq | 1:55a6170b404f | 499 | { |
nexpaq | 1:55a6170b404f | 500 | int ret; |
nexpaq | 1:55a6170b404f | 501 | size_t plen; |
nexpaq | 1:55a6170b404f | 502 | |
nexpaq | 1:55a6170b404f | 503 | if( ilen < 1 ) |
nexpaq | 1:55a6170b404f | 504 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 505 | |
nexpaq | 1:55a6170b404f | 506 | if( buf[0] == 0x00 ) |
nexpaq | 1:55a6170b404f | 507 | { |
nexpaq | 1:55a6170b404f | 508 | if( ilen == 1 ) |
nexpaq | 1:55a6170b404f | 509 | return( mbedtls_ecp_set_zero( pt ) ); |
nexpaq | 1:55a6170b404f | 510 | else |
nexpaq | 1:55a6170b404f | 511 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 512 | } |
nexpaq | 1:55a6170b404f | 513 | |
nexpaq | 1:55a6170b404f | 514 | plen = mbedtls_mpi_size( &grp->P ); |
nexpaq | 1:55a6170b404f | 515 | |
nexpaq | 1:55a6170b404f | 516 | if( buf[0] != 0x04 ) |
nexpaq | 1:55a6170b404f | 517 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
nexpaq | 1:55a6170b404f | 518 | |
nexpaq | 1:55a6170b404f | 519 | if( ilen != 2 * plen + 1 ) |
nexpaq | 1:55a6170b404f | 520 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 521 | |
nexpaq | 1:55a6170b404f | 522 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) ); |
nexpaq | 1:55a6170b404f | 523 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) ); |
nexpaq | 1:55a6170b404f | 524 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) ); |
nexpaq | 1:55a6170b404f | 525 | |
nexpaq | 1:55a6170b404f | 526 | cleanup: |
nexpaq | 1:55a6170b404f | 527 | return( ret ); |
nexpaq | 1:55a6170b404f | 528 | } |
nexpaq | 1:55a6170b404f | 529 | |
nexpaq | 1:55a6170b404f | 530 | /* |
nexpaq | 1:55a6170b404f | 531 | * Import a point from a TLS ECPoint record (RFC 4492) |
nexpaq | 1:55a6170b404f | 532 | * struct { |
nexpaq | 1:55a6170b404f | 533 | * opaque point <1..2^8-1>; |
nexpaq | 1:55a6170b404f | 534 | * } ECPoint; |
nexpaq | 1:55a6170b404f | 535 | */ |
nexpaq | 1:55a6170b404f | 536 | int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
nexpaq | 1:55a6170b404f | 537 | const unsigned char **buf, size_t buf_len ) |
nexpaq | 1:55a6170b404f | 538 | { |
nexpaq | 1:55a6170b404f | 539 | unsigned char data_len; |
nexpaq | 1:55a6170b404f | 540 | const unsigned char *buf_start; |
nexpaq | 1:55a6170b404f | 541 | |
nexpaq | 1:55a6170b404f | 542 | /* |
nexpaq | 1:55a6170b404f | 543 | * We must have at least two bytes (1 for length, at least one for data) |
nexpaq | 1:55a6170b404f | 544 | */ |
nexpaq | 1:55a6170b404f | 545 | if( buf_len < 2 ) |
nexpaq | 1:55a6170b404f | 546 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 547 | |
nexpaq | 1:55a6170b404f | 548 | data_len = *(*buf)++; |
nexpaq | 1:55a6170b404f | 549 | if( data_len < 1 || data_len > buf_len - 1 ) |
nexpaq | 1:55a6170b404f | 550 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 551 | |
nexpaq | 1:55a6170b404f | 552 | /* |
nexpaq | 1:55a6170b404f | 553 | * Save buffer start for read_binary and update buf |
nexpaq | 1:55a6170b404f | 554 | */ |
nexpaq | 1:55a6170b404f | 555 | buf_start = *buf; |
nexpaq | 1:55a6170b404f | 556 | *buf += data_len; |
nexpaq | 1:55a6170b404f | 557 | |
nexpaq | 1:55a6170b404f | 558 | return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ); |
nexpaq | 1:55a6170b404f | 559 | } |
nexpaq | 1:55a6170b404f | 560 | |
nexpaq | 1:55a6170b404f | 561 | /* |
nexpaq | 1:55a6170b404f | 562 | * Export a point as a TLS ECPoint record (RFC 4492) |
nexpaq | 1:55a6170b404f | 563 | * struct { |
nexpaq | 1:55a6170b404f | 564 | * opaque point <1..2^8-1>; |
nexpaq | 1:55a6170b404f | 565 | * } ECPoint; |
nexpaq | 1:55a6170b404f | 566 | */ |
nexpaq | 1:55a6170b404f | 567 | int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, |
nexpaq | 1:55a6170b404f | 568 | int format, size_t *olen, |
nexpaq | 1:55a6170b404f | 569 | unsigned char *buf, size_t blen ) |
nexpaq | 1:55a6170b404f | 570 | { |
nexpaq | 1:55a6170b404f | 571 | int ret; |
nexpaq | 1:55a6170b404f | 572 | |
nexpaq | 1:55a6170b404f | 573 | /* |
nexpaq | 1:55a6170b404f | 574 | * buffer length must be at least one, for our length byte |
nexpaq | 1:55a6170b404f | 575 | */ |
nexpaq | 1:55a6170b404f | 576 | if( blen < 1 ) |
nexpaq | 1:55a6170b404f | 577 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 578 | |
nexpaq | 1:55a6170b404f | 579 | if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format, |
nexpaq | 1:55a6170b404f | 580 | olen, buf + 1, blen - 1) ) != 0 ) |
nexpaq | 1:55a6170b404f | 581 | return( ret ); |
nexpaq | 1:55a6170b404f | 582 | |
nexpaq | 1:55a6170b404f | 583 | /* |
nexpaq | 1:55a6170b404f | 584 | * write length to the first byte and update total length |
nexpaq | 1:55a6170b404f | 585 | */ |
nexpaq | 1:55a6170b404f | 586 | buf[0] = (unsigned char) *olen; |
nexpaq | 1:55a6170b404f | 587 | ++*olen; |
nexpaq | 1:55a6170b404f | 588 | |
nexpaq | 1:55a6170b404f | 589 | return( 0 ); |
nexpaq | 1:55a6170b404f | 590 | } |
nexpaq | 1:55a6170b404f | 591 | |
nexpaq | 1:55a6170b404f | 592 | /* |
nexpaq | 1:55a6170b404f | 593 | * Set a group from an ECParameters record (RFC 4492) |
nexpaq | 1:55a6170b404f | 594 | */ |
nexpaq | 1:55a6170b404f | 595 | int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len ) |
nexpaq | 1:55a6170b404f | 596 | { |
nexpaq | 1:55a6170b404f | 597 | uint16_t tls_id; |
nexpaq | 1:55a6170b404f | 598 | const mbedtls_ecp_curve_info *curve_info; |
nexpaq | 1:55a6170b404f | 599 | |
nexpaq | 1:55a6170b404f | 600 | /* |
nexpaq | 1:55a6170b404f | 601 | * We expect at least three bytes (see below) |
nexpaq | 1:55a6170b404f | 602 | */ |
nexpaq | 1:55a6170b404f | 603 | if( len < 3 ) |
nexpaq | 1:55a6170b404f | 604 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 605 | |
nexpaq | 1:55a6170b404f | 606 | /* |
nexpaq | 1:55a6170b404f | 607 | * First byte is curve_type; only named_curve is handled |
nexpaq | 1:55a6170b404f | 608 | */ |
nexpaq | 1:55a6170b404f | 609 | if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE ) |
nexpaq | 1:55a6170b404f | 610 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 611 | |
nexpaq | 1:55a6170b404f | 612 | /* |
nexpaq | 1:55a6170b404f | 613 | * Next two bytes are the namedcurve value |
nexpaq | 1:55a6170b404f | 614 | */ |
nexpaq | 1:55a6170b404f | 615 | tls_id = *(*buf)++; |
nexpaq | 1:55a6170b404f | 616 | tls_id <<= 8; |
nexpaq | 1:55a6170b404f | 617 | tls_id |= *(*buf)++; |
nexpaq | 1:55a6170b404f | 618 | |
nexpaq | 1:55a6170b404f | 619 | if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL ) |
nexpaq | 1:55a6170b404f | 620 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
nexpaq | 1:55a6170b404f | 621 | |
nexpaq | 1:55a6170b404f | 622 | return mbedtls_ecp_group_load( grp, curve_info->grp_id ); |
nexpaq | 1:55a6170b404f | 623 | } |
nexpaq | 1:55a6170b404f | 624 | |
nexpaq | 1:55a6170b404f | 625 | /* |
nexpaq | 1:55a6170b404f | 626 | * Write the ECParameters record corresponding to a group (RFC 4492) |
nexpaq | 1:55a6170b404f | 627 | */ |
nexpaq | 1:55a6170b404f | 628 | int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, |
nexpaq | 1:55a6170b404f | 629 | unsigned char *buf, size_t blen ) |
nexpaq | 1:55a6170b404f | 630 | { |
nexpaq | 1:55a6170b404f | 631 | const mbedtls_ecp_curve_info *curve_info; |
nexpaq | 1:55a6170b404f | 632 | |
nexpaq | 1:55a6170b404f | 633 | if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL ) |
nexpaq | 1:55a6170b404f | 634 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 635 | |
nexpaq | 1:55a6170b404f | 636 | /* |
nexpaq | 1:55a6170b404f | 637 | * We are going to write 3 bytes (see below) |
nexpaq | 1:55a6170b404f | 638 | */ |
nexpaq | 1:55a6170b404f | 639 | *olen = 3; |
nexpaq | 1:55a6170b404f | 640 | if( blen < *olen ) |
nexpaq | 1:55a6170b404f | 641 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
nexpaq | 1:55a6170b404f | 642 | |
nexpaq | 1:55a6170b404f | 643 | /* |
nexpaq | 1:55a6170b404f | 644 | * First byte is curve_type, always named_curve |
nexpaq | 1:55a6170b404f | 645 | */ |
nexpaq | 1:55a6170b404f | 646 | *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE; |
nexpaq | 1:55a6170b404f | 647 | |
nexpaq | 1:55a6170b404f | 648 | /* |
nexpaq | 1:55a6170b404f | 649 | * Next two bytes are the namedcurve value |
nexpaq | 1:55a6170b404f | 650 | */ |
nexpaq | 1:55a6170b404f | 651 | buf[0] = curve_info->tls_id >> 8; |
nexpaq | 1:55a6170b404f | 652 | buf[1] = curve_info->tls_id & 0xFF; |
nexpaq | 1:55a6170b404f | 653 | |
nexpaq | 1:55a6170b404f | 654 | return( 0 ); |
nexpaq | 1:55a6170b404f | 655 | } |
nexpaq | 1:55a6170b404f | 656 | |
nexpaq | 1:55a6170b404f | 657 | /* |
nexpaq | 1:55a6170b404f | 658 | * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi. |
nexpaq | 1:55a6170b404f | 659 | * See the documentation of struct mbedtls_ecp_group. |
nexpaq | 1:55a6170b404f | 660 | * |
nexpaq | 1:55a6170b404f | 661 | * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf. |
nexpaq | 1:55a6170b404f | 662 | */ |
nexpaq | 1:55a6170b404f | 663 | static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp ) |
nexpaq | 1:55a6170b404f | 664 | { |
nexpaq | 1:55a6170b404f | 665 | int ret; |
nexpaq | 1:55a6170b404f | 666 | |
nexpaq | 1:55a6170b404f | 667 | if( grp->modp == NULL ) |
nexpaq | 1:55a6170b404f | 668 | return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) ); |
nexpaq | 1:55a6170b404f | 669 | |
nexpaq | 1:55a6170b404f | 670 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
nexpaq | 1:55a6170b404f | 671 | if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) || |
nexpaq | 1:55a6170b404f | 672 | mbedtls_mpi_bitlen( N ) > 2 * grp->pbits ) |
nexpaq | 1:55a6170b404f | 673 | { |
nexpaq | 1:55a6170b404f | 674 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 675 | } |
nexpaq | 1:55a6170b404f | 676 | |
nexpaq | 1:55a6170b404f | 677 | MBEDTLS_MPI_CHK( grp->modp( N ) ); |
nexpaq | 1:55a6170b404f | 678 | |
nexpaq | 1:55a6170b404f | 679 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
nexpaq | 1:55a6170b404f | 680 | while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) |
nexpaq | 1:55a6170b404f | 681 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) ); |
nexpaq | 1:55a6170b404f | 682 | |
nexpaq | 1:55a6170b404f | 683 | while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 ) |
nexpaq | 1:55a6170b404f | 684 | /* we known P, N and the result are positive */ |
nexpaq | 1:55a6170b404f | 685 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) ); |
nexpaq | 1:55a6170b404f | 686 | |
nexpaq | 1:55a6170b404f | 687 | cleanup: |
nexpaq | 1:55a6170b404f | 688 | return( ret ); |
nexpaq | 1:55a6170b404f | 689 | } |
nexpaq | 1:55a6170b404f | 690 | |
nexpaq | 1:55a6170b404f | 691 | /* |
nexpaq | 1:55a6170b404f | 692 | * Fast mod-p functions expect their argument to be in the 0..p^2 range. |
nexpaq | 1:55a6170b404f | 693 | * |
nexpaq | 1:55a6170b404f | 694 | * In order to guarantee that, we need to ensure that operands of |
nexpaq | 1:55a6170b404f | 695 | * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will |
nexpaq | 1:55a6170b404f | 696 | * bring the result back to this range. |
nexpaq | 1:55a6170b404f | 697 | * |
nexpaq | 1:55a6170b404f | 698 | * The following macros are shortcuts for doing that. |
nexpaq | 1:55a6170b404f | 699 | */ |
nexpaq | 1:55a6170b404f | 700 | |
nexpaq | 1:55a6170b404f | 701 | /* |
nexpaq | 1:55a6170b404f | 702 | * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi |
nexpaq | 1:55a6170b404f | 703 | */ |
nexpaq | 1:55a6170b404f | 704 | #if defined(MBEDTLS_SELF_TEST) |
nexpaq | 1:55a6170b404f | 705 | #define INC_MUL_COUNT mul_count++; |
nexpaq | 1:55a6170b404f | 706 | #else |
nexpaq | 1:55a6170b404f | 707 | #define INC_MUL_COUNT |
nexpaq | 1:55a6170b404f | 708 | #endif |
nexpaq | 1:55a6170b404f | 709 | |
nexpaq | 1:55a6170b404f | 710 | #define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \ |
nexpaq | 1:55a6170b404f | 711 | while( 0 ) |
nexpaq | 1:55a6170b404f | 712 | |
nexpaq | 1:55a6170b404f | 713 | /* |
nexpaq | 1:55a6170b404f | 714 | * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi |
nexpaq | 1:55a6170b404f | 715 | * N->s < 0 is a very fast test, which fails only if N is 0 |
nexpaq | 1:55a6170b404f | 716 | */ |
nexpaq | 1:55a6170b404f | 717 | #define MOD_SUB( N ) \ |
nexpaq | 1:55a6170b404f | 718 | while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \ |
nexpaq | 1:55a6170b404f | 719 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) ) |
nexpaq | 1:55a6170b404f | 720 | |
nexpaq | 1:55a6170b404f | 721 | /* |
nexpaq | 1:55a6170b404f | 722 | * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int. |
nexpaq | 1:55a6170b404f | 723 | * We known P, N and the result are positive, so sub_abs is correct, and |
nexpaq | 1:55a6170b404f | 724 | * a bit faster. |
nexpaq | 1:55a6170b404f | 725 | */ |
nexpaq | 1:55a6170b404f | 726 | #define MOD_ADD( N ) \ |
nexpaq | 1:55a6170b404f | 727 | while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \ |
nexpaq | 1:55a6170b404f | 728 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) ) |
nexpaq | 1:55a6170b404f | 729 | |
nexpaq | 1:55a6170b404f | 730 | #if defined(ECP_SHORTWEIERSTRASS) |
nexpaq | 1:55a6170b404f | 731 | /* |
nexpaq | 1:55a6170b404f | 732 | * For curves in short Weierstrass form, we do all the internal operations in |
nexpaq | 1:55a6170b404f | 733 | * Jacobian coordinates. |
nexpaq | 1:55a6170b404f | 734 | * |
nexpaq | 1:55a6170b404f | 735 | * For multiplication, we'll use a comb method with coutermeasueres against |
nexpaq | 1:55a6170b404f | 736 | * SPA, hence timing attacks. |
nexpaq | 1:55a6170b404f | 737 | */ |
nexpaq | 1:55a6170b404f | 738 | |
nexpaq | 1:55a6170b404f | 739 | /* |
nexpaq | 1:55a6170b404f | 740 | * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1) |
nexpaq | 1:55a6170b404f | 741 | * Cost: 1N := 1I + 3M + 1S |
nexpaq | 1:55a6170b404f | 742 | */ |
nexpaq | 1:55a6170b404f | 743 | static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 744 | { |
nexpaq | 1:55a6170b404f | 745 | int ret; |
nexpaq | 1:55a6170b404f | 746 | mbedtls_mpi Zi, ZZi; |
nexpaq | 1:55a6170b404f | 747 | |
nexpaq | 1:55a6170b404f | 748 | if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
nexpaq | 1:55a6170b404f | 749 | return( 0 ); |
nexpaq | 1:55a6170b404f | 750 | |
nexpaq | 1:55a6170b404f | 751 | mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi ); |
nexpaq | 1:55a6170b404f | 752 | |
nexpaq | 1:55a6170b404f | 753 | /* |
nexpaq | 1:55a6170b404f | 754 | * X = X / Z^2 mod p |
nexpaq | 1:55a6170b404f | 755 | */ |
nexpaq | 1:55a6170b404f | 756 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) ); |
nexpaq | 1:55a6170b404f | 757 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
nexpaq | 1:55a6170b404f | 758 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X ); |
nexpaq | 1:55a6170b404f | 759 | |
nexpaq | 1:55a6170b404f | 760 | /* |
nexpaq | 1:55a6170b404f | 761 | * Y = Y / Z^3 mod p |
nexpaq | 1:55a6170b404f | 762 | */ |
nexpaq | 1:55a6170b404f | 763 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y ); |
nexpaq | 1:55a6170b404f | 764 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y ); |
nexpaq | 1:55a6170b404f | 765 | |
nexpaq | 1:55a6170b404f | 766 | /* |
nexpaq | 1:55a6170b404f | 767 | * Z = 1 |
nexpaq | 1:55a6170b404f | 768 | */ |
nexpaq | 1:55a6170b404f | 769 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) ); |
nexpaq | 1:55a6170b404f | 770 | |
nexpaq | 1:55a6170b404f | 771 | cleanup: |
nexpaq | 1:55a6170b404f | 772 | |
nexpaq | 1:55a6170b404f | 773 | mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi ); |
nexpaq | 1:55a6170b404f | 774 | |
nexpaq | 1:55a6170b404f | 775 | return( ret ); |
nexpaq | 1:55a6170b404f | 776 | } |
nexpaq | 1:55a6170b404f | 777 | |
nexpaq | 1:55a6170b404f | 778 | /* |
nexpaq | 1:55a6170b404f | 779 | * Normalize jacobian coordinates of an array of (pointers to) points, |
nexpaq | 1:55a6170b404f | 780 | * using Montgomery's trick to perform only one inversion mod P. |
nexpaq | 1:55a6170b404f | 781 | * (See for example Cohen's "A Course in Computational Algebraic Number |
nexpaq | 1:55a6170b404f | 782 | * Theory", Algorithm 10.3.4.) |
nexpaq | 1:55a6170b404f | 783 | * |
nexpaq | 1:55a6170b404f | 784 | * Warning: fails (returning an error) if one of the points is zero! |
nexpaq | 1:55a6170b404f | 785 | * This should never happen, see choice of w in ecp_mul_comb(). |
nexpaq | 1:55a6170b404f | 786 | * |
nexpaq | 1:55a6170b404f | 787 | * Cost: 1N(t) := 1I + (6t - 3)M + 1S |
nexpaq | 1:55a6170b404f | 788 | */ |
nexpaq | 1:55a6170b404f | 789 | static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 790 | mbedtls_ecp_point *T[], size_t t_len ) |
nexpaq | 1:55a6170b404f | 791 | { |
nexpaq | 1:55a6170b404f | 792 | int ret; |
nexpaq | 1:55a6170b404f | 793 | size_t i; |
nexpaq | 1:55a6170b404f | 794 | mbedtls_mpi *c, u, Zi, ZZi; |
nexpaq | 1:55a6170b404f | 795 | |
nexpaq | 1:55a6170b404f | 796 | if( t_len < 2 ) |
nexpaq | 1:55a6170b404f | 797 | return( ecp_normalize_jac( grp, *T ) ); |
nexpaq | 1:55a6170b404f | 798 | |
nexpaq | 1:55a6170b404f | 799 | if( ( c = mbedtls_calloc( t_len, sizeof( mbedtls_mpi ) ) ) == NULL ) |
nexpaq | 1:55a6170b404f | 800 | return( MBEDTLS_ERR_ECP_ALLOC_FAILED ); |
nexpaq | 1:55a6170b404f | 801 | |
nexpaq | 1:55a6170b404f | 802 | mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi ); |
nexpaq | 1:55a6170b404f | 803 | |
nexpaq | 1:55a6170b404f | 804 | /* |
nexpaq | 1:55a6170b404f | 805 | * c[i] = Z_0 * ... * Z_i |
nexpaq | 1:55a6170b404f | 806 | */ |
nexpaq | 1:55a6170b404f | 807 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) ); |
nexpaq | 1:55a6170b404f | 808 | for( i = 1; i < t_len; i++ ) |
nexpaq | 1:55a6170b404f | 809 | { |
nexpaq | 1:55a6170b404f | 810 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) ); |
nexpaq | 1:55a6170b404f | 811 | MOD_MUL( c[i] ); |
nexpaq | 1:55a6170b404f | 812 | } |
nexpaq | 1:55a6170b404f | 813 | |
nexpaq | 1:55a6170b404f | 814 | /* |
nexpaq | 1:55a6170b404f | 815 | * u = 1 / (Z_0 * ... * Z_n) mod P |
nexpaq | 1:55a6170b404f | 816 | */ |
nexpaq | 1:55a6170b404f | 817 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) ); |
nexpaq | 1:55a6170b404f | 818 | |
nexpaq | 1:55a6170b404f | 819 | for( i = t_len - 1; ; i-- ) |
nexpaq | 1:55a6170b404f | 820 | { |
nexpaq | 1:55a6170b404f | 821 | /* |
nexpaq | 1:55a6170b404f | 822 | * Zi = 1 / Z_i mod p |
nexpaq | 1:55a6170b404f | 823 | * u = 1 / (Z_0 * ... * Z_i) mod P |
nexpaq | 1:55a6170b404f | 824 | */ |
nexpaq | 1:55a6170b404f | 825 | if( i == 0 ) { |
nexpaq | 1:55a6170b404f | 826 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) ); |
nexpaq | 1:55a6170b404f | 827 | } |
nexpaq | 1:55a6170b404f | 828 | else |
nexpaq | 1:55a6170b404f | 829 | { |
nexpaq | 1:55a6170b404f | 830 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi ); |
nexpaq | 1:55a6170b404f | 831 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u ); |
nexpaq | 1:55a6170b404f | 832 | } |
nexpaq | 1:55a6170b404f | 833 | |
nexpaq | 1:55a6170b404f | 834 | /* |
nexpaq | 1:55a6170b404f | 835 | * proceed as in normalize() |
nexpaq | 1:55a6170b404f | 836 | */ |
nexpaq | 1:55a6170b404f | 837 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
nexpaq | 1:55a6170b404f | 838 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X ); |
nexpaq | 1:55a6170b404f | 839 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y ); |
nexpaq | 1:55a6170b404f | 840 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y ); |
nexpaq | 1:55a6170b404f | 841 | |
nexpaq | 1:55a6170b404f | 842 | /* |
nexpaq | 1:55a6170b404f | 843 | * Post-precessing: reclaim some memory by shrinking coordinates |
nexpaq | 1:55a6170b404f | 844 | * - not storing Z (always 1) |
nexpaq | 1:55a6170b404f | 845 | * - shrinking other coordinates, but still keeping the same number of |
nexpaq | 1:55a6170b404f | 846 | * limbs as P, as otherwise it will too likely be regrown too fast. |
nexpaq | 1:55a6170b404f | 847 | */ |
nexpaq | 1:55a6170b404f | 848 | MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) ); |
nexpaq | 1:55a6170b404f | 849 | MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) ); |
nexpaq | 1:55a6170b404f | 850 | mbedtls_mpi_free( &T[i]->Z ); |
nexpaq | 1:55a6170b404f | 851 | |
nexpaq | 1:55a6170b404f | 852 | if( i == 0 ) |
nexpaq | 1:55a6170b404f | 853 | break; |
nexpaq | 1:55a6170b404f | 854 | } |
nexpaq | 1:55a6170b404f | 855 | |
nexpaq | 1:55a6170b404f | 856 | cleanup: |
nexpaq | 1:55a6170b404f | 857 | |
nexpaq | 1:55a6170b404f | 858 | mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi ); |
nexpaq | 1:55a6170b404f | 859 | for( i = 0; i < t_len; i++ ) |
nexpaq | 1:55a6170b404f | 860 | mbedtls_mpi_free( &c[i] ); |
nexpaq | 1:55a6170b404f | 861 | mbedtls_free( c ); |
nexpaq | 1:55a6170b404f | 862 | |
nexpaq | 1:55a6170b404f | 863 | return( ret ); |
nexpaq | 1:55a6170b404f | 864 | } |
nexpaq | 1:55a6170b404f | 865 | |
nexpaq | 1:55a6170b404f | 866 | /* |
nexpaq | 1:55a6170b404f | 867 | * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak. |
nexpaq | 1:55a6170b404f | 868 | * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid |
nexpaq | 1:55a6170b404f | 869 | */ |
nexpaq | 1:55a6170b404f | 870 | static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 871 | mbedtls_ecp_point *Q, |
nexpaq | 1:55a6170b404f | 872 | unsigned char inv ) |
nexpaq | 1:55a6170b404f | 873 | { |
nexpaq | 1:55a6170b404f | 874 | int ret; |
nexpaq | 1:55a6170b404f | 875 | unsigned char nonzero; |
nexpaq | 1:55a6170b404f | 876 | mbedtls_mpi mQY; |
nexpaq | 1:55a6170b404f | 877 | |
nexpaq | 1:55a6170b404f | 878 | mbedtls_mpi_init( &mQY ); |
nexpaq | 1:55a6170b404f | 879 | |
nexpaq | 1:55a6170b404f | 880 | /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */ |
nexpaq | 1:55a6170b404f | 881 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) ); |
nexpaq | 1:55a6170b404f | 882 | nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0; |
nexpaq | 1:55a6170b404f | 883 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) ); |
nexpaq | 1:55a6170b404f | 884 | |
nexpaq | 1:55a6170b404f | 885 | cleanup: |
nexpaq | 1:55a6170b404f | 886 | mbedtls_mpi_free( &mQY ); |
nexpaq | 1:55a6170b404f | 887 | |
nexpaq | 1:55a6170b404f | 888 | return( ret ); |
nexpaq | 1:55a6170b404f | 889 | } |
nexpaq | 1:55a6170b404f | 890 | |
nexpaq | 1:55a6170b404f | 891 | /* |
nexpaq | 1:55a6170b404f | 892 | * Point doubling R = 2 P, Jacobian coordinates |
nexpaq | 1:55a6170b404f | 893 | * |
nexpaq | 1:55a6170b404f | 894 | * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 . |
nexpaq | 1:55a6170b404f | 895 | * |
nexpaq | 1:55a6170b404f | 896 | * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR |
nexpaq | 1:55a6170b404f | 897 | * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring. |
nexpaq | 1:55a6170b404f | 898 | * |
nexpaq | 1:55a6170b404f | 899 | * Standard optimizations are applied when curve parameter A is one of { 0, -3 }. |
nexpaq | 1:55a6170b404f | 900 | * |
nexpaq | 1:55a6170b404f | 901 | * Cost: 1D := 3M + 4S (A == 0) |
nexpaq | 1:55a6170b404f | 902 | * 4M + 4S (A == -3) |
nexpaq | 1:55a6170b404f | 903 | * 3M + 6S + 1a otherwise |
nexpaq | 1:55a6170b404f | 904 | */ |
nexpaq | 1:55a6170b404f | 905 | static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 906 | const mbedtls_ecp_point *P ) |
nexpaq | 1:55a6170b404f | 907 | { |
nexpaq | 1:55a6170b404f | 908 | int ret; |
nexpaq | 1:55a6170b404f | 909 | mbedtls_mpi M, S, T, U; |
nexpaq | 1:55a6170b404f | 910 | |
nexpaq | 1:55a6170b404f | 911 | #if defined(MBEDTLS_SELF_TEST) |
nexpaq | 1:55a6170b404f | 912 | dbl_count++; |
nexpaq | 1:55a6170b404f | 913 | #endif |
nexpaq | 1:55a6170b404f | 914 | |
nexpaq | 1:55a6170b404f | 915 | mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U ); |
nexpaq | 1:55a6170b404f | 916 | |
nexpaq | 1:55a6170b404f | 917 | /* Special case for A = -3 */ |
nexpaq | 1:55a6170b404f | 918 | if( grp->A.p == NULL ) |
nexpaq | 1:55a6170b404f | 919 | { |
nexpaq | 1:55a6170b404f | 920 | /* M = 3(X + Z^2)(X - Z^2) */ |
nexpaq | 1:55a6170b404f | 921 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 922 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T ); |
nexpaq | 1:55a6170b404f | 923 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U ); |
nexpaq | 1:55a6170b404f | 924 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 925 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M ); |
nexpaq | 1:55a6170b404f | 926 | } |
nexpaq | 1:55a6170b404f | 927 | else |
nexpaq | 1:55a6170b404f | 928 | { |
nexpaq | 1:55a6170b404f | 929 | /* M = 3.X^2 */ |
nexpaq | 1:55a6170b404f | 930 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 931 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M ); |
nexpaq | 1:55a6170b404f | 932 | |
nexpaq | 1:55a6170b404f | 933 | /* Optimize away for "koblitz" curves with A = 0 */ |
nexpaq | 1:55a6170b404f | 934 | if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 ) |
nexpaq | 1:55a6170b404f | 935 | { |
nexpaq | 1:55a6170b404f | 936 | /* M += A.Z^4 */ |
nexpaq | 1:55a6170b404f | 937 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 938 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T ); |
nexpaq | 1:55a6170b404f | 939 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 940 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M ); |
nexpaq | 1:55a6170b404f | 941 | } |
nexpaq | 1:55a6170b404f | 942 | } |
nexpaq | 1:55a6170b404f | 943 | |
nexpaq | 1:55a6170b404f | 944 | /* S = 4.X.Y^2 */ |
nexpaq | 1:55a6170b404f | 945 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T ); |
nexpaq | 1:55a6170b404f | 946 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T ); |
nexpaq | 1:55a6170b404f | 947 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 948 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S ); |
nexpaq | 1:55a6170b404f | 949 | |
nexpaq | 1:55a6170b404f | 950 | /* U = 8.Y^4 */ |
nexpaq | 1:55a6170b404f | 951 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U ); |
nexpaq | 1:55a6170b404f | 952 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U ); |
nexpaq | 1:55a6170b404f | 953 | |
nexpaq | 1:55a6170b404f | 954 | /* T = M^2 - 2.S */ |
nexpaq | 1:55a6170b404f | 955 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T ); |
nexpaq | 1:55a6170b404f | 956 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T ); |
nexpaq | 1:55a6170b404f | 957 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T ); |
nexpaq | 1:55a6170b404f | 958 | |
nexpaq | 1:55a6170b404f | 959 | /* S = M(S - T) - U */ |
nexpaq | 1:55a6170b404f | 960 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S ); |
nexpaq | 1:55a6170b404f | 961 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S ); |
nexpaq | 1:55a6170b404f | 962 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S ); |
nexpaq | 1:55a6170b404f | 963 | |
nexpaq | 1:55a6170b404f | 964 | /* U = 2.Y.Z */ |
nexpaq | 1:55a6170b404f | 965 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U ); |
nexpaq | 1:55a6170b404f | 966 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U ); |
nexpaq | 1:55a6170b404f | 967 | |
nexpaq | 1:55a6170b404f | 968 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) ); |
nexpaq | 1:55a6170b404f | 969 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) ); |
nexpaq | 1:55a6170b404f | 970 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) ); |
nexpaq | 1:55a6170b404f | 971 | |
nexpaq | 1:55a6170b404f | 972 | cleanup: |
nexpaq | 1:55a6170b404f | 973 | mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U ); |
nexpaq | 1:55a6170b404f | 974 | |
nexpaq | 1:55a6170b404f | 975 | return( ret ); |
nexpaq | 1:55a6170b404f | 976 | } |
nexpaq | 1:55a6170b404f | 977 | |
nexpaq | 1:55a6170b404f | 978 | /* |
nexpaq | 1:55a6170b404f | 979 | * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22) |
nexpaq | 1:55a6170b404f | 980 | * |
nexpaq | 1:55a6170b404f | 981 | * The coordinates of Q must be normalized (= affine), |
nexpaq | 1:55a6170b404f | 982 | * but those of P don't need to. R is not normalized. |
nexpaq | 1:55a6170b404f | 983 | * |
nexpaq | 1:55a6170b404f | 984 | * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q. |
nexpaq | 1:55a6170b404f | 985 | * None of these cases can happen as intermediate step in ecp_mul_comb(): |
nexpaq | 1:55a6170b404f | 986 | * - at each step, P, Q and R are multiples of the base point, the factor |
nexpaq | 1:55a6170b404f | 987 | * being less than its order, so none of them is zero; |
nexpaq | 1:55a6170b404f | 988 | * - Q is an odd multiple of the base point, P an even multiple, |
nexpaq | 1:55a6170b404f | 989 | * due to the choice of precomputed points in the modified comb method. |
nexpaq | 1:55a6170b404f | 990 | * So branches for these cases do not leak secret information. |
nexpaq | 1:55a6170b404f | 991 | * |
nexpaq | 1:55a6170b404f | 992 | * We accept Q->Z being unset (saving memory in tables) as meaning 1. |
nexpaq | 1:55a6170b404f | 993 | * |
nexpaq | 1:55a6170b404f | 994 | * Cost: 1A := 8M + 3S |
nexpaq | 1:55a6170b404f | 995 | */ |
nexpaq | 1:55a6170b404f | 996 | static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 997 | const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ) |
nexpaq | 1:55a6170b404f | 998 | { |
nexpaq | 1:55a6170b404f | 999 | int ret; |
nexpaq | 1:55a6170b404f | 1000 | mbedtls_mpi T1, T2, T3, T4, X, Y, Z; |
nexpaq | 1:55a6170b404f | 1001 | |
nexpaq | 1:55a6170b404f | 1002 | #if defined(MBEDTLS_SELF_TEST) |
nexpaq | 1:55a6170b404f | 1003 | add_count++; |
nexpaq | 1:55a6170b404f | 1004 | #endif |
nexpaq | 1:55a6170b404f | 1005 | |
nexpaq | 1:55a6170b404f | 1006 | /* |
nexpaq | 1:55a6170b404f | 1007 | * Trivial cases: P == 0 or Q == 0 (case 1) |
nexpaq | 1:55a6170b404f | 1008 | */ |
nexpaq | 1:55a6170b404f | 1009 | if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 ) |
nexpaq | 1:55a6170b404f | 1010 | return( mbedtls_ecp_copy( R, Q ) ); |
nexpaq | 1:55a6170b404f | 1011 | |
nexpaq | 1:55a6170b404f | 1012 | if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 ) |
nexpaq | 1:55a6170b404f | 1013 | return( mbedtls_ecp_copy( R, P ) ); |
nexpaq | 1:55a6170b404f | 1014 | |
nexpaq | 1:55a6170b404f | 1015 | /* |
nexpaq | 1:55a6170b404f | 1016 | * Make sure Q coordinates are normalized |
nexpaq | 1:55a6170b404f | 1017 | */ |
nexpaq | 1:55a6170b404f | 1018 | if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 ) |
nexpaq | 1:55a6170b404f | 1019 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1020 | |
nexpaq | 1:55a6170b404f | 1021 | mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 ); |
nexpaq | 1:55a6170b404f | 1022 | mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); |
nexpaq | 1:55a6170b404f | 1023 | |
nexpaq | 1:55a6170b404f | 1024 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
nexpaq | 1:55a6170b404f | 1025 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 ); |
nexpaq | 1:55a6170b404f | 1026 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 ); |
nexpaq | 1:55a6170b404f | 1027 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 ); |
nexpaq | 1:55a6170b404f | 1028 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 ); |
nexpaq | 1:55a6170b404f | 1029 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 ); |
nexpaq | 1:55a6170b404f | 1030 | |
nexpaq | 1:55a6170b404f | 1031 | /* Special cases (2) and (3) */ |
nexpaq | 1:55a6170b404f | 1032 | if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 ) |
nexpaq | 1:55a6170b404f | 1033 | { |
nexpaq | 1:55a6170b404f | 1034 | if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 ) |
nexpaq | 1:55a6170b404f | 1035 | { |
nexpaq | 1:55a6170b404f | 1036 | ret = ecp_double_jac( grp, R, P ); |
nexpaq | 1:55a6170b404f | 1037 | goto cleanup; |
nexpaq | 1:55a6170b404f | 1038 | } |
nexpaq | 1:55a6170b404f | 1039 | else |
nexpaq | 1:55a6170b404f | 1040 | { |
nexpaq | 1:55a6170b404f | 1041 | ret = mbedtls_ecp_set_zero( R ); |
nexpaq | 1:55a6170b404f | 1042 | goto cleanup; |
nexpaq | 1:55a6170b404f | 1043 | } |
nexpaq | 1:55a6170b404f | 1044 | } |
nexpaq | 1:55a6170b404f | 1045 | |
nexpaq | 1:55a6170b404f | 1046 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z ); |
nexpaq | 1:55a6170b404f | 1047 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 ); |
nexpaq | 1:55a6170b404f | 1048 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 ); |
nexpaq | 1:55a6170b404f | 1049 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 ); |
nexpaq | 1:55a6170b404f | 1050 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
nexpaq | 1:55a6170b404f | 1051 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
nexpaq | 1:55a6170b404f | 1052 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
nexpaq | 1:55a6170b404f | 1053 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X ); |
nexpaq | 1:55a6170b404f | 1054 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 ); |
nexpaq | 1:55a6170b404f | 1055 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 ); |
nexpaq | 1:55a6170b404f | 1056 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 ); |
nexpaq | 1:55a6170b404f | 1057 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y ); |
nexpaq | 1:55a6170b404f | 1058 | |
nexpaq | 1:55a6170b404f | 1059 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) ); |
nexpaq | 1:55a6170b404f | 1060 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) ); |
nexpaq | 1:55a6170b404f | 1061 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) ); |
nexpaq | 1:55a6170b404f | 1062 | |
nexpaq | 1:55a6170b404f | 1063 | cleanup: |
nexpaq | 1:55a6170b404f | 1064 | |
nexpaq | 1:55a6170b404f | 1065 | mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 ); |
nexpaq | 1:55a6170b404f | 1066 | mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); |
nexpaq | 1:55a6170b404f | 1067 | |
nexpaq | 1:55a6170b404f | 1068 | return( ret ); |
nexpaq | 1:55a6170b404f | 1069 | } |
nexpaq | 1:55a6170b404f | 1070 | |
nexpaq | 1:55a6170b404f | 1071 | /* |
nexpaq | 1:55a6170b404f | 1072 | * Randomize jacobian coordinates: |
nexpaq | 1:55a6170b404f | 1073 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l |
nexpaq | 1:55a6170b404f | 1074 | * This is sort of the reverse operation of ecp_normalize_jac(). |
nexpaq | 1:55a6170b404f | 1075 | * |
nexpaq | 1:55a6170b404f | 1076 | * This countermeasure was first suggested in [2]. |
nexpaq | 1:55a6170b404f | 1077 | */ |
nexpaq | 1:55a6170b404f | 1078 | static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
nexpaq | 1:55a6170b404f | 1079 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
nexpaq | 1:55a6170b404f | 1080 | { |
nexpaq | 1:55a6170b404f | 1081 | int ret; |
nexpaq | 1:55a6170b404f | 1082 | mbedtls_mpi l, ll; |
nexpaq | 1:55a6170b404f | 1083 | size_t p_size = ( grp->pbits + 7 ) / 8; |
nexpaq | 1:55a6170b404f | 1084 | int count = 0; |
nexpaq | 1:55a6170b404f | 1085 | |
nexpaq | 1:55a6170b404f | 1086 | mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll ); |
nexpaq | 1:55a6170b404f | 1087 | |
nexpaq | 1:55a6170b404f | 1088 | /* Generate l such that 1 < l < p */ |
nexpaq | 1:55a6170b404f | 1089 | do |
nexpaq | 1:55a6170b404f | 1090 | { |
nexpaq | 1:55a6170b404f | 1091 | mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ); |
nexpaq | 1:55a6170b404f | 1092 | |
nexpaq | 1:55a6170b404f | 1093 | while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
nexpaq | 1:55a6170b404f | 1094 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) ); |
nexpaq | 1:55a6170b404f | 1095 | |
nexpaq | 1:55a6170b404f | 1096 | if( count++ > 10 ) |
nexpaq | 1:55a6170b404f | 1097 | return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
nexpaq | 1:55a6170b404f | 1098 | } |
nexpaq | 1:55a6170b404f | 1099 | while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 ); |
nexpaq | 1:55a6170b404f | 1100 | |
nexpaq | 1:55a6170b404f | 1101 | /* Z = l * Z */ |
nexpaq | 1:55a6170b404f | 1102 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z ); |
nexpaq | 1:55a6170b404f | 1103 | |
nexpaq | 1:55a6170b404f | 1104 | /* X = l^2 * X */ |
nexpaq | 1:55a6170b404f | 1105 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll ); |
nexpaq | 1:55a6170b404f | 1106 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X ); |
nexpaq | 1:55a6170b404f | 1107 | |
nexpaq | 1:55a6170b404f | 1108 | /* Y = l^3 * Y */ |
nexpaq | 1:55a6170b404f | 1109 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll ); |
nexpaq | 1:55a6170b404f | 1110 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y ); |
nexpaq | 1:55a6170b404f | 1111 | |
nexpaq | 1:55a6170b404f | 1112 | cleanup: |
nexpaq | 1:55a6170b404f | 1113 | mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll ); |
nexpaq | 1:55a6170b404f | 1114 | |
nexpaq | 1:55a6170b404f | 1115 | return( ret ); |
nexpaq | 1:55a6170b404f | 1116 | } |
nexpaq | 1:55a6170b404f | 1117 | |
nexpaq | 1:55a6170b404f | 1118 | /* |
nexpaq | 1:55a6170b404f | 1119 | * Check and define parameters used by the comb method (see below for details) |
nexpaq | 1:55a6170b404f | 1120 | */ |
nexpaq | 1:55a6170b404f | 1121 | #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7 |
nexpaq | 1:55a6170b404f | 1122 | #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds" |
nexpaq | 1:55a6170b404f | 1123 | #endif |
nexpaq | 1:55a6170b404f | 1124 | |
nexpaq | 1:55a6170b404f | 1125 | /* d = ceil( n / w ) */ |
nexpaq | 1:55a6170b404f | 1126 | #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2 |
nexpaq | 1:55a6170b404f | 1127 | |
nexpaq | 1:55a6170b404f | 1128 | /* number of precomputed points */ |
nexpaq | 1:55a6170b404f | 1129 | #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) |
nexpaq | 1:55a6170b404f | 1130 | |
nexpaq | 1:55a6170b404f | 1131 | /* |
nexpaq | 1:55a6170b404f | 1132 | * Compute the representation of m that will be used with our comb method. |
nexpaq | 1:55a6170b404f | 1133 | * |
nexpaq | 1:55a6170b404f | 1134 | * The basic comb method is described in GECC 3.44 for example. We use a |
nexpaq | 1:55a6170b404f | 1135 | * modified version that provides resistance to SPA by avoiding zero |
nexpaq | 1:55a6170b404f | 1136 | * digits in the representation as in [3]. We modify the method further by |
nexpaq | 1:55a6170b404f | 1137 | * requiring that all K_i be odd, which has the small cost that our |
nexpaq | 1:55a6170b404f | 1138 | * representation uses one more K_i, due to carries. |
nexpaq | 1:55a6170b404f | 1139 | * |
nexpaq | 1:55a6170b404f | 1140 | * Also, for the sake of compactness, only the seven low-order bits of x[i] |
nexpaq | 1:55a6170b404f | 1141 | * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in |
nexpaq | 1:55a6170b404f | 1142 | * the paper): it is set if and only if if s_i == -1; |
nexpaq | 1:55a6170b404f | 1143 | * |
nexpaq | 1:55a6170b404f | 1144 | * Calling conventions: |
nexpaq | 1:55a6170b404f | 1145 | * - x is an array of size d + 1 |
nexpaq | 1:55a6170b404f | 1146 | * - w is the size, ie number of teeth, of the comb, and must be between |
nexpaq | 1:55a6170b404f | 1147 | * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE) |
nexpaq | 1:55a6170b404f | 1148 | * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d |
nexpaq | 1:55a6170b404f | 1149 | * (the result will be incorrect if these assumptions are not satisfied) |
nexpaq | 1:55a6170b404f | 1150 | */ |
nexpaq | 1:55a6170b404f | 1151 | static void ecp_comb_fixed( unsigned char x[], size_t d, |
nexpaq | 1:55a6170b404f | 1152 | unsigned char w, const mbedtls_mpi *m ) |
nexpaq | 1:55a6170b404f | 1153 | { |
nexpaq | 1:55a6170b404f | 1154 | size_t i, j; |
nexpaq | 1:55a6170b404f | 1155 | unsigned char c, cc, adjust; |
nexpaq | 1:55a6170b404f | 1156 | |
nexpaq | 1:55a6170b404f | 1157 | memset( x, 0, d+1 ); |
nexpaq | 1:55a6170b404f | 1158 | |
nexpaq | 1:55a6170b404f | 1159 | /* First get the classical comb values (except for x_d = 0) */ |
nexpaq | 1:55a6170b404f | 1160 | for( i = 0; i < d; i++ ) |
nexpaq | 1:55a6170b404f | 1161 | for( j = 0; j < w; j++ ) |
nexpaq | 1:55a6170b404f | 1162 | x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j; |
nexpaq | 1:55a6170b404f | 1163 | |
nexpaq | 1:55a6170b404f | 1164 | /* Now make sure x_1 .. x_d are odd */ |
nexpaq | 1:55a6170b404f | 1165 | c = 0; |
nexpaq | 1:55a6170b404f | 1166 | for( i = 1; i <= d; i++ ) |
nexpaq | 1:55a6170b404f | 1167 | { |
nexpaq | 1:55a6170b404f | 1168 | /* Add carry and update it */ |
nexpaq | 1:55a6170b404f | 1169 | cc = x[i] & c; |
nexpaq | 1:55a6170b404f | 1170 | x[i] = x[i] ^ c; |
nexpaq | 1:55a6170b404f | 1171 | c = cc; |
nexpaq | 1:55a6170b404f | 1172 | |
nexpaq | 1:55a6170b404f | 1173 | /* Adjust if needed, avoiding branches */ |
nexpaq | 1:55a6170b404f | 1174 | adjust = 1 - ( x[i] & 0x01 ); |
nexpaq | 1:55a6170b404f | 1175 | c |= x[i] & ( x[i-1] * adjust ); |
nexpaq | 1:55a6170b404f | 1176 | x[i] = x[i] ^ ( x[i-1] * adjust ); |
nexpaq | 1:55a6170b404f | 1177 | x[i-1] |= adjust << 7; |
nexpaq | 1:55a6170b404f | 1178 | } |
nexpaq | 1:55a6170b404f | 1179 | } |
nexpaq | 1:55a6170b404f | 1180 | |
nexpaq | 1:55a6170b404f | 1181 | /* |
nexpaq | 1:55a6170b404f | 1182 | * Precompute points for the comb method |
nexpaq | 1:55a6170b404f | 1183 | * |
nexpaq | 1:55a6170b404f | 1184 | * If i = i_{w-1} ... i_1 is the binary representation of i, then |
nexpaq | 1:55a6170b404f | 1185 | * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P |
nexpaq | 1:55a6170b404f | 1186 | * |
nexpaq | 1:55a6170b404f | 1187 | * T must be able to hold 2^{w - 1} elements |
nexpaq | 1:55a6170b404f | 1188 | * |
nexpaq | 1:55a6170b404f | 1189 | * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1) |
nexpaq | 1:55a6170b404f | 1190 | */ |
nexpaq | 1:55a6170b404f | 1191 | static int ecp_precompute_comb( const mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 1192 | mbedtls_ecp_point T[], const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 1193 | unsigned char w, size_t d ) |
nexpaq | 1:55a6170b404f | 1194 | { |
nexpaq | 1:55a6170b404f | 1195 | int ret; |
nexpaq | 1:55a6170b404f | 1196 | unsigned char i, k; |
nexpaq | 1:55a6170b404f | 1197 | size_t j; |
nexpaq | 1:55a6170b404f | 1198 | mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1]; |
nexpaq | 1:55a6170b404f | 1199 | |
nexpaq | 1:55a6170b404f | 1200 | /* |
nexpaq | 1:55a6170b404f | 1201 | * Set T[0] = P and |
nexpaq | 1:55a6170b404f | 1202 | * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value) |
nexpaq | 1:55a6170b404f | 1203 | */ |
nexpaq | 1:55a6170b404f | 1204 | MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) ); |
nexpaq | 1:55a6170b404f | 1205 | |
nexpaq | 1:55a6170b404f | 1206 | k = 0; |
nexpaq | 1:55a6170b404f | 1207 | for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 ) |
nexpaq | 1:55a6170b404f | 1208 | { |
nexpaq | 1:55a6170b404f | 1209 | cur = T + i; |
nexpaq | 1:55a6170b404f | 1210 | MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) ); |
nexpaq | 1:55a6170b404f | 1211 | for( j = 0; j < d; j++ ) |
nexpaq | 1:55a6170b404f | 1212 | MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) ); |
nexpaq | 1:55a6170b404f | 1213 | |
nexpaq | 1:55a6170b404f | 1214 | TT[k++] = cur; |
nexpaq | 1:55a6170b404f | 1215 | } |
nexpaq | 1:55a6170b404f | 1216 | |
nexpaq | 1:55a6170b404f | 1217 | MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) ); |
nexpaq | 1:55a6170b404f | 1218 | |
nexpaq | 1:55a6170b404f | 1219 | /* |
nexpaq | 1:55a6170b404f | 1220 | * Compute the remaining ones using the minimal number of additions |
nexpaq | 1:55a6170b404f | 1221 | * Be careful to update T[2^l] only after using it! |
nexpaq | 1:55a6170b404f | 1222 | */ |
nexpaq | 1:55a6170b404f | 1223 | k = 0; |
nexpaq | 1:55a6170b404f | 1224 | for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 ) |
nexpaq | 1:55a6170b404f | 1225 | { |
nexpaq | 1:55a6170b404f | 1226 | j = i; |
nexpaq | 1:55a6170b404f | 1227 | while( j-- ) |
nexpaq | 1:55a6170b404f | 1228 | { |
nexpaq | 1:55a6170b404f | 1229 | MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) ); |
nexpaq | 1:55a6170b404f | 1230 | TT[k++] = &T[i + j]; |
nexpaq | 1:55a6170b404f | 1231 | } |
nexpaq | 1:55a6170b404f | 1232 | } |
nexpaq | 1:55a6170b404f | 1233 | |
nexpaq | 1:55a6170b404f | 1234 | MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) ); |
nexpaq | 1:55a6170b404f | 1235 | |
nexpaq | 1:55a6170b404f | 1236 | cleanup: |
nexpaq | 1:55a6170b404f | 1237 | return( ret ); |
nexpaq | 1:55a6170b404f | 1238 | } |
nexpaq | 1:55a6170b404f | 1239 | |
nexpaq | 1:55a6170b404f | 1240 | /* |
nexpaq | 1:55a6170b404f | 1241 | * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ] |
nexpaq | 1:55a6170b404f | 1242 | */ |
nexpaq | 1:55a6170b404f | 1243 | static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1244 | const mbedtls_ecp_point T[], unsigned char t_len, |
nexpaq | 1:55a6170b404f | 1245 | unsigned char i ) |
nexpaq | 1:55a6170b404f | 1246 | { |
nexpaq | 1:55a6170b404f | 1247 | int ret; |
nexpaq | 1:55a6170b404f | 1248 | unsigned char ii, j; |
nexpaq | 1:55a6170b404f | 1249 | |
nexpaq | 1:55a6170b404f | 1250 | /* Ignore the "sign" bit and scale down */ |
nexpaq | 1:55a6170b404f | 1251 | ii = ( i & 0x7Fu ) >> 1; |
nexpaq | 1:55a6170b404f | 1252 | |
nexpaq | 1:55a6170b404f | 1253 | /* Read the whole table to thwart cache-based timing attacks */ |
nexpaq | 1:55a6170b404f | 1254 | for( j = 0; j < t_len; j++ ) |
nexpaq | 1:55a6170b404f | 1255 | { |
nexpaq | 1:55a6170b404f | 1256 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) ); |
nexpaq | 1:55a6170b404f | 1257 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) ); |
nexpaq | 1:55a6170b404f | 1258 | } |
nexpaq | 1:55a6170b404f | 1259 | |
nexpaq | 1:55a6170b404f | 1260 | /* Safely invert result if i is "negative" */ |
nexpaq | 1:55a6170b404f | 1261 | MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) ); |
nexpaq | 1:55a6170b404f | 1262 | |
nexpaq | 1:55a6170b404f | 1263 | cleanup: |
nexpaq | 1:55a6170b404f | 1264 | return( ret ); |
nexpaq | 1:55a6170b404f | 1265 | } |
nexpaq | 1:55a6170b404f | 1266 | |
nexpaq | 1:55a6170b404f | 1267 | /* |
nexpaq | 1:55a6170b404f | 1268 | * Core multiplication algorithm for the (modified) comb method. |
nexpaq | 1:55a6170b404f | 1269 | * This part is actually common with the basic comb method (GECC 3.44) |
nexpaq | 1:55a6170b404f | 1270 | * |
nexpaq | 1:55a6170b404f | 1271 | * Cost: d A + d D + 1 R |
nexpaq | 1:55a6170b404f | 1272 | */ |
nexpaq | 1:55a6170b404f | 1273 | static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1274 | const mbedtls_ecp_point T[], unsigned char t_len, |
nexpaq | 1:55a6170b404f | 1275 | const unsigned char x[], size_t d, |
nexpaq | 1:55a6170b404f | 1276 | int (*f_rng)(void *, unsigned char *, size_t), |
nexpaq | 1:55a6170b404f | 1277 | void *p_rng ) |
nexpaq | 1:55a6170b404f | 1278 | { |
nexpaq | 1:55a6170b404f | 1279 | int ret; |
nexpaq | 1:55a6170b404f | 1280 | mbedtls_ecp_point Txi; |
nexpaq | 1:55a6170b404f | 1281 | size_t i; |
nexpaq | 1:55a6170b404f | 1282 | |
nexpaq | 1:55a6170b404f | 1283 | mbedtls_ecp_point_init( &Txi ); |
nexpaq | 1:55a6170b404f | 1284 | |
nexpaq | 1:55a6170b404f | 1285 | /* Start with a non-zero point and randomize its coordinates */ |
nexpaq | 1:55a6170b404f | 1286 | i = d; |
nexpaq | 1:55a6170b404f | 1287 | MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) ); |
nexpaq | 1:55a6170b404f | 1288 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) ); |
nexpaq | 1:55a6170b404f | 1289 | if( f_rng != 0 ) |
nexpaq | 1:55a6170b404f | 1290 | MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1291 | |
nexpaq | 1:55a6170b404f | 1292 | while( i-- != 0 ) |
nexpaq | 1:55a6170b404f | 1293 | { |
nexpaq | 1:55a6170b404f | 1294 | MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) ); |
nexpaq | 1:55a6170b404f | 1295 | MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) ); |
nexpaq | 1:55a6170b404f | 1296 | MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) ); |
nexpaq | 1:55a6170b404f | 1297 | } |
nexpaq | 1:55a6170b404f | 1298 | |
nexpaq | 1:55a6170b404f | 1299 | cleanup: |
nexpaq | 1:55a6170b404f | 1300 | mbedtls_ecp_point_free( &Txi ); |
nexpaq | 1:55a6170b404f | 1301 | |
nexpaq | 1:55a6170b404f | 1302 | return( ret ); |
nexpaq | 1:55a6170b404f | 1303 | } |
nexpaq | 1:55a6170b404f | 1304 | |
nexpaq | 1:55a6170b404f | 1305 | /* |
nexpaq | 1:55a6170b404f | 1306 | * Multiplication using the comb method, |
nexpaq | 1:55a6170b404f | 1307 | * for curves in short Weierstrass form |
nexpaq | 1:55a6170b404f | 1308 | */ |
nexpaq | 1:55a6170b404f | 1309 | static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1310 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 1311 | int (*f_rng)(void *, unsigned char *, size_t), |
nexpaq | 1:55a6170b404f | 1312 | void *p_rng ) |
nexpaq | 1:55a6170b404f | 1313 | { |
nexpaq | 1:55a6170b404f | 1314 | int ret; |
nexpaq | 1:55a6170b404f | 1315 | unsigned char w, m_is_odd, p_eq_g, pre_len, i; |
nexpaq | 1:55a6170b404f | 1316 | size_t d; |
nexpaq | 1:55a6170b404f | 1317 | unsigned char k[COMB_MAX_D + 1]; |
nexpaq | 1:55a6170b404f | 1318 | mbedtls_ecp_point *T; |
nexpaq | 1:55a6170b404f | 1319 | mbedtls_mpi M, mm; |
nexpaq | 1:55a6170b404f | 1320 | |
nexpaq | 1:55a6170b404f | 1321 | mbedtls_mpi_init( &M ); |
nexpaq | 1:55a6170b404f | 1322 | mbedtls_mpi_init( &mm ); |
nexpaq | 1:55a6170b404f | 1323 | |
nexpaq | 1:55a6170b404f | 1324 | /* we need N to be odd to trnaform m in an odd number, check now */ |
nexpaq | 1:55a6170b404f | 1325 | if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 ) |
nexpaq | 1:55a6170b404f | 1326 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1327 | |
nexpaq | 1:55a6170b404f | 1328 | /* |
nexpaq | 1:55a6170b404f | 1329 | * Minimize the number of multiplications, that is minimize |
nexpaq | 1:55a6170b404f | 1330 | * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w ) |
nexpaq | 1:55a6170b404f | 1331 | * (see costs of the various parts, with 1S = 1M) |
nexpaq | 1:55a6170b404f | 1332 | */ |
nexpaq | 1:55a6170b404f | 1333 | w = grp->nbits >= 384 ? 5 : 4; |
nexpaq | 1:55a6170b404f | 1334 | |
nexpaq | 1:55a6170b404f | 1335 | /* |
nexpaq | 1:55a6170b404f | 1336 | * If P == G, pre-compute a bit more, since this may be re-used later. |
nexpaq | 1:55a6170b404f | 1337 | * Just adding one avoids upping the cost of the first mul too much, |
nexpaq | 1:55a6170b404f | 1338 | * and the memory cost too. |
nexpaq | 1:55a6170b404f | 1339 | */ |
nexpaq | 1:55a6170b404f | 1340 | #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1 |
nexpaq | 1:55a6170b404f | 1341 | p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && |
nexpaq | 1:55a6170b404f | 1342 | mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 ); |
nexpaq | 1:55a6170b404f | 1343 | if( p_eq_g ) |
nexpaq | 1:55a6170b404f | 1344 | w++; |
nexpaq | 1:55a6170b404f | 1345 | #else |
nexpaq | 1:55a6170b404f | 1346 | p_eq_g = 0; |
nexpaq | 1:55a6170b404f | 1347 | #endif |
nexpaq | 1:55a6170b404f | 1348 | |
nexpaq | 1:55a6170b404f | 1349 | /* |
nexpaq | 1:55a6170b404f | 1350 | * Make sure w is within bounds. |
nexpaq | 1:55a6170b404f | 1351 | * (The last test is useful only for very small curves in the test suite.) |
nexpaq | 1:55a6170b404f | 1352 | */ |
nexpaq | 1:55a6170b404f | 1353 | if( w > MBEDTLS_ECP_WINDOW_SIZE ) |
nexpaq | 1:55a6170b404f | 1354 | w = MBEDTLS_ECP_WINDOW_SIZE; |
nexpaq | 1:55a6170b404f | 1355 | if( w >= grp->nbits ) |
nexpaq | 1:55a6170b404f | 1356 | w = 2; |
nexpaq | 1:55a6170b404f | 1357 | |
nexpaq | 1:55a6170b404f | 1358 | /* Other sizes that depend on w */ |
nexpaq | 1:55a6170b404f | 1359 | pre_len = 1U << ( w - 1 ); |
nexpaq | 1:55a6170b404f | 1360 | d = ( grp->nbits + w - 1 ) / w; |
nexpaq | 1:55a6170b404f | 1361 | |
nexpaq | 1:55a6170b404f | 1362 | /* |
nexpaq | 1:55a6170b404f | 1363 | * Prepare precomputed points: if P == G we want to |
nexpaq | 1:55a6170b404f | 1364 | * use grp->T if already initialized, or initialize it. |
nexpaq | 1:55a6170b404f | 1365 | */ |
nexpaq | 1:55a6170b404f | 1366 | T = p_eq_g ? grp->T : NULL; |
nexpaq | 1:55a6170b404f | 1367 | |
nexpaq | 1:55a6170b404f | 1368 | if( T == NULL ) |
nexpaq | 1:55a6170b404f | 1369 | { |
nexpaq | 1:55a6170b404f | 1370 | T = mbedtls_calloc( pre_len, sizeof( mbedtls_ecp_point ) ); |
nexpaq | 1:55a6170b404f | 1371 | if( T == NULL ) |
nexpaq | 1:55a6170b404f | 1372 | { |
nexpaq | 1:55a6170b404f | 1373 | ret = MBEDTLS_ERR_ECP_ALLOC_FAILED; |
nexpaq | 1:55a6170b404f | 1374 | goto cleanup; |
nexpaq | 1:55a6170b404f | 1375 | } |
nexpaq | 1:55a6170b404f | 1376 | |
nexpaq | 1:55a6170b404f | 1377 | MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) ); |
nexpaq | 1:55a6170b404f | 1378 | |
nexpaq | 1:55a6170b404f | 1379 | if( p_eq_g ) |
nexpaq | 1:55a6170b404f | 1380 | { |
nexpaq | 1:55a6170b404f | 1381 | grp->T = T; |
nexpaq | 1:55a6170b404f | 1382 | grp->T_size = pre_len; |
nexpaq | 1:55a6170b404f | 1383 | } |
nexpaq | 1:55a6170b404f | 1384 | } |
nexpaq | 1:55a6170b404f | 1385 | |
nexpaq | 1:55a6170b404f | 1386 | /* |
nexpaq | 1:55a6170b404f | 1387 | * Make sure M is odd (M = m or M = N - m, since N is odd) |
nexpaq | 1:55a6170b404f | 1388 | * using the fact that m * P = - (N - m) * P |
nexpaq | 1:55a6170b404f | 1389 | */ |
nexpaq | 1:55a6170b404f | 1390 | m_is_odd = ( mbedtls_mpi_get_bit( m, 0 ) == 1 ); |
nexpaq | 1:55a6170b404f | 1391 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) ); |
nexpaq | 1:55a6170b404f | 1392 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) ); |
nexpaq | 1:55a6170b404f | 1393 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) ); |
nexpaq | 1:55a6170b404f | 1394 | |
nexpaq | 1:55a6170b404f | 1395 | /* |
nexpaq | 1:55a6170b404f | 1396 | * Go for comb multiplication, R = M * P |
nexpaq | 1:55a6170b404f | 1397 | */ |
nexpaq | 1:55a6170b404f | 1398 | ecp_comb_fixed( k, d, w, &M ); |
nexpaq | 1:55a6170b404f | 1399 | MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1400 | |
nexpaq | 1:55a6170b404f | 1401 | /* |
nexpaq | 1:55a6170b404f | 1402 | * Now get m * P from M * P and normalize it |
nexpaq | 1:55a6170b404f | 1403 | */ |
nexpaq | 1:55a6170b404f | 1404 | MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) ); |
nexpaq | 1:55a6170b404f | 1405 | MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) ); |
nexpaq | 1:55a6170b404f | 1406 | |
nexpaq | 1:55a6170b404f | 1407 | cleanup: |
nexpaq | 1:55a6170b404f | 1408 | |
nexpaq | 1:55a6170b404f | 1409 | if( T != NULL && ! p_eq_g ) |
nexpaq | 1:55a6170b404f | 1410 | { |
nexpaq | 1:55a6170b404f | 1411 | for( i = 0; i < pre_len; i++ ) |
nexpaq | 1:55a6170b404f | 1412 | mbedtls_ecp_point_free( &T[i] ); |
nexpaq | 1:55a6170b404f | 1413 | mbedtls_free( T ); |
nexpaq | 1:55a6170b404f | 1414 | } |
nexpaq | 1:55a6170b404f | 1415 | |
nexpaq | 1:55a6170b404f | 1416 | mbedtls_mpi_free( &M ); |
nexpaq | 1:55a6170b404f | 1417 | mbedtls_mpi_free( &mm ); |
nexpaq | 1:55a6170b404f | 1418 | |
nexpaq | 1:55a6170b404f | 1419 | if( ret != 0 ) |
nexpaq | 1:55a6170b404f | 1420 | mbedtls_ecp_point_free( R ); |
nexpaq | 1:55a6170b404f | 1421 | |
nexpaq | 1:55a6170b404f | 1422 | return( ret ); |
nexpaq | 1:55a6170b404f | 1423 | } |
nexpaq | 1:55a6170b404f | 1424 | |
nexpaq | 1:55a6170b404f | 1425 | #endif /* ECP_SHORTWEIERSTRASS */ |
nexpaq | 1:55a6170b404f | 1426 | |
nexpaq | 1:55a6170b404f | 1427 | #if defined(ECP_MONTGOMERY) |
nexpaq | 1:55a6170b404f | 1428 | /* |
nexpaq | 1:55a6170b404f | 1429 | * For Montgomery curves, we do all the internal arithmetic in projective |
nexpaq | 1:55a6170b404f | 1430 | * coordinates. Import/export of points uses only the x coordinates, which is |
nexpaq | 1:55a6170b404f | 1431 | * internaly represented as X / Z. |
nexpaq | 1:55a6170b404f | 1432 | * |
nexpaq | 1:55a6170b404f | 1433 | * For scalar multiplication, we'll use a Montgomery ladder. |
nexpaq | 1:55a6170b404f | 1434 | */ |
nexpaq | 1:55a6170b404f | 1435 | |
nexpaq | 1:55a6170b404f | 1436 | /* |
nexpaq | 1:55a6170b404f | 1437 | * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1 |
nexpaq | 1:55a6170b404f | 1438 | * Cost: 1M + 1I |
nexpaq | 1:55a6170b404f | 1439 | */ |
nexpaq | 1:55a6170b404f | 1440 | static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P ) |
nexpaq | 1:55a6170b404f | 1441 | { |
nexpaq | 1:55a6170b404f | 1442 | int ret; |
nexpaq | 1:55a6170b404f | 1443 | |
nexpaq | 1:55a6170b404f | 1444 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) ); |
nexpaq | 1:55a6170b404f | 1445 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X ); |
nexpaq | 1:55a6170b404f | 1446 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) ); |
nexpaq | 1:55a6170b404f | 1447 | |
nexpaq | 1:55a6170b404f | 1448 | cleanup: |
nexpaq | 1:55a6170b404f | 1449 | return( ret ); |
nexpaq | 1:55a6170b404f | 1450 | } |
nexpaq | 1:55a6170b404f | 1451 | |
nexpaq | 1:55a6170b404f | 1452 | /* |
nexpaq | 1:55a6170b404f | 1453 | * Randomize projective x/z coordinates: |
nexpaq | 1:55a6170b404f | 1454 | * (X, Z) -> (l X, l Z) for random l |
nexpaq | 1:55a6170b404f | 1455 | * This is sort of the reverse operation of ecp_normalize_mxz(). |
nexpaq | 1:55a6170b404f | 1456 | * |
nexpaq | 1:55a6170b404f | 1457 | * This countermeasure was first suggested in [2]. |
nexpaq | 1:55a6170b404f | 1458 | * Cost: 2M |
nexpaq | 1:55a6170b404f | 1459 | */ |
nexpaq | 1:55a6170b404f | 1460 | static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 1461 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
nexpaq | 1:55a6170b404f | 1462 | { |
nexpaq | 1:55a6170b404f | 1463 | int ret; |
nexpaq | 1:55a6170b404f | 1464 | mbedtls_mpi l; |
nexpaq | 1:55a6170b404f | 1465 | size_t p_size = ( grp->pbits + 7 ) / 8; |
nexpaq | 1:55a6170b404f | 1466 | int count = 0; |
nexpaq | 1:55a6170b404f | 1467 | |
nexpaq | 1:55a6170b404f | 1468 | mbedtls_mpi_init( &l ); |
nexpaq | 1:55a6170b404f | 1469 | |
nexpaq | 1:55a6170b404f | 1470 | /* Generate l such that 1 < l < p */ |
nexpaq | 1:55a6170b404f | 1471 | do |
nexpaq | 1:55a6170b404f | 1472 | { |
nexpaq | 1:55a6170b404f | 1473 | mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ); |
nexpaq | 1:55a6170b404f | 1474 | |
nexpaq | 1:55a6170b404f | 1475 | while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
nexpaq | 1:55a6170b404f | 1476 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) ); |
nexpaq | 1:55a6170b404f | 1477 | |
nexpaq | 1:55a6170b404f | 1478 | if( count++ > 10 ) |
nexpaq | 1:55a6170b404f | 1479 | return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
nexpaq | 1:55a6170b404f | 1480 | } |
nexpaq | 1:55a6170b404f | 1481 | while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 ); |
nexpaq | 1:55a6170b404f | 1482 | |
nexpaq | 1:55a6170b404f | 1483 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X ); |
nexpaq | 1:55a6170b404f | 1484 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z ); |
nexpaq | 1:55a6170b404f | 1485 | |
nexpaq | 1:55a6170b404f | 1486 | cleanup: |
nexpaq | 1:55a6170b404f | 1487 | mbedtls_mpi_free( &l ); |
nexpaq | 1:55a6170b404f | 1488 | |
nexpaq | 1:55a6170b404f | 1489 | return( ret ); |
nexpaq | 1:55a6170b404f | 1490 | } |
nexpaq | 1:55a6170b404f | 1491 | |
nexpaq | 1:55a6170b404f | 1492 | /* |
nexpaq | 1:55a6170b404f | 1493 | * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q), |
nexpaq | 1:55a6170b404f | 1494 | * for Montgomery curves in x/z coordinates. |
nexpaq | 1:55a6170b404f | 1495 | * |
nexpaq | 1:55a6170b404f | 1496 | * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3 |
nexpaq | 1:55a6170b404f | 1497 | * with |
nexpaq | 1:55a6170b404f | 1498 | * d = X1 |
nexpaq | 1:55a6170b404f | 1499 | * P = (X2, Z2) |
nexpaq | 1:55a6170b404f | 1500 | * Q = (X3, Z3) |
nexpaq | 1:55a6170b404f | 1501 | * R = (X4, Z4) |
nexpaq | 1:55a6170b404f | 1502 | * S = (X5, Z5) |
nexpaq | 1:55a6170b404f | 1503 | * and eliminating temporary variables tO, ..., t4. |
nexpaq | 1:55a6170b404f | 1504 | * |
nexpaq | 1:55a6170b404f | 1505 | * Cost: 5M + 4S |
nexpaq | 1:55a6170b404f | 1506 | */ |
nexpaq | 1:55a6170b404f | 1507 | static int ecp_double_add_mxz( const mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 1508 | mbedtls_ecp_point *R, mbedtls_ecp_point *S, |
nexpaq | 1:55a6170b404f | 1509 | const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q, |
nexpaq | 1:55a6170b404f | 1510 | const mbedtls_mpi *d ) |
nexpaq | 1:55a6170b404f | 1511 | { |
nexpaq | 1:55a6170b404f | 1512 | int ret; |
nexpaq | 1:55a6170b404f | 1513 | mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB; |
nexpaq | 1:55a6170b404f | 1514 | |
nexpaq | 1:55a6170b404f | 1515 | mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B ); |
nexpaq | 1:55a6170b404f | 1516 | mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C ); |
nexpaq | 1:55a6170b404f | 1517 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB ); |
nexpaq | 1:55a6170b404f | 1518 | |
nexpaq | 1:55a6170b404f | 1519 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A ); |
nexpaq | 1:55a6170b404f | 1520 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA ); |
nexpaq | 1:55a6170b404f | 1521 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B ); |
nexpaq | 1:55a6170b404f | 1522 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB ); |
nexpaq | 1:55a6170b404f | 1523 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E ); |
nexpaq | 1:55a6170b404f | 1524 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C ); |
nexpaq | 1:55a6170b404f | 1525 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D ); |
nexpaq | 1:55a6170b404f | 1526 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA ); |
nexpaq | 1:55a6170b404f | 1527 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB ); |
nexpaq | 1:55a6170b404f | 1528 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X ); |
nexpaq | 1:55a6170b404f | 1529 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X ); |
nexpaq | 1:55a6170b404f | 1530 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z ); |
nexpaq | 1:55a6170b404f | 1531 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z ); |
nexpaq | 1:55a6170b404f | 1532 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z ); |
nexpaq | 1:55a6170b404f | 1533 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X ); |
nexpaq | 1:55a6170b404f | 1534 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z ); |
nexpaq | 1:55a6170b404f | 1535 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z ); |
nexpaq | 1:55a6170b404f | 1536 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z ); |
nexpaq | 1:55a6170b404f | 1537 | |
nexpaq | 1:55a6170b404f | 1538 | cleanup: |
nexpaq | 1:55a6170b404f | 1539 | mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B ); |
nexpaq | 1:55a6170b404f | 1540 | mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C ); |
nexpaq | 1:55a6170b404f | 1541 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB ); |
nexpaq | 1:55a6170b404f | 1542 | |
nexpaq | 1:55a6170b404f | 1543 | return( ret ); |
nexpaq | 1:55a6170b404f | 1544 | } |
nexpaq | 1:55a6170b404f | 1545 | |
nexpaq | 1:55a6170b404f | 1546 | /* |
nexpaq | 1:55a6170b404f | 1547 | * Multiplication with Montgomery ladder in x/z coordinates, |
nexpaq | 1:55a6170b404f | 1548 | * for curves in Montgomery form |
nexpaq | 1:55a6170b404f | 1549 | */ |
nexpaq | 1:55a6170b404f | 1550 | static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1551 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 1552 | int (*f_rng)(void *, unsigned char *, size_t), |
nexpaq | 1:55a6170b404f | 1553 | void *p_rng ) |
nexpaq | 1:55a6170b404f | 1554 | { |
nexpaq | 1:55a6170b404f | 1555 | int ret; |
nexpaq | 1:55a6170b404f | 1556 | size_t i; |
nexpaq | 1:55a6170b404f | 1557 | unsigned char b; |
nexpaq | 1:55a6170b404f | 1558 | mbedtls_ecp_point RP; |
nexpaq | 1:55a6170b404f | 1559 | mbedtls_mpi PX; |
nexpaq | 1:55a6170b404f | 1560 | |
nexpaq | 1:55a6170b404f | 1561 | mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX ); |
nexpaq | 1:55a6170b404f | 1562 | |
nexpaq | 1:55a6170b404f | 1563 | /* Save PX and read from P before writing to R, in case P == R */ |
nexpaq | 1:55a6170b404f | 1564 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) ); |
nexpaq | 1:55a6170b404f | 1565 | MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) ); |
nexpaq | 1:55a6170b404f | 1566 | |
nexpaq | 1:55a6170b404f | 1567 | /* Set R to zero in modified x/z coordinates */ |
nexpaq | 1:55a6170b404f | 1568 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) ); |
nexpaq | 1:55a6170b404f | 1569 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) ); |
nexpaq | 1:55a6170b404f | 1570 | mbedtls_mpi_free( &R->Y ); |
nexpaq | 1:55a6170b404f | 1571 | |
nexpaq | 1:55a6170b404f | 1572 | /* RP.X might be sligtly larger than P, so reduce it */ |
nexpaq | 1:55a6170b404f | 1573 | MOD_ADD( RP.X ); |
nexpaq | 1:55a6170b404f | 1574 | |
nexpaq | 1:55a6170b404f | 1575 | /* Randomize coordinates of the starting point */ |
nexpaq | 1:55a6170b404f | 1576 | if( f_rng != NULL ) |
nexpaq | 1:55a6170b404f | 1577 | MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1578 | |
nexpaq | 1:55a6170b404f | 1579 | /* Loop invariant: R = result so far, RP = R + P */ |
nexpaq | 1:55a6170b404f | 1580 | i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */ |
nexpaq | 1:55a6170b404f | 1581 | while( i-- > 0 ) |
nexpaq | 1:55a6170b404f | 1582 | { |
nexpaq | 1:55a6170b404f | 1583 | b = mbedtls_mpi_get_bit( m, i ); |
nexpaq | 1:55a6170b404f | 1584 | /* |
nexpaq | 1:55a6170b404f | 1585 | * if (b) R = 2R + P else R = 2R, |
nexpaq | 1:55a6170b404f | 1586 | * which is: |
nexpaq | 1:55a6170b404f | 1587 | * if (b) double_add( RP, R, RP, R ) |
nexpaq | 1:55a6170b404f | 1588 | * else double_add( R, RP, R, RP ) |
nexpaq | 1:55a6170b404f | 1589 | * but using safe conditional swaps to avoid leaks |
nexpaq | 1:55a6170b404f | 1590 | */ |
nexpaq | 1:55a6170b404f | 1591 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) ); |
nexpaq | 1:55a6170b404f | 1592 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) ); |
nexpaq | 1:55a6170b404f | 1593 | MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) ); |
nexpaq | 1:55a6170b404f | 1594 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) ); |
nexpaq | 1:55a6170b404f | 1595 | MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) ); |
nexpaq | 1:55a6170b404f | 1596 | } |
nexpaq | 1:55a6170b404f | 1597 | |
nexpaq | 1:55a6170b404f | 1598 | MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) ); |
nexpaq | 1:55a6170b404f | 1599 | |
nexpaq | 1:55a6170b404f | 1600 | cleanup: |
nexpaq | 1:55a6170b404f | 1601 | mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX ); |
nexpaq | 1:55a6170b404f | 1602 | |
nexpaq | 1:55a6170b404f | 1603 | return( ret ); |
nexpaq | 1:55a6170b404f | 1604 | } |
nexpaq | 1:55a6170b404f | 1605 | |
nexpaq | 1:55a6170b404f | 1606 | #endif /* ECP_MONTGOMERY */ |
nexpaq | 1:55a6170b404f | 1607 | |
nexpaq | 1:55a6170b404f | 1608 | /* |
nexpaq | 1:55a6170b404f | 1609 | * Multiplication R = m * P |
nexpaq | 1:55a6170b404f | 1610 | */ |
nexpaq | 1:55a6170b404f | 1611 | int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1612 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 1613 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
nexpaq | 1:55a6170b404f | 1614 | { |
nexpaq | 1:55a6170b404f | 1615 | int ret; |
nexpaq | 1:55a6170b404f | 1616 | |
nexpaq | 1:55a6170b404f | 1617 | /* Common sanity checks */ |
nexpaq | 1:55a6170b404f | 1618 | if( mbedtls_mpi_cmp_int( &P->Z, 1 ) != 0 ) |
nexpaq | 1:55a6170b404f | 1619 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1620 | |
nexpaq | 1:55a6170b404f | 1621 | if( ( ret = mbedtls_ecp_check_privkey( grp, m ) ) != 0 || |
nexpaq | 1:55a6170b404f | 1622 | ( ret = mbedtls_ecp_check_pubkey( grp, P ) ) != 0 ) |
nexpaq | 1:55a6170b404f | 1623 | return( ret ); |
nexpaq | 1:55a6170b404f | 1624 | |
nexpaq | 1:55a6170b404f | 1625 | #if defined(ECP_MONTGOMERY) |
nexpaq | 1:55a6170b404f | 1626 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
nexpaq | 1:55a6170b404f | 1627 | return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1628 | #endif |
nexpaq | 1:55a6170b404f | 1629 | #if defined(ECP_SHORTWEIERSTRASS) |
nexpaq | 1:55a6170b404f | 1630 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
nexpaq | 1:55a6170b404f | 1631 | return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1632 | #endif |
nexpaq | 1:55a6170b404f | 1633 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1634 | } |
nexpaq | 1:55a6170b404f | 1635 | |
nexpaq | 1:55a6170b404f | 1636 | #if defined(ECP_SHORTWEIERSTRASS) |
nexpaq | 1:55a6170b404f | 1637 | /* |
nexpaq | 1:55a6170b404f | 1638 | * Check that an affine point is valid as a public key, |
nexpaq | 1:55a6170b404f | 1639 | * short weierstrass curves (SEC1 3.2.3.1) |
nexpaq | 1:55a6170b404f | 1640 | */ |
nexpaq | 1:55a6170b404f | 1641 | static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 1642 | { |
nexpaq | 1:55a6170b404f | 1643 | int ret; |
nexpaq | 1:55a6170b404f | 1644 | mbedtls_mpi YY, RHS; |
nexpaq | 1:55a6170b404f | 1645 | |
nexpaq | 1:55a6170b404f | 1646 | /* pt coordinates must be normalized for our checks */ |
nexpaq | 1:55a6170b404f | 1647 | if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 || |
nexpaq | 1:55a6170b404f | 1648 | mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 || |
nexpaq | 1:55a6170b404f | 1649 | mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 || |
nexpaq | 1:55a6170b404f | 1650 | mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 ) |
nexpaq | 1:55a6170b404f | 1651 | return( MBEDTLS_ERR_ECP_INVALID_KEY ); |
nexpaq | 1:55a6170b404f | 1652 | |
nexpaq | 1:55a6170b404f | 1653 | mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS ); |
nexpaq | 1:55a6170b404f | 1654 | |
nexpaq | 1:55a6170b404f | 1655 | /* |
nexpaq | 1:55a6170b404f | 1656 | * YY = Y^2 |
nexpaq | 1:55a6170b404f | 1657 | * RHS = X (X^2 + A) + B = X^3 + A X + B |
nexpaq | 1:55a6170b404f | 1658 | */ |
nexpaq | 1:55a6170b404f | 1659 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY ); |
nexpaq | 1:55a6170b404f | 1660 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS ); |
nexpaq | 1:55a6170b404f | 1661 | |
nexpaq | 1:55a6170b404f | 1662 | /* Special case for A = -3 */ |
nexpaq | 1:55a6170b404f | 1663 | if( grp->A.p == NULL ) |
nexpaq | 1:55a6170b404f | 1664 | { |
nexpaq | 1:55a6170b404f | 1665 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS ); |
nexpaq | 1:55a6170b404f | 1666 | } |
nexpaq | 1:55a6170b404f | 1667 | else |
nexpaq | 1:55a6170b404f | 1668 | { |
nexpaq | 1:55a6170b404f | 1669 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS ); |
nexpaq | 1:55a6170b404f | 1670 | } |
nexpaq | 1:55a6170b404f | 1671 | |
nexpaq | 1:55a6170b404f | 1672 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS ); |
nexpaq | 1:55a6170b404f | 1673 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS ); |
nexpaq | 1:55a6170b404f | 1674 | |
nexpaq | 1:55a6170b404f | 1675 | if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 ) |
nexpaq | 1:55a6170b404f | 1676 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
nexpaq | 1:55a6170b404f | 1677 | |
nexpaq | 1:55a6170b404f | 1678 | cleanup: |
nexpaq | 1:55a6170b404f | 1679 | |
nexpaq | 1:55a6170b404f | 1680 | mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS ); |
nexpaq | 1:55a6170b404f | 1681 | |
nexpaq | 1:55a6170b404f | 1682 | return( ret ); |
nexpaq | 1:55a6170b404f | 1683 | } |
nexpaq | 1:55a6170b404f | 1684 | #endif /* ECP_SHORTWEIERSTRASS */ |
nexpaq | 1:55a6170b404f | 1685 | |
nexpaq | 1:55a6170b404f | 1686 | /* |
nexpaq | 1:55a6170b404f | 1687 | * R = m * P with shortcuts for m == 1 and m == -1 |
nexpaq | 1:55a6170b404f | 1688 | * NOT constant-time - ONLY for short Weierstrass! |
nexpaq | 1:55a6170b404f | 1689 | */ |
nexpaq | 1:55a6170b404f | 1690 | static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 1691 | mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1692 | const mbedtls_mpi *m, |
nexpaq | 1:55a6170b404f | 1693 | const mbedtls_ecp_point *P ) |
nexpaq | 1:55a6170b404f | 1694 | { |
nexpaq | 1:55a6170b404f | 1695 | int ret; |
nexpaq | 1:55a6170b404f | 1696 | |
nexpaq | 1:55a6170b404f | 1697 | if( mbedtls_mpi_cmp_int( m, 1 ) == 0 ) |
nexpaq | 1:55a6170b404f | 1698 | { |
nexpaq | 1:55a6170b404f | 1699 | MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) ); |
nexpaq | 1:55a6170b404f | 1700 | } |
nexpaq | 1:55a6170b404f | 1701 | else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 ) |
nexpaq | 1:55a6170b404f | 1702 | { |
nexpaq | 1:55a6170b404f | 1703 | MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) ); |
nexpaq | 1:55a6170b404f | 1704 | if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 ) |
nexpaq | 1:55a6170b404f | 1705 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) ); |
nexpaq | 1:55a6170b404f | 1706 | } |
nexpaq | 1:55a6170b404f | 1707 | else |
nexpaq | 1:55a6170b404f | 1708 | { |
nexpaq | 1:55a6170b404f | 1709 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 1710 | } |
nexpaq | 1:55a6170b404f | 1711 | |
nexpaq | 1:55a6170b404f | 1712 | cleanup: |
nexpaq | 1:55a6170b404f | 1713 | return( ret ); |
nexpaq | 1:55a6170b404f | 1714 | } |
nexpaq | 1:55a6170b404f | 1715 | |
nexpaq | 1:55a6170b404f | 1716 | /* |
nexpaq | 1:55a6170b404f | 1717 | * Linear combination |
nexpaq | 1:55a6170b404f | 1718 | * NOT constant-time |
nexpaq | 1:55a6170b404f | 1719 | */ |
nexpaq | 1:55a6170b404f | 1720 | int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
nexpaq | 1:55a6170b404f | 1721 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
nexpaq | 1:55a6170b404f | 1722 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q ) |
nexpaq | 1:55a6170b404f | 1723 | { |
nexpaq | 1:55a6170b404f | 1724 | int ret; |
nexpaq | 1:55a6170b404f | 1725 | mbedtls_ecp_point mP; |
nexpaq | 1:55a6170b404f | 1726 | |
nexpaq | 1:55a6170b404f | 1727 | if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS ) |
nexpaq | 1:55a6170b404f | 1728 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
nexpaq | 1:55a6170b404f | 1729 | |
nexpaq | 1:55a6170b404f | 1730 | mbedtls_ecp_point_init( &mP ); |
nexpaq | 1:55a6170b404f | 1731 | |
nexpaq | 1:55a6170b404f | 1732 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, &mP, m, P ) ); |
nexpaq | 1:55a6170b404f | 1733 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, R, n, Q ) ); |
nexpaq | 1:55a6170b404f | 1734 | |
nexpaq | 1:55a6170b404f | 1735 | MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) ); |
nexpaq | 1:55a6170b404f | 1736 | MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) ); |
nexpaq | 1:55a6170b404f | 1737 | |
nexpaq | 1:55a6170b404f | 1738 | cleanup: |
nexpaq | 1:55a6170b404f | 1739 | mbedtls_ecp_point_free( &mP ); |
nexpaq | 1:55a6170b404f | 1740 | |
nexpaq | 1:55a6170b404f | 1741 | return( ret ); |
nexpaq | 1:55a6170b404f | 1742 | } |
nexpaq | 1:55a6170b404f | 1743 | |
nexpaq | 1:55a6170b404f | 1744 | |
nexpaq | 1:55a6170b404f | 1745 | #if defined(ECP_MONTGOMERY) |
nexpaq | 1:55a6170b404f | 1746 | /* |
nexpaq | 1:55a6170b404f | 1747 | * Check validity of a public key for Montgomery curves with x-only schemes |
nexpaq | 1:55a6170b404f | 1748 | */ |
nexpaq | 1:55a6170b404f | 1749 | static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 1750 | { |
nexpaq | 1:55a6170b404f | 1751 | /* [Curve25519 p. 5] Just check X is the correct number of bytes */ |
nexpaq | 1:55a6170b404f | 1752 | if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 ) |
nexpaq | 1:55a6170b404f | 1753 | return( MBEDTLS_ERR_ECP_INVALID_KEY ); |
nexpaq | 1:55a6170b404f | 1754 | |
nexpaq | 1:55a6170b404f | 1755 | return( 0 ); |
nexpaq | 1:55a6170b404f | 1756 | } |
nexpaq | 1:55a6170b404f | 1757 | #endif /* ECP_MONTGOMERY */ |
nexpaq | 1:55a6170b404f | 1758 | |
nexpaq | 1:55a6170b404f | 1759 | /* |
nexpaq | 1:55a6170b404f | 1760 | * Check that a point is valid as a public key |
nexpaq | 1:55a6170b404f | 1761 | */ |
nexpaq | 1:55a6170b404f | 1762 | int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ) |
nexpaq | 1:55a6170b404f | 1763 | { |
nexpaq | 1:55a6170b404f | 1764 | /* Must use affine coordinates */ |
nexpaq | 1:55a6170b404f | 1765 | if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 ) |
nexpaq | 1:55a6170b404f | 1766 | return( MBEDTLS_ERR_ECP_INVALID_KEY ); |
nexpaq | 1:55a6170b404f | 1767 | |
nexpaq | 1:55a6170b404f | 1768 | #if defined(ECP_MONTGOMERY) |
nexpaq | 1:55a6170b404f | 1769 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
nexpaq | 1:55a6170b404f | 1770 | return( ecp_check_pubkey_mx( grp, pt ) ); |
nexpaq | 1:55a6170b404f | 1771 | #endif |
nexpaq | 1:55a6170b404f | 1772 | #if defined(ECP_SHORTWEIERSTRASS) |
nexpaq | 1:55a6170b404f | 1773 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
nexpaq | 1:55a6170b404f | 1774 | return( ecp_check_pubkey_sw( grp, pt ) ); |
nexpaq | 1:55a6170b404f | 1775 | #endif |
nexpaq | 1:55a6170b404f | 1776 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1777 | } |
nexpaq | 1:55a6170b404f | 1778 | |
nexpaq | 1:55a6170b404f | 1779 | /* |
nexpaq | 1:55a6170b404f | 1780 | * Check that an mbedtls_mpi is valid as a private key |
nexpaq | 1:55a6170b404f | 1781 | */ |
nexpaq | 1:55a6170b404f | 1782 | int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ) |
nexpaq | 1:55a6170b404f | 1783 | { |
nexpaq | 1:55a6170b404f | 1784 | #if defined(ECP_MONTGOMERY) |
nexpaq | 1:55a6170b404f | 1785 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
nexpaq | 1:55a6170b404f | 1786 | { |
nexpaq | 1:55a6170b404f | 1787 | /* see [Curve25519] page 5 */ |
nexpaq | 1:55a6170b404f | 1788 | if( mbedtls_mpi_get_bit( d, 0 ) != 0 || |
nexpaq | 1:55a6170b404f | 1789 | mbedtls_mpi_get_bit( d, 1 ) != 0 || |
nexpaq | 1:55a6170b404f | 1790 | mbedtls_mpi_get_bit( d, 2 ) != 0 || |
nexpaq | 1:55a6170b404f | 1791 | mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */ |
nexpaq | 1:55a6170b404f | 1792 | return( MBEDTLS_ERR_ECP_INVALID_KEY ); |
nexpaq | 1:55a6170b404f | 1793 | else |
nexpaq | 1:55a6170b404f | 1794 | return( 0 ); |
nexpaq | 1:55a6170b404f | 1795 | } |
nexpaq | 1:55a6170b404f | 1796 | #endif /* ECP_MONTGOMERY */ |
nexpaq | 1:55a6170b404f | 1797 | #if defined(ECP_SHORTWEIERSTRASS) |
nexpaq | 1:55a6170b404f | 1798 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
nexpaq | 1:55a6170b404f | 1799 | { |
nexpaq | 1:55a6170b404f | 1800 | /* see SEC1 3.2 */ |
nexpaq | 1:55a6170b404f | 1801 | if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || |
nexpaq | 1:55a6170b404f | 1802 | mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
nexpaq | 1:55a6170b404f | 1803 | return( MBEDTLS_ERR_ECP_INVALID_KEY ); |
nexpaq | 1:55a6170b404f | 1804 | else |
nexpaq | 1:55a6170b404f | 1805 | return( 0 ); |
nexpaq | 1:55a6170b404f | 1806 | } |
nexpaq | 1:55a6170b404f | 1807 | #endif /* ECP_SHORTWEIERSTRASS */ |
nexpaq | 1:55a6170b404f | 1808 | |
nexpaq | 1:55a6170b404f | 1809 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1810 | } |
nexpaq | 1:55a6170b404f | 1811 | |
nexpaq | 1:55a6170b404f | 1812 | /* |
nexpaq | 1:55a6170b404f | 1813 | * Generate a keypair with configurable base point |
nexpaq | 1:55a6170b404f | 1814 | */ |
nexpaq | 1:55a6170b404f | 1815 | int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 1816 | const mbedtls_ecp_point *G, |
nexpaq | 1:55a6170b404f | 1817 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
nexpaq | 1:55a6170b404f | 1818 | int (*f_rng)(void *, unsigned char *, size_t), |
nexpaq | 1:55a6170b404f | 1819 | void *p_rng ) |
nexpaq | 1:55a6170b404f | 1820 | { |
nexpaq | 1:55a6170b404f | 1821 | int ret; |
nexpaq | 1:55a6170b404f | 1822 | size_t n_size = ( grp->nbits + 7 ) / 8; |
nexpaq | 1:55a6170b404f | 1823 | |
nexpaq | 1:55a6170b404f | 1824 | #if defined(ECP_MONTGOMERY) |
nexpaq | 1:55a6170b404f | 1825 | if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) |
nexpaq | 1:55a6170b404f | 1826 | { |
nexpaq | 1:55a6170b404f | 1827 | /* [M225] page 5 */ |
nexpaq | 1:55a6170b404f | 1828 | size_t b; |
nexpaq | 1:55a6170b404f | 1829 | |
nexpaq | 1:55a6170b404f | 1830 | do { |
nexpaq | 1:55a6170b404f | 1831 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1832 | } while( mbedtls_mpi_bitlen( d ) == 0); |
nexpaq | 1:55a6170b404f | 1833 | |
nexpaq | 1:55a6170b404f | 1834 | /* Make sure the most significant bit is nbits */ |
nexpaq | 1:55a6170b404f | 1835 | b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */ |
nexpaq | 1:55a6170b404f | 1836 | if( b > grp->nbits ) |
nexpaq | 1:55a6170b404f | 1837 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) ); |
nexpaq | 1:55a6170b404f | 1838 | else |
nexpaq | 1:55a6170b404f | 1839 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) ); |
nexpaq | 1:55a6170b404f | 1840 | |
nexpaq | 1:55a6170b404f | 1841 | /* Make sure the last three bits are unset */ |
nexpaq | 1:55a6170b404f | 1842 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) ); |
nexpaq | 1:55a6170b404f | 1843 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) ); |
nexpaq | 1:55a6170b404f | 1844 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) ); |
nexpaq | 1:55a6170b404f | 1845 | } |
nexpaq | 1:55a6170b404f | 1846 | else |
nexpaq | 1:55a6170b404f | 1847 | #endif /* ECP_MONTGOMERY */ |
nexpaq | 1:55a6170b404f | 1848 | #if defined(ECP_SHORTWEIERSTRASS) |
nexpaq | 1:55a6170b404f | 1849 | if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS ) |
nexpaq | 1:55a6170b404f | 1850 | { |
nexpaq | 1:55a6170b404f | 1851 | /* SEC1 3.2.1: Generate d such that 1 <= n < N */ |
nexpaq | 1:55a6170b404f | 1852 | int count = 0; |
nexpaq | 1:55a6170b404f | 1853 | unsigned char rnd[MBEDTLS_ECP_MAX_BYTES]; |
nexpaq | 1:55a6170b404f | 1854 | |
nexpaq | 1:55a6170b404f | 1855 | /* |
nexpaq | 1:55a6170b404f | 1856 | * Match the procedure given in RFC 6979 (deterministic ECDSA): |
nexpaq | 1:55a6170b404f | 1857 | * - use the same byte ordering; |
nexpaq | 1:55a6170b404f | 1858 | * - keep the leftmost nbits bits of the generated octet string; |
nexpaq | 1:55a6170b404f | 1859 | * - try until result is in the desired range. |
nexpaq | 1:55a6170b404f | 1860 | * This also avoids any biais, which is especially important for ECDSA. |
nexpaq | 1:55a6170b404f | 1861 | */ |
nexpaq | 1:55a6170b404f | 1862 | do |
nexpaq | 1:55a6170b404f | 1863 | { |
nexpaq | 1:55a6170b404f | 1864 | MBEDTLS_MPI_CHK( f_rng( p_rng, rnd, n_size ) ); |
nexpaq | 1:55a6170b404f | 1865 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( d, rnd, n_size ) ); |
nexpaq | 1:55a6170b404f | 1866 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) ); |
nexpaq | 1:55a6170b404f | 1867 | |
nexpaq | 1:55a6170b404f | 1868 | /* |
nexpaq | 1:55a6170b404f | 1869 | * Each try has at worst a probability 1/2 of failing (the msb has |
nexpaq | 1:55a6170b404f | 1870 | * a probability 1/2 of being 0, and then the result will be < N), |
nexpaq | 1:55a6170b404f | 1871 | * so after 30 tries failure probability is a most 2**(-30). |
nexpaq | 1:55a6170b404f | 1872 | * |
nexpaq | 1:55a6170b404f | 1873 | * For most curves, 1 try is enough with overwhelming probability, |
nexpaq | 1:55a6170b404f | 1874 | * since N starts with a lot of 1s in binary, but some curves |
nexpaq | 1:55a6170b404f | 1875 | * such as secp224k1 are actually very close to the worst case. |
nexpaq | 1:55a6170b404f | 1876 | */ |
nexpaq | 1:55a6170b404f | 1877 | if( ++count > 30 ) |
nexpaq | 1:55a6170b404f | 1878 | return( MBEDTLS_ERR_ECP_RANDOM_FAILED ); |
nexpaq | 1:55a6170b404f | 1879 | } |
nexpaq | 1:55a6170b404f | 1880 | while( mbedtls_mpi_cmp_int( d, 1 ) < 0 || |
nexpaq | 1:55a6170b404f | 1881 | mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 ); |
nexpaq | 1:55a6170b404f | 1882 | } |
nexpaq | 1:55a6170b404f | 1883 | else |
nexpaq | 1:55a6170b404f | 1884 | #endif /* ECP_SHORTWEIERSTRASS */ |
nexpaq | 1:55a6170b404f | 1885 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1886 | |
nexpaq | 1:55a6170b404f | 1887 | cleanup: |
nexpaq | 1:55a6170b404f | 1888 | if( ret != 0 ) |
nexpaq | 1:55a6170b404f | 1889 | return( ret ); |
nexpaq | 1:55a6170b404f | 1890 | |
nexpaq | 1:55a6170b404f | 1891 | return( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1892 | } |
nexpaq | 1:55a6170b404f | 1893 | |
nexpaq | 1:55a6170b404f | 1894 | /* |
nexpaq | 1:55a6170b404f | 1895 | * Generate key pair, wrapper for conventional base point |
nexpaq | 1:55a6170b404f | 1896 | */ |
nexpaq | 1:55a6170b404f | 1897 | int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, |
nexpaq | 1:55a6170b404f | 1898 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
nexpaq | 1:55a6170b404f | 1899 | int (*f_rng)(void *, unsigned char *, size_t), |
nexpaq | 1:55a6170b404f | 1900 | void *p_rng ) |
nexpaq | 1:55a6170b404f | 1901 | { |
nexpaq | 1:55a6170b404f | 1902 | return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1903 | } |
nexpaq | 1:55a6170b404f | 1904 | |
nexpaq | 1:55a6170b404f | 1905 | /* |
nexpaq | 1:55a6170b404f | 1906 | * Generate a keypair, prettier wrapper |
nexpaq | 1:55a6170b404f | 1907 | */ |
nexpaq | 1:55a6170b404f | 1908 | int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
nexpaq | 1:55a6170b404f | 1909 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
nexpaq | 1:55a6170b404f | 1910 | { |
nexpaq | 1:55a6170b404f | 1911 | int ret; |
nexpaq | 1:55a6170b404f | 1912 | |
nexpaq | 1:55a6170b404f | 1913 | if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 ) |
nexpaq | 1:55a6170b404f | 1914 | return( ret ); |
nexpaq | 1:55a6170b404f | 1915 | |
nexpaq | 1:55a6170b404f | 1916 | return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) ); |
nexpaq | 1:55a6170b404f | 1917 | } |
nexpaq | 1:55a6170b404f | 1918 | |
nexpaq | 1:55a6170b404f | 1919 | /* |
nexpaq | 1:55a6170b404f | 1920 | * Check a public-private key pair |
nexpaq | 1:55a6170b404f | 1921 | */ |
nexpaq | 1:55a6170b404f | 1922 | int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ) |
nexpaq | 1:55a6170b404f | 1923 | { |
nexpaq | 1:55a6170b404f | 1924 | int ret; |
nexpaq | 1:55a6170b404f | 1925 | mbedtls_ecp_point Q; |
nexpaq | 1:55a6170b404f | 1926 | mbedtls_ecp_group grp; |
nexpaq | 1:55a6170b404f | 1927 | |
nexpaq | 1:55a6170b404f | 1928 | if( pub->grp.id == MBEDTLS_ECP_DP_NONE || |
nexpaq | 1:55a6170b404f | 1929 | pub->grp.id != prv->grp.id || |
nexpaq | 1:55a6170b404f | 1930 | mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) || |
nexpaq | 1:55a6170b404f | 1931 | mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) || |
nexpaq | 1:55a6170b404f | 1932 | mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) ) |
nexpaq | 1:55a6170b404f | 1933 | { |
nexpaq | 1:55a6170b404f | 1934 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
nexpaq | 1:55a6170b404f | 1935 | } |
nexpaq | 1:55a6170b404f | 1936 | |
nexpaq | 1:55a6170b404f | 1937 | mbedtls_ecp_point_init( &Q ); |
nexpaq | 1:55a6170b404f | 1938 | mbedtls_ecp_group_init( &grp ); |
nexpaq | 1:55a6170b404f | 1939 | |
nexpaq | 1:55a6170b404f | 1940 | /* mbedtls_ecp_mul() needs a non-const group... */ |
nexpaq | 1:55a6170b404f | 1941 | mbedtls_ecp_group_copy( &grp, &prv->grp ); |
nexpaq | 1:55a6170b404f | 1942 | |
nexpaq | 1:55a6170b404f | 1943 | /* Also checks d is valid */ |
nexpaq | 1:55a6170b404f | 1944 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 1945 | |
nexpaq | 1:55a6170b404f | 1946 | if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) || |
nexpaq | 1:55a6170b404f | 1947 | mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) || |
nexpaq | 1:55a6170b404f | 1948 | mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) ) |
nexpaq | 1:55a6170b404f | 1949 | { |
nexpaq | 1:55a6170b404f | 1950 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
nexpaq | 1:55a6170b404f | 1951 | goto cleanup; |
nexpaq | 1:55a6170b404f | 1952 | } |
nexpaq | 1:55a6170b404f | 1953 | |
nexpaq | 1:55a6170b404f | 1954 | cleanup: |
nexpaq | 1:55a6170b404f | 1955 | mbedtls_ecp_point_free( &Q ); |
nexpaq | 1:55a6170b404f | 1956 | mbedtls_ecp_group_free( &grp ); |
nexpaq | 1:55a6170b404f | 1957 | |
nexpaq | 1:55a6170b404f | 1958 | return( ret ); |
nexpaq | 1:55a6170b404f | 1959 | } |
nexpaq | 1:55a6170b404f | 1960 | |
nexpaq | 1:55a6170b404f | 1961 | #if defined(MBEDTLS_SELF_TEST) |
nexpaq | 1:55a6170b404f | 1962 | |
nexpaq | 1:55a6170b404f | 1963 | /* |
nexpaq | 1:55a6170b404f | 1964 | * Checkup routine |
nexpaq | 1:55a6170b404f | 1965 | */ |
nexpaq | 1:55a6170b404f | 1966 | int mbedtls_ecp_self_test( int verbose ) |
nexpaq | 1:55a6170b404f | 1967 | { |
nexpaq | 1:55a6170b404f | 1968 | int ret; |
nexpaq | 1:55a6170b404f | 1969 | size_t i; |
nexpaq | 1:55a6170b404f | 1970 | mbedtls_ecp_group grp; |
nexpaq | 1:55a6170b404f | 1971 | mbedtls_ecp_point R, P; |
nexpaq | 1:55a6170b404f | 1972 | mbedtls_mpi m; |
nexpaq | 1:55a6170b404f | 1973 | unsigned long add_c_prev, dbl_c_prev, mul_c_prev; |
nexpaq | 1:55a6170b404f | 1974 | /* exponents especially adapted for secp192r1 */ |
nexpaq | 1:55a6170b404f | 1975 | const char *exponents[] = |
nexpaq | 1:55a6170b404f | 1976 | { |
nexpaq | 1:55a6170b404f | 1977 | "000000000000000000000000000000000000000000000001", /* one */ |
nexpaq | 1:55a6170b404f | 1978 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */ |
nexpaq | 1:55a6170b404f | 1979 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */ |
nexpaq | 1:55a6170b404f | 1980 | "400000000000000000000000000000000000000000000000", /* one and zeros */ |
nexpaq | 1:55a6170b404f | 1981 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */ |
nexpaq | 1:55a6170b404f | 1982 | "555555555555555555555555555555555555555555555555", /* 101010... */ |
nexpaq | 1:55a6170b404f | 1983 | }; |
nexpaq | 1:55a6170b404f | 1984 | |
nexpaq | 1:55a6170b404f | 1985 | mbedtls_ecp_group_init( &grp ); |
nexpaq | 1:55a6170b404f | 1986 | mbedtls_ecp_point_init( &R ); |
nexpaq | 1:55a6170b404f | 1987 | mbedtls_ecp_point_init( &P ); |
nexpaq | 1:55a6170b404f | 1988 | mbedtls_mpi_init( &m ); |
nexpaq | 1:55a6170b404f | 1989 | |
nexpaq | 1:55a6170b404f | 1990 | /* Use secp192r1 if available, or any available curve */ |
nexpaq | 1:55a6170b404f | 1991 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
nexpaq | 1:55a6170b404f | 1992 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) ); |
nexpaq | 1:55a6170b404f | 1993 | #else |
nexpaq | 1:55a6170b404f | 1994 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) ); |
nexpaq | 1:55a6170b404f | 1995 | #endif |
nexpaq | 1:55a6170b404f | 1996 | |
nexpaq | 1:55a6170b404f | 1997 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 1998 | mbedtls_printf( " ECP test #1 (constant op_count, base point G): " ); |
nexpaq | 1:55a6170b404f | 1999 | |
nexpaq | 1:55a6170b404f | 2000 | /* Do a dummy multiplication first to trigger precomputation */ |
nexpaq | 1:55a6170b404f | 2001 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) ); |
nexpaq | 1:55a6170b404f | 2002 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 2003 | |
nexpaq | 1:55a6170b404f | 2004 | add_count = 0; |
nexpaq | 1:55a6170b404f | 2005 | dbl_count = 0; |
nexpaq | 1:55a6170b404f | 2006 | mul_count = 0; |
nexpaq | 1:55a6170b404f | 2007 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) ); |
nexpaq | 1:55a6170b404f | 2008 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 2009 | |
nexpaq | 1:55a6170b404f | 2010 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
nexpaq | 1:55a6170b404f | 2011 | { |
nexpaq | 1:55a6170b404f | 2012 | add_c_prev = add_count; |
nexpaq | 1:55a6170b404f | 2013 | dbl_c_prev = dbl_count; |
nexpaq | 1:55a6170b404f | 2014 | mul_c_prev = mul_count; |
nexpaq | 1:55a6170b404f | 2015 | add_count = 0; |
nexpaq | 1:55a6170b404f | 2016 | dbl_count = 0; |
nexpaq | 1:55a6170b404f | 2017 | mul_count = 0; |
nexpaq | 1:55a6170b404f | 2018 | |
nexpaq | 1:55a6170b404f | 2019 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) ); |
nexpaq | 1:55a6170b404f | 2020 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 2021 | |
nexpaq | 1:55a6170b404f | 2022 | if( add_count != add_c_prev || |
nexpaq | 1:55a6170b404f | 2023 | dbl_count != dbl_c_prev || |
nexpaq | 1:55a6170b404f | 2024 | mul_count != mul_c_prev ) |
nexpaq | 1:55a6170b404f | 2025 | { |
nexpaq | 1:55a6170b404f | 2026 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2027 | mbedtls_printf( "failed (%u)\n", (unsigned int) i ); |
nexpaq | 1:55a6170b404f | 2028 | |
nexpaq | 1:55a6170b404f | 2029 | ret = 1; |
nexpaq | 1:55a6170b404f | 2030 | goto cleanup; |
nexpaq | 1:55a6170b404f | 2031 | } |
nexpaq | 1:55a6170b404f | 2032 | } |
nexpaq | 1:55a6170b404f | 2033 | |
nexpaq | 1:55a6170b404f | 2034 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2035 | mbedtls_printf( "passed\n" ); |
nexpaq | 1:55a6170b404f | 2036 | |
nexpaq | 1:55a6170b404f | 2037 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2038 | mbedtls_printf( " ECP test #2 (constant op_count, other point): " ); |
nexpaq | 1:55a6170b404f | 2039 | /* We computed P = 2G last time, use it */ |
nexpaq | 1:55a6170b404f | 2040 | |
nexpaq | 1:55a6170b404f | 2041 | add_count = 0; |
nexpaq | 1:55a6170b404f | 2042 | dbl_count = 0; |
nexpaq | 1:55a6170b404f | 2043 | mul_count = 0; |
nexpaq | 1:55a6170b404f | 2044 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) ); |
nexpaq | 1:55a6170b404f | 2045 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 2046 | |
nexpaq | 1:55a6170b404f | 2047 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
nexpaq | 1:55a6170b404f | 2048 | { |
nexpaq | 1:55a6170b404f | 2049 | add_c_prev = add_count; |
nexpaq | 1:55a6170b404f | 2050 | dbl_c_prev = dbl_count; |
nexpaq | 1:55a6170b404f | 2051 | mul_c_prev = mul_count; |
nexpaq | 1:55a6170b404f | 2052 | add_count = 0; |
nexpaq | 1:55a6170b404f | 2053 | dbl_count = 0; |
nexpaq | 1:55a6170b404f | 2054 | mul_count = 0; |
nexpaq | 1:55a6170b404f | 2055 | |
nexpaq | 1:55a6170b404f | 2056 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) ); |
nexpaq | 1:55a6170b404f | 2057 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
nexpaq | 1:55a6170b404f | 2058 | |
nexpaq | 1:55a6170b404f | 2059 | if( add_count != add_c_prev || |
nexpaq | 1:55a6170b404f | 2060 | dbl_count != dbl_c_prev || |
nexpaq | 1:55a6170b404f | 2061 | mul_count != mul_c_prev ) |
nexpaq | 1:55a6170b404f | 2062 | { |
nexpaq | 1:55a6170b404f | 2063 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2064 | mbedtls_printf( "failed (%u)\n", (unsigned int) i ); |
nexpaq | 1:55a6170b404f | 2065 | |
nexpaq | 1:55a6170b404f | 2066 | ret = 1; |
nexpaq | 1:55a6170b404f | 2067 | goto cleanup; |
nexpaq | 1:55a6170b404f | 2068 | } |
nexpaq | 1:55a6170b404f | 2069 | } |
nexpaq | 1:55a6170b404f | 2070 | |
nexpaq | 1:55a6170b404f | 2071 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2072 | mbedtls_printf( "passed\n" ); |
nexpaq | 1:55a6170b404f | 2073 | |
nexpaq | 1:55a6170b404f | 2074 | cleanup: |
nexpaq | 1:55a6170b404f | 2075 | |
nexpaq | 1:55a6170b404f | 2076 | if( ret < 0 && verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2077 | mbedtls_printf( "Unexpected error, return code = %08X\n", ret ); |
nexpaq | 1:55a6170b404f | 2078 | |
nexpaq | 1:55a6170b404f | 2079 | mbedtls_ecp_group_free( &grp ); |
nexpaq | 1:55a6170b404f | 2080 | mbedtls_ecp_point_free( &R ); |
nexpaq | 1:55a6170b404f | 2081 | mbedtls_ecp_point_free( &P ); |
nexpaq | 1:55a6170b404f | 2082 | mbedtls_mpi_free( &m ); |
nexpaq | 1:55a6170b404f | 2083 | |
nexpaq | 1:55a6170b404f | 2084 | if( verbose != 0 ) |
nexpaq | 1:55a6170b404f | 2085 | mbedtls_printf( "\n" ); |
nexpaq | 1:55a6170b404f | 2086 | |
nexpaq | 1:55a6170b404f | 2087 | return( ret ); |
nexpaq | 1:55a6170b404f | 2088 | } |
nexpaq | 1:55a6170b404f | 2089 | |
nexpaq | 1:55a6170b404f | 2090 | #endif /* MBEDTLS_SELF_TEST */ |
nexpaq | 1:55a6170b404f | 2091 | |
nexpaq | 1:55a6170b404f | 2092 | #endif /* MBEDTLS_ECP_C */ |