Synss / python-mbedtls

Cryptographic library with an mbed TLS back end
MIT License
79 stars 28 forks source link

Can I call mbedtls_ecdh_gen_public directly with python-mbedtls. #40

Closed zhongfelix closed 3 years ago

zhongfelix commented 3 years ago

NOTE: Please use stackoverflow for support questions. This repository's issues are reserved for feature requests and bug reports.

I am submitting a …

Description

hi Synss, I want to specific argments p_rng for mbedtls_ecdh_gen_public, it's usually in c coding, but I can't find a way to do this with python-mbedtls.

thanks for your great work on python-mbedtls, it's very useful to me.

Current behavior

currently, I try to this in python code, but it always crash at so.mbedtls_ecdh_gen_public.

so = ctypes.CDLL("/usr/local/lib/python2.7/site-packages/mbedtls/pk.so") ecdh_ctx = (c_char_p 1024)(addressof(ctypes.create_string_buffer(1024))) d = (c_char_p 1024)(addressof(ctypes.create_string_buffer(1024))) Q = (c_char_p 1024)(addressof(ctypes.create_string_buffer(1024))) RNG = (c_char_p 1024)(addressof(ctypes.create_string_buffer(1024)))

so.mbedtls_ecdh_init(ecdh_ctx) so.mbedtls_mpi_init(d) so.mbedtls_ecp_point_init(Q) so.mbedtls_ctr_drbg_init(RNG)

so.mbedtls_ecp_group_load(ecdh_ctx, 9) so.mbedtls_ecdh_gen_public(ecdh_ctx, d, Q, so.mbedtls_ctr_drbg_random, RNG) print ecdh_ctx

Expected behavior

Steps to reproduce

1. 1. 1.

Minimal demo of the problem

>>> import mbedtls
...

Other information

Synss commented 3 years ago

Hi zhongfelix!

I am afraid my library will not be very useful for ctypes. You should probably call into libmbedtls directly instead. Nevertheless, mbedtls_ecdh_gen_public() is wrapped as ECDHNaive.generate() of the mbedtls.pk module. You can use it to generate a public key with, for example,

import mbedtls
naive = mbedtls.pk.ECDHNaive()
pub = naive.generate()  # <-- here, call to ecdh_gen_public, the public key is now in pub

I have documented a full key exchanges using ECDH in the README (search for ECDHServer and ECDHClient).

Does that answer your question?

Cheers, Mathias

Synss commented 3 years ago

I guess I answered your question 😃