4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
7 #if !defined(_AESKey_h_)
15 //! An AES-128 key is 128 bits, or 16 bytes.
16 typedef uint8_t aes128_key_t[16];
19 * \brief Base class for AESKey<S>.
21 * This class implements some bigger, non-template methods used in the
22 * AESKey<S> templated subclass.
27 //! \brief Reads hex encoded data from \a stream.
28 void _readFromStream(std::istream & stream, unsigned bytes, uint8_t * buffer);
30 //! \brief Writes hex encoded data to \a stream.
31 void _writeToStream(std::ostream & stream, unsigned bytes, const uint8_t * buffer);
35 * \brief Generic AES key class.
37 * The template parameter \a S is the number of bits in the key.
39 * The underlying key type can be accessed like this: AESKey<128>::key_t
41 * When a key instance is destroyed, it erases the key data by setting it
44 * \todo Add a way to allow only key sizes of 128, 192, and 256 bits.
45 * \todo Find a cross platform way to prevent the key data from being written
48 * AESKey<128> key = AESKey<128>::readFromStream(s);
51 class AESKey : public AESKeyBase
54 //! Type for this size of AES key.
55 typedef uint8_t key_t[S/8];
58 //! \brief Default constructor.
60 //! Initializes the key to 0.
63 memset(m_key, 0, sizeof(m_key));
66 //! \brief Constructor taking a key value reference.
67 AESKey(const key_t & key)
69 memcpy(m_key, &key, sizeof(m_key));
72 // \brief Constructor taking a key value pointer.
73 AESKey(const key_t * key)
75 memcpy(m_key, key, sizeof(m_key));
78 //! \brief Constructor, reads key from stream in hex format.
79 AESKey(std::istream & stream)
81 readFromStream(stream);
84 //! \brief Copy constructor.
85 AESKey(const AESKey<S> & other)
87 memcpy(m_key, other.m_key, sizeof(m_key));
90 //! \brief Destructor.
92 //! Sets the key value to zero.
95 memset(m_key, 0, sizeof(m_key));
98 //! \brief Set to the key to a randomly generated value.
101 RandomNumberGenerator rng;
102 rng.generateBlock(m_key, sizeof(m_key));
105 //! \brief Reads the key from a hex encoded data stream.
106 void readFromStream(std::istream & stream)
108 _readFromStream(stream, S/8, reinterpret_cast<uint8_t*>(&m_key));
111 //! \brief Writes the key to a data stream in hex encoded format.
112 void writeToStream(std::ostream & stream)
114 _writeToStream(stream, S/8, reinterpret_cast<uint8_t*>(&m_key));
117 //! \name Key accessors
119 inline const key_t & getKey() const { return m_key; }
120 inline void getKey(key_t * key) const { memcpy(key, m_key, sizeof(m_key)); }
122 inline void setKey(const key_t & key) { memcpy(m_key, &key, sizeof(m_key)); }
123 inline void setKey(const key_t * key) { memcpy(m_key, key, sizeof(m_key)); }
124 inline void setKey(const AESKey<S> & key) { memcpy(m_key, key.m_key, sizeof(m_key)); }
129 const AESKey<S> & operator = (const AESKey<S> & key) { setKey(key); return *this; }
130 const AESKey<S> & operator = (const key_t & key) { setKey(key); return *this; }
131 const AESKey<S> & operator = (const key_t * key) { setKey(key); return *this; }
133 operator const key_t & () const { return m_key; }
134 operator const key_t * () const { return m_key; }
138 key_t m_key; //!< The key value.
141 //! Standard type definition for an AES-128 key.
142 typedef AESKey<128> AES128Key;