Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mbedtls_ecdh_gen_public speed dosen't improve #219

Open
kakaroot1998 opened this issue Nov 7, 2023 · 1 comment
Open

mbedtls_ecdh_gen_public speed dosen't improve #219

kakaroot1998 opened this issue Nov 7, 2023 · 1 comment

Comments

@kakaroot1998
Copy link

kakaroot1998 commented Nov 7, 2023

int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
mbedtls_mpi *d, mbedtls_ecp_point *Q,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
ECP_VALIDATE_RET( grp != NULL );
ECP_VALIDATE_RET( d != NULL );
ECP_VALIDATE_RET( Q != NULL );
ECP_VALIDATE_RET( f_rng != NULL );

#if 1
unsigned char priKey[ECDH_PRI_KEY_LEN];
unsigned char pub_key_buf[ECDH_PUB_KEY_LEN + 1];
unsigned char *pubKey = pub_key_buf + 1;

if (grp->id == MBEDTLS_ECP_DP_SECP256R1) {
    if (uECC_make_key(pubKey, priKey, uECC_secp256r1())) {
        pub_key_buf[0] = 0x04;
        mbedtls_ecp_point_read_binary(grp, Q, pub_key_buf, ECDH_PUB_KEY_LEN + 1);
        mbedtls_mpi_read_binary(d, priKey, ECDH_PRI_KEY_LEN);
        return 0;
    }
}

#endif

return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );

}

use uECC_make_key to gen ecc key pair seems cost same time compared to mbedtls_ecp_gen_keypair_base
but mbedtls_ecdh_compute_shared speed improves s lot;

int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng )
{
ECDH_VALIDATE_RET( grp != NULL );
ECDH_VALIDATE_RET( Q != NULL );
ECDH_VALIDATE_RET( d != NULL );
ECDH_VALIDATE_RET( z != NULL );

#if 1
if (grp->id == MBEDTLS_ECP_DP_SECP256R1) {
unsigned char priKey[ECDH_PRI_KEY_LEN];
unsigned char pub_key_buf[ECDH_PUB_KEY_LEN + 1];
unsigned char sharedKey[ECDH_SHARED_KEY_LEN];
unsigned char *pubKey = pub_key_buf + 1;
size_t len;

    mbedtls_ecp_point_write_binary(grp, Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &len, pub_key_buf, sizeof(pub_key_buf));
    mbedtls_mpi_write_binary(d, priKey, ECDH_PRI_KEY_LEN);
    if (uECC_shared_secret(pubKey, priKey, sharedKey, uECC_secp256r1())) {
        mbedtls_mpi_read_binary(z, sharedKey, ECDH_SHARED_KEY_LEN);
        return 0;
    }
}

#endif

return( ecdh_compute_shared_restartable( grp, z, Q, d,
                                         f_rng, p_rng, NULL ) );
@kakaroot1998
Copy link
Author

OK , i forget call the uECC_set_rng function;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant