-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.c
740 lines (612 loc) · 24.8 KB
/
utils.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// SPDX-License-Identifier: MIT
#include <unistd.h>
#include "common.h"
#include "utils.h"
static int generate_locking_range_set_command(struct disk_device *dev, unsigned char *buffer, size_t *i,
unsigned char locking_range, uint64_t range_start, uint64_t range_length,
char read_lock_enabled, char write_lock_enabled, char read_locked,
char write_locked)
{
unsigned char locking_range_uid_str[9] = { 0 };
if (locking_range == 0) {
memcpy(locking_range_uid_str, LOCKING_RANGE_GLOBAL_UID, 8);
} else {
memcpy(locking_range_uid_str, LOCKING_RANGE_NNNN_UID, 8);
hex_add(locking_range_uid_str, 8, locking_range);
}
prepare_method(buffer, i, dev, locking_range_uid_str, METHOD_SET_UID);
{
start_name(buffer, i);
tiny_atom(buffer, i, 0, 1);
start_list(buffer, i);
{
if (range_start != UINT64_MAX) {
unsigned char tmp[] = "\x00\x00\x00\x00\x00\x00\x00\x00";
hex_add(tmp, 8, range_start);
LOG(INFO, "range_start = %" PRIu64 " (%02x%02x%02x%02x%02x%02x%02x%02x)\n", range_start, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7]);
start_name(buffer, i);
tiny_atom(buffer, i, 0, LOCKING_RANGE_COLUMN_RANGE_START);
short_atom(buffer, i, 0, 0, tmp, 8);
end_name(buffer, i);
}
if (range_length != UINT64_MAX) {
unsigned char tmp[] = "\x00\x00\x00\x00\x00\x00\x00\x00";
hex_add(tmp, 8, range_length);
LOG(INFO, "range_length = %" PRIu64 " (%02x%02x%02x%02x%02x%02x%02x%02x)\n", range_length, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7]);
start_name(buffer, i);
tiny_atom(buffer, i, 0, LOCKING_RANGE_COLUMN_RANGE_LENGTH);
short_atom(buffer, i, 0, 0, tmp, 8);
end_name(buffer, i);
}
if (read_lock_enabled != -1) {
LOG(INFO, "read_lock_enabled = %i\n", read_lock_enabled);
start_name(buffer, i);
tiny_atom(buffer, i, 0, LOCKING_RANGE_COLUMN_READ_LOCK_ENABLED);
tiny_atom(buffer, i, 0, read_lock_enabled);
end_name(buffer, i);
}
if (write_lock_enabled != -1) {
LOG(INFO, "write_lock_enabled = %i\n", write_lock_enabled);
start_name(buffer, i);
tiny_atom(buffer, i, 0, LOCKING_RANGE_COLUMN_WRITE_LOCK_ENABLED);
tiny_atom(buffer, i, 0, write_lock_enabled);
end_name(buffer, i);
}
if (read_locked != -1) {
LOG(INFO, "read_locked = %i\n", read_locked);
start_name(buffer, i);
tiny_atom(buffer, i, 0, LOCKING_RANGE_COLUMN_READ_LOCKED);
tiny_atom(buffer, i, 0, read_locked);
end_name(buffer, i);
}
if (write_locked != -1) {
LOG(INFO, "write_locked = %i\n", write_locked);
start_name(buffer, i);
tiny_atom(buffer, i, 0, LOCKING_RANGE_COLUMN_WRITE_LOCKED);
tiny_atom(buffer, i, 0, write_locked);
end_name(buffer, i);
}
}
end_list(buffer, i);
end_name(buffer, i);
}
finish_method(buffer, i);
return 0;
}
int unlock_range(struct disk_device *dev, unsigned char locking_range, size_t user_uid, char read_locked,
char write_locked, unsigned char *challenge, size_t challenge_len)
{
int err = 0;
unsigned char buffer[2048] = { 0 };
size_t i = 0;
unsigned char response[2048] = { 0 };
if ((err = start_session(dev, LOCKING_SP_UID, user_uid, challenge, challenge_len))) {
LOG(ERROR, "Failed when setting starting session for setting locking range parameters.\n");
return err;
}
generate_locking_range_set_command(dev, buffer, &i, locking_range, -1, -1, -1, -1, read_locked, write_locked);
if ((err = invoke_method(dev, buffer, i, response, sizeof(response)))) {
LOG(ERROR, "Failed when setting locking range parameters.\n");
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed when closing locking session.\n");
return err;
}
return err;
}
int setup_range(struct disk_device *dev, unsigned char locking_range, unsigned char *challenge,
size_t challenge_len, uint64_t start, uint64_t length, size_t users[], size_t users_len)
{
int err = 0;
unsigned char buffer[512] = { 0 };
size_t buffer_len = 0;
unsigned char response[512] = { 0 };
if ((err = start_session(dev, LOCKING_SP_UID, ADMIN_BASE_ID + 1, challenge, challenge_len))) {
return err;
}
// Configures the range and enables read and write locking by changing
// RangeStart, RangeLength, ReadLockEnabled and WriteLockEnabled for
// Locking_Range1
generate_locking_range_set_command(dev, buffer, &buffer_len, locking_range, start, length, 1, 1, -1, -1);
if ((err = invoke_method(dev, buffer, buffer_len, response, sizeof(response)))) {
LOG(ERROR, "Failed to setup locking range.\n");
close_session(dev);
return err;
}
// Create ACE with all the users.
if (users_len == 0) {
LOG(ERROR, "Empty authority list may cause INVALID_PARAMETER error.\n");
}
unsigned char boolean_ace[1024] = { 0 };
size_t boolean_ace_len = 0;
start_list(boolean_ace, &boolean_ace_len);
for (size_t i = 0; i < users_len; ++i) {
unsigned char user_uid[8];
memcpy(user_uid, AUTHORITY_XXXX_UID, 8);
hex_add(user_uid, 8, users[i]);
start_name(boolean_ace, &boolean_ace_len);
short_atom(boolean_ace, &boolean_ace_len, 1, 0, HALF_UID_AUTHORITY_OBJECT_REF, 4);
short_atom(boolean_ace, &boolean_ace_len, 1, 0, user_uid, 8);
end_name(boolean_ace, &boolean_ace_len);
if (i != 0) {
start_name(boolean_ace, &boolean_ace_len);
short_atom(boolean_ace, &boolean_ace_len, 1, 0, HALF_UID_BOOLEAN_ACE, 4);
tiny_atom(boolean_ace, &boolean_ace_len, 0, BOOLEAN_OR);
end_name(boolean_ace, &boolean_ace_len);
}
}
unsigned char admin_uid[8];
memcpy(admin_uid, AUTHORITY_XXXX_UID, 8);
hex_add(admin_uid, 8, ADMIN_BASE_ID + 1);
start_name(boolean_ace, &boolean_ace_len);
short_atom(boolean_ace, &boolean_ace_len, 1, 0, HALF_UID_AUTHORITY_OBJECT_REF, 4);
short_atom(boolean_ace, &boolean_ace_len, 1, 0, admin_uid, 8);
end_name(boolean_ace, &boolean_ace_len);
start_name(boolean_ace, &boolean_ace_len);
short_atom(boolean_ace, &boolean_ace_len, 1, 0, HALF_UID_BOOLEAN_ACE, 4);
tiny_atom(boolean_ace, &boolean_ace_len, 0, BOOLEAN_OR);
end_name(boolean_ace, &boolean_ace_len);
end_list(boolean_ace, &boolean_ace_len);
for (int i = 0; i < 3; ++i) {
unsigned char ace_uid[8] = { 0 };
if (i == 0) {
memcpy(ace_uid, TABLE_ACE_ROW_LOCKING_RANGE_XXXX_SET_RD_LOCKED, 8);
} else if (i == 1) {
memcpy(ace_uid, TABLE_ACE_ROW_LOCKING_RANGE_XXXX_SET_WR_LOCKED, 8);
} else {
memcpy(ace_uid, TABLE_ACE_LOCKING_RANGE_XXXX_GET_PARAMS, 8);
}
hex_add(ace_uid, 8, locking_range);
if ((err = set_row(dev, ace_uid, TABLE_ACE_COLUMN_BOOLEAN_EXPR, boolean_ace, boolean_ace_len))) {
LOG(ERROR, "Failed to set rights for locking range (%d: %s).\n", i, i == 0 ? "reading" : "writing");
close_session(dev);
return err;
}
}
if ((err = close_session(dev))) {
return err;
}
return err;
}
int list_range(struct disk_device *dev, unsigned locking_range, unsigned char *challenge, size_t challenge_len, size_t user)
{
int err = 0;
unsigned char locking_range_uid_str[9] = { 0 };
uint64_t start, length, rlocked, wlocked, rlck_enabled, wlck_enabled;
if (locking_range == 0) {
memcpy(locking_range_uid_str, LOCKING_RANGE_GLOBAL_UID, 8);
} else {
memcpy(locking_range_uid_str, LOCKING_RANGE_NNNN_UID, 8);
hex_add(locking_range_uid_str, 8, locking_range);
}
// Get Locking Authority session
if ((err = start_session(dev, LOCKING_SP_UID, user, challenge, challenge_len))) {
LOG(ERROR, "Failed when setting starting session for getting locking range parameters.\n");
return err;
}
// Get Locking Range info (TODO: Request values in a single request. This is suboptimal)
if ((err = get_row_int(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_RANGE_START, &start))) {
LOG(ERROR, "Failed to read Locking Range %u start.\n", locking_range);
close_session(dev);
return err;
}
if ((err = get_row_int(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_RANGE_LENGTH, &length))) {
LOG(ERROR, "Failed to read Locking Range %u length.\n", locking_range);
close_session(dev);
return err;
}
if ((err = get_row_int(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_READ_LOCKED, &rlocked))) {
LOG(ERROR, "Failed to read Locking Range %u read locked.\n", locking_range);
close_session(dev);
return err;
}
if ((err = get_row_int(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_WRITE_LOCKED, &wlocked))) {
LOG(ERROR, "Failed to read Locking Range %u write locked.\n", locking_range);
close_session(dev);
return err;
}
if ((err = get_row_int(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_READ_LOCK_ENABLED, &rlck_enabled))) {
LOG(ERROR, "Failed to read Locking Range %u read lock enabled.\n", locking_range);
close_session(dev);
return err;
}
if ((err = get_row_int(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_WRITE_LOCK_ENABLED, &wlck_enabled))) {
LOG(ERROR, "Failed to read Locking Range %u write lock enabled.\n", locking_range);
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to close session.\n");
return err;
}
fprintf(stdout,
"Locking range %u: Start: %" PRIu64 ", length: %" PRIu64 ", R locked: %s, W locked: %s, R lock enabled: %s, W lock enabled: %s.\n",
locking_range,
start,length,
rlocked ? "yes" : "no",
wlocked ? "yes" : "no",
rlck_enabled ? "yes" : "no",
wlck_enabled ? "yes" : "no");
return 0;
}
int setup_user(struct disk_device *dev, size_t user_uid, unsigned char *admin_pin, size_t admin_pin_len,
unsigned char *user_pin, size_t user_pin_len)
{
int err = 0;
if ((err = start_session(dev, LOCKING_SP_UID, ADMIN_BASE_ID + 1, admin_pin, admin_pin_len))) {
LOG(ERROR, "Failed to start session with Locking SP.\n");
return err;
}
unsigned char user_uid_str[9] = { 0 };
memcpy(user_uid_str, AUTHORITY_XXXX_UID, 8);
hex_add(user_uid_str, 8, user_uid);
unsigned char atom_true[32] = { 0 };
size_t atom_true_len = 0;
tiny_atom(atom_true, &atom_true_len, 0, 1);
if ((err = set_row(dev, user_uid_str, TABLE_AUTHORITY_COLUMN_ENABLED, atom_true, atom_true_len))) {
LOG(ERROR, "Failed to enable User authority.\n");
close_session(dev);
return err;
}
unsigned char c_pin_str[9] = { 0 };
memcpy(c_pin_str, TABLE_C_PIN_UID, 8);
hex_add(c_pin_str, 8, user_uid);
unsigned char atom_pin[256] = { 0 };
size_t atom_pin_len = 0;
medium_atom(atom_pin, &atom_pin_len, 1, 0, user_pin, user_pin_len);
if ((err = set_row(dev, c_pin_str, TABLE_C_PIN_COLUMN_PIN, atom_pin, atom_pin_len))) {
LOG(ERROR, "Failed to set pin of User authority.\n");
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to close a session.\n");
}
return err;
}
int setup_programmatic_reset(struct disk_device *dev, const unsigned char *pwd, size_t pwd_len,
char locking_range)
{
int err = 0;
// Enable TPER_RESET command.
if ((err = start_session(dev, ADMIN_SP_UID, SID_USER_ID, pwd, pwd_len))) {
LOG(ERROR, "Failed to start Admin SP session as SID.\n");
return err;
}
unsigned char atom_true[32] = { 0 };
size_t atom_true_len = 0;
tiny_atom(atom_true, &atom_true_len, 0, 1);
if ((err = set_row(dev, TABLE_TPER_INFO_OBJ_UID, TABLE_TPER_INFO_COLUMN_PROGRAMMATIC_RESET_ENABLE,
atom_true, atom_true_len))) {
LOG(ERROR, "Failed to enable programmatic reset.\n");
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to finish session.\n");
return err;
}
// Change LockOnReset for the locking range.
if (locking_range >= 0) {
if ((err = start_session(dev, LOCKING_SP_UID, ADMIN_BASE_ID + 1, pwd, pwd_len))) {
LOG(ERROR, "Failed to start Admin SP session.\n");
return err;
}
unsigned char locking_range_uid[8];
memcpy(locking_range_uid, LOCKING_RANGE_NNNN_UID, 8);
hex_add(locking_range_uid, 8, locking_range);
unsigned char atom_resets[32] = { 0 };
size_t atom_resets_len = 0;
start_list(atom_resets, &atom_resets_len);
tiny_atom(atom_resets, &atom_resets_len, 0, LOCKING_RANGE_COLUMN_LOCK_ON_RESET_POWER_CYCLE);
tiny_atom(atom_resets, &atom_resets_len, 0, LOCKING_RANGE_COLUMN_LOCK_ON_RESET_PROGRAMMATIC);
end_list(atom_resets, &atom_resets_len);
if ((err = set_row(dev, locking_range_uid, LOCKING_RANGE_COLUMN_LOCK_ON_RESET,
atom_resets, atom_resets_len))) {
LOG(ERROR, "Failed to set LockOnReset.\n");
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to finish session.\n");
return err;
}
}
return err;
}
int tper_reset(struct disk_device *dev)
{
int err = 0;
unsigned char buffer[512] = { 0 };
LOG(INFO, "TPer reset\n");
if ((err = trusted_command(dev, buffer, sizeof(buffer), IF_SEND, TCG_PROTOCOL_ID_2, TPER_RESET_COMID))) {
LOG(ERROR, "Failed to send programmatic reset.\n");
}
return err;
}
int get_comid(struct disk_device *dev, int *comid)
{
int err;
struct get_comid_response resp = {};
if ((err = trusted_command(dev, (uint8_t*)&resp, sizeof(resp), IF_RECV, TCG_PROTOCOL_ID_2, TCG_GET_COMID)))
LOG(ERROR, "Failed to Get ComID.\n");
else {
*comid = be16_to_cpu(resp.comid);
LOG(ERROR, "Get ComID 0x%04x\n", *comid);
}
return err;
}
int comid_valid(struct disk_device *dev, int comid)
{
int err;
struct comid_valid_request rq = {
.request_code = cpu_to_be32(TCG_REQUEST_CODE_COMID_VALID)
};
struct comid_valid_response resp = { 0 };
if ((err = do_level_0_discovery(dev))) {
LOG(ERROR, "Failed to get ComID from Discovery0.\n");
return err;
}
LOG(INFO, "Commid valid, Base ComID 0x%04x (Ext ComID 0x%04x)\n", dev->base_com_id, comid);
rq.comid = cpu_to_be16(dev->base_com_id);
if ((err = trusted_command(dev, (uint8_t*)&rq, sizeof(rq), IF_SEND, TCG_PROTOCOL_ID_2, dev->base_com_id))) {
LOG(ERROR, "Failed to send stack reset.\n");
return err;
}
if ((err = trusted_command(dev, (uint8_t*)&resp, sizeof(resp), IF_RECV, TCG_PROTOCOL_ID_2, dev->base_com_id))) {
LOG(ERROR, "Failed to receive command: %i\n", err);
return err;
}
LOG(INFO, "ComID valid response: ComID 0x%04x, Code 0x%x, Length 0x%x, State 0x%x\n",
be16_to_cpu(resp.comid), be32_to_cpu(resp.request_code),
be16_to_cpu(resp.data_length), be32_to_cpu(resp.state));
LOG_HEX(&resp, 48);
return 0;
}
int stack_reset(struct disk_device *dev)
{
int err = 0, i = 0;
struct stack_reset_request rq = {
.request_code = cpu_to_be32(TCG_REQUEST_CODE_STACK_RESET)
};
struct stack_reset_response resp = { 0 };
if ((err = do_level_0_discovery(dev))) {
LOG(ERROR, "Failed to get ComID from Discovery0.\n");
return err;
}
LOG(INFO, "Stack reset, Base ComID 0x%04x\n", dev->base_com_id);
rq.comid = cpu_to_be16(dev->base_com_id);
if ((err = trusted_command(dev, (uint8_t*)&rq, sizeof(rq), IF_SEND, TCG_PROTOCOL_ID_2, dev->base_com_id))) {
LOG(ERROR, "Failed to send stack reset.\n");
return err;
}
do {
if ((err = trusted_command(dev, (uint8_t*)&resp, sizeof(resp), IF_RECV, TCG_PROTOCOL_ID_2, dev->base_com_id))) {
LOG(ERROR, "Failed to receive command: %i\n", err);
return err;
}
LOG(INFO, "Stack reset #%i response: ComID 0x%04x, Code 0x%x, Length 0x%x, Response 0x%x\n", i,
be16_to_cpu(resp.comid), be32_to_cpu(resp.request_code),
be16_to_cpu(resp.data_length), be32_to_cpu(resp.response));
if (resp.data_length == 0)
sleep(1);
} while (resp.data_length == 0 && ++i < 3);
return 0;
}
int setup_tper(struct disk_device *dev, const unsigned char *sid_pwd, size_t sid_pwd_len)
{
int err = 0;
unsigned char msid[2048] = { 0 };
size_t msid_len = 0;
// Get MSID.
if ((err = start_session(dev, ADMIN_SP_UID, ANYBODY_USER_ID, NULL, 0))) {
LOG(ERROR, "Failed to start Admin SP session as Anybody.\n");
return err;
}
if ((err = get_row_bytes(dev, TABLE_C_PIN_ROW_MSID_UID, TABLE_C_PIN_COLUMN_PIN, msid, sizeof(msid), &msid_len))) {
LOG(ERROR, "Failed to read MSID.\n");
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to close session.\n");
return err;
}
// Update SID password.
if ((err = start_session(dev, ADMIN_SP_UID, SID_USER_ID, msid, msid_len))) {
LOG(ERROR, "Failed to start Admin SP session as SID with MSID.\n");
return err;
}
unsigned char atom[1024] = { 0 };
size_t atom_len = 0;
medium_atom(atom, &atom_len, 1, 0, sid_pwd, sid_pwd_len);
if ((err = set_row(dev, TABLE_C_PIN_ROW_SID_UID, TABLE_C_PIN_COLUMN_PIN, atom, atom_len))) {
LOG(ERROR, "Failed to update SID password.\n");
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to close session.\n");
return err;
}
// Activate locking SP.
if ((err = start_session(dev, ADMIN_SP_UID, SID_USER_ID, sid_pwd, sid_pwd_len))) {
LOG(ERROR, "Failed to start Admin SP session as SID with updated password.\n");
return err;
}
/*
SPObjectUID.Activate[ ]
=>
[ ]
*/
unsigned char buffer[512] = { 0 };
size_t buffer_used = 0;
unsigned char response[512] = { 0 };
prepare_method(buffer, &buffer_used, dev, LOCKING_SP_UID, METHOD_ACTIVATE_UID);
finish_method(buffer, &buffer_used);
if ((err = invoke_method(dev, buffer, buffer_used, response, sizeof(response)))) {
LOG(ERROR, "Failed to activate Locking SP.\n");
close_session(dev);
return err;
}
if ((err = close_session(dev))) {
LOG(ERROR, "Failed to close session.\n");
return err;
}
return err;
}
int psid_revert(struct disk_device *dev, const unsigned char *psid, size_t psid_len)
{
int err = 0;
unsigned char buffer[1024] = { 0 };
size_t buffer_used = 0;
unsigned char response[1024] = { 0 };
LOG(INFO, "PSID revert\n");
if ((err = start_session(dev, ADMIN_SP_UID, PSID_USER_ID, psid, psid_len))) {
LOG(ERROR, "Cannot initialise session.\n");
return err;
}
/*
SPObjectUID.Revert[ ]
=>
[ ]
*/
prepare_method(buffer, &buffer_used, dev, ADMIN_SP_UID, METHOD_REVERT_UID);
finish_method(buffer, &buffer_used);
if ((err = invoke_method(dev, buffer, buffer_used, response, sizeof(response)))) {
LOG(ERROR, "Failed to revert the TPer.\n");
}
// The session is aborted automatically.
LOG(INFO, "------- ABORT SESSION -------\n\n");
return err;
}
#define RANDOM_REQUEST_SIZE 32 // Opal says that the minimal supported maximum is 32 bytes
int get_random(struct disk_device *dev, unsigned char *output, size_t output_len)
{
int err = 0;
unsigned char command[512] = { 0 };
size_t command_len = 0;
LOG(INFO, "Getting Random numbers:\n");
if ((err = start_session(dev, ADMIN_SP_UID, ANYBODY_USER_ID, NULL, 0))) {
LOG(ERROR, "Failed to initialise session.\n");
goto cleanup;
}
/*
ThisSP.Random[
Count : uinteger,
BufferOut = cell_block ]
=>
[ Result : bytes ]
*/
prepare_method(command, &command_len, dev, THISSP, METHOD_RANDOM_UID);
tiny_atom(command, &command_len, 0, RANDOM_REQUEST_SIZE); // Count
finish_method(command, &command_len);
for (size_t done = 0; done < output_len && !quit; done += RANDOM_REQUEST_SIZE) {
unsigned char response[512] = { 0 };
if ((err = invoke_method(dev, command, command_len, response, sizeof(response)))) {
LOG(ERROR, "Failed to get random data from the device.\n");
goto cleanup;
}
size_t offset = 0;
if ((err = skip_to_parameter(response, &offset, 0, 0))) {
LOG(ERROR, "Received unexpected token.\n");
goto cleanup;
}
unsigned char rng_buffer[RANDOM_REQUEST_SIZE] = { 0 };
size_t len = 0;
if ((err = parse_bytes(response, &offset, rng_buffer, sizeof(rng_buffer), &len))) {
LOG(ERROR, "Received unexpected token.\n");
goto cleanup;
}
size_t remaining = output_len - done;
memcpy(output, rng_buffer, remaining > RANDOM_REQUEST_SIZE ? RANDOM_REQUEST_SIZE : remaining);
output += RANDOM_REQUEST_SIZE;
}
cleanup:
if (quit && !err) {
err = -1;
LOG(ERROR, "Interrupted by a signal.\n");
}
close_session(dev);
return err;
}
int get_random_session(struct disk_device *dev, unsigned char *output, size_t output_len)
{
int err;
unsigned char rng_buffer[RANDOM_REQUEST_SIZE] = { 0 };
unsigned char response[512] = { 0 };
unsigned char command[512] = { 0 };
size_t command_len = 0, done, offset, len, remaining;
/*
ThisSP.Random[
Count : uinteger,
BufferOut = cell_block ]
=>
[ Result : bytes ]
*/
prepare_method(command, &command_len, dev, THISSP, METHOD_RANDOM_UID);
tiny_atom(command, &command_len, 0, RANDOM_REQUEST_SIZE); // Count
finish_method(command, &command_len);
for (done = 0; done < output_len && !quit; done += RANDOM_REQUEST_SIZE) {
if ((err = invoke_method(dev, command, command_len, response, sizeof(response)))) {
LOG(ERROR, "Failed to get random data from the device.\n");
return -1;
}
if ((err = skip_to_parameter(response, &offset, 0, 0))) {
LOG(ERROR, "Received unexpected token.\n");
return -1;
}
if ((err = parse_bytes(response, &offset, rng_buffer, sizeof(rng_buffer), &len))) {
LOG(ERROR, "Received unexpected token.\n");
return -1;
}
remaining = output_len - done;
memcpy(output, rng_buffer, remaining > RANDOM_REQUEST_SIZE ? RANDOM_REQUEST_SIZE : remaining);
output += RANDOM_REQUEST_SIZE;
}
if (quit) {
LOG(ERROR, "Interrupted by a signal.\n");
return -1;
}
return 0;
}
int regenerate_key(struct disk_device *dev, unsigned char locking_range, unsigned char *admin_pin,
size_t admin_pin_len)
{
int err = 0;
unsigned char locking_range_uid_str[8] = { 0 };
if (locking_range == 0) {
memcpy(locking_range_uid_str, LOCKING_RANGE_GLOBAL_UID, 8);
} else {
memcpy(locking_range_uid_str, LOCKING_RANGE_NNNN_UID, 8);
locking_range_uid_str[7] = locking_range;
}
if ((err = start_session(dev, LOCKING_SP_UID, ADMIN_BASE_ID + 1, admin_pin, admin_pin_len))) {
LOG(ERROR, "Failed to initialise session.\n");
goto cleanup;
}
unsigned char active_key_uid[128];
if ((err = get_row_bytes(dev, locking_range_uid_str, LOCKING_RANGE_COLUMN_ACTIVE_KEY, active_key_uid,
sizeof(active_key_uid), NULL))) {
LOG(ERROR, "Failed to get key type used in the locking range.\n");
goto cleanup;
}
unsigned char command[512] = { 0 };
size_t command_len = 0;
/*
CredentialObjectUID.GenKey [
PublicExponent = uinteger,
PinLength = uinteger ]
=>
[ ]
*/
prepare_method(command, &command_len, dev, active_key_uid, METHOD_GENKEY_UID);
finish_method(command, &command_len);
if ((err = invoke_method(dev, command, command_len, command, sizeof(command)))) {
LOG(ERROR, "Failed to re-generate the key.\n");
goto cleanup;
}
cleanup:
close_session(dev);
return err;
}