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