public key exchange using ecdh in C

0

I am using ecdh algorithm for key exchange.**I am not sure of syntax for public key or private key.**I got implemented algorithm from git hub link.The private and public key pair work well when it is generated by the algorithm itself.

But this not what I need,public key has to be exchanged between two machines.So I have designed the code with manual insertion of public key but the algorithm fails to work as required.Given below is the sample code I have implemented.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "ecdh.h"

#include <string.h>
/* pseudo random number generator with 128 bit internal state... probably not suited for cryptographical usage */
typedef struct
{
  uint32_t a;
  uint32_t b;
  uint32_t c;
  uint32_t d;
} prng_t;

static prng_t prng_ctx;

static uint32_t prng_rotate(uint32_t x, uint32_t k)
{
  return (x << k) | (x >> (32 - k)); 
}

static uint32_t prng_next(void)
{
  uint32_t e = prng_ctx.a - prng_rotate(prng_ctx.b, 27); 
  prng_ctx.a = prng_ctx.b ^ prng_rotate(prng_ctx.c, 17); 
  prng_ctx.b = prng_ctx.c + prng_ctx.d;
  prng_ctx.c = prng_ctx.d + e; 
  prng_ctx.d = e + prng_ctx.a;


  return prng_ctx.d;
}

static void prng_init(uint32_t seed)
{
  uint32_t i;
  prng_ctx.a = 0xf1ea5eed;
  prng_ctx.b = prng_ctx.c = prng_ctx.d = seed;

  for (i = 0; i < 31; ++i) 
  {
    (void) prng_next();
  }
}

static void ecdh_demo(void)
{
  static uint8_t pubb[ECC_PUB_KEY_SIZE];
  static uint8_t puba[ECC_PUB_KEY_SIZE];
  static uint8_t prva[ECC_PRV_KEY_SIZE];
  static uint8_t seca[ECC_PUB_KEY_SIZE];
  static uint8_t prvb[ECC_PRV_KEY_SIZE];
  static uint8_t secb[ECC_PUB_KEY_SIZE];
  uint32_t i;
  /*IAm not sure of pubb syntax here******************/
  //static uint8_t prva[ECC_PRV_KEY_SIZE]={0x002f,0x0046,0x0053,0x00b8,0x0026,0x0067,0x005e,0x0049,0x0072,0x006b,0x0053,0x00e4,0x0099,0x0055,0x0009,0x0099,0x00be,0x0039,0x0047,0x0060,0x0002};
  //static uint8_t pubb[ECC_PUB_KEY_SIZE]={0x00000044,0x00000014,0x000000a6,0x0000003b,0x000000d5,0x0000009f,0x000000cb,0x0000006d,0x0000002f,0x00000085,0x000000e6,0x000000cf,0x00000050,0x00000035,0x0000007a,0x000000df,0x000000ed,0x00000041,0x00000036,0x000000e5,0x00000006,0x00000000,0x00000000,0x00000000,0x000000d5,0x000000ff,0x000000b8,0x000000d6,0x00000058,0x00000071,0x000000bc,0x000000ac,0x000000fa,0x000000a7,0x000000d4,0x000000ef,0x000000d3,0x00000054,0x00000053,0x0000004b,0x000000af,0x0000009d};

  /* 0. Initialize and seed random number generator */
  static int initialized = 0;
  if (!initialized)
  {
    prng_init((0xbad ^ 0xc0ffee ^ 42) | 0xcafebabe | 666);
    initialized = 1;
  }

  /* 1. Alice picks a (secret) random natural number 'a', calculates P = a * g and sends P to Bob. */
  for (i = 0; i < ECC_PRV_KEY_SIZE; ++i)
  {
    prva[i] = prng_next();
  }


   ecdh_generate_keys(puba,prva); 
   /* 3. Alice calculates S = a * Q = a * (b * g). */
    ecdh_shared_secret(prva, pubb, seca);

   printf("The shared secret between bob and alice:from alice\n");

    for (i = 0; i <ECC_PUB_KEY_SIZE ; ++i)
        printf("%.2x", seca[i]);
    printf("\n");


}


int main(int argc, char* argv[])
{

  int i;
  int ncycles = 1;

  //setup();

  if (argc > 1)
  {
    ncycles = atoi(argv[1]);
  }


  for (i = 0; i < ncycles; ++i)
  {
    ecdh_demo();
  }
   //display();
  return 0;
}
c
ecdh
asked on Stack Overflow Feb 15, 2018 by Vidatha Karuppan • edited Feb 15, 2018 by dbush

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0