From 0fc3efc0ecbf4e546742ba87e1598ddbffbd5792 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 13 Dec 2023 13:20:57 +0100 Subject: [PATCH] Add first interfaces for new system --- include/relay/interface/CryptoSource.h | 68 +++++++++++++++++ include/relay/interface/StorageSource.h | 98 +++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 include/relay/interface/CryptoSource.h create mode 100644 include/relay/interface/StorageSource.h diff --git a/include/relay/interface/CryptoSource.h b/include/relay/interface/CryptoSource.h new file mode 100644 index 0000000..0741951 --- /dev/null +++ b/include/relay/interface/CryptoSource.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include "relay/CryptoPrimitives.h" + +/** + * @brief An abstract definition of an instance capable of crypto operations + * + */ +class CryptoSource { + +public: + + /** + * @brief Indicate that the crypto functions can be used. + * + * @return true The crypto functions are available + * @return false Some error prevents the use of the crypto functions. + */ + virtual + bool isAvailable() = 0; + + /** + * @brief Create a new random private key + * + * @param key The output buffer where the key will be stored + * @return true The key was created + * @return false The key could not be created + */ + virtual + bool createPrivateKey(PrivateKey* key) = 0; + + /** + * @brief Create a the public key corresponding to a private key + * + * @param privateKey The private key to use + * @param publicKey The output buffer where the public key will be stored + * @return true The key was created + * @return false The key could not be created + */ + virtual + bool createPublicKey(const PrivateKey* privateKey, PublicKey* publicKey) = 0; + + /** + * @brief Sign a message + * + * @param message The message payload to include in the message + * @param length The length of the payload + * @param signature The output buffer where the signature is written + * @return true The signature was created + * @return false The signature creation failed + */ + virtual + bool sign(const uint8_t *message, uint16_t length, Signature* signature, const PrivateKey* privateKey, const PublicKey* publicKey) = 0; + + /** + * @brief Verify a message + * + * @param signature The message signature + * @param publicKey The public key with which the message was signed + * @param message The pointer to the message data + * @param length The length of the message + * @return true The signature is valid + * @return false The signature is invalid + */ + virtual + bool verify(const Signature* signature, const PublicKey* publicKey, const void *message, uint16_t length) = 0; +}; \ No newline at end of file diff --git a/include/relay/interface/StorageSource.h b/include/relay/interface/StorageSource.h new file mode 100644 index 0000000..bf9b07a --- /dev/null +++ b/include/relay/interface/StorageSource.h @@ -0,0 +1,98 @@ +#pragma once + +#include + +/** + * @brief An abstract interface for persistent storage + * + * @note It is assumed that read operations are cached and therefore quick. + */ +class StorageSource { + +public: + + /** + * @brief Write a byte to disk + * + * @note The data is only persisted if the function `commitData()` is called afterwards. + * + * @param byte The byte to write + * @param index The index where to write the byte + * @return true + * @return false + */ + virtual + bool writeByteAtIndex(uint8_t byte, uint16_t index) = 0; + + /** + * @brief Ensure that enough storage is available for all data + * + * @param size + * @return true The space was initialized an has sufficient size + * @return false + */ + virtual + bool canProvideStorageWithSize(uint16_t size) = 0; + + /** + * @brief Write the data to persistent storage after a block of bytes was changed. + * + * @return true The data was persisted + * @return false The data could not be saved + */ + virtual + bool commitData() = 0; + + /** + * @brief Read a single byte from persistent storage + * + * @param index The index of the byte in the data + * @return uint8_t The byte at the given index + */ + virtual + uint8_t readByteAtIndex(uint16_t index) = 0; + + /** + * @brief Read a number of bytes from storage + * + * @param startIndex The index of the start byte + * @param count The number of bytes to read + * @param output The location to write the bytes + * @return uint8_t The number of bytes read + */ + virtual + uint16_t readBytes(uint16_t startIndex, uint16_t count, uint8_t* output) { + uint16_t endIndex = startIndex + count; + if (endIndex < startIndex) { + return 0; // Overflow + } + for (uint16_t i = 0; i < endIndex; i += 1) { + output[i] = readByteAtIndex(startIndex + i); + } + return count; + } + + /** + * @brief Write a number of bytes to storage + * + * @note The data is only persisted if the function `commitData()` is called afterwards. + * + * @param bytes The memory holding the bytes + * @param count The number of bytes to write + * @param startIndex The index where the bytes should be written + * @return uint16_t The number of bytes written to storage + */ + virtual + uint16_t writeBytes(uint8_t* bytes, uint16_t count, uint16_t startIndex) { + uint16_t endIndex = startIndex + count; + if (endIndex < startIndex) { + return 0; // Overflow + } + for (uint16_t i = 0; i < endIndex; i += 1) { + if (!writeByteAtIndex(bytes[i], startIndex + i)) { + return i; // Failed to write byte + } + } + return count; + } +}; \ No newline at end of file