Skip to content

Commit

Permalink
Setup for HMAC authentication capabilities
Browse files Browse the repository at this point in the history
@SRGDamia1, general HMAC functions could benefit all dataPublishers, so I am adding it to the dataPublisherBase. Does that make sense?
Once I get general HMAC SHA256 tokens to work, I'll then be creating a new publisher for Azure EventHubs. AWS IoT has a similar endpoint, so this could be widely used.
  • Loading branch information
aufdenkampe committed Apr 14, 2022
1 parent 7a1c63a commit 0ca66e3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
12 changes: 11 additions & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@
"authors": ["Sara Damiano", "Anthony Aufdenkampe"],
"frameworks": "arduino",
"platforms": "atmelavr, atmelsam"
}
},
{
"name": "cryptosuite2",
"owner": "envirodiy",
"url": "https://github.com/EnviroDIY/cryptosuite2",
"version": "~0.2.7",
"note": "Arduino/Generic C library for SHA256, SHA1 hashing and SHA256-HMAC, SHA1-HMAC",
"authors": [],
"frameworks": "arduino",
"platforms": "*"
}
]
}
8 changes: 8 additions & 0 deletions src/dataPublisherBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ String dataPublisher::parseMQTTState(int state) {
default: return String(state) + ": UNKNOWN";
}
}


String dataPublisher::writeHMACsignature(char* key, char* string_to_sign) {
// Create a HexMap to save 16 bytes of SRAM
const char hexMap[] PROGMEM = "0123456789abcdef"; // This is from the cryptosuite2 example, but there must be a better way.
// Anthony to add more code here
return signature;
}
14 changes: 14 additions & 0 deletions src/dataPublisherBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#undef MS_DEBUGGING_STD
#include "LoggerBase.h"
#include "Client.h"
#include "sha256.h" // `cryptosuite2` library's SHA256 functions

/**
* @brief The dataPublisher class is a virtual class used by other publishers to
Expand Down Expand Up @@ -268,6 +269,19 @@ class dataPublisher {
*/
String parseMQTTState(int state);

/**
* @brief Write an HMAC-SHA256 signature -- which is a keyed-hash message
* authentication code (HMAC) created using the SHA-256 cryptographic
* hash algorithm -- for generating tokens for authenticating requests
* using the authorization header.
*
* @param key The shared secret key used to "salt" the hash
* @param string_to_sign The string that gets hashed into a signature token.
* @return **String** The signed HMAC-SHA256 authorization token, or
* signature.
*/
String writeHMACsignature(char* key, char* string_to_sign);


protected:
/**
Expand Down

2 comments on commit 0ca66e3

@aufdenkampe
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses #410

@SRGDamia1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable. I like the "add code here" part.

Please sign in to comment.