Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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