Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/AESKey.h
1 /*
2  * File: AESKey.h
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 #if !defined(_AESKey_h_)
8 #define _AESKey_h_
9 
10 #include <string.h>
11 #include <iostream>
12 #include "Random.h"
13 
15 typedef uint8_t aes128_key_t[16];
16 
23 class AESKeyBase
24 {
25 public:
27  void _readFromStream(std::istream &stream, unsigned bytes, uint8_t *buffer) const;
28 
30  void _writeToStream(std::ostream &stream, unsigned bytes, const uint8_t *buffer) const;
31 };
32 
49 template <int S>
50 class AESKey : public AESKeyBase
51 {
52 public:
54  typedef uint8_t key_t[S / 8];
55 
56 public:
60  AESKey() { memset(m_key, 0, sizeof(m_key)); }
62  AESKey(const key_t &key) { memcpy(m_key, &key, sizeof(m_key)); }
63  // \brief Constructor taking a key value pointer.
64  AESKey(const key_t *key) { memcpy(m_key, key, sizeof(m_key)); }
66  AESKey(std::istream &stream) { readFromStream(stream); }
68  AESKey(const AESKey<S> &other) { memcpy(m_key, other.m_key, sizeof(m_key)); }
72  ~AESKey() { memset(m_key, 0, sizeof(m_key)); }
74  void randomize()
75  {
77  rng.generateBlock(m_key, sizeof(m_key));
78  }
79 
81  void readFromStream(std::istream &stream) { _readFromStream(stream, S / 8, reinterpret_cast<uint8_t *>(&m_key)); }
83  void writeToStream(std::ostream &stream) const
84  {
85  _writeToStream(stream, S / 8, reinterpret_cast<const uint8_t *>(&m_key));
86  }
87 
89 
90  inline const key_t &getKey() const { return m_key; }
91  inline void getKey(key_t *key) const { memcpy(key, m_key, sizeof(m_key)); }
92  inline void setKey(const key_t &key) { memcpy(m_key, &key, sizeof(m_key)); }
93  inline void setKey(const key_t *key) { memcpy(m_key, key, sizeof(m_key)); }
94  inline void setKey(const AESKey<S> &key) { memcpy(m_key, key.m_key, sizeof(m_key)); }
96 
98 
99  const AESKey<S> &operator=(const AESKey<S> &key)
100  {
101  setKey(key);
102  return *this;
103  }
104  const AESKey<S> &operator=(const key_t &key)
105  {
106  setKey(key);
107  return *this;
108  }
109  const AESKey<S> &operator=(const key_t *key)
110  {
111  setKey(key);
112  return *this;
113  }
114 
115  operator const key_t &() const { return m_key; }
116  operator const key_t *() const { return m_key; }
117  friend std::ostream &operator<<(std::ostream &os, const AESKey<S> &key)
118  {
119  key.writeToStream(os);
120  return os;
121  }
122 
124 
125 protected:
126  key_t m_key;
127 };
128 
130 typedef AESKey<128> AES128Key;
131 
132 #endif // _AESKey_h_
AESKey(const AESKey< S > &other)
Copy constructor.
Definition: src/blfwk/AESKey.h:68
Definition: apps/elftosb/common/Random.h:37
AESKey(const key_t &key)
Constructor taking a key value reference.
Definition: src/blfwk/AESKey.h:62
void readFromStream(std::istream &stream)
Reads the key from a hex encoded data stream.
Definition: src/blfwk/AESKey.h:81
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()
Destructor.
Definition: src/blfwk/AESKey.h:72
void _readFromStream(std::istream &stream, unsigned bytes, uint8_t *buffer)
Reads hex encoded data from stream.
Definition: apps/elftosb/common/AESKey.cpp:30
key_t m_key
The key value.
Definition: apps/elftosb/common/AESKey.h:117
AESKey(std::istream &stream)
Constructor, reads key from stream in hex format.
Definition: src/blfwk/AESKey.h:66
void randomize()
Set to the key to a randomly generated value.
Definition: src/blfwk/AESKey.h:74
Generic AES key class.
Definition: apps/elftosb/common/AESKey.h:51
AESKey()
Default constructor.
Definition: src/blfwk/AESKey.h:60
Base class for AESKey<S>.
Definition: apps/elftosb/common/AESKey.h:24
void writeToStream(std::ostream &stream) const
Writes the key to a data stream in hex encoded format.
Definition: src/blfwk/AESKey.h:83