I am trying to do an ECDH between embedded device running mbedTLS and Java using BouncyCastle. When I compare the produced key lengths I get a 66 bytes key made by mbedTLS and 65 bytes made by BC. Attaching pseudocode:
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "BC");
kpg.initialize(256);
KeyPair localKp = kpg.generateKey();
ASN1Sequence sequence = DERSequence.getInstance(localKp.getPublic().getEncoded());
DERBitString subjectPublicKey = (DERBitString) sequence.getObjectAt(1);
byte[] encodedLocalPublicKey = subjectPublicKey.getBytes();
// encodedLocalPublicKey.length -> 65
MbedTLS:
mbedtls_ecdh_context ecdh;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_entropy_context entropy;
const char pers[] = "ecdh";
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
mbedtls_ecdh_init(&ecdh);
int ret = 0;
if((ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers,
sizeof pers )) != 0) {
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
}
ret = mbedtls_ecp_group_load(&ecdh.grp, MBEDTLS_ECP_DP_SECP256R1);
if (ret != 0) {
mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
}
size_t olen;
unsigned char buf[1024];
ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf), mbedtls_ctr_drbg_random, &ctr_drbg);
// ret is 0, olen is 66
When I load the MbedTLS key into Java it throws java.lang.IllegalArgumentException: Invalid point encoding 0x41 :
byte[] publicKeyBytes = ... FROM MbedTLS
log.info("Public key length: {}", publicKeyBytes.length); // Shows 66
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-256");
ECPoint point = ecSpec.getCurve().decodePoint(publicKeyBytes); // This line throws
I tried to do ECDH Java between Java and MbedTLS between MbedTLS. Both of the tests succeeded but they somehow cannot exchange cross platforms.
What am I doing wrong? Sorry for maybe obvious problem but I am trying get handle of it. I will appreciate any help.
Thank you.