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