Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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