Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R&D: WAMP CryptoSign #1

Open
om26er opened this issue May 29, 2024 · 3 comments
Open

R&D: WAMP CryptoSign #1

om26er opened this issue May 29, 2024 · 3 comments
Assignees

Comments

@om26er
Copy link
Member

om26er commented May 29, 2024

Need code to sign cryptosign challenge

@om26er
Copy link
Member Author

om26er commented May 30, 2024

@asimfarooq5
Copy link
Collaborator

asimfarooq5 commented Jun 26, 2024

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
Copy link
Member

muzzammilshahid commented Sep 25, 2024

works as expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants