mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2025-12-23 15:55:10 +01:00
Merge branch 'ecc-devel-mpg' into development
This commit is contained in:
commit
00c1f43743
27 changed files with 3852 additions and 3 deletions
|
|
@ -16,6 +16,9 @@ set(src
|
|||
debug.c
|
||||
des.c
|
||||
dhm.c
|
||||
ecp.c
|
||||
ecdh.c
|
||||
ecdsa.c
|
||||
entropy.c
|
||||
entropy_poll.c
|
||||
error.c
|
||||
|
|
|
|||
|
|
@ -39,7 +39,9 @@ OBJS= aes.o arc4.o asn1parse.o \
|
|||
blowfish.o camellia.o \
|
||||
certs.o cipher.o cipher_wrap.o \
|
||||
ctr_drbg.o debug.o des.o \
|
||||
dhm.o entropy.o entropy_poll.o \
|
||||
dhm.o ecdh.o ecdsa.o \
|
||||
ecp.o \
|
||||
entropy.o entropy_poll.o \
|
||||
error.o gcm.o havege.o \
|
||||
md.o md_wrap.o md2.o \
|
||||
md4.o md5.o net.o \
|
||||
|
|
|
|||
232
library/ecdh.c
Normal file
232
library/ecdh.c
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
/*
|
||||
* Elliptic curve Diffie-Hellman
|
||||
*
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
* RFC 4492
|
||||
*/
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#if defined(POLARSSL_ECDH_C)
|
||||
|
||||
#include "polarssl/ecdh.h"
|
||||
|
||||
/*
|
||||
* Generate public key: simple wrapper around ecp_gen_keypair
|
||||
*/
|
||||
int ecdh_gen_public( const ecp_group *grp, mpi *d, ecp_point *Q,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng )
|
||||
{
|
||||
return ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute shared secret (SEC1 3.3.1)
|
||||
*/
|
||||
int ecdh_compute_shared( const ecp_group *grp, mpi *z,
|
||||
const ecp_point *Q, const mpi *d )
|
||||
{
|
||||
int ret;
|
||||
ecp_point P;
|
||||
|
||||
ecp_point_init( &P );
|
||||
|
||||
/*
|
||||
* Make sure Q is a valid pubkey before using it
|
||||
*/
|
||||
MPI_CHK( ecp_check_pubkey( grp, Q ) );
|
||||
|
||||
MPI_CHK( ecp_mul( grp, &P, d, Q ) );
|
||||
|
||||
if( ecp_is_zero( &P ) )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
MPI_CHK( mpi_copy( z, &P.X ) );
|
||||
|
||||
cleanup:
|
||||
ecp_point_free( &P );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize context
|
||||
*/
|
||||
void ecdh_init( ecdh_context *ctx )
|
||||
{
|
||||
ecp_group_init( &ctx->grp );
|
||||
mpi_init ( &ctx->d );
|
||||
ecp_point_init( &ctx->Q );
|
||||
ecp_point_init( &ctx->Qp );
|
||||
mpi_init ( &ctx->z );
|
||||
ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free context
|
||||
*/
|
||||
void ecdh_free( ecdh_context *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return;
|
||||
|
||||
ecp_group_free( &ctx->grp );
|
||||
mpi_free ( &ctx->d );
|
||||
ecp_point_free( &ctx->Q );
|
||||
ecp_point_free( &ctx->Qp );
|
||||
mpi_free ( &ctx->z );
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup and write the ServerKeyExhange parameters (RFC 4492)
|
||||
* struct {
|
||||
* ECParameters curve_params;
|
||||
* ECPoint public;
|
||||
* } ServerECDHParams;
|
||||
*/
|
||||
int ecdh_make_params( ecdh_context *ctx, size_t *olen,
|
||||
unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng )
|
||||
{
|
||||
int ret;
|
||||
size_t grp_len, pt_len;
|
||||
|
||||
if( ctx == NULL || ctx->grp.pbits == 0 )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
|
||||
!= 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
|
||||
!= 0 )
|
||||
return( ret );
|
||||
|
||||
buf += grp_len;
|
||||
blen -= grp_len;
|
||||
|
||||
if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
|
||||
&pt_len, buf, blen ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
*olen = grp_len + pt_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the ServerKeyExhange parameters (RFC 4492)
|
||||
* struct {
|
||||
* ECParameters curve_params;
|
||||
* ECPoint public;
|
||||
* } ServerECDHParams;
|
||||
*/
|
||||
int ecdh_read_params( ecdh_context *ctx,
|
||||
const unsigned char **buf, const unsigned char *end )
|
||||
{
|
||||
int ret;
|
||||
|
||||
ecdh_init( ctx );
|
||||
|
||||
if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
|
||||
!= 0 )
|
||||
return( ret );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup and export the client public value
|
||||
*/
|
||||
int ecdh_make_public( ecdh_context *ctx, size_t *olen,
|
||||
unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng )
|
||||
{
|
||||
int ret;
|
||||
|
||||
if( ctx == NULL || ctx->grp.pbits == 0 )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
|
||||
!= 0 )
|
||||
return( ret );
|
||||
|
||||
return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
|
||||
olen, buf, blen );
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse and import the client's public value
|
||||
*/
|
||||
int ecdh_read_public( ecdh_context *ctx,
|
||||
const unsigned char *buf, size_t blen )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
return ecp_tls_read_point( &ctx->grp, &ctx->Qp, &buf, blen );
|
||||
}
|
||||
|
||||
/*
|
||||
* Derive and export the shared secret
|
||||
*/
|
||||
int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
|
||||
unsigned char *buf, size_t blen )
|
||||
{
|
||||
int ret;
|
||||
|
||||
if( ctx == NULL )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d ) )
|
||||
!= 0 )
|
||||
return( ret );
|
||||
|
||||
*olen = mpi_size( &ctx->z );
|
||||
return mpi_write_binary( &ctx->z, buf, blen );
|
||||
}
|
||||
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int ecdh_self_test( int verbose )
|
||||
{
|
||||
return( verbose++ );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* defined(POLARSSL_ECDH_C) */
|
||||
190
library/ecdsa.c
Normal file
190
library/ecdsa.c
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/*
|
||||
* Elliptic curve DSA
|
||||
*
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "polarssl/config.h"
|
||||
|
||||
#if defined(POLARSSL_ECDSA_C)
|
||||
|
||||
#include "polarssl/ecdsa.h"
|
||||
|
||||
/*
|
||||
* Derive a suitable integer for group grp from a buffer of length len
|
||||
* SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
|
||||
*/
|
||||
static int derive_mpi( const ecp_group *grp, mpi *x,
|
||||
const unsigned char *buf, size_t blen )
|
||||
{
|
||||
size_t n_size = (grp->nbits + 7) / 8;
|
||||
return( mpi_read_binary( x, buf, blen > n_size ? n_size : blen ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute ECDSA signature of a hashed message (SEC1 4.1.3)
|
||||
* Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
|
||||
*/
|
||||
int ecdsa_sign( const ecp_group *grp, mpi *r, mpi *s,
|
||||
const mpi *d, const unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
||||
{
|
||||
int ret, key_tries, sign_tries;
|
||||
ecp_point R;
|
||||
mpi k, e;
|
||||
|
||||
ecp_point_init( &R );
|
||||
mpi_init( &k );
|
||||
mpi_init( &e );
|
||||
|
||||
sign_tries = 0;
|
||||
do
|
||||
{
|
||||
/*
|
||||
* Steps 1-3: generate a suitable ephemeral keypair
|
||||
*/
|
||||
key_tries = 0;
|
||||
do
|
||||
{
|
||||
MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
|
||||
MPI_CHK( mpi_copy( r, &R.X ) );
|
||||
|
||||
if( key_tries++ > 10 )
|
||||
return( POLARSSL_ERR_ECP_GENERIC );
|
||||
}
|
||||
while( mpi_cmp_int( r, 0 ) == 0 );
|
||||
|
||||
/*
|
||||
* Step 5: derive MPI from hashed message
|
||||
*/
|
||||
MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
|
||||
|
||||
/*
|
||||
* Step 6: compute s = (e + r * d) / k mod n
|
||||
*/
|
||||
MPI_CHK( mpi_mul_mpi( s, r, d ) );
|
||||
MPI_CHK( mpi_add_mpi( &e, &e, s ) );
|
||||
MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
|
||||
MPI_CHK( mpi_mul_mpi( s, s, &e ) );
|
||||
MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
|
||||
|
||||
if( sign_tries++ > 10 )
|
||||
return( POLARSSL_ERR_ECP_GENERIC );
|
||||
}
|
||||
while( mpi_cmp_int( s, 0 ) == 0 );
|
||||
|
||||
cleanup:
|
||||
ecp_point_free( &R );
|
||||
mpi_free( &k );
|
||||
mpi_free( &e );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify ECDSA signature of hashed message (SEC1 4.1.4)
|
||||
* Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
|
||||
*/
|
||||
int ecdsa_verify( const ecp_group *grp,
|
||||
const unsigned char *buf, size_t blen,
|
||||
const ecp_point *Q, const mpi *r, const mpi *s)
|
||||
{
|
||||
int ret;
|
||||
mpi e, s_inv, u1, u2;
|
||||
ecp_point R, P;
|
||||
|
||||
ecp_point_init( &R ); ecp_point_init( &P );
|
||||
mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
|
||||
|
||||
/*
|
||||
* Step 1: make sure r and s are in range 1..n-1
|
||||
*/
|
||||
if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
|
||||
mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
|
||||
{
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
}
|
||||
|
||||
/*
|
||||
* Additional precaution: make sure Q is valid
|
||||
*/
|
||||
MPI_CHK( ecp_check_pubkey( grp, Q ) );
|
||||
|
||||
/*
|
||||
* Step 3: derive MPI from hashed message
|
||||
*/
|
||||
MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
|
||||
|
||||
/*
|
||||
* Step 4: u1 = e / s mod n, u2 = r / s mod n
|
||||
*/
|
||||
MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
|
||||
|
||||
MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
|
||||
MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
|
||||
|
||||
MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
|
||||
MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
|
||||
|
||||
/*
|
||||
* Step 5: R = u1 G + u2 Q
|
||||
*/
|
||||
MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G ) );
|
||||
MPI_CHK( ecp_mul( grp, &P, &u2, Q ) );
|
||||
MPI_CHK( ecp_add( grp, &R, &R, &P ) );
|
||||
|
||||
if( ecp_is_zero( &R ) )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
/*
|
||||
* Step 6: check that xR == r
|
||||
*/
|
||||
if( mpi_cmp_mpi( &R.X, r ) != 0 )
|
||||
return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
|
||||
|
||||
cleanup:
|
||||
ecp_point_free( &R ); ecp_point_free( &P );
|
||||
mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
|
||||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int ecdsa_self_test( int verbose )
|
||||
{
|
||||
return( verbose++ );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* defined(POLARSSL_ECDSA_C) */
|
||||
1364
library/ecp.c
Normal file
1364
library/ecp.c
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -63,6 +63,10 @@
|
|||
#include "polarssl/dhm.h"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
#include "polarssl/ecp.h"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ENTROPY_C)
|
||||
#include "polarssl/entropy.h"
|
||||
#endif
|
||||
|
|
@ -182,6 +186,13 @@ void error_strerror( int ret, char *buf, size_t buflen )
|
|||
snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" );
|
||||
#endif /* POLARSSL_DHM_C */
|
||||
|
||||
#if defined(POLARSSL_ECP_C)
|
||||
if( use_ret == -(POLARSSL_ERR_ECP_BAD_INPUT_DATA) )
|
||||
snprintf( buf, buflen, "ECP - Bad input parameters to function" );
|
||||
if( use_ret == -(POLARSSL_ERR_ECP_GENERIC) )
|
||||
snprintf( buf, buflen, "ECP - Generic ECP error" );
|
||||
#endif /* POLARSSL_ECP_C */
|
||||
|
||||
#if defined(POLARSSL_MD_C)
|
||||
if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) )
|
||||
snprintf( buf, buflen, "MD - The selected feature is not available" );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue