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 CRA #2

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

R&D: WAMP CRA #2

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

Comments

@om26er
Copy link
Member

om26er commented May 29, 2024

Need code to sign cra challenge

@asimfarooq5
Copy link
Collaborator

asimfarooq5 commented Sep 24, 2024

Arduino WAMPCRA Signing

Create the WAMPCRAAuthenticator Class

WAMPCRAAuthenticator.h

#ifndef WAMPCRAAuthenticator_h
#define WAMPCRAAuthenticator_h

#include <Arduino.h>
#include <mbedtls/md.h>  // For HMAC

class WAMPCRAAuthenticator {
public:
    WAMPCRAAuthenticator(const String& secret);
    String signChallenge(const String& challenge);

private:
    String secret;
};

#endif

WAMPCRAAuthenticator.cpp

#include "WAMPCRAAuthenticator.h"

// Constructor to initialize the secret
WAMPCRAAuthenticator::WAMPCRAAuthenticator(const String& secret) {
    this->secret = secret;
}

// Function to compute HMAC-SHA256
String WAMPCRAAuthenticator::signChallenge(const String& challenge) {
    const size_t key_length = secret.length();
    const size_t challenge_length = challenge.length();
    unsigned char hmac_result[32];  // HMAC-SHA256 generates a 32-byte output

    mbedtls_md_context_t ctx;
    mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;

    // Initialize the context for HMAC
    mbedtls_md_init(&ctx);
    mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 1);
    mbedtls_md_hmac_starts(&ctx, (const unsigned char*)secret.c_str(), key_length);
    mbedtls_md_hmac_update(&ctx, (const unsigned char*)challenge.c_str(), challenge_length);
    mbedtls_md_hmac_finish(&ctx, hmac_result);
    mbedtls_md_free(&ctx);

    // Convert the result into a hex string
    String hmac_hex_str = "";
    for (int i = 0; i < 32; i++) {
        char hex[3];
        sprintf(hex, "%02x", hmac_result[i]);
        hmac_hex_str += hex;
    }

    return hmac_hex_str;  // Return the hex string
}

Example Usage

WAMPCRAAuthenticator.ino

#include <Arduino.h>
#include "WAMPCRAAuthenticator.h"

// Define the secret
const String secret = "662844e29567480a1c7e37cad65dc10161f6490793109adfea1bf02afec1fa91";

WAMPCRAAuthenticator authenticator(secret);

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

void loop() {
    delay(500);

    String challenge = "2KWqh/ScQM3L1apZGBPmg==";
    String signed_challenge = authenticator.signChallenge(challenge);

    Serial.println("Signed Challenge: " + signed_challenge);
}

Usage Instructions

Create the Class Files:

Create two files in your Arduino project: WAMPCRAAuthenticator.h and WAMPCRAAuthenticator.cpp.
Paste the class definitions into these files.
Include the Class:

In your main sketch (WAMPCRAAuthenticator.ino), include the class by adding #include "WAMPCRAAuthenticator.h".

Upload the Sketch:

Upload the sketch to your Arduino board, and it will compute and print the signed challenge in hexadecimal format.

@muzzammilshahid
Copy link
Member

The code works as expected. I have tested it using wampproto cli. However, instead of passing the secret to the constructor, we should pass it to the function, as we do in our other projects(example) and few other adjustments also needed in code.

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