From f75f912c31b52f200b54dd5ef763208db4412226 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Mon, 7 Jan 2019 15:36:51 +0000 Subject: [PATCH] Add functions to psa_util module to convert EC public keys --- include/mbedtls/psa_util.h | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/include/mbedtls/psa_util.h b/include/mbedtls/psa_util.h index fbf25e6383..dca4fa4f53 100644 --- a/include/mbedtls/psa_util.h +++ b/include/mbedtls/psa_util.h @@ -43,6 +43,8 @@ #include "pk.h" #include "oid.h" +#include + /* Translations for symmetric crypto. */ static inline psa_key_type_t mbedtls_psa_translate_cipher_type( @@ -352,6 +354,48 @@ static inline psa_ecc_curve_t mbedtls_psa_parse_tls_ecc_group( return( (psa_ecc_curve_t) tls_ecc_grp_reg_id ); } +/* This function takes a buffer holding an EC public key + * exported through psa_export_public_key(), and converts + * it into an ECPoint structure to be put into a ClientKeyExchange + * message in an ECDHE exchange. + * + * Both the present and the foreseeable future format of EC public keys + * used by PSA have the ECPoint structure contained in the exported key + * as a subbuffer, and the function merely selects this subbuffer instead + * of making a copy. + */ +static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src, + size_t srclen, + unsigned char **dst, + size_t *dstlen ) +{ + *dst = src; + *dstlen = srclen; + return( 0 ); +} + +/* This function takes a buffer holding an ECPoint structure + * (as contained in a TLS ServerKeyExchange message for ECDHE + * exchanges) and converts it into a format that the PSA key + * agreement API understands. + */ +static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( psa_ecc_curve_t curve, + unsigned char const *src, + size_t srclen, + unsigned char *dst, + size_t dstlen, + size_t *olen ) +{ + ((void) curve); + + if( srclen > dstlen ) + return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); + + memcpy( dst, src, srclen ); + *olen = srclen; + return( 0 ); +} + #endif /* MBEDTLS_USE_PSA_CRYPTO */ #endif /* MBEDTLS_PSA_UTIL_H */