-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1961.cpp
329 lines (270 loc) · 7.28 KB
/
ds1961.cpp
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "OneWire.h"
#include "ds1961.h"
// commands used in the DS1961
#define CMD_WRITE_SCRATCHPAD 0x0F
#define CMD_COMPUTE_NEXT_SECRET 0x33
#define CMD_COPY_SCRATCHPAD 0x55
#define CMD_LOAD_FIRST_SECRET 0x5A
#define CMD_REFRESH_SCRATCHPAD 0xA3
#define CMD_READ_AUTH_PAGE 0xA5
#define CMD_READ_SCRATCHPAD 0xAA
#define CMD_READ_MEMORY 0xF0
// memory ranges
#define MEM_DATA_PAGE_0 0x00
#define MEM_DATA_PAGE_1 0x20
#define MEM_DATA_PAGE_2 0x40
#define MEM_DATA_PAGE_3 0x60
#define MEM_SECRET 0x80
#define MEM_IDENTITY 0x90
// timing (ms)
#define T_CSHA 2 // actually 1.5
#define T_PROG 10
DS1961::DS1961(OneWire *oneWire)
{
ow = oneWire;
}
static bool ResetAndSelect(OneWire *ow, const uint8_t id[8])
{
if (!ow->reset()) {
return false;
}
if (id) {
ow->select((uint8_t *) id);
} else {
ow->skip();
}
return true;
}
static bool WriteScratchPad(OneWire *ow, const uint8_t id[8], uint16_t addr, const uint8_t data[8])
{
uint8_t buf[11];
uint8_t crc[2];
int len = 0;
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// perform write scratchpad command
buf[len++] = CMD_WRITE_SCRATCHPAD;
buf[len++] = (addr >> 0) & 0xFF; // 2 byte target address
buf[len++] = (addr >> 8) & 0xFF; // 2 byte target address
memcpy(buf + len, data, 8);
len += 8;
ow->write_bytes(buf, len);
ow->read_bytes(crc, 2);
return ow->check_crc16(buf, len, crc);
}
static bool RefreshScratchPad(OneWire *ow, const uint8_t id[8], uint16_t addr, const uint8_t data[8])
{
uint8_t buf[11];
uint8_t crc[2];
int len = 0;
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// perform refresh scratchpad command
buf[len++] = CMD_REFRESH_SCRATCHPAD;
buf[len++] = (addr >> 0) & 0xFF; // 2 byte target address
buf[len++] = (addr >> 8) & 0xFF; // 2 byte target address
memcpy(buf + len, data, 8);
len += 8;
ow->write_bytes(buf, len);
ow->read_bytes(crc, 2);
return ow->check_crc16(buf, len, crc);
}
static bool ReadScratchPad(OneWire *ow, const uint8_t id[8], uint16_t *addr, uint8_t *es, uint8_t data[8])
{
uint8_t buf[12];
uint8_t crc[2];
int len = 0;
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// send read scratchpad command
buf[len++] = CMD_READ_SCRATCHPAD;
ow->write_bytes(buf, len);
// get TA0/1 and ES
ow->read_bytes(buf + len, 3);
len += 3;
*addr = (buf[2] << 8) | buf[1];
*es = buf[3];
// get data
ow->read_bytes(buf + len, 8);
len += 8;
memcpy(data, buf + 4, 8);
// check CRC
ow->read_bytes(crc, 2);
return ow->check_crc16(buf, len, crc);
}
static bool CopyScratchPad(OneWire *ow, const uint8_t id[8], uint16_t addr, uint8_t es, const uint8_t mac[20])
{
uint8_t buf[4];
int len = 0;
uint8_t status;
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// send copy scratchpad command + arguments
buf[len++] = CMD_COPY_SCRATCHPAD;
buf[len++] = (addr >> 0) & 0xFF; // 2 byte target address
buf[len++] = (addr >> 8) & 0xFF; // 2 byte target address
buf[len++] = es; // es
ow->write_bytes(buf, len, 1); // write and keep powered
// wait while MAC is calculated
delay(T_CSHA);
// send MAC
ow->write_bytes(mac, 20);
// wait 10 ms
delay(T_PROG);
ow->depower();
// check final status byte
status = ow->read();
return (status == 0xAA);
}
static bool ReadAuthPage(OneWire *ow, const uint8_t id[8], uint16_t addr, uint8_t data[32], uint8_t mac[20])
{
uint8_t buf[36];
uint8_t crc[2];
uint8_t status;
int len = 0;
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// send command
buf[len++] = CMD_READ_AUTH_PAGE;
buf[len++] = (addr >> 0) & 0xFF;
buf[len++] = (addr >> 8) & 0xFF;
ow->write_bytes(buf, len);
// read data part + 0xFF
ow->read_bytes(buf + len, 33);
len += 33;
if (buf[35] != 0xFF) {
return false;
}
ow->read_bytes(crc, 2);
if (!ow->check_crc16(buf, len, crc)) {
return false;
}
memcpy(data, buf + 3, 32);
// read mac part
delay(T_CSHA);
ow->read_bytes(mac, 20);
ow->read_bytes(crc, 2);
if (!ow->check_crc16(mac, 20, crc)) {
return false;
}
// check final status byte
status = ow->read();
return (status == 0xAA);
}
static bool LoadFirstSecret(OneWire *ow, const uint8_t id[8], uint16_t addr, uint8_t es)
{
uint8_t status;
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// write auth code
ow->write(CMD_LOAD_FIRST_SECRET);
ow->write((addr >> 0) & 0xFF);
ow->write((addr >> 8) & 0xFF);
ow->write(es, 1);
delay(T_PROG);
ow->depower();
status = ow->read();
return (status == 0xAA);
}
static bool ReadMemory(OneWire *ow, const uint8_t id[8], int addr, int len, uint8_t data[])
{
// reset and select
if (!ResetAndSelect(ow, id)) {
return false;
}
// write command/addr
ow->write(CMD_READ_MEMORY);
ow->write((addr >> 0) & 0xFF);
ow->write((addr >> 8) & 0xFF);
// read data
ow->read_bytes(data, len);
return true;
}
bool DS1961::ReadAuthWithChallenge(const uint8_t id[8], uint16_t addr, const uint8_t challenge[3], uint8_t data[32], uint8_t mac[20])
{
uint8_t scratchpad[8];
// put the challenge in the scratchpad
memset(scratchpad, 0, sizeof(scratchpad));
memcpy(scratchpad + 4, challenge, 3);
if (!WriteScratchPad(ow, id, addr, scratchpad)) {
// Serial.println("WriteScratchPad failed!");
return false;
}
// perform the authenticated read
if (!ReadAuthPage(ow, id, addr, data, mac)) {
// Serial.println("ReadAuthPage failed!");
return false;
}
return true;
}
bool DS1961::WriteSecret(const uint8_t id[8], const uint8_t secret[8])
{
uint16_t addr;
uint8_t es;
uint8_t data[8];
// write secret to scratch pad
if (!WriteScratchPad(ow, id, MEM_SECRET, secret)) {
// Serial.println("WriteScratchPad failed!");
return false;
}
// read scratch pad for auth code
if (!ReadScratchPad(ow, id, &addr, &es, data)) {
// Serial.println("ReadScratchPad failed!");
return false;
}
if (!LoadFirstSecret(ow, id, addr, es)) {
// Serial.println("LoadFirstSecret failed!");
return false;
}
return true;
}
/*
* Writes 8 bytes of data to specified address
*/
bool DS1961::WriteData(const uint8_t id[8], int addr, const uint8_t data[8], const uint8_t mac[20])
{
uint8_t spad[8];
uint16_t ad;
uint8_t es;
// write data into scratchpad
if (!WriteScratchPad(ow, id, addr, data)) {
// Serial.println("WriteScratchPad failed!");
return false;
}
// read scratch pad for auth code
if (!ReadScratchPad(ow, id, &ad, &es, spad)) {
// Serial.println("ReadScratchPad failed!");
return false;
}
// copy scratchpad to EEPROM
if (!CopyScratchPad(ow, id, ad, es, mac)) {
// Serial.println("CopyScratchPad failed!");
return false;
}
// refresh scratchpad
if (!RefreshScratchPad(ow, id, addr, data)) {
// Serial.println("RefreshScratchPad failed!");
return false;
}
// re-write with load first secret
if (!LoadFirstSecret(ow, id, addr, es)) {
// Serial.println("LoadFirstSecret failed!");
return false;
}
return true;
}