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