xconnio / wampproto-esp32

Sans-IO WAMP protocol implementation in C++ for ESP32
MIT License
0 stars 1 forks source link

R&D: WAMP CryptoSign #1

Open om26er opened 6 months ago

om26er commented 6 months ago

Need code to sign cryptosign challenge

om26er commented 5 months ago
asimfarooq5 commented 5 months ago

This Arduino sketch demonstrates how to perform cryptographic signing of a challenge using the Ed25519 algorithm. The code includes functions to convert between hexadecimal and binary formats and uses global variables for the private key, public key, and challenge for ease of access throughout the program.

Functionality

Hex to Binary Conversion:

The hex2bin function converts a hexadecimal string to a binary format.

Binary to Hex Conversion:

The bin2hex function converts a binary array to a hexadecimal string.

Global Variables:

private_key, public_key, and challenge are declared globally and initialized with specific values in the setup function.

Signing Function:

The sign_cryptosign_challenge function generates a cryptographic signature of the challenge using the Ed25519 algorithm, converts the signature to a hexadecimal string, and prints the concatenated signature and challenge in hexadecimal format.

#include <Arduino.h>
#include <Crypto.h>
#include <SHA256.h>
#include <Curve25519.h>
#include <Ed25519.h>
#include <stdexcept> // For error handling

// Function to convert hex to binary with error handling
bool hex2bin(const char* hex, uint8_t* bin, size_t bin_len) {
  size_t hex_len = strlen(hex);
  if (hex_len != bin_len * 2) {
    Serial.println("Error: Invalid hex length.");
    return false;
  }

  for (size_t i = 0; i < bin_len; i++) {
    if (sscanf(&hex[2 * i], "%2hhx", &bin[i]) != 1) {
      Serial.println("Error: Invalid hex character.");
      return false;
    }
  }
  return true;
}

// Function to convert binary to hex
void bin2hex(const uint8_t* bin, size_t bin_len, char* hex) {
  for (size_t i = 0; i < bin_len; i++) {
    sprintf(&hex[i * 2], "%02x", bin[i]);
  }
}

// Global variables for private key, public key, and challenge
uint8_t private_key[32] = {0};
uint8_t public_key[32] = {0};
uint8_t challenge[32] = {0};

// Function to sign the challenge
String sign_cryptosign_challenge() {
  uint8_t signature[64] = {0};

  // Sign the challenge with Ed25519
  Ed25519::sign(signature, private_key, public_key, challenge, 32);

  // Convert signature to hexadecimal format
  char signature_hex[129] = {0};
  bin2hex(signature, 64, signature_hex);
  signature_hex[128] = '\0';

  // Convert challenge to hexadecimal format
  char challenge_hex[65] = {0};
  bin2hex(challenge, 32, challenge_hex);
  challenge_hex[64] = '\0';

  // Concatenate the signature and challenge hex strings
  String result = String(signature_hex) + String(challenge_hex);
  Serial.println(result);
  return result;
}

void setup() {
  Serial.begin(115200);
  Serial.println("Setup started");

  // Sample hex values for the private key, public key, and challenge
  const char* private_key_hex = "662844e29567480a1c7e37cad65dc10161f6490793109adfea1bf02afec1fa91";
  const char* public_key_hex = "9dcd543bb0692ff8230d7e380cbe498b2418272e6ee9ce61b72c625e22a36cfe";
  const char* challenge_hex = "c9a91fbabb23e5fb2039ee608e2dec2e550e912262381895ad4828ce381b512a";

  // Convert hex strings to binary arrays with error checking
  if (!hex2bin(private_key_hex, private_key, 32)) {
    Serial.println("Error: Invalid private key format.");
    return;
  }
  if (!hex2bin(public_key_hex, public_key, 32)) {
    Serial.println("Error: Invalid public key format.");
    return;
  }
  if (!hex2bin(challenge_hex, challenge, 32)) {
    Serial.println("Error: Invalid challenge format.");
    return;
  }
}

void loop() {
  delay(500); // 500 ms delay between signing operations
  sign_cryptosign_challenge();
}

How to Use

Setup:

Ensure you have the necessary libraries (Crypto, SHA256, Curve25519, Ed25519) installed in your Arduino IDE. You can find these libraries in the arduino-crypto repository.

Upload:

Upload the sketch to your Arduino board.

Monitor Output:

Open the Serial Monitor to see the generated signature and challenge in hexadecimal format.

muzzammilshahid commented 2 months ago

works as expected