Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curve J-PAKE |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Manuel Pégourié-Gonnard | 6b798b9 | 2015-08-14 11:18:30 +0200 | [diff] [blame] | 21 | * References in the code are to the Thread v1.0 Specification, |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 22 | * available to members of the Thread Group http://threadgroup.org/ |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 25 | #include "common.h" |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 26 | |
| 27 | #if defined(MBEDTLS_ECJPAKE_C) |
| 28 | |
| 29 | #include "mbedtls/ecjpake.h" |
Hanno Becker | 71c8e1b | 2018-12-14 17:09:39 +0000 | [diff] [blame] | 30 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 31 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 32 | |
Neil Armstrong | ecaba1c | 2022-08-11 10:47:08 +0200 | [diff] [blame^] | 33 | /* We use MD first if it's available (for compatibility reasons) |
| 34 | * and "fall back" to PSA otherwise (which needs psa_crypto_init()). */ |
| 35 | #if !defined(MBEDTLS_MD_C) |
| 36 | #include "psa/crypto.h" |
| 37 | #include "mbedtls/psa_util.h" |
| 38 | #endif /* !MBEDTLS_MD_C */ |
| 39 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 40 | #include "hash_info.h" |
| 41 | |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 42 | #include <string.h> |
| 43 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame] | 44 | #if !defined(MBEDTLS_ECJPAKE_ALT) |
| 45 | |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 46 | /* |
Manuel Pégourié-Gonnard | e0ad57b | 2015-08-14 11:10:39 +0200 | [diff] [blame] | 47 | * Convert a mbedtls_ecjpake_role to identifier string |
| 48 | */ |
| 49 | static const char * const ecjpake_id[] = { |
| 50 | "client", |
| 51 | "server" |
| 52 | }; |
| 53 | |
| 54 | #define ID_MINE ( ecjpake_id[ ctx->role ] ) |
| 55 | #define ID_PEER ( ecjpake_id[ 1 - ctx->role ] ) |
| 56 | |
Neil Armstrong | ecaba1c | 2022-08-11 10:47:08 +0200 | [diff] [blame^] | 57 | /** |
| 58 | * Helper to Compute a hash from md_type |
| 59 | */ |
| 60 | static int mbedtls_ecjpake_compute_hash( mbedtls_md_type_t md_type, |
| 61 | const unsigned char *input, size_t ilen, |
| 62 | unsigned char *output ) |
| 63 | { |
| 64 | #if defined(MBEDTLS_MD_C) |
| 65 | return( mbedtls_md( mbedtls_md_info_from_type( md_type ), |
| 66 | input, ilen, output ) ); |
| 67 | #else |
| 68 | psa_algorithm_t alg = mbedtls_psa_translate_md( md_type ); |
| 69 | psa_status_t status; |
| 70 | size_t out_size = PSA_HASH_LENGTH( alg ); |
| 71 | size_t out_len; |
| 72 | |
| 73 | status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len ); |
| 74 | |
| 75 | return( mbedtls_md_error_from_psa( status ) ); |
| 76 | #endif /* !MBEDTLS_MD_C */ |
| 77 | } |
| 78 | |
Manuel Pégourié-Gonnard | e0ad57b | 2015-08-14 11:10:39 +0200 | [diff] [blame] | 79 | /* |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 80 | * Initialize context |
| 81 | */ |
| 82 | void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ) |
| 83 | { |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 84 | ctx->md_type = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 85 | mbedtls_ecp_group_init( &ctx->grp ); |
Manuel Pégourié-Gonnard | b7da194 | 2015-10-19 13:35:22 +0200 | [diff] [blame] | 86 | ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 88 | mbedtls_ecp_point_init( &ctx->Xm1 ); |
| 89 | mbedtls_ecp_point_init( &ctx->Xm2 ); |
| 90 | mbedtls_ecp_point_init( &ctx->Xp1 ); |
| 91 | mbedtls_ecp_point_init( &ctx->Xp2 ); |
| 92 | mbedtls_ecp_point_init( &ctx->Xp ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 93 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 94 | mbedtls_mpi_init( &ctx->xm1 ); |
| 95 | mbedtls_mpi_init( &ctx->xm2 ); |
| 96 | mbedtls_mpi_init( &ctx->s ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Free context |
| 101 | */ |
| 102 | void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ) |
| 103 | { |
| 104 | if( ctx == NULL ) |
| 105 | return; |
| 106 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 107 | ctx->md_type = MBEDTLS_MD_NONE; |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 108 | mbedtls_ecp_group_free( &ctx->grp ); |
| 109 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 110 | mbedtls_ecp_point_free( &ctx->Xm1 ); |
| 111 | mbedtls_ecp_point_free( &ctx->Xm2 ); |
| 112 | mbedtls_ecp_point_free( &ctx->Xp1 ); |
| 113 | mbedtls_ecp_point_free( &ctx->Xp2 ); |
| 114 | mbedtls_ecp_point_free( &ctx->Xp ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 116 | mbedtls_mpi_free( &ctx->xm1 ); |
| 117 | mbedtls_mpi_free( &ctx->xm2 ); |
| 118 | mbedtls_mpi_free( &ctx->s ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Setup context |
| 123 | */ |
| 124 | int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 125 | mbedtls_ecjpake_role role, |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 126 | mbedtls_md_type_t hash, |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 127 | mbedtls_ecp_group_id curve, |
| 128 | const unsigned char *secret, |
| 129 | size_t len ) |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 130 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 131 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 185e516 | 2018-12-19 09:48:50 +0000 | [diff] [blame] | 132 | |
Tuvshinzaya Erdenekhuu | a891f83 | 2022-07-27 15:13:04 +0100 | [diff] [blame] | 133 | if( role != MBEDTLS_ECJPAKE_CLIENT && role != MBEDTLS_ECJPAKE_SERVER ) |
| 134 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 135 | |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 136 | ctx->role = role; |
| 137 | |
Neil Armstrong | ecaba1c | 2022-08-11 10:47:08 +0200 | [diff] [blame^] | 138 | #if defined(MBEDTLS_MD_C) |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 139 | if( ( mbedtls_md_info_from_type( hash ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 140 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
Neil Armstrong | ecaba1c | 2022-08-11 10:47:08 +0200 | [diff] [blame^] | 141 | #else |
| 142 | if( mbedtls_psa_translate_md( hash ) == MBEDTLS_MD_NONE ) |
| 143 | return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); |
| 144 | #endif |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 145 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 146 | ctx->md_type = hash; |
| 147 | |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 148 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ctx->grp, curve ) ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 149 | |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 150 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->s, secret, len ) ); |
Manuel Pégourié-Gonnard | 23dcbe3 | 2015-08-13 09:37:00 +0200 | [diff] [blame] | 151 | |
| 152 | cleanup: |
| 153 | if( ret != 0 ) |
| 154 | mbedtls_ecjpake_free( ctx ); |
| 155 | |
| 156 | return( ret ); |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Gilles Peskine | cd07e22 | 2021-05-27 23:17:34 +0200 | [diff] [blame] | 159 | int mbedtls_ecjpake_set_point_format( mbedtls_ecjpake_context *ctx, |
| 160 | int point_format ) |
| 161 | { |
| 162 | switch( point_format ) |
| 163 | { |
| 164 | case MBEDTLS_ECP_PF_UNCOMPRESSED: |
| 165 | case MBEDTLS_ECP_PF_COMPRESSED: |
| 166 | ctx->point_format = point_format; |
| 167 | return( 0 ); |
| 168 | default: |
| 169 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 170 | } |
| 171 | } |
| 172 | |
Manuel Pégourié-Gonnard | 7af8bc1 | 2015-08-12 16:58:50 +0200 | [diff] [blame] | 173 | /* |
Manuel Pégourié-Gonnard | b813acc | 2015-09-15 15:34:09 +0200 | [diff] [blame] | 174 | * Check if context is ready for use |
| 175 | */ |
| 176 | int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ) |
| 177 | { |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 178 | if( ctx->md_type == MBEDTLS_MD_NONE || |
Manuel Pégourié-Gonnard | b813acc | 2015-09-15 15:34:09 +0200 | [diff] [blame] | 179 | ctx->grp.id == MBEDTLS_ECP_DP_NONE || |
| 180 | ctx->s.p == NULL ) |
| 181 | { |
| 182 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 183 | } |
| 184 | |
| 185 | return( 0 ); |
| 186 | } |
| 187 | |
| 188 | /* |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 189 | * Write a point plus its length to a buffer |
| 190 | */ |
| 191 | static int ecjpake_write_len_point( unsigned char **p, |
| 192 | const unsigned char *end, |
| 193 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 194 | const int pf, |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 195 | const mbedtls_ecp_point *P ) |
| 196 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 197 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 198 | size_t len; |
| 199 | |
| 200 | /* Need at least 4 for length plus 1 for point */ |
| 201 | if( end < *p || end - *p < 5 ) |
| 202 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 203 | |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 204 | ret = mbedtls_ecp_point_write_binary( grp, P, pf, |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 205 | &len, *p + 4, end - ( *p + 4 ) ); |
| 206 | if( ret != 0 ) |
| 207 | return( ret ); |
| 208 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 209 | MBEDTLS_PUT_UINT32_BE( len, *p, 0 ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 210 | |
| 211 | *p += 4 + len; |
| 212 | |
| 213 | return( 0 ); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Size of the temporary buffer for ecjpake_hash: |
Manuel Pégourié-Gonnard | 4b20c0e | 2015-10-20 16:16:38 +0200 | [diff] [blame] | 218 | * 3 EC points plus their length, plus ID and its length (4 + 6 bytes) |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 219 | */ |
Manuel Pégourié-Gonnard | 4b20c0e | 2015-10-20 16:16:38 +0200 | [diff] [blame] | 220 | #define ECJPAKE_HASH_BUF_LEN ( 3 * ( 4 + MBEDTLS_ECP_MAX_PT_LEN ) + 4 + 6 ) |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * Compute hash for ZKP (7.4.2.2.2.1) |
| 224 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 225 | static int ecjpake_hash( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 226 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 227 | const int pf, |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 228 | const mbedtls_ecp_point *G, |
| 229 | const mbedtls_ecp_point *V, |
| 230 | const mbedtls_ecp_point *X, |
| 231 | const char *id, |
| 232 | mbedtls_mpi *h ) |
| 233 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 234 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 235 | unsigned char buf[ECJPAKE_HASH_BUF_LEN]; |
| 236 | unsigned char *p = buf; |
| 237 | const unsigned char *end = buf + sizeof( buf ); |
Manuel Pégourié-Gonnard | 8489f17 | 2015-08-07 17:47:39 +0200 | [diff] [blame] | 238 | const size_t id_len = strlen( id ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 239 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
| 240 | |
| 241 | /* Write things to temporary buffer */ |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 242 | MBEDTLS_MPI_CHK( ecjpake_write_len_point( &p, end, grp, pf, G ) ); |
| 243 | MBEDTLS_MPI_CHK( ecjpake_write_len_point( &p, end, grp, pf, V ) ); |
| 244 | MBEDTLS_MPI_CHK( ecjpake_write_len_point( &p, end, grp, pf, X ) ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 245 | |
Manuel Pégourié-Gonnard | 4b20c0e | 2015-10-20 16:16:38 +0200 | [diff] [blame] | 246 | if( end - p < 4 ) |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 247 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 248 | |
Joe Subbiani | a5cb0d2 | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 249 | MBEDTLS_PUT_UINT32_BE( id_len, p, 0 ); |
| 250 | p += 4; |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 251 | |
Manuel Pégourié-Gonnard | 4b20c0e | 2015-10-20 16:16:38 +0200 | [diff] [blame] | 252 | if( end < p || (size_t)( end - p ) < id_len ) |
| 253 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 254 | |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 255 | memcpy( p, id, id_len ); |
| 256 | p += id_len; |
| 257 | |
| 258 | /* Compute hash */ |
Neil Armstrong | ecaba1c | 2022-08-11 10:47:08 +0200 | [diff] [blame^] | 259 | MBEDTLS_MPI_CHK( mbedtls_ecjpake_compute_hash( md_type, |
| 260 | buf, p - buf, hash ) ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 261 | |
| 262 | /* Turn it into an integer mod n */ |
| 263 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( h, hash, |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 264 | mbedtls_hash_info_get_size( md_type ) ) ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 265 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( h, h, &grp->N ) ); |
| 266 | |
| 267 | cleanup: |
| 268 | return( ret ); |
| 269 | } |
| 270 | |
Manuel Pégourié-Gonnard | 8489f17 | 2015-08-07 17:47:39 +0200 | [diff] [blame] | 271 | /* |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 272 | * Parse a ECShnorrZKP (7.4.2.2.2) and verify it (7.4.2.3.3) |
| 273 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 274 | static int ecjpake_zkp_read( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 275 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 276 | const int pf, |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 277 | const mbedtls_ecp_point *G, |
| 278 | const mbedtls_ecp_point *X, |
| 279 | const char *id, |
Manuel Pégourié-Gonnard | 9028c5a | 2015-08-12 14:51:36 +0200 | [diff] [blame] | 280 | const unsigned char **p, |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 281 | const unsigned char *end ) |
| 282 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 283 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 284 | mbedtls_ecp_point V, VV; |
| 285 | mbedtls_mpi r, h; |
| 286 | size_t r_len; |
| 287 | |
| 288 | mbedtls_ecp_point_init( &V ); |
| 289 | mbedtls_ecp_point_init( &VV ); |
| 290 | mbedtls_mpi_init( &r ); |
| 291 | mbedtls_mpi_init( &h ); |
| 292 | |
| 293 | /* |
| 294 | * struct { |
| 295 | * ECPoint V; |
| 296 | * opaque r<1..2^8-1>; |
| 297 | * } ECSchnorrZKP; |
| 298 | */ |
| 299 | if( end < *p ) |
| 300 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 301 | |
Manuel Pégourié-Gonnard | 9028c5a | 2015-08-12 14:51:36 +0200 | [diff] [blame] | 302 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_read_point( grp, &V, p, end - *p ) ); |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 303 | |
| 304 | if( end < *p || (size_t)( end - *p ) < 1 ) |
Manuel Pégourié-Gonnard | 4f2cd95 | 2015-08-12 11:17:55 +0200 | [diff] [blame] | 305 | { |
| 306 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 307 | goto cleanup; |
| 308 | } |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 309 | |
| 310 | r_len = *(*p)++; |
Manuel Pégourié-Gonnard | 9028c5a | 2015-08-12 14:51:36 +0200 | [diff] [blame] | 311 | |
TRodziewicz | 782a7ea | 2021-03-17 11:35:16 +0100 | [diff] [blame] | 312 | if( end < *p || (size_t)( end - *p ) < r_len || r_len == 0 ) |
Manuel Pégourié-Gonnard | 4f2cd95 | 2015-08-12 11:17:55 +0200 | [diff] [blame] | 313 | { |
| 314 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 315 | goto cleanup; |
| 316 | } |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 317 | |
| 318 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r, *p, r_len ) ); |
| 319 | *p += r_len; |
| 320 | |
| 321 | /* |
| 322 | * Verification |
| 323 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 324 | MBEDTLS_MPI_CHK( ecjpake_hash( md_type, grp, pf, G, &V, X, id, &h ) ); |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 325 | MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( (mbedtls_ecp_group *) grp, |
| 326 | &VV, &h, X, &r, G ) ); |
| 327 | |
| 328 | if( mbedtls_ecp_point_cmp( &VV, &V ) != 0 ) |
Manuel Pégourié-Gonnard | 4f2cd95 | 2015-08-12 11:17:55 +0200 | [diff] [blame] | 329 | { |
| 330 | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
| 331 | goto cleanup; |
| 332 | } |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 333 | |
| 334 | cleanup: |
| 335 | mbedtls_ecp_point_free( &V ); |
| 336 | mbedtls_ecp_point_free( &VV ); |
| 337 | mbedtls_mpi_free( &r ); |
| 338 | mbedtls_mpi_free( &h ); |
| 339 | |
| 340 | return( ret ); |
| 341 | } |
| 342 | |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 343 | /* |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 344 | * Generate ZKP (7.4.2.3.2) and write it as ECSchnorrZKP (7.4.2.2.2) |
| 345 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 346 | static int ecjpake_zkp_write( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 347 | const mbedtls_ecp_group *grp, |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 348 | const int pf, |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 349 | const mbedtls_ecp_point *G, |
| 350 | const mbedtls_mpi *x, |
| 351 | const mbedtls_ecp_point *X, |
| 352 | const char *id, |
| 353 | unsigned char **p, |
| 354 | const unsigned char *end, |
| 355 | int (*f_rng)(void *, unsigned char *, size_t), |
| 356 | void *p_rng ) |
| 357 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 358 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 359 | mbedtls_ecp_point V; |
| 360 | mbedtls_mpi v; |
| 361 | mbedtls_mpi h; /* later recycled to hold r */ |
| 362 | size_t len; |
| 363 | |
| 364 | if( end < *p ) |
| 365 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 366 | |
| 367 | mbedtls_ecp_point_init( &V ); |
| 368 | mbedtls_mpi_init( &v ); |
| 369 | mbedtls_mpi_init( &h ); |
| 370 | |
| 371 | /* Compute signature */ |
| 372 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair_base( (mbedtls_ecp_group *) grp, |
| 373 | G, &v, &V, f_rng, p_rng ) ); |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 374 | MBEDTLS_MPI_CHK( ecjpake_hash( md_type, grp, pf, G, &V, X, id, &h ) ); |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 375 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &h, &h, x ) ); /* x*h */ |
| 376 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &h, &v, &h ) ); /* v - x*h */ |
| 377 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &h, &h, &grp->N ) ); /* r */ |
| 378 | |
| 379 | /* Write it out */ |
| 380 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_point( grp, &V, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 381 | pf, &len, *p, end - *p ) ); |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 382 | *p += len; |
| 383 | |
| 384 | len = mbedtls_mpi_size( &h ); /* actually r */ |
| 385 | if( end < *p || (size_t)( end - *p ) < 1 + len || len > 255 ) |
| 386 | { |
| 387 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 388 | goto cleanup; |
| 389 | } |
| 390 | |
Joe Subbiani | 2194dc4 | 2021-07-14 12:31:31 +0100 | [diff] [blame] | 391 | *(*p)++ = MBEDTLS_BYTE_0( len ); |
Manuel Pégourié-Gonnard | 3aed185 | 2015-08-12 14:53:56 +0200 | [diff] [blame] | 392 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, *p, len ) ); /* r */ |
| 393 | *p += len; |
| 394 | |
| 395 | cleanup: |
| 396 | mbedtls_ecp_point_free( &V ); |
| 397 | mbedtls_mpi_free( &v ); |
| 398 | mbedtls_mpi_free( &h ); |
| 399 | |
| 400 | return( ret ); |
| 401 | } |
| 402 | |
| 403 | /* |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 404 | * Parse a ECJPAKEKeyKP (7.4.2.2.1) and check proof |
| 405 | * Output: verified public key X |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 406 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 407 | static int ecjpake_kkp_read( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 408 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 409 | const int pf, |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 410 | const mbedtls_ecp_point *G, |
| 411 | mbedtls_ecp_point *X, |
| 412 | const char *id, |
Manuel Pégourié-Gonnard | 9028c5a | 2015-08-12 14:51:36 +0200 | [diff] [blame] | 413 | const unsigned char **p, |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 414 | const unsigned char *end ) |
| 415 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 416 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 417 | |
| 418 | if( end < *p ) |
| 419 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); |
| 420 | |
| 421 | /* |
| 422 | * struct { |
| 423 | * ECPoint X; |
| 424 | * ECSchnorrZKP zkp; |
| 425 | * } ECJPAKEKeyKP; |
| 426 | */ |
Manuel Pégourié-Gonnard | 9028c5a | 2015-08-12 14:51:36 +0200 | [diff] [blame] | 427 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_read_point( grp, X, p, end - *p ) ); |
Manuel Pégourié-Gonnard | 3059095 | 2015-08-17 10:37:40 +0200 | [diff] [blame] | 428 | if( mbedtls_ecp_is_zero( X ) ) |
| 429 | { |
| 430 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 431 | goto cleanup; |
| 432 | } |
| 433 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 434 | MBEDTLS_MPI_CHK( ecjpake_zkp_read( md_type, grp, pf, G, X, id, p, end ) ); |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 435 | |
| 436 | cleanup: |
| 437 | return( ret ); |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * Generate an ECJPAKEKeyKP |
| 442 | * Output: the serialized structure, plus private/public key pair |
| 443 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 444 | static int ecjpake_kkp_write( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 445 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 446 | const int pf, |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 447 | const mbedtls_ecp_point *G, |
| 448 | mbedtls_mpi *x, |
| 449 | mbedtls_ecp_point *X, |
| 450 | const char *id, |
| 451 | unsigned char **p, |
| 452 | const unsigned char *end, |
| 453 | int (*f_rng)(void *, unsigned char *, size_t), |
| 454 | void *p_rng ) |
| 455 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 456 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 457 | size_t len; |
| 458 | |
| 459 | if( end < *p ) |
| 460 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 461 | |
| 462 | /* Generate key (7.4.2.3.1) and write it out */ |
| 463 | MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair_base( (mbedtls_ecp_group *) grp, G, x, X, |
| 464 | f_rng, p_rng ) ); |
| 465 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_point( grp, X, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 466 | pf, &len, *p, end - *p ) ); |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 467 | *p += len; |
| 468 | |
| 469 | /* Generate and write proof */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 470 | MBEDTLS_MPI_CHK( ecjpake_zkp_write( md_type, grp, pf, G, x, X, id, |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 471 | p, end, f_rng, p_rng ) ); |
| 472 | |
| 473 | cleanup: |
| 474 | return( ret ); |
| 475 | } |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 476 | |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 477 | /* |
| 478 | * Read a ECJPAKEKeyKPPairList (7.4.2.3) and check proofs |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 479 | * Outputs: verified peer public keys Xa, Xb |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 480 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 481 | static int ecjpake_kkpp_read( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 482 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 483 | const int pf, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 484 | const mbedtls_ecp_point *G, |
| 485 | mbedtls_ecp_point *Xa, |
| 486 | mbedtls_ecp_point *Xb, |
| 487 | const char *id, |
| 488 | const unsigned char *buf, |
| 489 | size_t len ) |
| 490 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 491 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9028c5a | 2015-08-12 14:51:36 +0200 | [diff] [blame] | 492 | const unsigned char *p = buf; |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 493 | const unsigned char *end = buf + len; |
| 494 | |
| 495 | /* |
| 496 | * struct { |
| 497 | * ECJPAKEKeyKP ecjpake_key_kp_pair_list[2]; |
| 498 | * } ECJPAKEKeyKPPairList; |
| 499 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 500 | MBEDTLS_MPI_CHK( ecjpake_kkp_read( md_type, grp, pf, G, Xa, id, &p, end ) ); |
| 501 | MBEDTLS_MPI_CHK( ecjpake_kkp_read( md_type, grp, pf, G, Xb, id, &p, end ) ); |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 502 | |
| 503 | if( p != end ) |
| 504 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 505 | |
| 506 | cleanup: |
| 507 | return( ret ); |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Generate a ECJPAKEKeyKPPairList |
| 512 | * Outputs: the serialized structure, plus two private/public key pairs |
| 513 | */ |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 514 | static int ecjpake_kkpp_write( const mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 515 | const mbedtls_ecp_group *grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 516 | const int pf, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 517 | const mbedtls_ecp_point *G, |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 518 | mbedtls_mpi *xm1, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 519 | mbedtls_ecp_point *Xa, |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 520 | mbedtls_mpi *xm2, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 521 | mbedtls_ecp_point *Xb, |
| 522 | const char *id, |
| 523 | unsigned char *buf, |
| 524 | size_t len, |
| 525 | size_t *olen, |
| 526 | int (*f_rng)(void *, unsigned char *, size_t), |
| 527 | void *p_rng ) |
| 528 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 529 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 530 | unsigned char *p = buf; |
| 531 | const unsigned char *end = buf + len; |
| 532 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 533 | MBEDTLS_MPI_CHK( ecjpake_kkp_write( md_type, grp, pf, G, xm1, Xa, id, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 534 | &p, end, f_rng, p_rng ) ); |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 535 | MBEDTLS_MPI_CHK( ecjpake_kkp_write( md_type, grp, pf, G, xm2, Xb, id, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 536 | &p, end, f_rng, p_rng ) ); |
| 537 | |
| 538 | *olen = p - buf; |
| 539 | |
| 540 | cleanup: |
| 541 | return( ret ); |
| 542 | } |
| 543 | |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 544 | /* |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 545 | * Read and process the first round message |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 546 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 547 | int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, |
| 548 | const unsigned char *buf, |
| 549 | size_t len ) |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 550 | { |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 551 | return( ecjpake_kkpp_read( ctx->md_type, &ctx->grp, ctx->point_format, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 552 | &ctx->grp.G, |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 553 | &ctx->Xp1, &ctx->Xp2, ID_PEER, |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 554 | buf, len ) ); |
| 555 | } |
| 556 | |
| 557 | /* |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 558 | * Generate and write the first round message |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 559 | */ |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 560 | int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 561 | unsigned char *buf, size_t len, size_t *olen, |
| 562 | int (*f_rng)(void *, unsigned char *, size_t), |
| 563 | void *p_rng ) |
| 564 | { |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 565 | return( ecjpake_kkpp_write( ctx->md_type, &ctx->grp, ctx->point_format, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 566 | &ctx->grp.G, |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 567 | &ctx->xm1, &ctx->Xm1, &ctx->xm2, &ctx->Xm2, |
Manuel Pégourié-Gonnard | e0ad57b | 2015-08-14 11:10:39 +0200 | [diff] [blame] | 568 | ID_MINE, buf, len, olen, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 4e8bc78 | 2015-08-12 20:50:31 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 571 | /* |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 572 | * Compute the sum of three points R = A + B + C |
| 573 | */ |
| 574 | static int ecjpake_ecp_add3( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 575 | const mbedtls_ecp_point *A, |
| 576 | const mbedtls_ecp_point *B, |
| 577 | const mbedtls_ecp_point *C ) |
| 578 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 579 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 580 | mbedtls_mpi one; |
| 581 | |
| 582 | mbedtls_mpi_init( &one ); |
| 583 | |
| 584 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &one, 1 ) ); |
| 585 | MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, R, &one, A, &one, B ) ); |
| 586 | MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, R, &one, R, &one, C ) ); |
| 587 | |
| 588 | cleanup: |
| 589 | mbedtls_mpi_free( &one ); |
| 590 | |
| 591 | return( ret ); |
| 592 | } |
| 593 | |
| 594 | /* |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 595 | * Read and process second round message (C: 7.4.2.5, S: 7.4.2.6) |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 596 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 597 | int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 598 | const unsigned char *buf, |
| 599 | size_t len ) |
| 600 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 601 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 602 | const unsigned char *p = buf; |
| 603 | const unsigned char *end = buf + len; |
| 604 | mbedtls_ecp_group grp; |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 605 | mbedtls_ecp_point G; /* C: GB, S: GA */ |
Hanno Becker | 185e516 | 2018-12-19 09:48:50 +0000 | [diff] [blame] | 606 | |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 607 | mbedtls_ecp_group_init( &grp ); |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 608 | mbedtls_ecp_point_init( &G ); |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 609 | |
| 610 | /* |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 611 | * Server: GA = X3 + X4 + X1 (7.4.2.6.1) |
| 612 | * Client: GB = X1 + X2 + X3 (7.4.2.5.1) |
| 613 | * Unified: G = Xm1 + Xm2 + Xp1 |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 614 | * We need that before parsing in order to check Xp as we read it |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 615 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 616 | MBEDTLS_MPI_CHK( ecjpake_ecp_add3( &ctx->grp, &G, |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 617 | &ctx->Xm1, &ctx->Xm2, &ctx->Xp1 ) ); |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 618 | |
| 619 | /* |
| 620 | * struct { |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 621 | * ECParameters curve_params; // only client reading server msg |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 622 | * ECJPAKEKeyKP ecjpake_key_kp; |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 623 | * } Client/ServerECJPAKEParams; |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 624 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 625 | if( ctx->role == MBEDTLS_ECJPAKE_CLIENT ) |
Manuel Pégourié-Gonnard | d9802af | 2015-08-17 12:47:38 +0200 | [diff] [blame] | 626 | { |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 627 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_read_group( &grp, &p, len ) ); |
Manuel Pégourié-Gonnard | d9802af | 2015-08-17 12:47:38 +0200 | [diff] [blame] | 628 | if( grp.id != ctx->grp.id ) |
| 629 | { |
| 630 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 631 | goto cleanup; |
| 632 | } |
| 633 | } |
| 634 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 635 | MBEDTLS_MPI_CHK( ecjpake_kkp_read( ctx->md_type, &ctx->grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 636 | ctx->point_format, |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 637 | &G, &ctx->Xp, ID_PEER, &p, end ) ); |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 638 | |
| 639 | if( p != end ) |
| 640 | { |
| 641 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 642 | goto cleanup; |
| 643 | } |
| 644 | |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 645 | cleanup: |
| 646 | mbedtls_ecp_group_free( &grp ); |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 647 | mbedtls_ecp_point_free( &G ); |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 648 | |
| 649 | return( ret ); |
| 650 | } |
| 651 | |
| 652 | /* |
Manuel Pégourié-Gonnard | d0d8a93 | 2015-08-14 15:14:50 +0200 | [diff] [blame] | 653 | * Compute R = +/- X * S mod N, taking care not to leak S |
| 654 | */ |
| 655 | static int ecjpake_mul_secret( mbedtls_mpi *R, int sign, |
| 656 | const mbedtls_mpi *X, |
| 657 | const mbedtls_mpi *S, |
| 658 | const mbedtls_mpi *N, |
| 659 | int (*f_rng)(void *, unsigned char *, size_t), |
| 660 | void *p_rng ) |
| 661 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 662 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d0d8a93 | 2015-08-14 15:14:50 +0200 | [diff] [blame] | 663 | mbedtls_mpi b; /* Blinding value, then s + N * blinding */ |
| 664 | |
| 665 | mbedtls_mpi_init( &b ); |
| 666 | |
| 667 | /* b = s + rnd-128-bit * N */ |
| 668 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &b, 16, f_rng, p_rng ) ); |
| 669 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &b, &b, N ) ); |
| 670 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &b, &b, S ) ); |
| 671 | |
| 672 | /* R = sign * X * b mod N */ |
| 673 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( R, X, &b ) ); |
| 674 | R->s *= sign; |
| 675 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( R, R, N ) ); |
| 676 | |
| 677 | cleanup: |
| 678 | mbedtls_mpi_free( &b ); |
| 679 | |
| 680 | return( ret ); |
| 681 | } |
| 682 | |
| 683 | /* |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 684 | * Generate and write the second round message (S: 7.4.2.5, C: 7.4.2.6) |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 685 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 686 | int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 687 | unsigned char *buf, size_t len, size_t *olen, |
| 688 | int (*f_rng)(void *, unsigned char *, size_t), |
| 689 | void *p_rng ) |
| 690 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 691 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 692 | mbedtls_ecp_point G; /* C: GA, S: GB */ |
| 693 | mbedtls_ecp_point Xm; /* C: Xc, S: Xs */ |
| 694 | mbedtls_mpi xm; /* C: xc, S: xs */ |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 695 | unsigned char *p = buf; |
| 696 | const unsigned char *end = buf + len; |
| 697 | size_t ec_len; |
Hanno Becker | 185e516 | 2018-12-19 09:48:50 +0000 | [diff] [blame] | 698 | |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 699 | mbedtls_ecp_point_init( &G ); |
| 700 | mbedtls_ecp_point_init( &Xm ); |
| 701 | mbedtls_mpi_init( &xm ); |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 702 | |
| 703 | /* |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 704 | * First generate private/public key pair (S: 7.4.2.5.1, C: 7.4.2.6.1) |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 705 | * |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 706 | * Client: GA = X1 + X3 + X4 | xs = x2 * s | Xc = xc * GA |
| 707 | * Server: GB = X3 + X1 + X2 | xs = x4 * s | Xs = xs * GB |
| 708 | * Unified: G = Xm1 + Xp1 + Xp2 | xm = xm2 * s | Xm = xm * G |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 709 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 710 | MBEDTLS_MPI_CHK( ecjpake_ecp_add3( &ctx->grp, &G, |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 711 | &ctx->Xp1, &ctx->Xp2, &ctx->Xm1 ) ); |
Manuel Pégourié-Gonnard | d0d8a93 | 2015-08-14 15:14:50 +0200 | [diff] [blame] | 712 | MBEDTLS_MPI_CHK( ecjpake_mul_secret( &xm, 1, &ctx->xm2, &ctx->s, |
| 713 | &ctx->grp.N, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 714 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &Xm, &xm, &G, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 715 | |
| 716 | /* |
| 717 | * Now write things out |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 718 | * |
| 719 | * struct { |
| 720 | * ECParameters curve_params; // only server writing its message |
| 721 | * ECJPAKEKeyKP ecjpake_key_kp; |
| 722 | * } Client/ServerECJPAKEParams; |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 723 | */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 724 | if( ctx->role == MBEDTLS_ECJPAKE_SERVER ) |
| 725 | { |
| 726 | if( end < p ) |
| 727 | { |
| 728 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 729 | goto cleanup; |
| 730 | } |
| 731 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_group( &ctx->grp, &ec_len, |
| 732 | p, end - p ) ); |
| 733 | p += ec_len; |
| 734 | } |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 735 | |
| 736 | if( end < p ) |
| 737 | { |
| 738 | ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 739 | goto cleanup; |
| 740 | } |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 741 | MBEDTLS_MPI_CHK( mbedtls_ecp_tls_write_point( &ctx->grp, &Xm, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 742 | ctx->point_format, &ec_len, p, end - p ) ); |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 743 | p += ec_len; |
| 744 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 745 | MBEDTLS_MPI_CHK( ecjpake_zkp_write( ctx->md_type, &ctx->grp, |
Robert Cragie | 7cdad77 | 2015-10-02 13:31:41 +0100 | [diff] [blame] | 746 | ctx->point_format, |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 747 | &G, &xm, &Xm, ID_MINE, |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 748 | &p, end, f_rng, p_rng ) ); |
| 749 | |
| 750 | *olen = p - buf; |
| 751 | |
| 752 | cleanup: |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 753 | mbedtls_ecp_point_free( &G ); |
| 754 | mbedtls_ecp_point_free( &Xm ); |
| 755 | mbedtls_mpi_free( &xm ); |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 756 | |
| 757 | return( ret ); |
| 758 | } |
| 759 | |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 760 | /* |
| 761 | * Derive PMS (7.4.2.7 / 7.4.2.8) |
| 762 | */ |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 763 | int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 764 | unsigned char *buf, size_t len, size_t *olen, |
| 765 | int (*f_rng)(void *, unsigned char *, size_t), |
| 766 | void *p_rng ) |
| 767 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 768 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 769 | mbedtls_ecp_point K; |
| 770 | mbedtls_mpi m_xm2_s, one; |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 771 | unsigned char kx[MBEDTLS_ECP_MAX_BYTES]; |
| 772 | size_t x_bytes; |
Hanno Becker | 185e516 | 2018-12-19 09:48:50 +0000 | [diff] [blame] | 773 | |
Neil Armstrong | 0d76341 | 2022-08-11 10:32:22 +0200 | [diff] [blame] | 774 | *olen = mbedtls_hash_info_get_size( ctx->md_type ); |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 775 | if( len < *olen ) |
| 776 | return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); |
| 777 | |
| 778 | mbedtls_ecp_point_init( &K ); |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 779 | mbedtls_mpi_init( &m_xm2_s ); |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 780 | mbedtls_mpi_init( &one ); |
| 781 | |
| 782 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &one, 1 ) ); |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 783 | |
| 784 | /* |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 785 | * Client: K = ( Xs - X4 * x2 * s ) * x2 |
| 786 | * Server: K = ( Xc - X2 * x4 * s ) * x4 |
Manuel Pégourié-Gonnard | d0d8a93 | 2015-08-14 15:14:50 +0200 | [diff] [blame] | 787 | * Unified: K = ( Xp - Xp2 * xm2 * s ) * xm2 |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 788 | */ |
Manuel Pégourié-Gonnard | d0d8a93 | 2015-08-14 15:14:50 +0200 | [diff] [blame] | 789 | MBEDTLS_MPI_CHK( ecjpake_mul_secret( &m_xm2_s, -1, &ctx->xm2, &ctx->s, |
| 790 | &ctx->grp.N, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 791 | MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( &ctx->grp, &K, |
| 792 | &one, &ctx->Xp, |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 793 | &m_xm2_s, &ctx->Xp2 ) ); |
Manuel Pégourié-Gonnard | ce45676 | 2015-08-14 11:54:35 +0200 | [diff] [blame] | 794 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &K, &ctx->xm2, &K, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 795 | f_rng, p_rng ) ); |
| 796 | |
| 797 | /* PMS = SHA-256( K.X ) */ |
| 798 | x_bytes = ( ctx->grp.pbits + 7 ) / 8; |
| 799 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &K.X, kx, x_bytes ) ); |
Neil Armstrong | ecaba1c | 2022-08-11 10:47:08 +0200 | [diff] [blame^] | 800 | MBEDTLS_MPI_CHK( mbedtls_ecjpake_compute_hash( ctx->md_type, |
| 801 | kx, x_bytes, buf ) ); |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 802 | |
| 803 | cleanup: |
| 804 | mbedtls_ecp_point_free( &K ); |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 805 | mbedtls_mpi_free( &m_xm2_s ); |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 806 | mbedtls_mpi_free( &one ); |
| 807 | |
| 808 | return( ret ); |
| 809 | } |
| 810 | |
Manuel Pégourié-Gonnard | e0ad57b | 2015-08-14 11:10:39 +0200 | [diff] [blame] | 811 | #undef ID_MINE |
| 812 | #undef ID_PEER |
| 813 | |
Hanno Becker | 616d1ca | 2018-01-24 10:25:05 +0000 | [diff] [blame] | 814 | #endif /* ! MBEDTLS_ECJPAKE_ALT */ |
Manuel Pégourié-Gonnard | e0ad57b | 2015-08-14 11:10:39 +0200 | [diff] [blame] | 815 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 816 | #if defined(MBEDTLS_SELF_TEST) |
| 817 | |
| 818 | #if defined(MBEDTLS_PLATFORM_C) |
| 819 | #include "mbedtls/platform.h" |
| 820 | #else |
| 821 | #include <stdio.h> |
| 822 | #define mbedtls_printf printf |
| 823 | #endif |
| 824 | |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 825 | #if !defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ |
| 826 | !defined(MBEDTLS_SHA256_C) |
| 827 | int mbedtls_ecjpake_self_test( int verbose ) |
| 828 | { |
| 829 | (void) verbose; |
| 830 | return( 0 ); |
| 831 | } |
| 832 | #else |
| 833 | |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 834 | static const unsigned char ecjpake_test_password[] = { |
| 835 | 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x6a, 0x70, 0x61, 0x6b, 0x65, 0x74, |
| 836 | 0x65, 0x73, 0x74 |
| 837 | }; |
| 838 | |
Steven Cooreman | 64f2773 | 2021-01-11 17:20:10 +0100 | [diff] [blame] | 839 | #if !defined(MBEDTLS_ECJPAKE_ALT) |
| 840 | |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 841 | static const unsigned char ecjpake_test_x1[] = { |
| 842 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, |
| 843 | 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 844 | 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x21 |
| 845 | }; |
| 846 | |
| 847 | static const unsigned char ecjpake_test_x2[] = { |
| 848 | 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, |
| 849 | 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, |
| 850 | 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81 |
| 851 | }; |
| 852 | |
| 853 | static const unsigned char ecjpake_test_x3[] = { |
| 854 | 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, |
| 855 | 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, |
| 856 | 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81 |
| 857 | }; |
| 858 | |
| 859 | static const unsigned char ecjpake_test_x4[] = { |
| 860 | 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, |
| 861 | 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, |
| 862 | 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe1 |
| 863 | }; |
| 864 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 865 | static const unsigned char ecjpake_test_cli_one[] = { |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 866 | 0x41, 0x04, 0xac, 0xcf, 0x01, 0x06, 0xef, 0x85, 0x8f, 0xa2, 0xd9, 0x19, |
| 867 | 0x33, 0x13, 0x46, 0x80, 0x5a, 0x78, 0xb5, 0x8b, 0xba, 0xd0, 0xb8, 0x44, |
| 868 | 0xe5, 0xc7, 0x89, 0x28, 0x79, 0x14, 0x61, 0x87, 0xdd, 0x26, 0x66, 0xad, |
| 869 | 0xa7, 0x81, 0xbb, 0x7f, 0x11, 0x13, 0x72, 0x25, 0x1a, 0x89, 0x10, 0x62, |
| 870 | 0x1f, 0x63, 0x4d, 0xf1, 0x28, 0xac, 0x48, 0xe3, 0x81, 0xfd, 0x6e, 0xf9, |
| 871 | 0x06, 0x07, 0x31, 0xf6, 0x94, 0xa4, 0x41, 0x04, 0x1d, 0xd0, 0xbd, 0x5d, |
| 872 | 0x45, 0x66, 0xc9, 0xbe, 0xd9, 0xce, 0x7d, 0xe7, 0x01, 0xb5, 0xe8, 0x2e, |
| 873 | 0x08, 0xe8, 0x4b, 0x73, 0x04, 0x66, 0x01, 0x8a, 0xb9, 0x03, 0xc7, 0x9e, |
| 874 | 0xb9, 0x82, 0x17, 0x22, 0x36, 0xc0, 0xc1, 0x72, 0x8a, 0xe4, 0xbf, 0x73, |
| 875 | 0x61, 0x0d, 0x34, 0xde, 0x44, 0x24, 0x6e, 0xf3, 0xd9, 0xc0, 0x5a, 0x22, |
| 876 | 0x36, 0xfb, 0x66, 0xa6, 0x58, 0x3d, 0x74, 0x49, 0x30, 0x8b, 0xab, 0xce, |
| 877 | 0x20, 0x72, 0xfe, 0x16, 0x66, 0x29, 0x92, 0xe9, 0x23, 0x5c, 0x25, 0x00, |
| 878 | 0x2f, 0x11, 0xb1, 0x50, 0x87, 0xb8, 0x27, 0x38, 0xe0, 0x3c, 0x94, 0x5b, |
Manuel Pégourié-Gonnard | 082767f | 2015-08-12 14:43:57 +0200 | [diff] [blame] | 879 | 0xf7, 0xa2, 0x99, 0x5d, 0xda, 0x1e, 0x98, 0x34, 0x58, 0x41, 0x04, 0x7e, |
| 880 | 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7, 0x92, 0x62, |
| 881 | 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40, 0x9a, 0xc5, |
| 882 | 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79, 0x0a, 0xeb, |
| 883 | 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1, 0xc3, 0x35, |
| 884 | 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3, 0x2b, 0xb0, |
| 885 | 0x13, 0xbb, 0x2b, 0x41, 0x04, 0xa4, 0x95, 0x58, 0xd3, 0x2e, 0xd1, 0xeb, |
| 886 | 0xfc, 0x18, 0x16, 0xaf, 0x4f, 0xf0, 0x9b, 0x55, 0xfc, 0xb4, 0xca, 0x47, |
| 887 | 0xb2, 0xa0, 0x2d, 0x1e, 0x7c, 0xaf, 0x11, 0x79, 0xea, 0x3f, 0xe1, 0x39, |
| 888 | 0x5b, 0x22, 0xb8, 0x61, 0x96, 0x40, 0x16, 0xfa, 0xba, 0xf7, 0x2c, 0x97, |
| 889 | 0x56, 0x95, 0xd9, 0x3d, 0x4d, 0xf0, 0xe5, 0x19, 0x7f, 0xe9, 0xf0, 0x40, |
| 890 | 0x63, 0x4e, 0xd5, 0x97, 0x64, 0x93, 0x77, 0x87, 0xbe, 0x20, 0xbc, 0x4d, |
| 891 | 0xee, 0xbb, 0xf9, 0xb8, 0xd6, 0x0a, 0x33, 0x5f, 0x04, 0x6c, 0xa3, 0xaa, |
| 892 | 0x94, 0x1e, 0x45, 0x86, 0x4c, 0x7c, 0xad, 0xef, 0x9c, 0xf7, 0x5b, 0x3d, |
| 893 | 0x8b, 0x01, 0x0e, 0x44, 0x3e, 0xf0 |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 894 | }; |
| 895 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 896 | static const unsigned char ecjpake_test_srv_one[] = { |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 897 | 0x41, 0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, |
| 898 | 0xd7, 0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, |
| 899 | 0x40, 0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, |
| 900 | 0x79, 0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, |
| 901 | 0xd1, 0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, |
| 902 | 0xe3, 0x2b, 0xb0, 0x13, 0xbb, 0x2b, 0x41, 0x04, 0x09, 0xf8, 0x5b, 0x3d, |
| 903 | 0x20, 0xeb, 0xd7, 0x88, 0x5c, 0xe4, 0x64, 0xc0, 0x8d, 0x05, 0x6d, 0x64, |
| 904 | 0x28, 0xfe, 0x4d, 0xd9, 0x28, 0x7a, 0xa3, 0x65, 0xf1, 0x31, 0xf4, 0x36, |
| 905 | 0x0f, 0xf3, 0x86, 0xd8, 0x46, 0x89, 0x8b, 0xc4, 0xb4, 0x15, 0x83, 0xc2, |
| 906 | 0xa5, 0x19, 0x7f, 0x65, 0xd7, 0x87, 0x42, 0x74, 0x6c, 0x12, 0xa5, 0xec, |
| 907 | 0x0a, 0x4f, 0xfe, 0x2f, 0x27, 0x0a, 0x75, 0x0a, 0x1d, 0x8f, 0xb5, 0x16, |
| 908 | 0x20, 0x93, 0x4d, 0x74, 0xeb, 0x43, 0xe5, 0x4d, 0xf4, 0x24, 0xfd, 0x96, |
| 909 | 0x30, 0x6c, 0x01, 0x17, 0xbf, 0x13, 0x1a, 0xfa, 0xbf, 0x90, 0xa9, 0xd3, |
| 910 | 0x3d, 0x11, 0x98, 0xd9, 0x05, 0x19, 0x37, 0x35, 0x14, 0x41, 0x04, 0x19, |
| 911 | 0x0a, 0x07, 0x70, 0x0f, 0xfa, 0x4b, 0xe6, 0xae, 0x1d, 0x79, 0xee, 0x0f, |
| 912 | 0x06, 0xae, 0xb5, 0x44, 0xcd, 0x5a, 0xdd, 0xaa, 0xbe, 0xdf, 0x70, 0xf8, |
| 913 | 0x62, 0x33, 0x21, 0x33, 0x2c, 0x54, 0xf3, 0x55, 0xf0, 0xfb, 0xfe, 0xc7, |
| 914 | 0x83, 0xed, 0x35, 0x9e, 0x5d, 0x0b, 0xf7, 0x37, 0x7a, 0x0f, 0xc4, 0xea, |
| 915 | 0x7a, 0xce, 0x47, 0x3c, 0x9c, 0x11, 0x2b, 0x41, 0xcc, 0xd4, 0x1a, 0xc5, |
| 916 | 0x6a, 0x56, 0x12, 0x41, 0x04, 0x36, 0x0a, 0x1c, 0xea, 0x33, 0xfc, 0xe6, |
| 917 | 0x41, 0x15, 0x64, 0x58, 0xe0, 0xa4, 0xea, 0xc2, 0x19, 0xe9, 0x68, 0x31, |
| 918 | 0xe6, 0xae, 0xbc, 0x88, 0xb3, 0xf3, 0x75, 0x2f, 0x93, 0xa0, 0x28, 0x1d, |
| 919 | 0x1b, 0xf1, 0xfb, 0x10, 0x60, 0x51, 0xdb, 0x96, 0x94, 0xa8, 0xd6, 0xe8, |
| 920 | 0x62, 0xa5, 0xef, 0x13, 0x24, 0xa3, 0xd9, 0xe2, 0x78, 0x94, 0xf1, 0xee, |
| 921 | 0x4f, 0x7c, 0x59, 0x19, 0x99, 0x65, 0xa8, 0xdd, 0x4a, 0x20, 0x91, 0x84, |
| 922 | 0x7d, 0x2d, 0x22, 0xdf, 0x3e, 0xe5, 0x5f, 0xaa, 0x2a, 0x3f, 0xb3, 0x3f, |
| 923 | 0xd2, 0xd1, 0xe0, 0x55, 0xa0, 0x7a, 0x7c, 0x61, 0xec, 0xfb, 0x8d, 0x80, |
| 924 | 0xec, 0x00, 0xc2, 0xc9, 0xeb, 0x12 |
| 925 | }; |
| 926 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 927 | static const unsigned char ecjpake_test_srv_two[] = { |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 928 | 0x03, 0x00, 0x17, 0x41, 0x04, 0x0f, 0xb2, 0x2b, 0x1d, 0x5d, 0x11, 0x23, |
| 929 | 0xe0, 0xef, 0x9f, 0xeb, 0x9d, 0x8a, 0x2e, 0x59, 0x0a, 0x1f, 0x4d, 0x7c, |
| 930 | 0xed, 0x2c, 0x2b, 0x06, 0x58, 0x6e, 0x8f, 0x2a, 0x16, 0xd4, 0xeb, 0x2f, |
| 931 | 0xda, 0x43, 0x28, 0xa2, 0x0b, 0x07, 0xd8, 0xfd, 0x66, 0x76, 0x54, 0xca, |
| 932 | 0x18, 0xc5, 0x4e, 0x32, 0xa3, 0x33, 0xa0, 0x84, 0x54, 0x51, 0xe9, 0x26, |
| 933 | 0xee, 0x88, 0x04, 0xfd, 0x7a, 0xf0, 0xaa, 0xa7, 0xa6, 0x41, 0x04, 0x55, |
| 934 | 0x16, 0xea, 0x3e, 0x54, 0xa0, 0xd5, 0xd8, 0xb2, 0xce, 0x78, 0x6b, 0x38, |
| 935 | 0xd3, 0x83, 0x37, 0x00, 0x29, 0xa5, 0xdb, 0xe4, 0x45, 0x9c, 0x9d, 0xd6, |
| 936 | 0x01, 0xb4, 0x08, 0xa2, 0x4a, 0xe6, 0x46, 0x5c, 0x8a, 0xc9, 0x05, 0xb9, |
| 937 | 0xeb, 0x03, 0xb5, 0xd3, 0x69, 0x1c, 0x13, 0x9e, 0xf8, 0x3f, 0x1c, 0xd4, |
| 938 | 0x20, 0x0f, 0x6c, 0x9c, 0xd4, 0xec, 0x39, 0x22, 0x18, 0xa5, 0x9e, 0xd2, |
| 939 | 0x43, 0xd3, 0xc8, 0x20, 0xff, 0x72, 0x4a, 0x9a, 0x70, 0xb8, 0x8c, 0xb8, |
| 940 | 0x6f, 0x20, 0xb4, 0x34, 0xc6, 0x86, 0x5a, 0xa1, 0xcd, 0x79, 0x06, 0xdd, |
| 941 | 0x7c, 0x9b, 0xce, 0x35, 0x25, 0xf5, 0x08, 0x27, 0x6f, 0x26, 0x83, 0x6c |
| 942 | }; |
| 943 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 944 | static const unsigned char ecjpake_test_cli_two[] = { |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 945 | 0x41, 0x04, 0x69, 0xd5, 0x4e, 0xe8, 0x5e, 0x90, 0xce, 0x3f, 0x12, 0x46, |
| 946 | 0x74, 0x2d, 0xe5, 0x07, 0xe9, 0x39, 0xe8, 0x1d, 0x1d, 0xc1, 0xc5, 0xcb, |
| 947 | 0x98, 0x8b, 0x58, 0xc3, 0x10, 0xc9, 0xfd, 0xd9, 0x52, 0x4d, 0x93, 0x72, |
| 948 | 0x0b, 0x45, 0x54, 0x1c, 0x83, 0xee, 0x88, 0x41, 0x19, 0x1d, 0xa7, 0xce, |
| 949 | 0xd8, 0x6e, 0x33, 0x12, 0xd4, 0x36, 0x23, 0xc1, 0xd6, 0x3e, 0x74, 0x98, |
| 950 | 0x9a, 0xba, 0x4a, 0xff, 0xd1, 0xee, 0x41, 0x04, 0x07, 0x7e, 0x8c, 0x31, |
| 951 | 0xe2, 0x0e, 0x6b, 0xed, 0xb7, 0x60, 0xc1, 0x35, 0x93, 0xe6, 0x9f, 0x15, |
| 952 | 0xbe, 0x85, 0xc2, 0x7d, 0x68, 0xcd, 0x09, 0xcc, 0xb8, 0xc4, 0x18, 0x36, |
| 953 | 0x08, 0x91, 0x7c, 0x5c, 0x3d, 0x40, 0x9f, 0xac, 0x39, 0xfe, 0xfe, 0xe8, |
| 954 | 0x2f, 0x72, 0x92, 0xd3, 0x6f, 0x0d, 0x23, 0xe0, 0x55, 0x91, 0x3f, 0x45, |
| 955 | 0xa5, 0x2b, 0x85, 0xdd, 0x8a, 0x20, 0x52, 0xe9, 0xe1, 0x29, 0xbb, 0x4d, |
| 956 | 0x20, 0x0f, 0x01, 0x1f, 0x19, 0x48, 0x35, 0x35, 0xa6, 0xe8, 0x9a, 0x58, |
| 957 | 0x0c, 0x9b, 0x00, 0x03, 0xba, 0xf2, 0x14, 0x62, 0xec, 0xe9, 0x1a, 0x82, |
| 958 | 0xcc, 0x38, 0xdb, 0xdc, 0xae, 0x60, 0xd9, 0xc5, 0x4c |
| 959 | }; |
| 960 | |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 961 | static const unsigned char ecjpake_test_pms[] = { |
| 962 | 0xf3, 0xd4, 0x7f, 0x59, 0x98, 0x44, 0xdb, 0x92, 0xa5, 0x69, 0xbb, 0xe7, |
| 963 | 0x98, 0x1e, 0x39, 0xd9, 0x31, 0xfd, 0x74, 0x3b, 0xf2, 0x2e, 0x98, 0xf9, |
| 964 | 0xb4, 0x38, 0xf7, 0x19, 0xd3, 0xc4, 0xf3, 0x51 |
| 965 | }; |
| 966 | |
Manuel Pégourié-Gonnard | aa3ed6f | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 967 | /* |
| 968 | * PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!! |
| 969 | * |
| 970 | * This is the linear congruential generator from numerical recipes, |
| 971 | * except we only use the low byte as the output. See |
| 972 | * https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use |
| 973 | */ |
| 974 | static int self_test_rng( void *ctx, unsigned char *out, size_t len ) |
| 975 | { |
| 976 | static uint32_t state = 42; |
| 977 | |
| 978 | (void) ctx; |
| 979 | |
| 980 | for( size_t i = 0; i < len; i++ ) |
| 981 | { |
| 982 | state = state * 1664525u + 1013904223u; |
| 983 | out[i] = (unsigned char) state; |
| 984 | } |
| 985 | |
| 986 | return( 0 ); |
| 987 | } |
| 988 | |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 989 | /* Load my private keys and generate the corresponding public keys */ |
Manuel Pégourié-Gonnard | e2d3a4e | 2015-08-14 12:03:04 +0200 | [diff] [blame] | 990 | static int ecjpake_test_load( mbedtls_ecjpake_context *ctx, |
| 991 | const unsigned char *xm1, size_t len1, |
| 992 | const unsigned char *xm2, size_t len2 ) |
| 993 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 994 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | e2d3a4e | 2015-08-14 12:03:04 +0200 | [diff] [blame] | 995 | |
| 996 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->xm1, xm1, len1 ) ); |
| 997 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->xm2, xm2, len2 ) ); |
| 998 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &ctx->Xm1, &ctx->xm1, |
Manuel Pégourié-Gonnard | aa3ed6f | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 999 | &ctx->grp.G, self_test_rng, NULL ) ); |
Manuel Pégourié-Gonnard | e2d3a4e | 2015-08-14 12:03:04 +0200 | [diff] [blame] | 1000 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &ctx->grp, &ctx->Xm2, &ctx->xm2, |
Manuel Pégourié-Gonnard | aa3ed6f | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 1001 | &ctx->grp.G, self_test_rng, NULL ) ); |
Manuel Pégourié-Gonnard | e2d3a4e | 2015-08-14 12:03:04 +0200 | [diff] [blame] | 1002 | |
| 1003 | cleanup: |
| 1004 | return( ret ); |
| 1005 | } |
| 1006 | |
Steven Cooreman | 64f2773 | 2021-01-11 17:20:10 +0100 | [diff] [blame] | 1007 | #endif /* ! MBEDTLS_ECJPAKE_ALT */ |
| 1008 | |
Manuel Pégourié-Gonnard | 8489f17 | 2015-08-07 17:47:39 +0200 | [diff] [blame] | 1009 | /* For tests we don't need a secure RNG; |
| 1010 | * use the LGC from Numerical Recipes for simplicity */ |
| 1011 | static int ecjpake_lgc( void *p, unsigned char *out, size_t len ) |
| 1012 | { |
| 1013 | static uint32_t x = 42; |
| 1014 | (void) p; |
| 1015 | |
| 1016 | while( len > 0 ) |
| 1017 | { |
| 1018 | size_t use_len = len > 4 ? 4 : len; |
| 1019 | x = 1664525 * x + 1013904223; |
| 1020 | memcpy( out, &x, use_len ); |
| 1021 | out += use_len; |
| 1022 | len -= use_len; |
| 1023 | } |
| 1024 | |
| 1025 | return( 0 ); |
| 1026 | } |
| 1027 | |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 1028 | #define TEST_ASSERT( x ) \ |
| 1029 | do { \ |
| 1030 | if( x ) \ |
| 1031 | ret = 0; \ |
| 1032 | else \ |
| 1033 | { \ |
| 1034 | ret = 1; \ |
| 1035 | goto cleanup; \ |
| 1036 | } \ |
| 1037 | } while( 0 ) |
| 1038 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 1039 | /* |
| 1040 | * Checkup routine |
| 1041 | */ |
| 1042 | int mbedtls_ecjpake_self_test( int verbose ) |
| 1043 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1044 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1045 | mbedtls_ecjpake_context cli; |
| 1046 | mbedtls_ecjpake_context srv; |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 1047 | unsigned char buf[512], pms[32]; |
| 1048 | size_t len, pmslen; |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 1049 | |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1050 | mbedtls_ecjpake_init( &cli ); |
| 1051 | mbedtls_ecjpake_init( &srv ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 1052 | |
| 1053 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1054 | mbedtls_printf( " ECJPAKE test #0 (setup): " ); |
Manuel Pégourié-Gonnard | cb7cd03 | 2015-08-13 10:09:10 +0200 | [diff] [blame] | 1055 | |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 1056 | TEST_ASSERT( mbedtls_ecjpake_setup( &cli, MBEDTLS_ECJPAKE_CLIENT, |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1057 | MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, |
| 1058 | ecjpake_test_password, |
| 1059 | sizeof( ecjpake_test_password ) ) == 0 ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 1060 | |
Manuel Pégourié-Gonnard | 6449391 | 2015-08-13 20:19:51 +0200 | [diff] [blame] | 1061 | TEST_ASSERT( mbedtls_ecjpake_setup( &srv, MBEDTLS_ECJPAKE_SERVER, |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1062 | MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, |
| 1063 | ecjpake_test_password, |
| 1064 | sizeof( ecjpake_test_password ) ) == 0 ); |
| 1065 | |
| 1066 | if( verbose != 0 ) |
| 1067 | mbedtls_printf( "passed\n" ); |
| 1068 | |
| 1069 | if( verbose != 0 ) |
| 1070 | mbedtls_printf( " ECJPAKE test #1 (random handshake): " ); |
| 1071 | |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 1072 | TEST_ASSERT( mbedtls_ecjpake_write_round_one( &cli, |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1073 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1074 | |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 1075 | TEST_ASSERT( mbedtls_ecjpake_read_round_one( &srv, buf, len ) == 0 ); |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1076 | |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 1077 | TEST_ASSERT( mbedtls_ecjpake_write_round_one( &srv, |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1078 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1079 | |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 1080 | TEST_ASSERT( mbedtls_ecjpake_read_round_one( &cli, buf, len ) == 0 ); |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1081 | |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 1082 | TEST_ASSERT( mbedtls_ecjpake_write_round_two( &srv, |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 1083 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1084 | |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 1085 | TEST_ASSERT( mbedtls_ecjpake_read_round_two( &cli, buf, len ) == 0 ); |
Manuel Pégourié-Gonnard | bed9e41 | 2015-08-13 18:53:59 +0200 | [diff] [blame] | 1086 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 1087 | TEST_ASSERT( mbedtls_ecjpake_derive_secret( &cli, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 1088 | pms, sizeof( pms ), &pmslen, ecjpake_lgc, NULL ) == 0 ); |
| 1089 | |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 1090 | TEST_ASSERT( mbedtls_ecjpake_write_round_two( &cli, |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 1091 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1092 | |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 1093 | TEST_ASSERT( mbedtls_ecjpake_read_round_two( &srv, buf, len ) == 0 ); |
Manuel Pégourié-Gonnard | 614bd5e | 2015-08-13 20:19:16 +0200 | [diff] [blame] | 1094 | |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 1095 | TEST_ASSERT( mbedtls_ecjpake_derive_secret( &srv, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 1096 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1097 | |
| 1098 | TEST_ASSERT( len == pmslen ); |
| 1099 | TEST_ASSERT( memcmp( buf, pms, len ) == 0 ); |
| 1100 | |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1101 | if( verbose != 0 ) |
| 1102 | mbedtls_printf( "passed\n" ); |
| 1103 | |
Steven Cooreman | 64f2773 | 2021-01-11 17:20:10 +0100 | [diff] [blame] | 1104 | #if !defined(MBEDTLS_ECJPAKE_ALT) |
| 1105 | /* 'reference handshake' tests can only be run against implementations |
| 1106 | * for which we have 100% control over how the random ephemeral keys |
| 1107 | * are generated. This is only the case for the internal mbed TLS |
| 1108 | * implementation, so these tests are skipped in case the internal |
| 1109 | * implementation is swapped out for an alternative one. */ |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1110 | if( verbose != 0 ) |
| 1111 | mbedtls_printf( " ECJPAKE test #2 (reference handshake): " ); |
| 1112 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 1113 | /* Simulate generation of round one */ |
Manuel Pégourié-Gonnard | e2d3a4e | 2015-08-14 12:03:04 +0200 | [diff] [blame] | 1114 | MBEDTLS_MPI_CHK( ecjpake_test_load( &cli, |
| 1115 | ecjpake_test_x1, sizeof( ecjpake_test_x1 ), |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1116 | ecjpake_test_x2, sizeof( ecjpake_test_x2 ) ) ); |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1117 | |
Manuel Pégourié-Gonnard | e2d3a4e | 2015-08-14 12:03:04 +0200 | [diff] [blame] | 1118 | MBEDTLS_MPI_CHK( ecjpake_test_load( &srv, |
| 1119 | ecjpake_test_x3, sizeof( ecjpake_test_x3 ), |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1120 | ecjpake_test_x4, sizeof( ecjpake_test_x4 ) ) ); |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1121 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 1122 | /* Read round one */ |
| 1123 | TEST_ASSERT( mbedtls_ecjpake_read_round_one( &srv, |
| 1124 | ecjpake_test_cli_one, |
| 1125 | sizeof( ecjpake_test_cli_one ) ) == 0 ); |
| 1126 | |
Manuel Pégourié-Gonnard | d8204a7 | 2015-08-14 13:36:55 +0200 | [diff] [blame] | 1127 | TEST_ASSERT( mbedtls_ecjpake_read_round_one( &cli, |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 1128 | ecjpake_test_srv_one, |
| 1129 | sizeof( ecjpake_test_srv_one ) ) == 0 ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 1130 | |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 1131 | /* Skip generation of round two, read round two */ |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 1132 | TEST_ASSERT( mbedtls_ecjpake_read_round_two( &cli, |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 1133 | ecjpake_test_srv_two, |
| 1134 | sizeof( ecjpake_test_srv_two ) ) == 0 ); |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 1135 | |
Manuel Pégourié-Gonnard | e192710 | 2015-08-14 14:20:48 +0200 | [diff] [blame] | 1136 | TEST_ASSERT( mbedtls_ecjpake_read_round_two( &srv, |
Manuel Pégourié-Gonnard | c907081 | 2015-08-14 14:48:50 +0200 | [diff] [blame] | 1137 | ecjpake_test_cli_two, |
| 1138 | sizeof( ecjpake_test_cli_two ) ) == 0 ); |
Manuel Pégourié-Gonnard | ec0eece | 2015-08-13 19:13:20 +0200 | [diff] [blame] | 1139 | |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 1140 | /* Server derives PMS */ |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 1141 | TEST_ASSERT( mbedtls_ecjpake_derive_secret( &srv, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 1142 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1143 | |
| 1144 | TEST_ASSERT( len == sizeof( ecjpake_test_pms ) ); |
| 1145 | TEST_ASSERT( memcmp( buf, ecjpake_test_pms, len ) == 0 ); |
| 1146 | |
| 1147 | memset( buf, 0, len ); /* Avoid interferences with next step */ |
| 1148 | |
| 1149 | /* Client derives PMS */ |
Manuel Pégourié-Gonnard | f7368c9 | 2015-08-14 14:33:05 +0200 | [diff] [blame] | 1150 | TEST_ASSERT( mbedtls_ecjpake_derive_secret( &cli, |
Manuel Pégourié-Gonnard | 5f18829 | 2015-08-14 10:52:39 +0200 | [diff] [blame] | 1151 | buf, sizeof( buf ), &len, ecjpake_lgc, NULL ) == 0 ); |
| 1152 | |
| 1153 | TEST_ASSERT( len == sizeof( ecjpake_test_pms ) ); |
| 1154 | TEST_ASSERT( memcmp( buf, ecjpake_test_pms, len ) == 0 ); |
| 1155 | |
Manuel Pégourié-Gonnard | 8489f17 | 2015-08-07 17:47:39 +0200 | [diff] [blame] | 1156 | if( verbose != 0 ) |
| 1157 | mbedtls_printf( "passed\n" ); |
Steven Cooreman | 64f2773 | 2021-01-11 17:20:10 +0100 | [diff] [blame] | 1158 | #endif /* ! MBEDTLS_ECJPAKE_ALT */ |
Manuel Pégourié-Gonnard | 8489f17 | 2015-08-07 17:47:39 +0200 | [diff] [blame] | 1159 | |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 1160 | cleanup: |
Manuel Pégourié-Gonnard | 8d31e80 | 2015-08-13 14:44:57 +0200 | [diff] [blame] | 1161 | mbedtls_ecjpake_free( &cli ); |
| 1162 | mbedtls_ecjpake_free( &srv ); |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 1163 | |
| 1164 | if( ret != 0 ) |
| 1165 | { |
| 1166 | if( verbose != 0 ) |
| 1167 | mbedtls_printf( "failed\n" ); |
| 1168 | |
| 1169 | ret = 1; |
| 1170 | } |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 1171 | |
| 1172 | if( verbose != 0 ) |
| 1173 | mbedtls_printf( "\n" ); |
| 1174 | |
| 1175 | return( ret ); |
| 1176 | } |
| 1177 | |
Manuel Pégourié-Gonnard | b1b250b | 2015-08-12 11:01:58 +0200 | [diff] [blame] | 1178 | #undef TEST_ASSERT |
| 1179 | |
Manuel Pégourié-Gonnard | 3dbf2fb | 2015-08-06 17:24:39 +0200 | [diff] [blame] | 1180 | #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED && MBEDTLS_SHA256_C */ |
| 1181 | |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 1182 | #endif /* MBEDTLS_SELF_TEST */ |
| 1183 | |
| 1184 | #endif /* MBEDTLS_ECJPAKE_C */ |