nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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