This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

micro-ecc can't work with android studio

I have to exchange information with smart cellphone. My 10040 board create a pair of key by micro-ecc and transfer the public key to the cellphone. However, the cellphone shows errors like this: the point is not in the curve.

I select the same curve(secp256r1) in 10040 and Android Studio, but it seems the defination of secp256r1 in micro-ecc is no the same as it is in android studion, is that true?

How do I deal with the problem?

Maybe I have to compile micro-ecc to a SO file for android and a dll file for windows, how ever, it's too difficult for me to do such works. Is there anyone knows how to compile micro ecc to the SO and the dll file?

-----More details--2071.3.3-----

I transfer the public key to cellphone in order to do ECDH works, and I failed in converting the row byte array to android ECPublicKey.

public  static String GeneratePublicKey() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    int[] sk_in10040={      0x4E,0xF6,0x1E,0x86,0x79,0x0A,0x36,0x01,0x1F,0x0D,0xFB,0x35,0xE0,0x4C,0x27,0xD0,
                            0xD6,0xCC,0x27,0x8C,0x95,0xEF,0x51,0xF6,0x3A,0xC9,0x60,0xC4,0xBB,0x04,0x34,0x64};
    int[] pk_from10040={    0x81,0x04,0x54,0x6A,0xFD,0xB5,0x0B,0xED,0x7C,0x46,0x72,0x22,0x0F,0x24,0x8E,0xE9,
                            0xF1,0xB1,0xAA,0xC9,0x7D,0xC7,0xD4,0x4C,0x3F,0xAB,0x2B,0xFD,0x8B,0x29,0xBD,0x12,
                            0x1D,0x4D,0xCE,0x82,0x53,0x65,0xB1,0x13,0x1F,0x65,0xB9,0x6D,0x61,0x39,0x40,0x3D,
                            0x14,0x4C,0xDF,0x22,0x80,0xD7,0xF4,0x32,0x61,0x23,0x92,0x0B,0xE2,0xFD,0xAA,0x98};
    byte[] rowbyte = new byte[64];
    for (int i = 0; i <64 ; i++) {
        rowbyte[i] = (byte) pk_from10040[i];
    }
    ECGenParameterSpec ecParamSpec = new ECGenParameterSpec("secp256r1");//select curve
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
    kpg.initialize(ecParamSpec);
    KeyPair kpA = kpg.generateKeyPair();
    ECPublicKey apk= (ECPublicKey) kpA.getPublic();//get a publickey in secp256r1 format
    byte[] android_pk_encode = apk.getEncoded();
    System.arraycopy(rowbyte,0,android_pk_encode,android_pk_encode.length-rowbyte.length,rowbyte.length);
    //keep the head remained while replace the ECPoint data by the row byte array from 10040
    X509EncodedKeySpec ecpks = new X509EncodedKeySpec(android_pk_encode);
    KeyFactory keyFactory = KeyFactory.getInstance("EC");
    ECPublicKey ECDHpk  = null;
    try {
        ECDHpk = (ECPublicKey) keyFactory.generatePublic(ecpks);
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();//failed in generating publickey!!!
    }
    return ECDHpk.toString();//show the information by textview in an activity
}
Parents
  • I'm guessing this is due to some problems with endianness. See if both libraries have been compiled with the same endianness (little endian or big endian). Also make sure that the libraries have been compiled with the correct toolchain. You will need to compile the micro-ecc pointing to the compiler in use in Android Studio. Micro-ecc is distributed using a makefile-based build system, which would require some editing to point to the correct include folders and the correct compiler and archiver.

    For a general purpose method to setting up a secure channel between phone and device, I would recommend looking into ECDH (Eliptical curve Diffie Helman key exchange). This is a method of letting both party exchange a known secret for encrypting the channel based on only distributing the public key to each other. We have an example in the nRF5x SDK that showcases ECDH used by BLE to set up a secure connection using micro-ecc. The example is present in examples/ble_central_and_peripheral/experimental/ble_app_multirole_lesc

    Although this example is for use in BLE, the methods used within should be the same. Both parties create a private-public key pair and then you use functions available in micro-ecc to calculate a shared secret. The calculated shared secret can be used for encryption of all types of communication, and is only know by the two parties.

    Documentation of the example is available here: infocenter.nordicsemi.com/.../ble_sdk_app_multirole_lesc.html

Reply
  • I'm guessing this is due to some problems with endianness. See if both libraries have been compiled with the same endianness (little endian or big endian). Also make sure that the libraries have been compiled with the correct toolchain. You will need to compile the micro-ecc pointing to the compiler in use in Android Studio. Micro-ecc is distributed using a makefile-based build system, which would require some editing to point to the correct include folders and the correct compiler and archiver.

    For a general purpose method to setting up a secure channel between phone and device, I would recommend looking into ECDH (Eliptical curve Diffie Helman key exchange). This is a method of letting both party exchange a known secret for encrypting the channel based on only distributing the public key to each other. We have an example in the nRF5x SDK that showcases ECDH used by BLE to set up a secure connection using micro-ecc. The example is present in examples/ble_central_and_peripheral/experimental/ble_app_multirole_lesc

    Although this example is for use in BLE, the methods used within should be the same. Both parties create a private-public key pair and then you use functions available in micro-ecc to calculate a shared secret. The calculated shared secret can be used for encryption of all types of communication, and is only know by the two parties.

    Documentation of the example is available here: infocenter.nordicsemi.com/.../ble_sdk_app_multirole_lesc.html

Children
Related