It may not matter, but you initially said Uno R4, but mostly recently Nano R4. Which do you have?
Uno R4 has a Software Secure Element that does SHA256. So you can therefore do HMAC-SHA256 with "a little bit" of code. While putting this together
#include <SoftwareATSE.h> // Software "AT" Secure Element
class Hasher {
public:
virtual size_t blockSize() const = 0;
virtual size_t lengthOut() const = 0;
virtual bool hash(uint8_t *digest, const uint8_t *message, size_t length) = 0;
};
class SHA256 : public Hasher {
static constexpr size_t SHA256_BLOCK = 64;
static constexpr size_t SHA256_LEN = 32;
public:
size_t blockSize() const override {
return SHA256_BLOCK;
}
size_t lengthOut() const override {
return SHA256_LEN;
}
bool hash(uint8_t *digest, const uint8_t *message, size_t length) override {
return SATSE.SHA256(message, length, digest);
}
} h256;
class HMAC {
Hasher &h;
uint8_t *key;
void blockSizedKey(const uint8_t *secretKey, size_t keyLength) {
key = new uint8_t[h.blockSize()];
if (!key) {
return;
}
size_t i;
if (keyLength > h.blockSize()) {
h.hash(key, secretKey, keyLength);
i = h.lengthOut();
} else {
memcpy(key, secretKey, keyLength);
i = keyLength;
}
while (i < h.blockSize()) {
key[i++] = 0;
}
}
void keyPad(uint8_t *pad, uint8_t value) {
for (size_t i = 0; i < h.blockSize(); i++) {
pad[i] = key[i] ^ value;
}
}
public:
HMAC(Hasher &hasher, const uint8_t *secretKey, size_t keyLength) : h(hasher) {
blockSizedKey(secretKey, keyLength);
}
~HMAC() {
if (key) delete[] key;
}
bool compute(uint8_t *digest, const uint8_t *message, size_t length) {
if (!key) {
return false;
}
uint8_t inner[h.blockSize()];
uint8_t outer[h.blockSize() + h.lengthOut()];
keyPad(inner, 0x36);
keyPad(outer, 0x5c);
size_t bufLen = h.blockSize() + length;
auto buf = new uint8_t[bufLen];
if (!buf) {
return false;
}
memcpy(buf, inner, h.blockSize());
memcpy(buf + h.blockSize(), message, length);
if (!h.hash(outer + h.blockSize(), buf, bufLen)) {
delete[] buf;
return false;
}
delete[] buf;
return h.hash(digest, outer, h.blockSize() + h.lengthOut());
}
};
void printBufferHex(const byte *input, size_t inputLength) {
for (size_t i = 0; i < inputLength; i++) {
if (i && i % 8 == 0) {
Serial.print(i & 8 ? " " : "\r\n");
}
Serial.print(input[i] >> 4, HEX);
Serial.print(input[i] & 0x0f, HEX);
}
Serial.println();
}
String key;
void setup() {
Serial.begin(115200);
// Important to see some of these startup messages, so wait to go
while (!Serial.available()) {
if (millis() % 1500 == 0) {
Serial.println("\n^^ press Enter to start");
delay(1);
}
}
Serial.readStringUntil('\n');
if (!SATSE.begin()) {
Serial.println("Failed to communicate with Software Secure Element!");
Serial.println("Make sure your WiFi firmware version is greater than 0.3.0");
for (;;);
}
while (!key.length()) {
Serial.println("^^ Enter secret shared key ^^");
while (!Serial.available()) delay(1);
key = Serial.readStringUntil('\n');
key.trim();
}
Serial.print("key length: ");
Serial.println(key.length());
}
void loop() {
static bool prompt = true;
if (prompt) {
prompt = false;
Serial.println("^^ Enter message to sign ^^");
}
if (!Serial.available()) {
return;
}
prompt = true;
String message = Serial.readStringUntil('\n');
message.trim();
Serial.print("signing: \"");
Serial.print(message);
Serial.println('"');
HMAC hmac(h256, reinterpret_cast<const uint8_t*>(key.c_str()), key.length());
uint8_t sig[h256.lengthOut()];
if (hmac.compute(sig, reinterpret_cast<const uint8_t*>(message.c_str()), message.length())) {
Serial.println("HMAC done");
printBufferHex(sig, h256.lengthOut());
} else {
Serial.println("HMAC failed");
}
}
it seems that the underlying MbedTLS does not allow the literal word "secret" as the key for the HMAC; the SATSE.SHA256 call fails. It's a horrible choice anyway, but still.
Equivalent JavaScript, taking a key in whatever form is convenient, and printing the results in hex for comparison
const crypto = require('node:crypto');
const h256 = (key, message) => {
const hmac = crypto.createHmac('sha256', key);
hmac.update(message);
return hmac.digest('hex');
};
prints
> h256('papaya', 'hello')
'6dd944cd627be304c73aad1f6833eb41e7ba205872c1f11804a3379c3aeb945e'
The sketch above, with the same six-letter fruit key
^^ press Enter to start
^^ Enter secret shared key ^^
key length: 6
^^ Enter message to sign ^^
signing: "hello"
HMAC done
6DD944CD627BE304 C73AAD1F6833EB41
E7BA205872C1F118 04A3379C3AEB945E
Having said all that, instead of a SAS token, apparently you can use a client cert with Azure IoT (just like with AWS). I posted in a thread a few days ago about getting mTLS on Uno R4 to work with Mosquitto.