apps/elftosb/common/AESKey.h
/*
* File: AESKey.h
*
* Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
#if !defined(_AESKey_h_)
#define _AESKey_h_
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include "Random.h"
typedef uint8_t aes128_key_t[16];
class AESKeyBase
{
public:
void _readFromStream(std::istream &stream, unsigned bytes, uint8_t *buffer);
void _writeToStream(std::ostream &stream, unsigned bytes, const uint8_t *buffer);
};
template <int S>
class AESKey : public AESKeyBase
{
public:
typedef uint8_t key_t[S / 8];
public:
AESKey() { memset(m_key, 0, sizeof(m_key)); }
AESKey(const key_t &key) { memcpy(m_key, &key, sizeof(m_key)); }
// \brief Constructor taking a key value pointer.
AESKey(const key_t *key) { memcpy(m_key, key, sizeof(m_key)); }
AESKey(std::istream &stream) { readFromStream(stream); }
AESKey(const AESKey<S> &other) { memcpy(m_key, other.m_key, sizeof(m_key)); }
~AESKey() { memset(m_key, 0, sizeof(m_key)); }
void randomize()
{
RandomNumberGenerator rng;
rng.generateBlock(m_key, sizeof(m_key));
}
void readFromStream(std::istream &stream) { _readFromStream(stream, S / 8, reinterpret_cast<uint8_t *>(&m_key)); }
void writeToStream(std::ostream &stream) { _writeToStream(stream, S / 8, reinterpret_cast<uint8_t *>(&m_key)); }
inline const key_t &getKey() const { return m_key; }
inline void getKey(key_t *key) const { memcpy(key, m_key, sizeof(m_key)); }
inline void setKey(const key_t &key) { memcpy(m_key, &key, sizeof(m_key)); }
inline void setKey(const key_t *key) { memcpy(m_key, key, sizeof(m_key)); }
inline void setKey(const AESKey<S> &key) { memcpy(m_key, key.m_key, sizeof(m_key)); }
const AESKey<S> &operator=(const AESKey<S> &key)
{
setKey(key);
return *this;
}
const AESKey<S> &operator=(const key_t &key)
{
setKey(key);
return *this;
}
const AESKey<S> &operator=(const key_t *key)
{
setKey(key);
return *this;
}
operator const key_t &() const { return m_key; }
operator const key_t *() const { return m_key; }
protected:
key_t m_key;
};
typedef AESKey<128> AES128Key;
#endif // _AESKey_h_
AESKey::AESKey
AESKey(const AESKey< S > &other)
Copy constructor.
Definition: apps/elftosb/common/AESKey.h:69
RandomNumberGenerator
Definition: apps/elftosb/common/Random.h:37
AESKey::AESKey
AESKey(const key_t &key)
Constructor taking a key value reference.
Definition: apps/elftosb/common/AESKey.h:63
AESKey::readFromStream
void readFromStream(std::istream &stream)
Reads the key from a hex encoded data stream.
Definition: apps/elftosb/common/AESKey.h:82
AESKeyBase::_writeToStream
void _writeToStream(std::ostream &stream, unsigned bytes, const uint8_t *buffer)
Writes hex encoded data to stream.
Definition: apps/elftosb/common/AESKey.cpp:62
AESKey::~AESKey
~AESKey()
Destructor.
Definition: apps/elftosb/common/AESKey.h:73
AESKeyBase::_readFromStream
void _readFromStream(std::istream &stream, unsigned bytes, uint8_t *buffer)
Reads hex encoded data from stream.
Definition: apps/elftosb/common/AESKey.cpp:30
AESKey::m_key
key_t m_key
The key value.
Definition: apps/elftosb/common/AESKey.h:117
AESKey::AESKey
AESKey(std::istream &stream)
Constructor, reads key from stream in hex format.
Definition: apps/elftosb/common/AESKey.h:67
AESKey::randomize
void randomize()
Set to the key to a randomly generated value.
Definition: apps/elftosb/common/AESKey.h:75
AESKey
Generic AES key class.
Definition: apps/elftosb/common/AESKey.h:51
AESKey::AESKey
AESKey()
Default constructor.
Definition: apps/elftosb/common/AESKey.h:61
AESKeyBase
Base class for AESKey<S>.
Definition: apps/elftosb/common/AESKey.h:24
AESKey::writeToStream
void writeToStream(std::ostream &stream)
Writes the key to a data stream in hex encoded format.
Definition: apps/elftosb/common/AESKey.h:84