Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curves over GF(p): curve-specific data and functions |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/ecp.h" |
Hanno Becker | 4f8e8e5 | 2018-12-14 15:08:03 +0000 | [diff] [blame] | 25 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 26 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 27 | |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 28 | #include "ecp_invasive.h" |
| 29 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 32 | #if !defined(MBEDTLS_ECP_ALT) |
| 33 | |
Hanno Becker | 4f8e8e5 | 2018-12-14 15:08:03 +0000 | [diff] [blame] | 34 | /* Parameter validation macros based on platform_util.h */ |
| 35 | #define ECP_VALIDATE_RET( cond ) \ |
| 36 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) |
| 37 | #define ECP_VALIDATE( cond ) \ |
| 38 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 39 | |
Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 40 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 41 | !defined(inline) && !defined(__cplusplus) |
Paul Bakker | 498fd35 | 2013-12-02 22:17:24 +0100 | [diff] [blame] | 42 | #define inline __inline |
Manuel Pégourié-Gonnard | 20af64d | 2015-07-07 18:33:39 +0200 | [diff] [blame] | 43 | #endif |
Paul Bakker | 498fd35 | 2013-12-02 22:17:24 +0100 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | baee5d4 | 2013-12-06 13:38:41 +0100 | [diff] [blame] | 45 | /* |
| 46 | * Conversion macros for embedded constants: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | * build lists of mbedtls_mpi_uint's from lists of unsigned char's grouped by 8, 4 or 2 |
Manuel Pégourié-Gonnard | baee5d4 | 2013-12-06 13:38:41 +0100 | [diff] [blame] | 48 | */ |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_HAVE_INT32) |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 50 | |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 51 | #define BYTES_TO_T_UINT_4( a, b, c, d ) \ |
| 52 | ( (mbedtls_mpi_uint) (a) << 0 ) | \ |
| 53 | ( (mbedtls_mpi_uint) (b) << 8 ) | \ |
| 54 | ( (mbedtls_mpi_uint) (c) << 16 ) | \ |
| 55 | ( (mbedtls_mpi_uint) (d) << 24 ) |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 57 | #define BYTES_TO_T_UINT_2( a, b ) \ |
| 58 | BYTES_TO_T_UINT_4( a, b, 0, 0 ) |
| 59 | |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 60 | #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \ |
Manuel Pégourié-Gonnard | 69ab354 | 2013-12-12 15:50:08 +0100 | [diff] [blame] | 61 | BYTES_TO_T_UINT_4( a, b, c, d ), \ |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 62 | BYTES_TO_T_UINT_4( e, f, g, h ) |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 63 | |
| 64 | #else /* 64-bits */ |
| 65 | |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 66 | #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 67 | ( (mbedtls_mpi_uint) (a) << 0 ) | \ |
| 68 | ( (mbedtls_mpi_uint) (b) << 8 ) | \ |
| 69 | ( (mbedtls_mpi_uint) (c) << 16 ) | \ |
| 70 | ( (mbedtls_mpi_uint) (d) << 24 ) | \ |
| 71 | ( (mbedtls_mpi_uint) (e) << 32 ) | \ |
| 72 | ( (mbedtls_mpi_uint) (f) << 40 ) | \ |
| 73 | ( (mbedtls_mpi_uint) (g) << 48 ) | \ |
| 74 | ( (mbedtls_mpi_uint) (h) << 56 ) |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 76 | #define BYTES_TO_T_UINT_4( a, b, c, d ) \ |
| 77 | BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 ) |
| 78 | |
| 79 | #define BYTES_TO_T_UINT_2( a, b ) \ |
| 80 | BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 ) |
| 81 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | #endif /* bits in mbedtls_mpi_uint */ |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 84 | /* |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 85 | * Note: the constants are in little-endian order |
| 86 | * to be directly usable in MPIs |
| 87 | */ |
| 88 | |
| 89 | /* |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 90 | * Domain parameters for secp192r1 |
| 91 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
| 93 | static const mbedtls_mpi_uint secp192r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 94 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 95 | BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 96 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 97 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | static const mbedtls_mpi_uint secp192r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 99 | BYTES_TO_T_UINT_8( 0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE ), |
| 100 | BYTES_TO_T_UINT_8( 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F ), |
| 101 | BYTES_TO_T_UINT_8( 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 102 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | static const mbedtls_mpi_uint secp192r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 104 | BYTES_TO_T_UINT_8( 0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4 ), |
| 105 | BYTES_TO_T_UINT_8( 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C ), |
| 106 | BYTES_TO_T_UINT_8( 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 107 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 108 | static const mbedtls_mpi_uint secp192r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 109 | BYTES_TO_T_UINT_8( 0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73 ), |
| 110 | BYTES_TO_T_UINT_8( 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63 ), |
| 111 | BYTES_TO_T_UINT_8( 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 112 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | static const mbedtls_mpi_uint secp192r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 114 | BYTES_TO_T_UINT_8( 0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14 ), |
| 115 | BYTES_TO_T_UINT_8( 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 116 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 117 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Domain parameters for secp224r1 |
| 122 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
| 124 | static const mbedtls_mpi_uint secp224r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 125 | BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), |
| 126 | BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 127 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 128 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 129 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | static const mbedtls_mpi_uint secp224r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 131 | BYTES_TO_T_UINT_8( 0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27 ), |
| 132 | BYTES_TO_T_UINT_8( 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50 ), |
| 133 | BYTES_TO_T_UINT_8( 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 134 | BYTES_TO_T_UINT_4( 0x85, 0x0A, 0x05, 0xB4 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 135 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | static const mbedtls_mpi_uint secp224r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 137 | BYTES_TO_T_UINT_8( 0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34 ), |
| 138 | BYTES_TO_T_UINT_8( 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A ), |
| 139 | BYTES_TO_T_UINT_8( 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 140 | BYTES_TO_T_UINT_4( 0xBD, 0x0C, 0x0E, 0xB7 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 141 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | static const mbedtls_mpi_uint secp224r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 143 | BYTES_TO_T_UINT_8( 0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44 ), |
| 144 | BYTES_TO_T_UINT_8( 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD ), |
| 145 | BYTES_TO_T_UINT_8( 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5 ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 146 | BYTES_TO_T_UINT_4( 0x88, 0x63, 0x37, 0xBD ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 147 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | static const mbedtls_mpi_uint secp224r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 149 | BYTES_TO_T_UINT_8( 0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13 ), |
| 150 | BYTES_TO_T_UINT_8( 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF ), |
| 151 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 152 | BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 153 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * Domain parameters for secp256r1 |
| 158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 160 | static const mbedtls_mpi_uint secp256r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 161 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 162 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ), |
| 163 | BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), |
| 164 | BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 165 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | static const mbedtls_mpi_uint secp256r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 167 | BYTES_TO_T_UINT_8( 0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B ), |
| 168 | BYTES_TO_T_UINT_8( 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65 ), |
| 169 | BYTES_TO_T_UINT_8( 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3 ), |
| 170 | BYTES_TO_T_UINT_8( 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 171 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | static const mbedtls_mpi_uint secp256r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 173 | BYTES_TO_T_UINT_8( 0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4 ), |
| 174 | BYTES_TO_T_UINT_8( 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77 ), |
| 175 | BYTES_TO_T_UINT_8( 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8 ), |
| 176 | BYTES_TO_T_UINT_8( 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 177 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | static const mbedtls_mpi_uint secp256r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 179 | BYTES_TO_T_UINT_8( 0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB ), |
| 180 | BYTES_TO_T_UINT_8( 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B ), |
| 181 | BYTES_TO_T_UINT_8( 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E ), |
| 182 | BYTES_TO_T_UINT_8( 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 183 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | static const mbedtls_mpi_uint secp256r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 185 | BYTES_TO_T_UINT_8( 0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3 ), |
| 186 | BYTES_TO_T_UINT_8( 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC ), |
| 187 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 188 | BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 189 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * Domain parameters for secp384r1 |
| 194 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 196 | static const mbedtls_mpi_uint secp384r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 197 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ), |
| 198 | BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 199 | BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 200 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 201 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 202 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 203 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | static const mbedtls_mpi_uint secp384r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 205 | BYTES_TO_T_UINT_8( 0xEF, 0x2A, 0xEC, 0xD3, 0xED, 0xC8, 0x85, 0x2A ), |
| 206 | BYTES_TO_T_UINT_8( 0x9D, 0xD1, 0x2E, 0x8A, 0x8D, 0x39, 0x56, 0xC6 ), |
| 207 | BYTES_TO_T_UINT_8( 0x5A, 0x87, 0x13, 0x50, 0x8F, 0x08, 0x14, 0x03 ), |
| 208 | BYTES_TO_T_UINT_8( 0x12, 0x41, 0x81, 0xFE, 0x6E, 0x9C, 0x1D, 0x18 ), |
| 209 | BYTES_TO_T_UINT_8( 0x19, 0x2D, 0xF8, 0xE3, 0x6B, 0x05, 0x8E, 0x98 ), |
| 210 | BYTES_TO_T_UINT_8( 0xE4, 0xE7, 0x3E, 0xE2, 0xA7, 0x2F, 0x31, 0xB3 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 211 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | static const mbedtls_mpi_uint secp384r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 213 | BYTES_TO_T_UINT_8( 0xB7, 0x0A, 0x76, 0x72, 0x38, 0x5E, 0x54, 0x3A ), |
| 214 | BYTES_TO_T_UINT_8( 0x6C, 0x29, 0x55, 0xBF, 0x5D, 0xF2, 0x02, 0x55 ), |
| 215 | BYTES_TO_T_UINT_8( 0x38, 0x2A, 0x54, 0x82, 0xE0, 0x41, 0xF7, 0x59 ), |
| 216 | BYTES_TO_T_UINT_8( 0x98, 0x9B, 0xA7, 0x8B, 0x62, 0x3B, 0x1D, 0x6E ), |
| 217 | BYTES_TO_T_UINT_8( 0x74, 0xAD, 0x20, 0xF3, 0x1E, 0xC7, 0xB1, 0x8E ), |
| 218 | BYTES_TO_T_UINT_8( 0x37, 0x05, 0x8B, 0xBE, 0x22, 0xCA, 0x87, 0xAA ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 219 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | static const mbedtls_mpi_uint secp384r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 221 | BYTES_TO_T_UINT_8( 0x5F, 0x0E, 0xEA, 0x90, 0x7C, 0x1D, 0x43, 0x7A ), |
| 222 | BYTES_TO_T_UINT_8( 0x9D, 0x81, 0x7E, 0x1D, 0xCE, 0xB1, 0x60, 0x0A ), |
| 223 | BYTES_TO_T_UINT_8( 0xC0, 0xB8, 0xF0, 0xB5, 0x13, 0x31, 0xDA, 0xE9 ), |
| 224 | BYTES_TO_T_UINT_8( 0x7C, 0x14, 0x9A, 0x28, 0xBD, 0x1D, 0xF4, 0xF8 ), |
| 225 | BYTES_TO_T_UINT_8( 0x29, 0xDC, 0x92, 0x92, 0xBF, 0x98, 0x9E, 0x5D ), |
| 226 | BYTES_TO_T_UINT_8( 0x6F, 0x2C, 0x26, 0x96, 0x4A, 0xDE, 0x17, 0x36 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 227 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | static const mbedtls_mpi_uint secp384r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 229 | BYTES_TO_T_UINT_8( 0x73, 0x29, 0xC5, 0xCC, 0x6A, 0x19, 0xEC, 0xEC ), |
| 230 | BYTES_TO_T_UINT_8( 0x7A, 0xA7, 0xB0, 0x48, 0xB2, 0x0D, 0x1A, 0x58 ), |
| 231 | BYTES_TO_T_UINT_8( 0xDF, 0x2D, 0x37, 0xF4, 0x81, 0x4D, 0x63, 0xC7 ), |
| 232 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 233 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 234 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 235 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * Domain parameters for secp521r1 |
| 240 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 242 | static const mbedtls_mpi_uint secp521r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 243 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 244 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 245 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 246 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 247 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 248 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 249 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 250 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 251 | BYTES_TO_T_UINT_2( 0xFF, 0x01 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 252 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | static const mbedtls_mpi_uint secp521r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 254 | BYTES_TO_T_UINT_8( 0x00, 0x3F, 0x50, 0x6B, 0xD4, 0x1F, 0x45, 0xEF ), |
| 255 | BYTES_TO_T_UINT_8( 0xF1, 0x34, 0x2C, 0x3D, 0x88, 0xDF, 0x73, 0x35 ), |
| 256 | BYTES_TO_T_UINT_8( 0x07, 0xBF, 0xB1, 0x3B, 0xBD, 0xC0, 0x52, 0x16 ), |
| 257 | BYTES_TO_T_UINT_8( 0x7B, 0x93, 0x7E, 0xEC, 0x51, 0x39, 0x19, 0x56 ), |
| 258 | BYTES_TO_T_UINT_8( 0xE1, 0x09, 0xF1, 0x8E, 0x91, 0x89, 0xB4, 0xB8 ), |
| 259 | BYTES_TO_T_UINT_8( 0xF3, 0x15, 0xB3, 0x99, 0x5B, 0x72, 0xDA, 0xA2 ), |
| 260 | BYTES_TO_T_UINT_8( 0xEE, 0x40, 0x85, 0xB6, 0xA0, 0x21, 0x9A, 0x92 ), |
| 261 | BYTES_TO_T_UINT_8( 0x1F, 0x9A, 0x1C, 0x8E, 0x61, 0xB9, 0x3E, 0x95 ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 262 | BYTES_TO_T_UINT_2( 0x51, 0x00 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 263 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | static const mbedtls_mpi_uint secp521r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 265 | BYTES_TO_T_UINT_8( 0x66, 0xBD, 0xE5, 0xC2, 0x31, 0x7E, 0x7E, 0xF9 ), |
| 266 | BYTES_TO_T_UINT_8( 0x9B, 0x42, 0x6A, 0x85, 0xC1, 0xB3, 0x48, 0x33 ), |
| 267 | BYTES_TO_T_UINT_8( 0xDE, 0xA8, 0xFF, 0xA2, 0x27, 0xC1, 0x1D, 0xFE ), |
| 268 | BYTES_TO_T_UINT_8( 0x28, 0x59, 0xE7, 0xEF, 0x77, 0x5E, 0x4B, 0xA1 ), |
| 269 | BYTES_TO_T_UINT_8( 0xBA, 0x3D, 0x4D, 0x6B, 0x60, 0xAF, 0x28, 0xF8 ), |
| 270 | BYTES_TO_T_UINT_8( 0x21, 0xB5, 0x3F, 0x05, 0x39, 0x81, 0x64, 0x9C ), |
| 271 | BYTES_TO_T_UINT_8( 0x42, 0xB4, 0x95, 0x23, 0x66, 0xCB, 0x3E, 0x9E ), |
| 272 | BYTES_TO_T_UINT_8( 0xCD, 0xE9, 0x04, 0x04, 0xB7, 0x06, 0x8E, 0x85 ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 273 | BYTES_TO_T_UINT_2( 0xC6, 0x00 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 274 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | static const mbedtls_mpi_uint secp521r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 276 | BYTES_TO_T_UINT_8( 0x50, 0x66, 0xD1, 0x9F, 0x76, 0x94, 0xBE, 0x88 ), |
| 277 | BYTES_TO_T_UINT_8( 0x40, 0xC2, 0x72, 0xA2, 0x86, 0x70, 0x3C, 0x35 ), |
| 278 | BYTES_TO_T_UINT_8( 0x61, 0x07, 0xAD, 0x3F, 0x01, 0xB9, 0x50, 0xC5 ), |
| 279 | BYTES_TO_T_UINT_8( 0x40, 0x26, 0xF4, 0x5E, 0x99, 0x72, 0xEE, 0x97 ), |
| 280 | BYTES_TO_T_UINT_8( 0x2C, 0x66, 0x3E, 0x27, 0x17, 0xBD, 0xAF, 0x17 ), |
| 281 | BYTES_TO_T_UINT_8( 0x68, 0x44, 0x9B, 0x57, 0x49, 0x44, 0xF5, 0x98 ), |
| 282 | BYTES_TO_T_UINT_8( 0xD9, 0x1B, 0x7D, 0x2C, 0xB4, 0x5F, 0x8A, 0x5C ), |
| 283 | BYTES_TO_T_UINT_8( 0x04, 0xC0, 0x3B, 0x9A, 0x78, 0x6A, 0x29, 0x39 ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 284 | BYTES_TO_T_UINT_2( 0x18, 0x01 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 285 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | static const mbedtls_mpi_uint secp521r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 287 | BYTES_TO_T_UINT_8( 0x09, 0x64, 0x38, 0x91, 0x1E, 0xB7, 0x6F, 0xBB ), |
| 288 | BYTES_TO_T_UINT_8( 0xAE, 0x47, 0x9C, 0x89, 0xB8, 0xC9, 0xB5, 0x3B ), |
| 289 | BYTES_TO_T_UINT_8( 0xD0, 0xA5, 0x09, 0xF7, 0x48, 0x01, 0xCC, 0x7F ), |
| 290 | BYTES_TO_T_UINT_8( 0x6B, 0x96, 0x2F, 0xBF, 0x83, 0x87, 0x86, 0x51 ), |
| 291 | BYTES_TO_T_UINT_8( 0xFA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 292 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 293 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 294 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
Manuel Pégourié-Gonnard | 14a96c5 | 2013-12-11 12:15:28 +0100 | [diff] [blame] | 295 | BYTES_TO_T_UINT_2( 0xFF, 0x01 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 296 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 298 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
| 300 | static const mbedtls_mpi_uint secp192k1_p[] = { |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 301 | BYTES_TO_T_UINT_8( 0x37, 0xEE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ), |
| 302 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 303 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 304 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | static const mbedtls_mpi_uint secp192k1_a[] = { |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 306 | BYTES_TO_T_UINT_2( 0x00, 0x00 ), |
| 307 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | static const mbedtls_mpi_uint secp192k1_b[] = { |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 309 | BYTES_TO_T_UINT_2( 0x03, 0x00 ), |
| 310 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | static const mbedtls_mpi_uint secp192k1_gx[] = { |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 312 | BYTES_TO_T_UINT_8( 0x7D, 0x6C, 0xE0, 0xEA, 0xB1, 0xD1, 0xA5, 0x1D ), |
| 313 | BYTES_TO_T_UINT_8( 0x34, 0xF4, 0xB7, 0x80, 0x02, 0x7D, 0xB0, 0x26 ), |
| 314 | BYTES_TO_T_UINT_8( 0xAE, 0xE9, 0x57, 0xC0, 0x0E, 0xF1, 0x4F, 0xDB ), |
| 315 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | static const mbedtls_mpi_uint secp192k1_gy[] = { |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 317 | BYTES_TO_T_UINT_8( 0x9D, 0x2F, 0x5E, 0xD9, 0x88, 0xAA, 0x82, 0x40 ), |
| 318 | BYTES_TO_T_UINT_8( 0x34, 0x86, 0xBE, 0x15, 0xD0, 0x63, 0x41, 0x84 ), |
| 319 | BYTES_TO_T_UINT_8( 0xA7, 0x28, 0x56, 0x9C, 0x6D, 0x2F, 0x2F, 0x9B ), |
| 320 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | static const mbedtls_mpi_uint secp192k1_n[] = { |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 322 | BYTES_TO_T_UINT_8( 0x8D, 0xFD, 0xDE, 0x74, 0x6A, 0x46, 0x69, 0x0F ), |
| 323 | BYTES_TO_T_UINT_8( 0x17, 0xFC, 0xF2, 0x26, 0xFE, 0xFF, 0xFF, 0xFF ), |
| 324 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 325 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
| 329 | static const mbedtls_mpi_uint secp224k1_p[] = { |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 330 | BYTES_TO_T_UINT_8( 0x6D, 0xE5, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ), |
| 331 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 332 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 333 | BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ), |
| 334 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | static const mbedtls_mpi_uint secp224k1_a[] = { |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 336 | BYTES_TO_T_UINT_2( 0x00, 0x00 ), |
| 337 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | static const mbedtls_mpi_uint secp224k1_b[] = { |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 339 | BYTES_TO_T_UINT_2( 0x05, 0x00 ), |
| 340 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | static const mbedtls_mpi_uint secp224k1_gx[] = { |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 342 | BYTES_TO_T_UINT_8( 0x5C, 0xA4, 0xB7, 0xB6, 0x0E, 0x65, 0x7E, 0x0F ), |
| 343 | BYTES_TO_T_UINT_8( 0xA9, 0x75, 0x70, 0xE4, 0xE9, 0x67, 0xA4, 0x69 ), |
| 344 | BYTES_TO_T_UINT_8( 0xA1, 0x28, 0xFC, 0x30, 0xDF, 0x99, 0xF0, 0x4D ), |
| 345 | BYTES_TO_T_UINT_4( 0x33, 0x5B, 0x45, 0xA1 ), |
| 346 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | static const mbedtls_mpi_uint secp224k1_gy[] = { |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 348 | BYTES_TO_T_UINT_8( 0xA5, 0x61, 0x6D, 0x55, 0xDB, 0x4B, 0xCA, 0xE2 ), |
| 349 | BYTES_TO_T_UINT_8( 0x59, 0xBD, 0xB0, 0xC0, 0xF7, 0x19, 0xE3, 0xF7 ), |
| 350 | BYTES_TO_T_UINT_8( 0xD6, 0xFB, 0xCA, 0x82, 0x42, 0x34, 0xBA, 0x7F ), |
| 351 | BYTES_TO_T_UINT_4( 0xED, 0x9F, 0x08, 0x7E ), |
| 352 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | static const mbedtls_mpi_uint secp224k1_n[] = { |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 354 | BYTES_TO_T_UINT_8( 0xF7, 0xB1, 0x9F, 0x76, 0x71, 0xA9, 0xF0, 0xCA ), |
| 355 | BYTES_TO_T_UINT_8( 0x84, 0x61, 0xEC, 0xD2, 0xE8, 0xDC, 0x01, 0x00 ), |
| 356 | BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ), |
| 357 | BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ), |
| 358 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 362 | static const mbedtls_mpi_uint secp256k1_p[] = { |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 363 | BYTES_TO_T_UINT_8( 0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ), |
| 364 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 365 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 366 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 367 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | static const mbedtls_mpi_uint secp256k1_a[] = { |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 369 | BYTES_TO_T_UINT_2( 0x00, 0x00 ), |
| 370 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 371 | static const mbedtls_mpi_uint secp256k1_b[] = { |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 372 | BYTES_TO_T_UINT_2( 0x07, 0x00 ), |
| 373 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | static const mbedtls_mpi_uint secp256k1_gx[] = { |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 375 | BYTES_TO_T_UINT_8( 0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59 ), |
| 376 | BYTES_TO_T_UINT_8( 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02 ), |
| 377 | BYTES_TO_T_UINT_8( 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55 ), |
| 378 | BYTES_TO_T_UINT_8( 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79 ), |
| 379 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | static const mbedtls_mpi_uint secp256k1_gy[] = { |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 381 | BYTES_TO_T_UINT_8( 0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C ), |
| 382 | BYTES_TO_T_UINT_8( 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD ), |
| 383 | BYTES_TO_T_UINT_8( 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D ), |
| 384 | BYTES_TO_T_UINT_8( 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48 ), |
| 385 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | static const mbedtls_mpi_uint secp256k1_n[] = { |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 387 | BYTES_TO_T_UINT_8( 0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF ), |
| 388 | BYTES_TO_T_UINT_8( 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA ), |
| 389 | BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 390 | BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ), |
| 391 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 393 | |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 394 | /* |
| 395 | * Domain parameters for brainpoolP256r1 (RFC 5639 3.4) |
| 396 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 397 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
| 398 | static const mbedtls_mpi_uint brainpoolP256r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 399 | BYTES_TO_T_UINT_8( 0x77, 0x53, 0x6E, 0x1F, 0x1D, 0x48, 0x13, 0x20 ), |
| 400 | BYTES_TO_T_UINT_8( 0x28, 0x20, 0x26, 0xD5, 0x23, 0xF6, 0x3B, 0x6E ), |
| 401 | BYTES_TO_T_UINT_8( 0x72, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ), |
| 402 | BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 403 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | static const mbedtls_mpi_uint brainpoolP256r1_a[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 405 | BYTES_TO_T_UINT_8( 0xD9, 0xB5, 0x30, 0xF3, 0x44, 0x4B, 0x4A, 0xE9 ), |
| 406 | BYTES_TO_T_UINT_8( 0x6C, 0x5C, 0xDC, 0x26, 0xC1, 0x55, 0x80, 0xFB ), |
| 407 | BYTES_TO_T_UINT_8( 0xE7, 0xFF, 0x7A, 0x41, 0x30, 0x75, 0xF6, 0xEE ), |
| 408 | BYTES_TO_T_UINT_8( 0x57, 0x30, 0x2C, 0xFC, 0x75, 0x09, 0x5A, 0x7D ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 409 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | static const mbedtls_mpi_uint brainpoolP256r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 411 | BYTES_TO_T_UINT_8( 0xB6, 0x07, 0x8C, 0xFF, 0x18, 0xDC, 0xCC, 0x6B ), |
| 412 | BYTES_TO_T_UINT_8( 0xCE, 0xE1, 0xF7, 0x5C, 0x29, 0x16, 0x84, 0x95 ), |
| 413 | BYTES_TO_T_UINT_8( 0xBF, 0x7C, 0xD7, 0xBB, 0xD9, 0xB5, 0x30, 0xF3 ), |
| 414 | BYTES_TO_T_UINT_8( 0x44, 0x4B, 0x4A, 0xE9, 0x6C, 0x5C, 0xDC, 0x26 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 415 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | static const mbedtls_mpi_uint brainpoolP256r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 417 | BYTES_TO_T_UINT_8( 0x62, 0x32, 0xCE, 0x9A, 0xBD, 0x53, 0x44, 0x3A ), |
| 418 | BYTES_TO_T_UINT_8( 0xC2, 0x23, 0xBD, 0xE3, 0xE1, 0x27, 0xDE, 0xB9 ), |
| 419 | BYTES_TO_T_UINT_8( 0xAF, 0xB7, 0x81, 0xFC, 0x2F, 0x48, 0x4B, 0x2C ), |
| 420 | BYTES_TO_T_UINT_8( 0xCB, 0x57, 0x7E, 0xCB, 0xB9, 0xAE, 0xD2, 0x8B ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 421 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | static const mbedtls_mpi_uint brainpoolP256r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 423 | BYTES_TO_T_UINT_8( 0x97, 0x69, 0x04, 0x2F, 0xC7, 0x54, 0x1D, 0x5C ), |
| 424 | BYTES_TO_T_UINT_8( 0x54, 0x8E, 0xED, 0x2D, 0x13, 0x45, 0x77, 0xC2 ), |
| 425 | BYTES_TO_T_UINT_8( 0xC9, 0x1D, 0x61, 0x14, 0x1A, 0x46, 0xF8, 0x97 ), |
| 426 | BYTES_TO_T_UINT_8( 0xFD, 0xC4, 0xDA, 0xC3, 0x35, 0xF8, 0x7E, 0x54 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 427 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | static const mbedtls_mpi_uint brainpoolP256r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 429 | BYTES_TO_T_UINT_8( 0xA7, 0x56, 0x48, 0x97, 0x82, 0x0E, 0x1E, 0x90 ), |
| 430 | BYTES_TO_T_UINT_8( 0xF7, 0xA6, 0x61, 0xB5, 0xA3, 0x7A, 0x39, 0x8C ), |
| 431 | BYTES_TO_T_UINT_8( 0x71, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ), |
| 432 | BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 433 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * Domain parameters for brainpoolP384r1 (RFC 5639 3.6) |
| 438 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
| 440 | static const mbedtls_mpi_uint brainpoolP384r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 441 | BYTES_TO_T_UINT_8( 0x53, 0xEC, 0x07, 0x31, 0x13, 0x00, 0x47, 0x87 ), |
| 442 | BYTES_TO_T_UINT_8( 0x71, 0x1A, 0x1D, 0x90, 0x29, 0xA7, 0xD3, 0xAC ), |
| 443 | BYTES_TO_T_UINT_8( 0x23, 0x11, 0xB7, 0x7F, 0x19, 0xDA, 0xB1, 0x12 ), |
| 444 | BYTES_TO_T_UINT_8( 0xB4, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ), |
| 445 | BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ), |
| 446 | BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 447 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | static const mbedtls_mpi_uint brainpoolP384r1_a[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 449 | BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ), |
| 450 | BYTES_TO_T_UINT_8( 0xEB, 0xD4, 0x3A, 0x50, 0x4A, 0x81, 0xA5, 0x8A ), |
| 451 | BYTES_TO_T_UINT_8( 0x0F, 0xF9, 0x91, 0xBA, 0xEF, 0x65, 0x91, 0x13 ), |
| 452 | BYTES_TO_T_UINT_8( 0x87, 0x27, 0xB2, 0x4F, 0x8E, 0xA2, 0xBE, 0xC2 ), |
| 453 | BYTES_TO_T_UINT_8( 0xA0, 0xAF, 0x05, 0xCE, 0x0A, 0x08, 0x72, 0x3C ), |
| 454 | BYTES_TO_T_UINT_8( 0x0C, 0x15, 0x8C, 0x3D, 0xC6, 0x82, 0xC3, 0x7B ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 455 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 456 | static const mbedtls_mpi_uint brainpoolP384r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 457 | BYTES_TO_T_UINT_8( 0x11, 0x4C, 0x50, 0xFA, 0x96, 0x86, 0xB7, 0x3A ), |
| 458 | BYTES_TO_T_UINT_8( 0x94, 0xC9, 0xDB, 0x95, 0x02, 0x39, 0xB4, 0x7C ), |
| 459 | BYTES_TO_T_UINT_8( 0xD5, 0x62, 0xEB, 0x3E, 0xA5, 0x0E, 0x88, 0x2E ), |
| 460 | BYTES_TO_T_UINT_8( 0xA6, 0xD2, 0xDC, 0x07, 0xE1, 0x7D, 0xB7, 0x2F ), |
| 461 | BYTES_TO_T_UINT_8( 0x7C, 0x44, 0xF0, 0x16, 0x54, 0xB5, 0x39, 0x8B ), |
| 462 | BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 463 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | static const mbedtls_mpi_uint brainpoolP384r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 465 | BYTES_TO_T_UINT_8( 0x1E, 0xAF, 0xD4, 0x47, 0xE2, 0xB2, 0x87, 0xEF ), |
| 466 | BYTES_TO_T_UINT_8( 0xAA, 0x46, 0xD6, 0x36, 0x34, 0xE0, 0x26, 0xE8 ), |
| 467 | BYTES_TO_T_UINT_8( 0xE8, 0x10, 0xBD, 0x0C, 0xFE, 0xCA, 0x7F, 0xDB ), |
| 468 | BYTES_TO_T_UINT_8( 0xE3, 0x4F, 0xF1, 0x7E, 0xE7, 0xA3, 0x47, 0x88 ), |
| 469 | BYTES_TO_T_UINT_8( 0x6B, 0x3F, 0xC1, 0xB7, 0x81, 0x3A, 0xA6, 0xA2 ), |
| 470 | BYTES_TO_T_UINT_8( 0xFF, 0x45, 0xCF, 0x68, 0xF0, 0x64, 0x1C, 0x1D ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 471 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | static const mbedtls_mpi_uint brainpoolP384r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 473 | BYTES_TO_T_UINT_8( 0x15, 0x53, 0x3C, 0x26, 0x41, 0x03, 0x82, 0x42 ), |
| 474 | BYTES_TO_T_UINT_8( 0x11, 0x81, 0x91, 0x77, 0x21, 0x46, 0x46, 0x0E ), |
| 475 | BYTES_TO_T_UINT_8( 0x28, 0x29, 0x91, 0xF9, 0x4F, 0x05, 0x9C, 0xE1 ), |
| 476 | BYTES_TO_T_UINT_8( 0x64, 0x58, 0xEC, 0xFE, 0x29, 0x0B, 0xB7, 0x62 ), |
| 477 | BYTES_TO_T_UINT_8( 0x52, 0xD5, 0xCF, 0x95, 0x8E, 0xEB, 0xB1, 0x5C ), |
| 478 | BYTES_TO_T_UINT_8( 0xA4, 0xC2, 0xF9, 0x20, 0x75, 0x1D, 0xBE, 0x8A ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 479 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | static const mbedtls_mpi_uint brainpoolP384r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 481 | BYTES_TO_T_UINT_8( 0x65, 0x65, 0x04, 0xE9, 0x02, 0x32, 0x88, 0x3B ), |
| 482 | BYTES_TO_T_UINT_8( 0x10, 0xC3, 0x7F, 0x6B, 0xAF, 0xB6, 0x3A, 0xCF ), |
| 483 | BYTES_TO_T_UINT_8( 0xA7, 0x25, 0x04, 0xAC, 0x6C, 0x6E, 0x16, 0x1F ), |
| 484 | BYTES_TO_T_UINT_8( 0xB3, 0x56, 0x54, 0xED, 0x09, 0x71, 0x2F, 0x15 ), |
| 485 | BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ), |
| 486 | BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 487 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Domain parameters for brainpoolP512r1 (RFC 5639 3.7) |
| 492 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
| 494 | static const mbedtls_mpi_uint brainpoolP512r1_p[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 495 | BYTES_TO_T_UINT_8( 0xF3, 0x48, 0x3A, 0x58, 0x56, 0x60, 0xAA, 0x28 ), |
| 496 | BYTES_TO_T_UINT_8( 0x85, 0xC6, 0x82, 0x2D, 0x2F, 0xFF, 0x81, 0x28 ), |
| 497 | BYTES_TO_T_UINT_8( 0xE6, 0x80, 0xA3, 0xE6, 0x2A, 0xA1, 0xCD, 0xAE ), |
| 498 | BYTES_TO_T_UINT_8( 0x42, 0x68, 0xC6, 0x9B, 0x00, 0x9B, 0x4D, 0x7D ), |
| 499 | BYTES_TO_T_UINT_8( 0x71, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ), |
| 500 | BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ), |
| 501 | BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ), |
| 502 | BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 503 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | static const mbedtls_mpi_uint brainpoolP512r1_a[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 505 | BYTES_TO_T_UINT_8( 0xCA, 0x94, 0xFC, 0x77, 0x4D, 0xAC, 0xC1, 0xE7 ), |
| 506 | BYTES_TO_T_UINT_8( 0xB9, 0xC7, 0xF2, 0x2B, 0xA7, 0x17, 0x11, 0x7F ), |
| 507 | BYTES_TO_T_UINT_8( 0xB5, 0xC8, 0x9A, 0x8B, 0xC9, 0xF1, 0x2E, 0x0A ), |
| 508 | BYTES_TO_T_UINT_8( 0xA1, 0x3A, 0x25, 0xA8, 0x5A, 0x5D, 0xED, 0x2D ), |
| 509 | BYTES_TO_T_UINT_8( 0xBC, 0x63, 0x98, 0xEA, 0xCA, 0x41, 0x34, 0xA8 ), |
| 510 | BYTES_TO_T_UINT_8( 0x10, 0x16, 0xF9, 0x3D, 0x8D, 0xDD, 0xCB, 0x94 ), |
| 511 | BYTES_TO_T_UINT_8( 0xC5, 0x4C, 0x23, 0xAC, 0x45, 0x71, 0x32, 0xE2 ), |
| 512 | BYTES_TO_T_UINT_8( 0x89, 0x3B, 0x60, 0x8B, 0x31, 0xA3, 0x30, 0x78 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 513 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | static const mbedtls_mpi_uint brainpoolP512r1_b[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 515 | BYTES_TO_T_UINT_8( 0x23, 0xF7, 0x16, 0x80, 0x63, 0xBD, 0x09, 0x28 ), |
| 516 | BYTES_TO_T_UINT_8( 0xDD, 0xE5, 0xBA, 0x5E, 0xB7, 0x50, 0x40, 0x98 ), |
| 517 | BYTES_TO_T_UINT_8( 0x67, 0x3E, 0x08, 0xDC, 0xCA, 0x94, 0xFC, 0x77 ), |
| 518 | BYTES_TO_T_UINT_8( 0x4D, 0xAC, 0xC1, 0xE7, 0xB9, 0xC7, 0xF2, 0x2B ), |
| 519 | BYTES_TO_T_UINT_8( 0xA7, 0x17, 0x11, 0x7F, 0xB5, 0xC8, 0x9A, 0x8B ), |
| 520 | BYTES_TO_T_UINT_8( 0xC9, 0xF1, 0x2E, 0x0A, 0xA1, 0x3A, 0x25, 0xA8 ), |
| 521 | BYTES_TO_T_UINT_8( 0x5A, 0x5D, 0xED, 0x2D, 0xBC, 0x63, 0x98, 0xEA ), |
| 522 | BYTES_TO_T_UINT_8( 0xCA, 0x41, 0x34, 0xA8, 0x10, 0x16, 0xF9, 0x3D ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 523 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | static const mbedtls_mpi_uint brainpoolP512r1_gx[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 525 | BYTES_TO_T_UINT_8( 0x22, 0xF8, 0xB9, 0xBC, 0x09, 0x22, 0x35, 0x8B ), |
| 526 | BYTES_TO_T_UINT_8( 0x68, 0x5E, 0x6A, 0x40, 0x47, 0x50, 0x6D, 0x7C ), |
| 527 | BYTES_TO_T_UINT_8( 0x5F, 0x7D, 0xB9, 0x93, 0x7B, 0x68, 0xD1, 0x50 ), |
| 528 | BYTES_TO_T_UINT_8( 0x8D, 0xD4, 0xD0, 0xE2, 0x78, 0x1F, 0x3B, 0xFF ), |
| 529 | BYTES_TO_T_UINT_8( 0x8E, 0x09, 0xD0, 0xF4, 0xEE, 0x62, 0x3B, 0xB4 ), |
| 530 | BYTES_TO_T_UINT_8( 0xC1, 0x16, 0xD9, 0xB5, 0x70, 0x9F, 0xED, 0x85 ), |
| 531 | BYTES_TO_T_UINT_8( 0x93, 0x6A, 0x4C, 0x9C, 0x2E, 0x32, 0x21, 0x5A ), |
| 532 | BYTES_TO_T_UINT_8( 0x64, 0xD9, 0x2E, 0xD8, 0xBD, 0xE4, 0xAE, 0x81 ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 533 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | static const mbedtls_mpi_uint brainpoolP512r1_gy[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 535 | BYTES_TO_T_UINT_8( 0x92, 0x08, 0xD8, 0x3A, 0x0F, 0x1E, 0xCD, 0x78 ), |
| 536 | BYTES_TO_T_UINT_8( 0x06, 0x54, 0xF0, 0xA8, 0x2F, 0x2B, 0xCA, 0xD1 ), |
| 537 | BYTES_TO_T_UINT_8( 0xAE, 0x63, 0x27, 0x8A, 0xD8, 0x4B, 0xCA, 0x5B ), |
| 538 | BYTES_TO_T_UINT_8( 0x5E, 0x48, 0x5F, 0x4A, 0x49, 0xDE, 0xDC, 0xB2 ), |
| 539 | BYTES_TO_T_UINT_8( 0x11, 0x81, 0x1F, 0x88, 0x5B, 0xC5, 0x00, 0xA0 ), |
| 540 | BYTES_TO_T_UINT_8( 0x1A, 0x7B, 0xA5, 0x24, 0x00, 0xF7, 0x09, 0xF2 ), |
| 541 | BYTES_TO_T_UINT_8( 0xFD, 0x22, 0x78, 0xCF, 0xA9, 0xBF, 0xEA, 0xC0 ), |
| 542 | BYTES_TO_T_UINT_8( 0xEC, 0x32, 0x63, 0x56, 0x5D, 0x38, 0xDE, 0x7D ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 543 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | static const mbedtls_mpi_uint brainpoolP512r1_n[] = { |
Manuel Pégourié-Gonnard | 95b45b7 | 2013-12-11 12:03:23 +0100 | [diff] [blame] | 545 | BYTES_TO_T_UINT_8( 0x69, 0x00, 0xA9, 0x9C, 0x82, 0x96, 0x87, 0xB5 ), |
| 546 | BYTES_TO_T_UINT_8( 0xDD, 0xDA, 0x5D, 0x08, 0x81, 0xD3, 0xB1, 0x1D ), |
| 547 | BYTES_TO_T_UINT_8( 0x47, 0x10, 0xAC, 0x7F, 0x19, 0x61, 0x86, 0x41 ), |
| 548 | BYTES_TO_T_UINT_8( 0x19, 0x26, 0xA9, 0x4C, 0x41, 0x5C, 0x3E, 0x55 ), |
| 549 | BYTES_TO_T_UINT_8( 0x70, 0x08, 0x33, 0x70, 0xCA, 0x9C, 0x63, 0xD6 ), |
| 550 | BYTES_TO_T_UINT_8( 0x0E, 0xD2, 0xC9, 0xB3, 0xB3, 0x8D, 0x30, 0xCB ), |
| 551 | BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ), |
| 552 | BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ), |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 553 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 555 | |
Gilles Peskine | aa9493a | 2018-09-12 14:44:03 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \ |
| 557 | defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ |
| 558 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ |
| 559 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \ |
| 560 | defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \ |
| 561 | defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \ |
| 562 | defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \ |
| 563 | defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \ |
| 564 | defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \ |
| 565 | defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \ |
| 566 | defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 567 | /* For these curves, we build the group parameters dynamically. */ |
| 568 | #define ECP_LOAD_GROUP |
| 569 | #endif |
| 570 | |
| 571 | #if defined(ECP_LOAD_GROUP) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 572 | /* |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 573 | * Create an MPI from embedded constants |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | * (assumes len is an exact multiple of sizeof mbedtls_mpi_uint) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 575 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | static inline void ecp_mpi_load( mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len ) |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 577 | { |
| 578 | X->s = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | X->n = len / sizeof( mbedtls_mpi_uint ); |
| 580 | X->p = (mbedtls_mpi_uint *) p; |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 584 | * Set an MPI to static value 1 |
| 585 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | static inline void ecp_mpi_set1( mbedtls_mpi *X ) |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | static mbedtls_mpi_uint one[] = { 1 }; |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 589 | X->s = 1; |
| 590 | X->n = 1; |
| 591 | X->p = one; |
| 592 | } |
| 593 | |
| 594 | /* |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 595 | * Make group available from embedded constants |
| 596 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | static int ecp_group_load( mbedtls_ecp_group *grp, |
| 598 | const mbedtls_mpi_uint *p, size_t plen, |
| 599 | const mbedtls_mpi_uint *a, size_t alen, |
| 600 | const mbedtls_mpi_uint *b, size_t blen, |
| 601 | const mbedtls_mpi_uint *gx, size_t gxlen, |
| 602 | const mbedtls_mpi_uint *gy, size_t gylen, |
| 603 | const mbedtls_mpi_uint *n, size_t nlen) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 604 | { |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 605 | ecp_mpi_load( &grp->P, p, plen ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 606 | if( a != NULL ) |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 607 | ecp_mpi_load( &grp->A, a, alen ); |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 608 | ecp_mpi_load( &grp->B, b, blen ); |
| 609 | ecp_mpi_load( &grp->N, n, nlen ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 610 | |
Manuel Pégourié-Gonnard | 731d08b | 2013-12-06 12:16:10 +0100 | [diff] [blame] | 611 | ecp_mpi_load( &grp->G.X, gx, gxlen ); |
| 612 | ecp_mpi_load( &grp->G.Y, gy, gylen ); |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 613 | ecp_mpi_set1( &grp->G.Z ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 614 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 615 | grp->pbits = mbedtls_mpi_bitlen( &grp->P ); |
| 616 | grp->nbits = mbedtls_mpi_bitlen( &grp->N ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 617 | |
Manuel Pégourié-Gonnard | 1f82b04 | 2013-12-06 12:51:50 +0100 | [diff] [blame] | 618 | grp->h = 1; |
| 619 | |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 620 | return( 0 ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 621 | } |
Gilles Peskine | aa9493a | 2018-09-12 14:44:03 +0200 | [diff] [blame] | 622 | #endif /* ECP_LOAD_GROUP */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 623 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | #if defined(MBEDTLS_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 625 | /* Forward declarations */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
| 627 | static int ecp_mod_p192( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 628 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
| 630 | static int ecp_mod_p224( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 631 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 633 | static int ecp_mod_p256( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 634 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 636 | static int ecp_mod_p384( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 637 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 639 | static int ecp_mod_p521( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 640 | #endif |
Manuel Pégourié-Gonnard | 3ee9000 | 2013-12-02 17:14:48 +0100 | [diff] [blame] | 641 | |
| 642 | #define NIST_MODP( P ) grp->modp = ecp_mod_ ## P; |
| 643 | #else |
| 644 | #define NIST_MODP( P ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | #endif /* MBEDTLS_ECP_NIST_OPTIM */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 646 | |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 647 | /* Additional forward declarations */ |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 648 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | static int ecp_mod_p255( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 650 | #endif |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 651 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 652 | static int ecp_mod_p448( mbedtls_mpi * ); |
| 653 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
| 655 | static int ecp_mod_p192k1( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 656 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
| 658 | static int ecp_mod_p224k1( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 659 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 660 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 661 | static int ecp_mod_p256k1( mbedtls_mpi * ); |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 662 | #endif |
| 663 | |
Gilles Peskine | aa9493a | 2018-09-12 14:44:03 +0200 | [diff] [blame] | 664 | #if defined(ECP_LOAD_GROUP) |
Manuel Pégourié-Gonnard | 81e1b10 | 2013-12-06 13:28:05 +0100 | [diff] [blame] | 665 | #define LOAD_GROUP_A( G ) ecp_group_load( grp, \ |
| 666 | G ## _p, sizeof( G ## _p ), \ |
| 667 | G ## _a, sizeof( G ## _a ), \ |
| 668 | G ## _b, sizeof( G ## _b ), \ |
| 669 | G ## _gx, sizeof( G ## _gx ), \ |
| 670 | G ## _gy, sizeof( G ## _gy ), \ |
| 671 | G ## _n, sizeof( G ## _n ) ) |
| 672 | |
| 673 | #define LOAD_GROUP( G ) ecp_group_load( grp, \ |
| 674 | G ## _p, sizeof( G ## _p ), \ |
| 675 | NULL, 0, \ |
| 676 | G ## _b, sizeof( G ## _b ), \ |
| 677 | G ## _gx, sizeof( G ## _gx ), \ |
| 678 | G ## _gy, sizeof( G ## _gy ), \ |
| 679 | G ## _n, sizeof( G ## _n ) ) |
Gilles Peskine | aa9493a | 2018-09-12 14:44:03 +0200 | [diff] [blame] | 680 | #endif /* ECP_LOAD_GROUP */ |
Manuel Pégourié-Gonnard | 81e1b10 | 2013-12-06 13:28:05 +0100 | [diff] [blame] | 681 | |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 682 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 683 | /* |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 684 | * Specialized function for creating the Curve25519 group |
| 685 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | static int ecp_use_curve25519( mbedtls_ecp_group *grp ) |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 687 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 688 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 689 | |
| 690 | /* Actually ( A + 2 ) / 4 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->A, 16, "01DB42" ) ); |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 692 | |
| 693 | /* P = 2^255 - 19 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->P, 1 ) ); |
| 695 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 255 ) ); |
| 696 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 19 ) ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 697 | grp->pbits = mbedtls_mpi_bitlen( &grp->P ); |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 698 | |
Nicholas Wilson | 54fc34e | 2016-05-16 15:15:45 +0100 | [diff] [blame] | 699 | /* N = 2^252 + 27742317777372353535851937790883648493 */ |
| 700 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->N, 16, |
| 701 | "14DEF9DEA2F79CD65812631A5CF5D3ED" ) ); |
| 702 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &grp->N, 252, 1 ) ); |
| 703 | |
Manuel Pégourié-Gonnard | 18b7843 | 2018-03-28 11:14:06 +0200 | [diff] [blame] | 704 | /* Y intentionally not set, since we use x/z coordinates. |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 705 | * This is used as a marker to identify Montgomery curves! */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 706 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.X, 9 ) ); |
| 707 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.Z, 1 ) ); |
| 708 | mbedtls_mpi_free( &grp->G.Y ); |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 709 | |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 710 | /* Actually, the required msb for private keys */ |
| 711 | grp->nbits = 254; |
| 712 | |
| 713 | cleanup: |
| 714 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | mbedtls_ecp_group_free( grp ); |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 716 | |
| 717 | return( ret ); |
| 718 | } |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 719 | #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 720 | |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 721 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 722 | /* |
| 723 | * Specialized function for creating the Curve448 group |
| 724 | */ |
| 725 | static int ecp_use_curve448( mbedtls_ecp_group *grp ) |
| 726 | { |
| 727 | mbedtls_mpi Ns; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 728 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 729 | |
| 730 | mbedtls_mpi_init( &Ns ); |
| 731 | |
| 732 | /* Actually ( A + 2 ) / 4 */ |
| 733 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->A, 16, "98AA" ) ); |
| 734 | |
| 735 | /* P = 2^448 - 2^224 - 1 */ |
| 736 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->P, 1 ) ); |
| 737 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 224 ) ); |
| 738 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 1 ) ); |
| 739 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 224 ) ); |
| 740 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 1 ) ); |
| 741 | grp->pbits = mbedtls_mpi_bitlen( &grp->P ); |
| 742 | |
| 743 | /* Y intentionally not set, since we use x/z coordinates. |
| 744 | * This is used as a marker to identify Montgomery curves! */ |
| 745 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.X, 5 ) ); |
| 746 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.Z, 1 ) ); |
| 747 | mbedtls_mpi_free( &grp->G.Y ); |
| 748 | |
| 749 | /* N = 2^446 - 13818066809895115352007386748515426880336692474882178609894547503885 */ |
| 750 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &grp->N, 446, 1 ) ); |
| 751 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &Ns, 16, |
| 752 | "8335DC163BB124B65129C96FDE933D8D723A70AADC873D6D54A7BB0D" ) ); |
| 753 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &grp->N, &grp->N, &Ns ) ); |
| 754 | |
| 755 | /* Actually, the required msb for private keys */ |
| 756 | grp->nbits = 447; |
| 757 | |
| 758 | cleanup: |
| 759 | mbedtls_mpi_free( &Ns ); |
| 760 | if( ret != 0 ) |
| 761 | mbedtls_ecp_group_free( grp ); |
| 762 | |
| 763 | return( ret ); |
| 764 | } |
| 765 | #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */ |
| 766 | |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 767 | /* |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 768 | * Set a group using well-known domain parameters |
| 769 | */ |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 770 | int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 771 | { |
Hanno Becker | 4f8e8e5 | 2018-12-14 15:08:03 +0000 | [diff] [blame] | 772 | ECP_VALIDATE_RET( grp != NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 773 | mbedtls_ecp_group_free( grp ); |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 774 | |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 775 | grp->id = id; |
| 776 | |
| 777 | switch( id ) |
| 778 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
| 780 | case MBEDTLS_ECP_DP_SECP192R1: |
Manuel Pégourié-Gonnard | 3ee9000 | 2013-12-02 17:14:48 +0100 | [diff] [blame] | 781 | NIST_MODP( p192 ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 782 | return( LOAD_GROUP( secp192r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 783 | #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
| 786 | case MBEDTLS_ECP_DP_SECP224R1: |
Manuel Pégourié-Gonnard | 3ee9000 | 2013-12-02 17:14:48 +0100 | [diff] [blame] | 787 | NIST_MODP( p224 ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 788 | return( LOAD_GROUP( secp224r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 790 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 792 | case MBEDTLS_ECP_DP_SECP256R1: |
Manuel Pégourié-Gonnard | 3ee9000 | 2013-12-02 17:14:48 +0100 | [diff] [blame] | 793 | NIST_MODP( p256 ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 794 | return( LOAD_GROUP( secp256r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 796 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 798 | case MBEDTLS_ECP_DP_SECP384R1: |
Manuel Pégourié-Gonnard | 3ee9000 | 2013-12-02 17:14:48 +0100 | [diff] [blame] | 799 | NIST_MODP( p384 ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 800 | return( LOAD_GROUP( secp384r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 801 | #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 802 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 803 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 804 | case MBEDTLS_ECP_DP_SECP521R1: |
Manuel Pégourié-Gonnard | 3ee9000 | 2013-12-02 17:14:48 +0100 | [diff] [blame] | 805 | NIST_MODP( p521 ); |
Manuel Pégourié-Gonnard | 9854fe9 | 2013-12-02 16:30:43 +0100 | [diff] [blame] | 806 | return( LOAD_GROUP( secp521r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 809 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
| 810 | case MBEDTLS_ECP_DP_SECP192K1: |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 811 | grp->modp = ecp_mod_p192k1; |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 812 | return( LOAD_GROUP_A( secp192k1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ |
Manuel Pégourié-Gonnard | ea499a7 | 2014-01-11 15:58:47 +0100 | [diff] [blame] | 814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
| 816 | case MBEDTLS_ECP_DP_SECP224K1: |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 817 | grp->modp = ecp_mod_p224k1; |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 818 | return( LOAD_GROUP_A( secp224k1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 819 | #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ |
Manuel Pégourié-Gonnard | 18e3ec9 | 2014-01-11 15:22:07 +0100 | [diff] [blame] | 820 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 822 | case MBEDTLS_ECP_DP_SECP256K1: |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 823 | grp->modp = ecp_mod_p256k1; |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 824 | return( LOAD_GROUP_A( secp256k1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 825 | #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ |
Manuel Pégourié-Gonnard | f51c8fc | 2014-01-10 18:17:18 +0100 | [diff] [blame] | 826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
| 828 | case MBEDTLS_ECP_DP_BP256R1: |
Manuel Pégourié-Gonnard | 81e1b10 | 2013-12-06 13:28:05 +0100 | [diff] [blame] | 829 | return( LOAD_GROUP_A( brainpoolP256r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 830 | #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 831 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
| 833 | case MBEDTLS_ECP_DP_BP384R1: |
Manuel Pégourié-Gonnard | 81e1b10 | 2013-12-06 13:28:05 +0100 | [diff] [blame] | 834 | return( LOAD_GROUP_A( brainpoolP384r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 835 | #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 836 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
| 838 | case MBEDTLS_ECP_DP_BP512R1: |
Manuel Pégourié-Gonnard | 81e1b10 | 2013-12-06 13:28:05 +0100 | [diff] [blame] | 839 | return( LOAD_GROUP_A( brainpoolP512r1 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 840 | #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 841 | |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 842 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 843 | case MBEDTLS_ECP_DP_CURVE25519: |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 844 | grp->modp = ecp_mod_p255; |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 845 | return( ecp_use_curve25519( grp ) ); |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 846 | #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ |
Manuel Pégourié-Gonnard | 6615366 | 2013-12-03 14:12:26 +0100 | [diff] [blame] | 847 | |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 848 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 849 | case MBEDTLS_ECP_DP_CURVE448: |
| 850 | grp->modp = ecp_mod_p448; |
| 851 | return( ecp_use_curve448( grp ) ); |
| 852 | #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */ |
| 853 | |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 854 | default: |
Alexander K | 56a74cd | 2019-09-10 17:58:20 +0300 | [diff] [blame] | 855 | grp->id = MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 856 | return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 860 | #if defined(MBEDTLS_ECP_NIST_OPTIM) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 861 | /* |
| 862 | * Fast reduction modulo the primes used by the NIST curves. |
| 863 | * |
| 864 | * These functions are critical for speed, but not needed for correct |
| 865 | * operations. So, we make the choice to heavily rely on the internals of our |
| 866 | * bignum library, which creates a tight coupling between these functions and |
| 867 | * our MPI implementation. However, the coupling between the ECP module and |
| 868 | * MPI remains loose, since these functions can be deactivated at will. |
| 869 | */ |
| 870 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 871 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 872 | /* |
| 873 | * Compared to the way things are presented in FIPS 186-3 D.2, |
| 874 | * we proceed in columns, from right (least significant chunk) to left, |
| 875 | * adding chunks to N in place, and keeping a carry for the next chunk. |
| 876 | * This avoids moving things around in memory, and uselessly adding zeros, |
| 877 | * compared to the more straightforward, line-oriented approach. |
| 878 | * |
| 879 | * For this prime we need to handle data in chunks of 64 bits. |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | * Since this is always a multiple of our basic mbedtls_mpi_uint, we can |
| 881 | * use a mbedtls_mpi_uint * to designate such a chunk, and small loops to handle it. |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 882 | */ |
| 883 | |
| 884 | /* Add 64-bit chunks (dst += src) and update carry */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 885 | static inline void add64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 886 | { |
| 887 | unsigned char i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | mbedtls_mpi_uint c = 0; |
| 889 | for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++, src++ ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 890 | { |
| 891 | *dst += c; c = ( *dst < c ); |
| 892 | *dst += *src; c += ( *dst < *src ); |
| 893 | } |
| 894 | *carry += c; |
| 895 | } |
| 896 | |
| 897 | /* Add carry to a 64-bit chunk and update carry */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | static inline void carry64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 899 | { |
| 900 | unsigned char i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++ ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 902 | { |
| 903 | *dst += *carry; |
| 904 | *carry = ( *dst < *carry ); |
| 905 | } |
| 906 | } |
| 907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | #define WIDTH 8 / sizeof( mbedtls_mpi_uint ) |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 909 | #define A( i ) N->p + (i) * WIDTH |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 910 | #define ADD( i ) add64( p, A( i ), &c ) |
| 911 | #define NEXT p += WIDTH; carry64( p, &c ) |
| 912 | #define LAST p += WIDTH; *p = c; while( ++p < end ) *p = 0 |
| 913 | |
| 914 | /* |
| 915 | * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1) |
| 916 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 917 | static int ecp_mod_p192( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 918 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 919 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 920 | mbedtls_mpi_uint c = 0; |
| 921 | mbedtls_mpi_uint *p, *end; |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 922 | |
| 923 | /* Make sure we have enough blocks so that A(5) is legal */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 924 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, 6 * WIDTH ) ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 925 | |
| 926 | p = N->p; |
| 927 | end = p + N->n; |
| 928 | |
| 929 | ADD( 3 ); ADD( 5 ); NEXT; // A0 += A3 + A5 |
| 930 | ADD( 3 ); ADD( 4 ); ADD( 5 ); NEXT; // A1 += A3 + A4 + A5 |
| 931 | ADD( 4 ); ADD( 5 ); LAST; // A2 += A4 + A5 |
| 932 | |
| 933 | cleanup: |
| 934 | return( ret ); |
| 935 | } |
| 936 | |
| 937 | #undef WIDTH |
| 938 | #undef A |
| 939 | #undef ADD |
| 940 | #undef NEXT |
| 941 | #undef LAST |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 942 | #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 944 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \ |
| 945 | defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ |
| 946 | defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 947 | /* |
| 948 | * The reader is advised to first understand ecp_mod_p192() since the same |
| 949 | * general structure is used here, but with additional complications: |
| 950 | * (1) chunks of 32 bits, and (2) subtractions. |
| 951 | */ |
| 952 | |
| 953 | /* |
| 954 | * For these primes, we need to handle data in chunks of 32 bits. |
| 955 | * This makes it more complicated if we use 64 bits limbs in MPI, |
| 956 | * which prevents us from using a uniform access method as for p192. |
| 957 | * |
| 958 | * So, we define a mini abstraction layer to access 32 bit chunks, |
| 959 | * load them in 'cur' for work, and store them back from 'cur' when done. |
| 960 | * |
| 961 | * While at it, also define the size of N in terms of 32-bit chunks. |
| 962 | */ |
| 963 | #define LOAD32 cur = A( i ); |
| 964 | |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 965 | #if defined(MBEDTLS_HAVE_INT32) /* 32 bit */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 966 | |
| 967 | #define MAX32 N->n |
| 968 | #define A( j ) N->p[j] |
| 969 | #define STORE32 N->p[i] = cur; |
| 970 | |
| 971 | #else /* 64-bit */ |
| 972 | |
| 973 | #define MAX32 N->n * 2 |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 974 | #define A( j ) (j) % 2 ? (uint32_t)( N->p[(j)/2] >> 32 ) : \ |
| 975 | (uint32_t)( N->p[(j)/2] ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 976 | #define STORE32 \ |
| 977 | if( i % 2 ) { \ |
| 978 | N->p[i/2] &= 0x00000000FFFFFFFF; \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 979 | N->p[i/2] |= ((mbedtls_mpi_uint) cur) << 32; \ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 980 | } else { \ |
| 981 | N->p[i/2] &= 0xFFFFFFFF00000000; \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | N->p[i/2] |= (mbedtls_mpi_uint) cur; \ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 983 | } |
| 984 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 985 | #endif /* sizeof( mbedtls_mpi_uint ) */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 986 | |
| 987 | /* |
| 988 | * Helpers for addition and subtraction of chunks, with signed carry. |
| 989 | */ |
| 990 | static inline void add32( uint32_t *dst, uint32_t src, signed char *carry ) |
| 991 | { |
| 992 | *dst += src; |
| 993 | *carry += ( *dst < src ); |
| 994 | } |
| 995 | |
| 996 | static inline void sub32( uint32_t *dst, uint32_t src, signed char *carry ) |
| 997 | { |
| 998 | *carry -= ( *dst < src ); |
| 999 | *dst -= src; |
| 1000 | } |
| 1001 | |
| 1002 | #define ADD( j ) add32( &cur, A( j ), &c ); |
| 1003 | #define SUB( j ) sub32( &cur, A( j ), &c ); |
| 1004 | |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1005 | #define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ |
Gilles Peskine | b76517b | 2021-03-10 23:44:28 +0100 | [diff] [blame] | 1006 | #define biL (ciL << 3) /* bits in limb */ |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1007 | |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1008 | /* |
| 1009 | * Helpers for the main 'loop' |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1010 | */ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 1011 | #define INIT( b ) \ |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1012 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 1013 | signed char c = 0, cc; \ |
| 1014 | uint32_t cur; \ |
| 1015 | size_t i = 0, bits = (b); \ |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1016 | /* N is the size of the product of two b-bit numbers, plus one */ \ |
| 1017 | /* limb for fix_negative */ \ |
| 1018 | MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, ( b ) * 2 / biL + 1 ) ); \ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1019 | LOAD32; |
| 1020 | |
| 1021 | #define NEXT \ |
| 1022 | STORE32; i++; LOAD32; \ |
| 1023 | cc = c; c = 0; \ |
| 1024 | if( cc < 0 ) \ |
| 1025 | sub32( &cur, -cc, &c ); \ |
| 1026 | else \ |
| 1027 | add32( &cur, cc, &c ); \ |
| 1028 | |
| 1029 | #define LAST \ |
| 1030 | STORE32; i++; \ |
| 1031 | cur = c > 0 ? c : 0; STORE32; \ |
| 1032 | cur = 0; while( ++i < MAX32 ) { STORE32; } \ |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1033 | if( c < 0 ) mbedtls_ecp_fix_negative( N, c, bits ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1034 | |
| 1035 | /* |
| 1036 | * If the result is negative, we get it in the form |
Gilles Peskine | 349b372 | 2021-04-03 21:40:11 +0200 | [diff] [blame] | 1037 | * c * 2^bits + N, with c negative and N positive shorter than 'bits' |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1038 | */ |
Gilles Peskine | 618be2e | 2021-04-03 21:47:53 +0200 | [diff] [blame] | 1039 | MBEDTLS_STATIC_TESTABLE |
| 1040 | void mbedtls_ecp_fix_negative( mbedtls_mpi *N, signed char c, size_t bits ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1041 | { |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1042 | size_t i; |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1043 | |
Gilles Peskine | ff6a32d | 2021-04-03 20:21:43 +0200 | [diff] [blame^] | 1044 | /* Set N := 2^bits - 1 - N. We know that 0 <= N < 2^bits, so |
| 1045 | * set the absolute value to 0xfff...fff - N. There is no carry |
| 1046 | * since we're subtracting from all-bits-one. */ |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1047 | for( i = 0; i <= bits / 8 / sizeof( mbedtls_mpi_uint ); i++ ) |
| 1048 | { |
| 1049 | N->p[i] = ~(mbedtls_mpi_uint)0 - N->p[i]; |
| 1050 | } |
Gilles Peskine | ff6a32d | 2021-04-03 20:21:43 +0200 | [diff] [blame^] | 1051 | /* Add 1, taking care of the carry. */ |
| 1052 | i = 0; |
| 1053 | do |
| 1054 | ++N->p[i]; |
| 1055 | while( N->p[i++] == 0 && i <= bits / 8 / sizeof( mbedtls_mpi_uint ) ); |
| 1056 | /* Invert the sign. |
| 1057 | * Now N = N0 - 2^bits where N0 is the initial value of N. */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1058 | N->s = -1; |
| 1059 | |
Gilles Peskine | 349b372 | 2021-04-03 21:40:11 +0200 | [diff] [blame] | 1060 | /* Add |c| * 2^bits to the absolute value. Since c and N are |
| 1061 | * negative, this adds c * 2^bits. */ |
Gilles Peskine | d10e8fa | 2020-07-22 19:58:28 +0200 | [diff] [blame] | 1062 | mbedtls_mpi_uint msw = (mbedtls_mpi_uint) -c; |
| 1063 | #if defined(MBEDTLS_HAVE_INT64) |
| 1064 | if( bits == 224 ) |
| 1065 | msw <<= 32; |
| 1066 | #endif |
| 1067 | N->p[bits / 8 / sizeof( mbedtls_mpi_uint)] += msw; |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1070 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1071 | /* |
| 1072 | * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2) |
| 1073 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | static int ecp_mod_p224( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1075 | { |
| 1076 | INIT( 224 ); |
| 1077 | |
| 1078 | SUB( 7 ); SUB( 11 ); NEXT; // A0 += -A7 - A11 |
| 1079 | SUB( 8 ); SUB( 12 ); NEXT; // A1 += -A8 - A12 |
| 1080 | SUB( 9 ); SUB( 13 ); NEXT; // A2 += -A9 - A13 |
| 1081 | SUB( 10 ); ADD( 7 ); ADD( 11 ); NEXT; // A3 += -A10 + A7 + A11 |
| 1082 | SUB( 11 ); ADD( 8 ); ADD( 12 ); NEXT; // A4 += -A11 + A8 + A12 |
| 1083 | SUB( 12 ); ADD( 9 ); ADD( 13 ); NEXT; // A5 += -A12 + A9 + A13 |
| 1084 | SUB( 13 ); ADD( 10 ); LAST; // A6 += -A13 + A10 |
| 1085 | |
| 1086 | cleanup: |
| 1087 | return( ret ); |
| 1088 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1092 | /* |
| 1093 | * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3) |
| 1094 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1095 | static int ecp_mod_p256( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1096 | { |
| 1097 | INIT( 256 ); |
| 1098 | |
| 1099 | ADD( 8 ); ADD( 9 ); |
| 1100 | SUB( 11 ); SUB( 12 ); SUB( 13 ); SUB( 14 ); NEXT; // A0 |
| 1101 | |
| 1102 | ADD( 9 ); ADD( 10 ); |
| 1103 | SUB( 12 ); SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A1 |
| 1104 | |
| 1105 | ADD( 10 ); ADD( 11 ); |
| 1106 | SUB( 13 ); SUB( 14 ); SUB( 15 ); NEXT; // A2 |
| 1107 | |
| 1108 | ADD( 11 ); ADD( 11 ); ADD( 12 ); ADD( 12 ); ADD( 13 ); |
| 1109 | SUB( 15 ); SUB( 8 ); SUB( 9 ); NEXT; // A3 |
| 1110 | |
| 1111 | ADD( 12 ); ADD( 12 ); ADD( 13 ); ADD( 13 ); ADD( 14 ); |
| 1112 | SUB( 9 ); SUB( 10 ); NEXT; // A4 |
| 1113 | |
| 1114 | ADD( 13 ); ADD( 13 ); ADD( 14 ); ADD( 14 ); ADD( 15 ); |
| 1115 | SUB( 10 ); SUB( 11 ); NEXT; // A5 |
| 1116 | |
| 1117 | ADD( 14 ); ADD( 14 ); ADD( 15 ); ADD( 15 ); ADD( 14 ); ADD( 13 ); |
| 1118 | SUB( 8 ); SUB( 9 ); NEXT; // A6 |
| 1119 | |
| 1120 | ADD( 15 ); ADD( 15 ); ADD( 15 ); ADD( 8 ); |
| 1121 | SUB( 10 ); SUB( 11 ); SUB( 12 ); SUB( 13 ); LAST; // A7 |
| 1122 | |
| 1123 | cleanup: |
| 1124 | return( ret ); |
| 1125 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1126 | #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1128 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1129 | /* |
| 1130 | * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4) |
| 1131 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | static int ecp_mod_p384( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1133 | { |
| 1134 | INIT( 384 ); |
| 1135 | |
| 1136 | ADD( 12 ); ADD( 21 ); ADD( 20 ); |
| 1137 | SUB( 23 ); NEXT; // A0 |
| 1138 | |
| 1139 | ADD( 13 ); ADD( 22 ); ADD( 23 ); |
| 1140 | SUB( 12 ); SUB( 20 ); NEXT; // A2 |
| 1141 | |
| 1142 | ADD( 14 ); ADD( 23 ); |
| 1143 | SUB( 13 ); SUB( 21 ); NEXT; // A2 |
| 1144 | |
| 1145 | ADD( 15 ); ADD( 12 ); ADD( 20 ); ADD( 21 ); |
| 1146 | SUB( 14 ); SUB( 22 ); SUB( 23 ); NEXT; // A3 |
| 1147 | |
| 1148 | ADD( 21 ); ADD( 21 ); ADD( 16 ); ADD( 13 ); ADD( 12 ); ADD( 20 ); ADD( 22 ); |
| 1149 | SUB( 15 ); SUB( 23 ); SUB( 23 ); NEXT; // A4 |
| 1150 | |
| 1151 | ADD( 22 ); ADD( 22 ); ADD( 17 ); ADD( 14 ); ADD( 13 ); ADD( 21 ); ADD( 23 ); |
| 1152 | SUB( 16 ); NEXT; // A5 |
| 1153 | |
| 1154 | ADD( 23 ); ADD( 23 ); ADD( 18 ); ADD( 15 ); ADD( 14 ); ADD( 22 ); |
| 1155 | SUB( 17 ); NEXT; // A6 |
| 1156 | |
| 1157 | ADD( 19 ); ADD( 16 ); ADD( 15 ); ADD( 23 ); |
| 1158 | SUB( 18 ); NEXT; // A7 |
| 1159 | |
| 1160 | ADD( 20 ); ADD( 17 ); ADD( 16 ); |
| 1161 | SUB( 19 ); NEXT; // A8 |
| 1162 | |
| 1163 | ADD( 21 ); ADD( 18 ); ADD( 17 ); |
| 1164 | SUB( 20 ); NEXT; // A9 |
| 1165 | |
| 1166 | ADD( 22 ); ADD( 19 ); ADD( 18 ); |
| 1167 | SUB( 21 ); NEXT; // A10 |
| 1168 | |
| 1169 | ADD( 23 ); ADD( 20 ); ADD( 19 ); |
| 1170 | SUB( 22 ); LAST; // A11 |
| 1171 | |
| 1172 | cleanup: |
| 1173 | return( ret ); |
| 1174 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1176 | |
| 1177 | #undef A |
| 1178 | #undef LOAD32 |
| 1179 | #undef STORE32 |
| 1180 | #undef MAX32 |
| 1181 | #undef INIT |
| 1182 | #undef NEXT |
| 1183 | #undef LAST |
| 1184 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1185 | #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED || |
| 1186 | MBEDTLS_ECP_DP_SECP256R1_ENABLED || |
| 1187 | MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1189 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1190 | /* |
| 1191 | * Here we have an actual Mersenne prime, so things are more straightforward. |
| 1192 | * However, chunks are aligned on a 'weird' boundary (521 bits). |
| 1193 | */ |
| 1194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1195 | /* Size of p521 in terms of mbedtls_mpi_uint */ |
| 1196 | #define P521_WIDTH ( 521 / 8 / sizeof( mbedtls_mpi_uint ) + 1 ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1198 | /* Bits to keep in the most significant mbedtls_mpi_uint */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1199 | #define P521_MASK 0x01FF |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* |
| 1202 | * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5) |
| 1203 | * Write N as A1 + 2^521 A0, return A0 + A1 |
| 1204 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1205 | static int ecp_mod_p521( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1206 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1207 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1208 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | mbedtls_mpi M; |
| 1210 | mbedtls_mpi_uint Mp[P521_WIDTH + 1]; |
| 1211 | /* Worst case for the size of M is when mbedtls_mpi_uint is 16 bits: |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1212 | * we need to hold bits 513 to 1056, which is 34 limbs, that is |
| 1213 | * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */ |
| 1214 | |
| 1215 | if( N->n < P521_WIDTH ) |
| 1216 | return( 0 ); |
| 1217 | |
| 1218 | /* M = A1 */ |
| 1219 | M.s = 1; |
| 1220 | M.n = N->n - ( P521_WIDTH - 1 ); |
| 1221 | if( M.n > P521_WIDTH + 1 ) |
| 1222 | M.n = P521_WIDTH + 1; |
| 1223 | M.p = Mp; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1224 | memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) ); |
| 1225 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 521 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1226 | |
| 1227 | /* N = A0 */ |
| 1228 | N->p[P521_WIDTH - 1] &= P521_MASK; |
| 1229 | for( i = P521_WIDTH; i < N->n; i++ ) |
| 1230 | N->p[i] = 0; |
| 1231 | |
| 1232 | /* N = A0 + A1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1233 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1234 | |
| 1235 | cleanup: |
| 1236 | return( ret ); |
| 1237 | } |
| 1238 | |
| 1239 | #undef P521_WIDTH |
| 1240 | #undef P521_MASK |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | #endif /* MBEDTLS_ECP_NIST_OPTIM */ |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 1244 | |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 1245 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1247 | /* Size of p255 in terms of mbedtls_mpi_uint */ |
| 1248 | #define P255_WIDTH ( 255 / 8 / sizeof( mbedtls_mpi_uint ) + 1 ) |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1249 | |
| 1250 | /* |
| 1251 | * Fast quasi-reduction modulo p255 = 2^255 - 19 |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1252 | * Write N as A0 + 2^255 A1, return A0 + 19 * A1 |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1253 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | static int ecp_mod_p255( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1255 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1256 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1257 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | mbedtls_mpi M; |
| 1259 | mbedtls_mpi_uint Mp[P255_WIDTH + 2]; |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1260 | |
| 1261 | if( N->n < P255_WIDTH ) |
| 1262 | return( 0 ); |
| 1263 | |
| 1264 | /* M = A1 */ |
| 1265 | M.s = 1; |
| 1266 | M.n = N->n - ( P255_WIDTH - 1 ); |
| 1267 | if( M.n > P255_WIDTH + 1 ) |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 1268 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1269 | M.p = Mp; |
| 1270 | memset( Mp, 0, sizeof Mp ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | memcpy( Mp, N->p + P255_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) ); |
| 1272 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 255 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1273 | M.n++; /* Make room for multiplication by 19 */ |
| 1274 | |
| 1275 | /* N = A0 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( N, 255, 0 ) ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1277 | for( i = P255_WIDTH; i < N->n; i++ ) |
| 1278 | N->p[i] = 0; |
| 1279 | |
| 1280 | /* N = A0 + 19 * A1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1281 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &M, 19 ) ); |
| 1282 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1283 | |
| 1284 | cleanup: |
| 1285 | return( ret ); |
| 1286 | } |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 1287 | #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ |
Manuel Pégourié-Gonnard | 3d7053a | 2013-12-04 20:51:13 +0100 | [diff] [blame] | 1288 | |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 1289 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 1290 | |
| 1291 | /* Size of p448 in terms of mbedtls_mpi_uint */ |
| 1292 | #define P448_WIDTH ( 448 / 8 / sizeof( mbedtls_mpi_uint ) ) |
| 1293 | |
| 1294 | /* Number of limbs fully occupied by 2^224 (max), and limbs used by it (min) */ |
| 1295 | #define DIV_ROUND_UP( X, Y ) ( ( ( X ) + ( Y ) - 1 ) / ( Y ) ) |
| 1296 | #define P224_WIDTH_MIN ( 28 / sizeof( mbedtls_mpi_uint ) ) |
| 1297 | #define P224_WIDTH_MAX DIV_ROUND_UP( 28, sizeof( mbedtls_mpi_uint ) ) |
| 1298 | #define P224_UNUSED_BITS ( ( P224_WIDTH_MAX * sizeof( mbedtls_mpi_uint ) * 8 ) - 224 ) |
| 1299 | |
| 1300 | /* |
| 1301 | * Fast quasi-reduction modulo p448 = 2^448 - 2^224 - 1 |
| 1302 | * Write N as A0 + 2^448 A1 and A1 as B0 + 2^224 B1, and return |
| 1303 | * A0 + A1 + B1 + (B0 + B1) * 2^224. This is different to the reference |
| 1304 | * implementation of Curve448, which uses its own special 56-bit limbs rather |
| 1305 | * than a generic bignum library. We could squeeze some extra speed out on |
| 1306 | * 32-bit machines by splitting N up into 32-bit limbs and doing the |
| 1307 | * arithmetic using the limbs directly as we do for the NIST primes above, |
| 1308 | * but for 64-bit targets it should use half the number of operations if we do |
| 1309 | * the reduction with 224-bit limbs, since mpi_add_mpi will then use 64-bit adds. |
| 1310 | */ |
| 1311 | static int ecp_mod_p448( mbedtls_mpi *N ) |
| 1312 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1313 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 1314 | size_t i; |
| 1315 | mbedtls_mpi M, Q; |
| 1316 | mbedtls_mpi_uint Mp[P448_WIDTH + 1], Qp[P448_WIDTH]; |
| 1317 | |
| 1318 | if( N->n <= P448_WIDTH ) |
| 1319 | return( 0 ); |
| 1320 | |
| 1321 | /* M = A1 */ |
| 1322 | M.s = 1; |
| 1323 | M.n = N->n - ( P448_WIDTH ); |
| 1324 | if( M.n > P448_WIDTH ) |
| 1325 | /* Shouldn't be called with N larger than 2^896! */ |
| 1326 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 1327 | M.p = Mp; |
| 1328 | memset( Mp, 0, sizeof( Mp ) ); |
| 1329 | memcpy( Mp, N->p + P448_WIDTH, M.n * sizeof( mbedtls_mpi_uint ) ); |
| 1330 | |
| 1331 | /* N = A0 */ |
| 1332 | for( i = P448_WIDTH; i < N->n; i++ ) |
| 1333 | N->p[i] = 0; |
| 1334 | |
| 1335 | /* N += A1 */ |
| 1336 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &M ) ); |
| 1337 | |
| 1338 | /* Q = B1, N += B1 */ |
| 1339 | Q = M; |
| 1340 | Q.p = Qp; |
| 1341 | memcpy( Qp, Mp, sizeof( Qp ) ); |
| 1342 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Q, 224 ) ); |
| 1343 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &Q ) ); |
| 1344 | |
| 1345 | /* M = (B0 + B1) * 2^224, N += M */ |
| 1346 | if( sizeof( mbedtls_mpi_uint ) > 4 ) |
| 1347 | Mp[P224_WIDTH_MIN] &= ( (mbedtls_mpi_uint)-1 ) >> ( P224_UNUSED_BITS ); |
| 1348 | for( i = P224_WIDTH_MAX; i < M.n; ++i ) |
| 1349 | Mp[i] = 0; |
| 1350 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &Q ) ); |
| 1351 | M.n = P448_WIDTH + 1; /* Make room for shifted carry bit from the addition */ |
| 1352 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &M, 224 ) ); |
| 1353 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &M ) ); |
| 1354 | |
| 1355 | cleanup: |
| 1356 | return( ret ); |
| 1357 | } |
| 1358 | #endif /* MBEDTLS_ECP_DP_CURVE448_ENABLED */ |
| 1359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \ |
| 1361 | defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \ |
| 1362 | defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1363 | /* |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1364 | * Fast quasi-reduction modulo P = 2^s - R, |
| 1365 | * with R about 33 bits, used by the Koblitz curves. |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1366 | * |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1367 | * Write N as A0 + 2^224 A1, return A0 + R * A1. |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1368 | * Actually do two passes, since R is big. |
| 1369 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1370 | #define P_KOBLITZ_MAX ( 256 / 8 / sizeof( mbedtls_mpi_uint ) ) // Max limbs in P |
| 1371 | #define P_KOBLITZ_R ( 8 / sizeof( mbedtls_mpi_uint ) ) // Limbs in R |
| 1372 | static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p_limbs, |
| 1373 | size_t adjust, size_t shift, mbedtls_mpi_uint mask ) |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1374 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1375 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1376 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | mbedtls_mpi M, R; |
Janos Follath | 7dadc2f | 2017-01-27 16:05:20 +0000 | [diff] [blame] | 1378 | mbedtls_mpi_uint Mp[P_KOBLITZ_MAX + P_KOBLITZ_R + 1]; |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1379 | |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1380 | if( N->n < p_limbs ) |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1381 | return( 0 ); |
| 1382 | |
| 1383 | /* Init R */ |
| 1384 | R.s = 1; |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1385 | R.p = Rp; |
| 1386 | R.n = P_KOBLITZ_R; |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* Common setup for M */ |
| 1389 | M.s = 1; |
| 1390 | M.p = Mp; |
| 1391 | |
| 1392 | /* M = A1 */ |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1393 | M.n = N->n - ( p_limbs - adjust ); |
| 1394 | if( M.n > p_limbs + adjust ) |
| 1395 | M.n = p_limbs + adjust; |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1396 | memset( Mp, 0, sizeof Mp ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1397 | memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1398 | if( shift != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1399 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) ); |
Janos Follath | 7dadc2f | 2017-01-27 16:05:20 +0000 | [diff] [blame] | 1400 | M.n += R.n; /* Make room for multiplication by R */ |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1401 | |
| 1402 | /* N = A0 */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1403 | if( mask != 0 ) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1404 | N->p[p_limbs - 1] &= mask; |
| 1405 | for( i = p_limbs; i < N->n; i++ ) |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1406 | N->p[i] = 0; |
| 1407 | |
| 1408 | /* N = A0 + R * A1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &M, &M, &R ) ); |
| 1410 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1411 | |
| 1412 | /* Second pass */ |
| 1413 | |
| 1414 | /* M = A1 */ |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1415 | M.n = N->n - ( p_limbs - adjust ); |
| 1416 | if( M.n > p_limbs + adjust ) |
| 1417 | M.n = p_limbs + adjust; |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1418 | memset( Mp, 0, sizeof Mp ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | memcpy( Mp, N->p + p_limbs - adjust, M.n * sizeof( mbedtls_mpi_uint ) ); |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1420 | if( shift != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1421 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, shift ) ); |
Janos Follath | 7dadc2f | 2017-01-27 16:05:20 +0000 | [diff] [blame] | 1422 | M.n += R.n; /* Make room for multiplication by R */ |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1423 | |
| 1424 | /* N = A0 */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1425 | if( mask != 0 ) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1426 | N->p[p_limbs - 1] &= mask; |
| 1427 | for( i = p_limbs; i < N->n; i++ ) |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1428 | N->p[i] = 0; |
| 1429 | |
| 1430 | /* N = A0 + R * A1 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &M, &M, &R ) ); |
| 1432 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) ); |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1433 | |
| 1434 | cleanup: |
| 1435 | return( ret ); |
| 1436 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1437 | #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED) || |
| 1438 | MBEDTLS_ECP_DP_SECP224K1_ENABLED) || |
| 1439 | MBEDTLS_ECP_DP_SECP256K1_ENABLED) */ |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1442 | /* |
| 1443 | * Fast quasi-reduction modulo p192k1 = 2^192 - R, |
| 1444 | * with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119 |
| 1445 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1446 | static int ecp_mod_p192k1( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1447 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1448 | static mbedtls_mpi_uint Rp[] = { |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1449 | BYTES_TO_T_UINT_8( 0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) }; |
| 1450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1451 | return( ecp_mod_koblitz( N, Rp, 192 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) ); |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1452 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */ |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1455 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1456 | /* |
| 1457 | * Fast quasi-reduction modulo p224k1 = 2^224 - R, |
| 1458 | * with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93 |
| 1459 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1460 | static int ecp_mod_p224k1( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | static mbedtls_mpi_uint Rp[] = { |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1463 | BYTES_TO_T_UINT_8( 0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) }; |
| 1464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1465 | #if defined(MBEDTLS_HAVE_INT64) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1466 | return( ecp_mod_koblitz( N, Rp, 4, 1, 32, 0xFFFFFFFF ) ); |
| 1467 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1468 | return( ecp_mod_koblitz( N, Rp, 224 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) ); |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1469 | #endif |
| 1470 | } |
| 1471 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1472 | #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */ |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1474 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1475 | /* |
| 1476 | * Fast quasi-reduction modulo p256k1 = 2^256 - R, |
| 1477 | * with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1 |
| 1478 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | static int ecp_mod_p256k1( mbedtls_mpi *N ) |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | static mbedtls_mpi_uint Rp[] = { |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1482 | BYTES_TO_T_UINT_8( 0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ) }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1483 | return( ecp_mod_koblitz( N, Rp, 256 / 8 / sizeof( mbedtls_mpi_uint ), 0, 0, 0 ) ); |
Manuel Pégourié-Gonnard | 9af7d3a | 2014-01-18 17:28:59 +0100 | [diff] [blame] | 1484 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */ |
Manuel Pégourié-Gonnard | 8887d8d | 2014-01-17 23:17:10 +0100 | [diff] [blame] | 1486 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1487 | #endif /* !MBEDTLS_ECP_ALT */ |
| 1488 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1489 | #endif /* MBEDTLS_ECP_C */ |