Sesame-Device/include/message.h

144 lines
3.7 KiB
C
Raw Normal View History

#pragma once
2023-08-09 12:55:11 +02:00
#include <stdint.h>
#include <stddef.h>
/**
* @brief The size of a message authentication code
*
* The MAC size is determined by the size of the output
* of the hash function used. In this case, for SHA256,
* the size is 32 bytes (= 256 bit)
*/
#define SHA256_MAC_SIZE 32
#pragma pack(push, 1)
2023-12-11 00:04:08 +01:00
enum class MessageType: uint8_t {
2023-12-05 20:46:41 +01:00
/// @brief The initial message from remote to device to request a challenge.
initial = 0,
/// @brief The second message in an unlock with the challenge from the device to the remote
challenge = 1,
/// @brief The third message with the signed challenge from the remote to the device
request = 2,
/// @brief The final message with the unlock result from the device to the remote
response = 3,
2023-12-11 00:04:08 +01:00
};
2023-12-05 20:46:41 +01:00
enum class MessageResult: uint8_t {
/// @brief The message was accepted.
MessageAccepted = 0,
/// @brief The web socket received text while waiting for binary data.
2023-12-11 00:05:18 +01:00
TextReceivedOverSocket = 1,
2023-12-05 20:46:41 +01:00
/// @brief An unexpected socket event occured while performing the exchange.
UnexpectedSocketEvent = 2,
/// @brief The received message size is invalid.
2023-12-11 00:05:18 +01:00
InvalidMessageSizeFromRemote = 3,
2023-12-05 20:46:41 +01:00
/// @brief The message signature was incorrect.
2023-12-11 00:05:18 +01:00
InvalidSignatureFromRemote = 4,
2023-12-05 20:46:41 +01:00
/// @brief The server challenge of the message did not match previous messages
2023-12-11 00:05:18 +01:00
InvalidServerChallengeFromRemote = 5,
2023-12-05 20:46:41 +01:00
/// @brief The client challenge of the message did not match previous messages
2023-12-11 00:05:18 +01:00
InvalidClientChallengeFromRemote = 6,
2023-12-05 20:46:41 +01:00
/// @brief An unexpected or unsupported message type was received
2023-12-11 00:05:18 +01:00
InvalidMessageTypeFromRemote = 7,
2023-12-05 20:46:41 +01:00
/// @brief A message is already being processed
TooManyRequests = 8,
2023-12-08 00:24:15 +01:00
/// @brief The received message result was invalid
2023-12-11 00:05:18 +01:00
InvalidMessageResultFromRemote = 9,
2023-12-08 00:24:15 +01:00
/// @brief An invalid Url parameter was set sending a message to the device over a local connection
InvalidUrlParameter = 10,
2023-12-05 20:46:41 +01:00
};
/**
2023-12-05 20:46:41 +01:00
* @brief A generic message to exchange during challenge-response authentication.
*/
typedef struct {
2023-12-05 20:46:41 +01:00
/// @brief The type of message being sent.
MessageType messageType;
/**
* @brief The random nonce created by the remote
*
2023-12-05 20:46:41 +01:00
* This nonce is a random number created by the remote, different for each unlock request.
* It is set for all message types.
*/
2023-12-05 20:46:41 +01:00
uint32_t clientChallenge;
2023-12-05 20:46:41 +01:00
/**
* @brief A random number to sign by the remote
*
2023-12-05 20:46:41 +01:00
* This nonce is set by the server after receiving an initial message.
* It is set for the message types `challenge`, `request`, and `response`.
*/
2023-12-05 20:46:41 +01:00
uint32_t serverChallenge;
/**
2023-12-05 20:46:41 +01:00
* @brief The response status for the previous message.
*
* It is set only for messages from the server, e.g. the `challenge` and `response` message types.
* Must be set to `MessageAccepted` for other messages.
*/
2023-12-05 20:46:41 +01:00
MessageResult result;
} Message;
/**
2023-12-05 20:46:41 +01:00
* @brief The signed version of a message.
*
*/
typedef struct {
/**
* @brief The authentication code of the message
*
* The code is created by performing HMAC-SHA256
* over the bytes of the `Message`.
*/
uint8_t mac[SHA256_MAC_SIZE];
2023-12-05 20:46:41 +01:00
/// @brief The message
Message message;
2023-12-05 20:46:41 +01:00
} SignedMessage;
constexpr size_t messageCounterSize = sizeof(uint32_t);
#pragma pack(pop)
2023-08-09 12:55:11 +02:00
constexpr int MESSAGE_CONTENT_SIZE = sizeof(Message);
2023-12-05 20:46:41 +01:00
constexpr int SIGNED_MESSAGE_SIZE = sizeof(SignedMessage);
2023-04-11 14:33:58 +02:00
/**
* @brief A callback for messages received over the socket
*
2023-08-09 12:55:11 +02:00
* The first parameter is a pointer to the byte buffer.
* The second parameter indicates the number of received bytes.
2023-04-11 14:33:58 +02:00
*/
2023-08-09 12:55:11 +02:00
typedef void (*MessageCallback)(uint8_t* payload, size_t length);
/**
* @brief A callback for socket errors
*/
2023-12-05 20:46:41 +01:00
typedef void (*ErrorCallback)(MessageResult event);
2023-08-09 12:55:11 +02:00