kokke / tiny-ECDH-c

Small portable Elliptic-Curve Diffie-Hellman in C
The Unlicense
254 stars 65 forks source link

Cannot generate a shared secret #13

Closed pur300 closed 5 years ago

pur300 commented 5 years ago

Hello,

I would like to use your library to generate a secret key which is going to be used to make an encrypted channel between PC and STM32l082 microcontroller. I wrote a test code on the microcontroller, where i simulate the generation of the keys and secret for both sides (PC and STM32l082). Public and private keys are successfully generated for both sides, but i'm having a problem when i want to generate a shared secret. For one side it is successfully generated, but for the other side, the function ecdh_shared_secret() always returns 0 (array secb is not populated with values).

Here is also my testing code snippet:

int main(void)
{
    uint8_t puba[ECC_PUB_KEY_SIZE] = {0};
    uint8_t prva[ECC_PRV_KEY_SIZE] = {0};
    uint8_t seca[ECC_PUB_KEY_SIZE] = {0};
    uint8_t pubb[ECC_PUB_KEY_SIZE] = {0};
    uint8_t prvb[ECC_PRV_KEY_SIZE] = {0};
    uint8_t secb[ECC_PUB_KEY_SIZE] = {0};

    // Initializes the Flash interface and the Systick
    HAL_Init();

    // Configure the system clock
    SystemClock_Config();
    SystemCoreClockUpdate();

    // Initialize peripherals
    Random_Init();
    Leds_Init();
    USB_Init();

    // Generate private and public keys for both sides
    do
    {
        for (int i = 0; i < ECC_PRV_KEY_SIZE; i++)
            prva[i] = Random_GetNumber() % 256;
    }
    while(ecdh_generate_keys(puba, prva) == 0);

    do
    {
        for (int i = 0; i < ECC_PRV_KEY_SIZE; i++)
            prvb[i] = Random_GetNumber() % 256;
    }
    while(ecdh_generate_keys(pubb, prvb) == 0);

    // Generate shared secret on both sides
    if(ecdh_shared_secret(prva, pubb, seca) == 0)
        for(;;);

    if(ecdh_shared_secret(prvb, puba, secb) == 0)
        for(;;); // Here is the problem. It always returns 0

    while (1)
    {

    }
}

I tried to compile the code also on the PC, where i get the same results. I'm using the sect163k1 elliptic curve. Where do you think is the problem?

Thank you in advance.

kokke commented 5 years ago

Hi @pur300 and thanks for taking an interest in this project :)

I should let you know that I am not sure that the quality of this project is production-ready just yet. I should probably note that in the README. I just haven't had time or motivation to work on this library lately.

I have successfully tested the example code on an STM32F103 and some other 32-bit machines, as well as on a PC (gcc and clang, linux 64bit), so I would expect the example code to work out of the box. Since it looks like your code resembles the example code, I am not sure what the problem could be.

Trying to narrow it down, how does the code you tested on the PC differ from the vanilla example code?

Also do check out this issue https://github.com/kokke/tiny-ECDH-c/issues/10, which is valid to my best belief.

kokke commented 5 years ago

Also, can you please verify for me that the arrays ARE initialized to zero. I have seen cases where the = {0};-construct only zero-initialized the first member of a struct, the rest contained garbage. I think it has to do with whether or not the compiler supports/works in C99-mode, which is the iso-version that introduced the = {0};-construct AFAIK.

You could do that by marking the arrays static or make the global (to force 0-initialization) or call memset(x, 0, sizeof(x)) on each of them - just to be sure.

kokke commented 5 years ago

Please also check that ECC_PUB_KEY_SIZE have the sizes you expect (check the ecdh.h)

pur300 commented 5 years ago

I checked the arrays and the values at the beginning are set to zero. But i also tried to set the buffer length for private key to 24 (for public key is 48 according to calculations) as suggested in issue #10, and now it seems to be working correctly (i tried to run it 100 times with different private keys).

Thank you for your time.