Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/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 "stdafx.h"
11 #include <string.h>
12 #include <iostream>
13 #include "Random.h"
14 
16 typedef uint8_t aes128_key_t[16];
17 
25 {
26 public:
28  void _readFromStream(std::istream &stream, unsigned bytes, uint8_t *buffer);
29 
31  void _writeToStream(std::ostream &stream, unsigned bytes, const uint8_t *buffer);
32 };
33 
50 template <int S>
51 class AESKey : public AESKeyBase
52 {
53 public:
55  typedef uint8_t key_t[S / 8];
56 
57 public:
61  AESKey() { memset(m_key, 0, sizeof(m_key)); }
63  AESKey(const key_t &key) { memcpy(m_key, &key, sizeof(m_key)); }
64  // \brief Constructor taking a key value pointer.
65  AESKey(const key_t *key) { memcpy(m_key, key, sizeof(m_key)); }
67  AESKey(std::istream &stream) { readFromStream(stream); }
69  AESKey(const AESKey<S> &other) { memcpy(m_key, other.m_key, sizeof(m_key)); }
73  ~AESKey() { memset(m_key, 0, sizeof(m_key)); }
75  void randomize()
76  {
78  rng.generateBlock(m_key, sizeof(m_key));
79  }
80 
82  void readFromStream(std::istream &stream) { _readFromStream(stream, S / 8, reinterpret_cast<uint8_t *>(&m_key)); }
84  void writeToStream(std::ostream &stream) { _writeToStream(stream, S / 8, reinterpret_cast<uint8_t *>(&m_key)); }
86 
87  inline const key_t &getKey() const { return m_key; }
88  inline void getKey(key_t *key) const { memcpy(key, m_key, sizeof(m_key)); }
89  inline void setKey(const key_t &key) { memcpy(m_key, &key, sizeof(m_key)); }
90  inline void setKey(const key_t *key) { memcpy(m_key, key, sizeof(m_key)); }
91  inline void setKey(const AESKey<S> &key) { memcpy(m_key, key.m_key, sizeof(m_key)); }
93 
95 
96  const AESKey<S> &operator=(const AESKey<S> &key)
97  {
98  setKey(key);
99  return *this;
100  }
101  const AESKey<S> &operator=(const key_t &key)
102  {
103  setKey(key);
104  return *this;
105  }
106  const AESKey<S> &operator=(const key_t *key)
107  {
108  setKey(key);
109  return *this;
110  }
111 
112  operator const key_t &() const { return m_key; }
113  operator const key_t *() const { return m_key; }
115 
116 protected:
117  key_t m_key;
118 };
119 
121 typedef AESKey<128> AES128Key;
122 
123 #endif // _AESKey_h_
AESKey(const AESKey< S > &other)
Copy constructor.
Definition: apps/elftosb/common/AESKey.h:69
Definition: apps/elftosb/common/Random.h:37
AESKey(const key_t &key)
Constructor taking a key value reference.
Definition: apps/elftosb/common/AESKey.h:63
void readFromStream(std::istream &stream)
Reads the key from a hex encoded data stream.
Definition: apps/elftosb/common/AESKey.h:82
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: apps/elftosb/common/AESKey.h:73
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: apps/elftosb/common/AESKey.h:67
void randomize()
Set to the key to a randomly generated value.
Definition: apps/elftosb/common/AESKey.h:75
Generic AES key class.
Definition: apps/elftosb/common/AESKey.h:51
AESKey()
Default constructor.
Definition: apps/elftosb/common/AESKey.h:61
Base class for AESKey<S>.
Definition: apps/elftosb/common/AESKey.h:24
void writeToStream(std::ostream &stream)
Writes the key to a data stream in hex encoded format.
Definition: apps/elftosb/common/AESKey.h:84