ecdh.c 7.14 KB
Newer Older
1 2 3
/*
 *  Elliptic curve Diffie-Hellman
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * References:
 *
 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
27
 * RFC 4492
28 29
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
30
#if !defined(MBEDTLS_CONFIG_FILE)
31
#include "mbedtls/config.h"
32
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
33
#include MBEDTLS_CONFIG_FILE
34
#endif
35

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
36
#if defined(MBEDTLS_ECDH_C)
37

38
#include "mbedtls/ecdh.h"
39

40 41
#include <string.h>

42
/*
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
43
 * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair
44
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
45
int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
46 47 48
                     int (*f_rng)(void *, unsigned char *, size_t),
                     void *p_rng )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
49
    return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
50 51 52 53 54
}

/*
 * Compute shared secret (SEC1 3.3.1)
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
55 56
int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
                         const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
57 58
                         int (*f_rng)(void *, unsigned char *, size_t),
                         void *p_rng )
59 60
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
61
    mbedtls_ecp_point P;
62

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
63
    mbedtls_ecp_point_init( &P );
64 65 66 67

    /*
     * Make sure Q is a valid pubkey before using it
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
68
    MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
69

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
70
    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
71

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
72
    if( mbedtls_ecp_is_zero( &P ) )
73
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
74
        ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
75 76
        goto cleanup;
    }
77

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
78
    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
79 80

cleanup:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
81
    mbedtls_ecp_point_free( &P );
82 83 84 85

    return( ret );
}

86 87 88
/*
 * Initialize context
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
89
void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
90
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
91
    memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
92 93 94 95 96
}

/*
 * Free context
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
97
void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
98 99 100 101
{
    if( ctx == NULL )
        return;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
102 103 104 105 106 107 108 109
    mbedtls_ecp_group_free( &ctx->grp );
    mbedtls_ecp_point_free( &ctx->Q   );
    mbedtls_ecp_point_free( &ctx->Qp  );
    mbedtls_ecp_point_free( &ctx->Vi  );
    mbedtls_ecp_point_free( &ctx->Vf  );
    mbedtls_mpi_free( &ctx->d  );
    mbedtls_mpi_free( &ctx->z  );
    mbedtls_mpi_free( &ctx->_d );
110 111
}

112
/*
113
 * Setup and write the ServerKeyExhange parameters (RFC 4492)
114 115 116 117 118
 *      struct {
 *          ECParameters    curve_params;
 *          ECPoint         public;
 *      } ServerECDHParams;
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
119
int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
120 121 122
                      unsigned char *buf, size_t blen,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
123 124 125 126
{
    int ret;
    size_t grp_len, pt_len;

127
    if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
128
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
129

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
130
    if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
131 132 133
                != 0 )
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
134
    if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
135 136 137 138 139 140
                != 0 )
        return( ret );

    buf += grp_len;
    blen -= grp_len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
141
    if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
142 143 144 145
                                     &pt_len, buf, blen ) ) != 0 )
        return( ret );

    *olen = grp_len + pt_len;
146
    return( 0 );
147 148
}

149 150 151 152 153 154 155
/*
 * Read the ServerKeyExhange parameters (RFC 4492)
 *      struct {
 *          ECParameters    curve_params;
 *          ECPoint         public;
 *      } ServerECDHParams;
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
156
int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
157 158 159 160
                      const unsigned char **buf, const unsigned char *end )
{
    int ret;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
161
    if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
162 163
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
164
    if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
165 166 167
                != 0 )
        return( ret );

168
    return( 0 );
169
}
170

171 172 173
/*
 * Get parameters from a keypair
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
174 175
int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
                     mbedtls_ecdh_side side )
176 177 178
{
    int ret;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
179
    if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
180 181 182
        return( ret );

    /* If it's not our key, just import the public part as Qp */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
183 184
    if( side == MBEDTLS_ECDH_THEIRS )
        return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
185 186

    /* Our key: import public (as Q) and private parts */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
187 188
    if( side != MBEDTLS_ECDH_OURS )
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
189

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
190 191
    if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
        ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
192 193 194 195 196
        return( ret );

    return( 0 );
}

197 198 199
/*
 * Setup and export the client public value
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
200
int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
201 202 203 204 205 206
                      unsigned char *buf, size_t blen,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
{
    int ret;

207
    if( ctx == NULL || ctx->grp.pbits == 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
208
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
209

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
210
    if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
211 212 213
                != 0 )
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
214
    return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
215 216 217 218 219 220
                                olen, buf, blen );
}

/*
 * Parse and import the client's public value
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
221
int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
222 223
                      const unsigned char *buf, size_t blen )
{
224 225 226
    int ret;
    const unsigned char *p = buf;

227
    if( ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
228
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
229

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
230
    if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
231 232 233
        return( ret );

    if( (size_t)( p - buf ) != blen )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
234
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
235 236

    return( 0 );
237 238
}

239 240 241
/*
 * Derive and export the shared secret
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
242
int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
243 244 245
                      unsigned char *buf, size_t blen,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
246 247 248
{
    int ret;

249
    if( ctx == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
250
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
251

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
252
    if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
253 254
                                     f_rng, p_rng ) ) != 0 )
    {
255
        return( ret );
256
    }
257

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
258 259
    if( mbedtls_mpi_size( &ctx->z ) > blen )
        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
260

261
    *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
262
    return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
263 264
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
265
#endif /* MBEDTLS_ECDH_C */