arduino-libraries / ArduinoECCX08

76 stars 49 forks source link

ECDH support #20

Open monsieur7 opened 4 years ago

monsieur7 commented 4 years ago

The ATECC508A ( ATECC608A ) chip support ECDH but I can't find support for this in the library. Can you add it, please?

KRolander commented 2 years ago

Hi @monsieur7, In the ECCX08.h you can add to the public functions:

 int ecdh(const byte publicKey[], int slot, byte share[]);

And to ECCX08.cpp you can add this:

int ECCX08Class::ecdh(const byte publicKey[], int slot, byte share[])
{

  if (!wakeup()) {
    return 0;
  }

  if (!sendCommand(0x43, 0x0C, slot, publicKey, 64)) {
    return 0;
  }

  delay(115);

  if (!receiveResponse(share, 32)) {
    return 0;
  }

  delay(1);

  idle();

  return 1;
}

In the main you can call like this:

uint8_t share[32];
  ECCX08.ecdh(pubkeyCipher, 2, share);
  if (res == 0)
  {

    Serial.println("[ecdh] Not Valid");
    Serial.println();
  }
  else
  {
    Serial.println("[ecdh] Valid");
    print_array(share, 32);
    Serial.println();
  }

void print_array(uint8_t *array, int arraySize)
{
  char buffer[3];
  for (int i = 0; i < arraySize; i++)
  {
    sprintf(buffer, "%02x", array[i]);
    Serial.print(buffer);
  }
  Serial.println("");
}