From a5402fee04ce27c0c8e5783a1b8fb2a68034cb4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 7 Nov 2012 20:24:05 +0100 Subject: [PATCH] Added ecp_use_known_dp() --- include/polarssl/ecp.h | 24 ++++++++++++-- include/polarssl/error.h | 1 + library/ecp.c | 68 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 5 deletions(-) diff --git a/include/polarssl/ecp.h b/include/polarssl/ecp.h index 2bede2ca8..8763fc18f 100644 --- a/include/polarssl/ecp.h +++ b/include/polarssl/ecp.h @@ -32,9 +32,9 @@ /* * ECP error codes * - * (The functions written up to now return MPI error codes only.) + * (Only one error code available...) */ - +#define POLARSSL_ERR_ECP_GENERIC -0x007E /**< Generic ECP error */ /** * \brief ECP point structure (affine coordinates) @@ -75,6 +75,12 @@ ecp_group; * parameters. Therefore, only well-known domain parameters from trusted * sources (such as the ones below) should be used. */ +#define POLARSSL_ECP_DP_SECP192R1 0 +#define POLARSSL_ECP_DP_SECP224R1 1 +#define POLARSSL_ECP_DP_SECP256R1 2 +#define POLARSSL_ECP_DP_SECP384R1 3 +#define POLARSSL_ECP_DP_SECP521R1 4 + #define POLARSSL_ECP_SECP192R1_P \ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF" #define POLARSSL_ECP_SECP192R1_B \ @@ -215,6 +221,20 @@ int ecp_group_read_string( ecp_group *grp, int radix, const char *p, const char *b, const char *gx, const char *gy, const char *n); +/** + * \brief Set a group using well-known domain parameters + * + * \param grp Destination group + * \param index Index in the list of well-known domain parameters + * + * \return O if successul, + * POLARSSL_ERR_MPI_XXX if initialization failed + * POLARSSL_ERR_ECP_GENERIC if index is out of range + * + * \note Index should be a POLARSSL_ECP_DP_XXX macro. + */ +int ecp_use_known_dp( ecp_group *grp, size_t index ); + /** * \brief Addition: R = P + Q * diff --git a/include/polarssl/error.h b/include/polarssl/error.h index 96815a73a..736e8a795 100644 --- a/include/polarssl/error.h +++ b/include/polarssl/error.h @@ -68,6 +68,7 @@ * SHA2 1 0x0078-0x0078 * SHA4 1 0x007A-0x007A * PBKDF2 1 0x007C-0x007C + * ECP 1 0x007E-0x007E * * High-level module nr (3 bits - 0x1...-0x8...) * Name ID Nr of Errors diff --git a/library/ecp.c b/library/ecp.c index db69e42c0..ee3d6bfcc 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -154,7 +154,57 @@ cleanup: return( ret ); } -#define dbg(X) printf(#X " = %s%lu\n", X.s < 0 ? "-" : "", X.p[0]) +/* + * Set a group using well-known domain parameters + */ +int ecp_use_known_dp( ecp_group *grp, size_t index ) +{ + switch( index ) + { + case POLARSSL_ECP_DP_SECP192R1: + return( ecp_group_read_string( grp, 16, + POLARSSL_ECP_SECP192R1_P, + POLARSSL_ECP_SECP192R1_B, + POLARSSL_ECP_SECP192R1_GX, + POLARSSL_ECP_SECP192R1_GY, + POLARSSL_ECP_SECP192R1_N ) + ); + case POLARSSL_ECP_DP_SECP224R1: + return( ecp_group_read_string( grp, 16, + POLARSSL_ECP_SECP224R1_P, + POLARSSL_ECP_SECP224R1_B, + POLARSSL_ECP_SECP224R1_GX, + POLARSSL_ECP_SECP224R1_GY, + POLARSSL_ECP_SECP224R1_N ) + ); + case POLARSSL_ECP_DP_SECP256R1: + return( ecp_group_read_string( grp, 16, + POLARSSL_ECP_SECP256R1_P, + POLARSSL_ECP_SECP256R1_B, + POLARSSL_ECP_SECP256R1_GX, + POLARSSL_ECP_SECP256R1_GY, + POLARSSL_ECP_SECP256R1_N ) + ); + case POLARSSL_ECP_DP_SECP384R1: + return( ecp_group_read_string( grp, 16, + POLARSSL_ECP_SECP384R1_P, + POLARSSL_ECP_SECP384R1_B, + POLARSSL_ECP_SECP384R1_GX, + POLARSSL_ECP_SECP384R1_GY, + POLARSSL_ECP_SECP384R1_N ) + ); + case POLARSSL_ECP_DP_SECP521R1: + return( ecp_group_read_string( grp, 16, + POLARSSL_ECP_SECP521R1_P, + POLARSSL_ECP_SECP521R1_B, + POLARSSL_ECP_SECP521R1_GX, + POLARSSL_ECP_SECP521R1_GY, + POLARSSL_ECP_SECP521R1_N ) + ); + } + + return( POLARSSL_ERR_ECP_GENERIC ); +} /* * Addition: R = P + Q, generic case (P != Q, P != 0, Q != 0, R != 0) @@ -476,7 +526,7 @@ int ecp_self_test( int verbose ) } } - if (verbose != 0 ) + if( verbose != 0 ) printf( "passed\n" ); MPI_CHK( ecp_copy( &mul_tbl[0], &O ) ); @@ -507,9 +557,21 @@ int ecp_self_test( int verbose ) } } - if (verbose != 0 ) + if( verbose != 0 ) printf( "passed\n" ); + if( verbose != 0 ) + printf( " ECP test #3 (use_known_dp): " ); + + for( i = 0; i <= POLARSSL_ECP_DP_SECP521R1; i++ ) + { + MPI_CHK( ecp_use_known_dp( &grp, i ) ); + } + + if( verbose != 0 ) + printf( "passed\n" ); + + cleanup: if( ret != 0 && verbose != 0 )