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