Files

256 lines
6.9 KiB
C++

/**
* @file include/ActivMiddleware/Misc/DesHelper.h
*
* @brief Helper class for DES functions in OpenSsl.
*/
#if (!defined ACTIV_MISC_DES_HELPER_H)
#define ACTIV_MISC_DES_HELPER_H
#include "ActivMiddleware/ActivBase/StatusCodeException.h"
#include "ActivMiddleware/ActivBase/MessageValidator.h"
#include "ActivMiddleware/Misc/Fmt.h"
#include <openssl/des.h>
//! Activ namespace.
namespace Activ
{
class DesHelper
{
private:
/**
* @brief non-instantiable class, so hide constructor
*/
DesHelper();
public:
/**
* @brief Wrapper class for DES keys
*/
class Key
{
public:
/**
* @brief Default constructor. Creates an uninitialized key
*/
Key();
/**
* @brief Constructor. Constructs key from a DES key block
*/
Key(const DES_cblock &keyBlock);
/**
* @brief Constructor. Constructs keys from a hex string
*/
Key(const String &hexString);
/**
* @brief Is the key initialized?
*/
bool IsInitialized() const;
/**
* @brief Get the underlying DES key block for the key
*/
const DES_cblock &GetKeyBlock() const;
/**
* @brief Get the underlying DES key schedule for the key
*/
const DES_key_schedule &GetKeySchedule() const;
/**
* @brief Randomize the key
*/
StatusCode Randomize();
/**
* @brief Set the key from another key
*/
void SetKey(const Key &key);
/**
* @brief Set the key from a DES key block
*/
StatusCode SetKey(const DES_cblock &keyBlock);
/**
* @brief Set the key from a hex string
*/
StatusCode SetKey(const String &hexString);
/**
* @brief Output key block as a hex string
*/
String ToString() const;
private:
bool m_isInitialized;
DES_cblock m_keyBlock;
DES_key_schedule m_keySchedule;
};
/**
* @brief Encrypt a DES_cblock using a specified key bundle
*
* @param input DES_cblock to encrypt
* @param output Encrypted data gets output into this variable
* @param key Key to encrypt data with
*/
static void Encrypt(const DES_cblock &input, DES_cblock &output, const DesHelper::Key key);
/**
* @brief Decrypt a DES_cblock using a specified key bundle
*
* @param input DES_cblock to decrypt
* @param output Decrypted data gets output into this variable
* @param key Key to encrypt data with
*/
static void Decrypt(const DES_cblock &input, DES_cblock &output, const DesHelper::Key key);
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline void DesHelper::Encrypt(const DES_cblock &input, DES_cblock &output, const DesHelper::Key key)
{
DES_ecb_encrypt(const_cast<const_DES_cblock *>(&input), &output, const_cast<DES_key_schedule *>(&key.GetKeySchedule()),
DES_ENCRYPT);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void DesHelper::Decrypt(const DES_cblock &input, DES_cblock &output, const DesHelper::Key key)
{
DES_ecb_encrypt(const_cast<const_DES_cblock *>(&input), &output, const_cast<DES_key_schedule *>(&key.GetKeySchedule()),
DES_DECRYPT);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline DesHelper::Key::Key() :
m_isInitialized(false)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DesHelper::Key::Key(const DES_cblock &keyBlock) :
m_isInitialized(false)
{
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, SetKey(keyBlock));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline DesHelper::Key::Key(const String &hexString) :
m_isInitialized(false)
{
SetKey(hexString);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool DesHelper::Key::IsInitialized() const
{
return m_isInitialized;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const DES_cblock &DesHelper::Key::GetKeyBlock() const
{
return m_keyBlock;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const DES_key_schedule &DesHelper::Key::GetKeySchedule() const
{
return m_keySchedule;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode DesHelper::Key::Randomize()
{
// DES_random_key returns zero on failure
if (DES_random_key(&m_keyBlock) == 0)
return STATUS_CODE_FAILURE;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, SetKey(m_keyBlock));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void DesHelper::Key::SetKey(const DesHelper::Key &key)
{
*this = key;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode DesHelper::Key::SetKey(const DES_cblock &keyBlock)
{
// DES_set_key_unchecked returns zero on success
DES_set_key_unchecked(const_cast<const_DES_cblock *>(&keyBlock), &m_keySchedule);
// Copy key block
if (&m_keyBlock != &keyBlock)
::memcpy(&m_keyBlock, &keyBlock, sizeof(DES_cblock));
m_isInitialized = true;
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode DesHelper::Key::SetKey(const String &hexString)
{
// String must be exactly twice the length of a DES_cblock (2 chars per byte)
if (hexString.length() != (2 * sizeof(DES_cblock)))
return STATUS_CODE_INVALID_LENGTH;
DES_cblock newKeyBlock;
MessageValidator hexValidator(hexString.data(), hexString.length());
// Validate the hex 2 chars (1 byte) at a time
for (size_t i = 0; i < sizeof(DES_cblock); ++i)
{
Activ::byte_t byte;
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, hexValidator.ValidateAsciiHexadecimalIntegral(2 * sizeof(byte), MessageHandler::ENDIAN_LOCAL, &byte));
newKeyBlock[i] = byte;
}
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, SetKey(newKeyBlock));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline String DesHelper::Key::ToString() const
{
String str;
for (size_t i = 0; i < sizeof(m_keyBlock); ++i)
{
str += Fmt::Format("{:02X}", static_cast<size_t>(m_keyBlock[i]));
}
return str;
}
} // namespace Activ;
#endif // (!defined ACTIV_MISC_DES_HELPER_H)