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