Files

333 lines
9.5 KiB
C++

/**
* @file include/ActivMiddleware/Misc/Des3Helper.h
*
* @brief Helper class for DES3 functions in OpenSsl.
*/
#if (!defined ACTIV_MISC_DES3_HELPER_H)
#define ACTIV_MISC_DES3_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 Des3Helper
{
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 class containing three DES key schedules
*/
class KeyBundle
{
public:
/**
* @brief Default constructor. Creates a bundle of uninitialized keys
*/
KeyBundle();
/**
* @brief Constructor. Constructs bundle of specified keys
*/
KeyBundle(const Des3Helper::Key &key1, const Des3Helper::Key &key2, const Des3Helper::Key &key3);
/**
* @brief Randomize all the keys in the bundle
*/
StatusCode Randomize();
/**
* @brief Set all three keys with specified keys
*/
void SetKeys(const Des3Helper::Key &key1, const Des3Helper::Key &key2, const Des3Helper::Key &key3);
Des3Helper::Key m_key1; ///< key 1
Des3Helper::Key m_key2; ///< key 2
Des3Helper::Key m_key3; ///< key 3
};
/**
* @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 keyBundle KeyBundle to encrypt data with
*/
static void Encrypt(const DES_cblock &input, DES_cblock &output, const KeyBundle &keyBundle);
/**
* @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 keyBundle KeyBundle to encrypt data with
*/
static void Decrypt(const DES_cblock &input, DES_cblock &output, const KeyBundle &keyBundle);
private:
/**
* @brief Constructor.
*
* Non-instantiable class, so constructor is not available.
*/
Des3Helper();
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Des3Helper::Encrypt(const DES_cblock &input, DES_cblock &output, const KeyBundle &keyBundle)
{
DES_ecb3_encrypt(const_cast<const_DES_cblock *>(&input), &output,
const_cast<DES_key_schedule *>(&keyBundle.m_key1.GetKeySchedule()),
const_cast<DES_key_schedule *>(&keyBundle.m_key2.GetKeySchedule()),
const_cast<DES_key_schedule *>(&keyBundle.m_key3.GetKeySchedule()),
DES_ENCRYPT);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Des3Helper::Decrypt(const DES_cblock &input, DES_cblock &output, const KeyBundle &keyBundle)
{
DES_ecb3_encrypt(const_cast<const_DES_cblock *>(&input), &output,
const_cast<DES_key_schedule *>(&keyBundle.m_key1.GetKeySchedule()),
const_cast<DES_key_schedule *>(&keyBundle.m_key2.GetKeySchedule()),
const_cast<DES_key_schedule *>(&keyBundle.m_key3.GetKeySchedule()),
DES_DECRYPT);
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline Des3Helper::Key::Key() :
m_isInitialized(false)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Des3Helper::Key::Key(const DES_cblock &keyBlock) :
m_isInitialized(false)
{
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, SetKey(keyBlock));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Des3Helper::Key::Key(const String &hexString) :
m_isInitialized(false)
{
SetKey(hexString);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Des3Helper::Key::IsInitialized() const
{
return m_isInitialized;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const DES_cblock &Des3Helper::Key::GetKeyBlock() const
{
return m_keyBlock;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const DES_key_schedule &Des3Helper::Key::GetKeySchedule() const
{
return m_keySchedule;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode Des3Helper::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 Des3Helper::Key::SetKey(const Des3Helper::Key &key)
{
*this = key;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode Des3Helper::Key::SetKey(const DES_cblock &keyBlock)
{
// DES_set_key_checked returns zero on success
if (DES_set_key_checked(const_cast<const_DES_cblock *>(&keyBlock), &m_keySchedule) != 0)
return STATUS_CODE_INVALID_KEY;
// Copy key block
if (&m_keyBlock != &keyBlock)
::memcpy(&m_keyBlock, &keyBlock, sizeof(DES_cblock));
m_isInitialized = true;
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode Des3Helper::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<MessageHandler::ENDIAN_LOCAL>(2 * sizeof(byte), byte));
newKeyBlock[i] = byte;
}
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, SetKey(newKeyBlock));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline String Des3Helper::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;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline Des3Helper::KeyBundle::KeyBundle()
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Des3Helper::KeyBundle::KeyBundle(const Des3Helper::Key &key1, const Des3Helper::Key &key2, const Des3Helper::Key &key3) :
m_key1(key1),
m_key2(key2),
m_key3(key3)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode Des3Helper::KeyBundle::Randomize()
{
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, m_key1.Randomize());
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, m_key2.Randomize());
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, m_key3.Randomize());
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Des3Helper::KeyBundle::SetKeys(const Des3Helper::Key &key1, const Des3Helper::Key &key2, const Des3Helper::Key &key3)
{
m_key1.SetKey(key1);
m_key2.SetKey(key2);
m_key3.SetKey(key3);
}
} // namespace Activ;
#endif // (!defined ACTIV_MISC_DES3_HELPER_H)