AskSin++
Sign.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2017-01-15 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 //- -----------------------------------------------------------------------------------------------------------------------
5 
6 #ifndef __SIGN_H__
7 #define __SIGN_H__
8 
9 #include <ChannelList.h>
10 #include <Message.h>
11 
12 #ifdef USE_AES
13 #include <aes.h>
14 #endif
15 
16 
17 namespace as {
18 
19 #ifdef USE_AES
20 
21 #define AES_KEY_SIZE 16
22 
23 #ifndef HM_DEF_KEY
24  #error No HM_DEF_KEY defined.
25 #endif
26 #ifndef HM_DEF_KEY_INDEX
27  #error No HM_DEF_KEY_INDEX defined.
28 #endif
29 
30 
31 class KeyStore : public BaseList {
32 
33 public:
34  uint8_t count;
35  uint8_t auth[4];
36  aes128_ctx_t ctx;
37  uint8_t initvector[16];
38  uint8_t keytmp[8];
39 
40  KeyStore(uint16_t a) : BaseList(a), count(0) {}
41 
42  // return space needed in Storage
43  static uint16_t size () {
44  return AES_KEY_SIZE + 1;
45  }
46 
47  void defaults () const {
48  static uint8_t aes_def_key[] = {HM_DEF_KEY};
49  setIndex(HM_DEF_KEY_INDEX); // default index
50  writeKey(aes_def_key); // default key
51  }
52 
53  bool readKey(uint8_t* key) const {
54  return getData(1,key,AES_KEY_SIZE);
55  }
56 
57  bool writeKey(uint8_t* key) const {
58  return setData(1,key,AES_KEY_SIZE);
59  }
60 
61  uint8_t getIndex () const {
62  return getByte(0);
63  }
64 
65  bool setIndex (uint8_t idx) const {
66  return setByte(0,idx);
67  }
68 
69  void init () {}
70 
71  void storeAuth (uint8_t c,const uint8_t* a) {
72  count = c;
73  memcpy(auth,a,4);
74  }
75 
76  void addAuth (Message& msg) {
77  if( msg.count() == count ) {
78  msg.append(auth,4);
79  count = 0;
80  }
81  }
82 
83  void fillInitVector (const Message& msg) {
84  uint8_t n = msg.length()-10;
85  memcpy(initvector,msg.buffer()+10,n);
86  memset(initvector+n,0x00,16-n);
87  }
88 
89  void applyVector (uint8_t* data) {
90  for( uint8_t i=0; i<16; i++ ) {
91  data[i] ^= initvector[i];
92  }
93  }
94 
95  bool challengeKey (const uint8_t* challenge,uint8_t index) {
96  if( hasKey(index) == true ) {
97  uint8_t key[AES_KEY_SIZE];
98  readKey(key);
99  for( uint8_t i=0; i<6; ++i ) {
100  key[i] ^= challenge[i];
101  }
102  aes128_init(key,&ctx);
103  return true;
104  }
105  return false;
106  }
107 
108  bool hasKey (uint8_t index) {
109  return getIndex() == index;
110  }
111 
112  bool exchange (AesExchangeMsg& msg) {
113  uint8_t key[AES_KEY_SIZE];
114  readKey(key);
115  aes128_init(key,&ctx);
116  uint8_t* data = msg.data();
117 // DHEX(data,10);
118  aes128_dec(data,&ctx);
119 // DHEX(data,10);
120  if( data[0] == 0x01 ) {
121  if( (data[1] & 0x01) == 0x00 ) {
122  memcpy(keytmp,data+2,8);
123  }
124  else {
125  memcpy(key,keytmp,8);
126  memcpy(key+8,data+2,8);
127  DPRINT(F("New Key: "));DHEX(key,16);
128  DPRINT(F("Index: "));DHEXLN((uint8_t)(data[1] & 0xfe));
129  writeKey(key);
130  setIndex(data[1] & 0xfe);
131  }
132  return true;
133  }
134  return false;
135  }
136 };
137 
138 #else
139 
140 #define AES_KEY_SIZE 0
141 
142 class KeyStore : public BaseList {
143 public:
144  KeyStore(uint16_t a) : BaseList(a) {}
145 
146  // return space needed in Storage
147  static uint16_t size () {
148  return 0;
149  }
150 
151  void defaults () {}
152 
153  void init () {}
154 
155  void addAuth (Message& msg) {}
156 
157 };
158 
159 #endif
160 
161 }
162 
163 #endif
as::Message
Definition: Message.h:51
as::AesExchangeMsg
Definition: Message.h:615
as::BaseList
Definition: ChannelList.h:14
as::KeyStore
Definition: Sign.h:31