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