-
Notifications
You must be signed in to change notification settings - Fork 15
/
scsiencrypt.cpp
335 lines (295 loc) · 9.6 KB
/
scsiencrypt.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
329
330
331
332
333
334
335
/*
Header file to send and recieve SPIN/SPOUT commands to SCSI device
Original program copyright 2010 John D. Coleman
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <config.h>
#include <cerrno>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#if defined(OS_LINUX)
#include <scsi/scsi.h>
#include <scsi/sg.h>
#define SCSI_TIMEOUT 5000
#elif defined(OS_FREEBSD)
#include <camlib.h>
#include <cam/scsi/scsi_message.h>
#define SCSI_TIMEOUT 5000
#else
#error "OS type is not set"
#endif
#include "scsiencrypt.h"
constexpr std::uint8_t SSP_SPIN_OPCODE = 0xa2;
constexpr std::uint8_t SSP_SPOUT_OPCODE = 0xb5;
constexpr std::uint8_t SSP_SP_CMD_LEN = 12;
constexpr std::uint8_t SSP_SP_PROTOCOL_TDE = 0x20;
constexpr int RETRYCOUNT = 1;
#define BSINTTOCHAR(x) \
static_cast<std::uint8_t>((x) >> 24), \
static_cast<std::uint8_t>((x) >> 16), \
static_cast<std::uint8_t>((x) >> 8), \
static_cast<std::uint8_t>((x))
// generic_deleter permits the use of std::unique_ptr for RAII on non-pointer
// types like file descriptors.
template<typename T, T null_value, typename Deleter, Deleter d>
struct generic_deleter {
class pointer {
T t;
public:
pointer() : t {null_value} {}
pointer(T t) : t {t} {}
pointer(std::nullptr_t) : t {null_value} {}
explicit operator bool() const noexcept { return t != null_value; }
friend bool operator ==(pointer lhs, pointer rhs) noexcept { return lhs.t == rhs.t; }
friend bool operator !=(pointer lhs, pointer rhs) noexcept { return !(lhs == rhs); }
operator T() const noexcept { return t; }
};
void operator()(pointer p) const noexcept { d(p); }
};
using unique_fd = std::unique_ptr<int, generic_deleter<int, -1, decltype(&close), &close>>;
enum class scsi_direction { to_device, from_device };
static void scsi_execute(const std::string& device, const std::uint8_t *cmd_p,
std::size_t cmd_len, const std::uint8_t *dxfer_p,
std::size_t dxfer_len, scsi_direction direction)
{
#if defined(OS_LINUX)
unique_fd fd {open(device.c_str(), O_RDONLY)};
if (!fd) {
std::ostringstream oss;
oss << "Cannot open device " << device;
throw std::system_error {errno, std::generic_category(), oss.str()};
}
sg_io_hdr cmdio {};
auto sense_buf {std::make_unique<scsi::sense_buffer>()};
cmdio.cmd_len = cmd_len;
cmdio.dxfer_direction = (direction == scsi_direction::to_device)
? SG_DXFER_TO_DEV : SG_DXFER_FROM_DEV;
cmdio.dxfer_len = dxfer_len;
cmdio.dxferp = const_cast<unsigned char*>(dxfer_p);
cmdio.cmdp = const_cast<unsigned char*>(cmd_p);
cmdio.sbp = sense_buf->data();
cmdio.mx_sb_len = sizeof(decltype(sense_buf)::element_type);
cmdio.timeout = SCSI_TIMEOUT;
cmdio.interface_id = 'S';
if (ioctl(fd.get(), SG_IO, &cmdio)) {
throw std::system_error {errno, std::generic_category()};
}
if (cmdio.status) {
throw scsi::scsi_error {std::move(sense_buf)};
}
#elif defined(OS_FREEBSD)
auto dev = std::unique_ptr<struct cam_device, decltype(&cam_close_device)>
{cam_open_device(device.c_str(), O_RDWR), &cam_close_device};
if (dev == nullptr) {
std::ostringstream oss;
oss << "Cannot open device " << device << ": " << cam_errbuf;
throw std::runtime_error {oss.str()};
}
auto ccb = std::unique_ptr<union ccb, decltype(&cam_freeccb)>
{cam_getccb(dev.get()), &cam_freeccb};
if (ccb == nullptr) {
throw std::bad_alloc {};
}
CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
cam_fill_csio(&ccb->csio, RETRYCOUNT, nullptr,
CAM_PASS_ERR_RECOVER | CAM_CDB_POINTER |
(direction == scsi_direction::to_device ? CAM_DIR_OUT : CAM_DIR_IN),
MSG_SIMPLE_Q_TAG, const_cast<u_int8_t*>(dxfer_p),
dxfer_len, SSD_FULL_SIZE, cmd_len, SCSI_TIMEOUT);
ccb->csio.cdb_io.cdb_ptr = const_cast<u_int8_t*>(cmd_p);
if (cam_send_ccb(dev.get(), ccb.get())) {
throw std::system_error {errno, std::generic_category()};
}
if (ccb->csio.scsi_status) {
auto sense_buf {std::make_unique<scsi::sense_buffer>()};
std::memcpy(sense_buf->data(), &ccb->csio.sense_data, sizeof(scsi::sense_buffer));
throw scsi::scsi_error {std::move(sense_buf)};
}
#else
#error "OS type is not set"
#endif
}
namespace scsi {
void get_des(const std::string& device, const std::uint8_t *buffer,
std::size_t length)
{
const std::uint8_t spin_des_command[] {
SSP_SPIN_OPCODE,
SSP_SP_PROTOCOL_TDE,
0,
0X20,
0,
0,
BSINTTOCHAR(length),
0,
0,
};
scsi_execute(device, spin_des_command, sizeof(spin_des_command),
buffer, length, scsi_direction::from_device);
}
void get_nbes(const std::string& device, const std::uint8_t *buffer,
std::size_t length)
{
const std::uint8_t spin_nbes_command[] {
SSP_SPIN_OPCODE,
SSP_SP_PROTOCOL_TDE,
0,
0X21,
0,
0,
BSINTTOCHAR(length),
0,
0,
};
scsi_execute(device, spin_nbes_command, sizeof(spin_nbes_command),
buffer, length, scsi_direction::from_device);
}
void get_dec(const std::string& device, const std::uint8_t *buffer,
std::size_t length)
{
const uint8_t spin_dec_command[] {
SSP_SPIN_OPCODE,
SSP_SP_PROTOCOL_TDE,
0x00, 0x10,
0,
0,
BSINTTOCHAR(length),
0,
0,
};
scsi_execute(device, spin_dec_command, sizeof(spin_dec_command),
buffer, length, scsi_direction::from_device);
}
inquiry_data get_inquiry(const std::string& device)
{
const uint8_t scsi_inq_command[] {0x12, 0, 0, 0, sizeof(inquiry_data), 0};
inquiry_data inq;
scsi_execute(device, scsi_inq_command, sizeof(scsi_inq_command),
reinterpret_cast<const std::uint8_t*>(&inq), sizeof(inq),
scsi_direction::from_device);
return inq;
}
std::unique_ptr<const std::uint8_t[]> make_sde(encrypt_mode enc_mode,
decrypt_mode dec_mode,
std::uint8_t algorithm_index,
const std::vector<std::uint8_t> key,
const std::string& key_name,
sde_rdmc rdmc, bool ckod)
{
std::size_t length {sizeof(page_sde) + key.size()};
if (!key_name.empty()) {
length += sizeof(kad) + key_name.size();
}
auto buffer {std::make_unique<std::uint8_t[]>(length)};
auto& page {reinterpret_cast<page_sde&>(*buffer.get())};
page.page_code = htons(0x10);
page.length = htons(length - sizeof(page_header));
page.control = std::byte {2u} << page_sde::control_scope_pos; // all IT nexus = 10b
page.flags |= std::byte {DEFAULT_CEEM} << page_sde::flags_ceem_pos;
page.flags |= std::byte {rdmc};
if (ckod) {
page.flags |= page_sde::flags_ckod_mask;
}
page.encryption_mode = enc_mode;
page.decryption_mode = dec_mode;
page.algorithm_index = algorithm_index;
page.key_length = htons(key.size());
std::memcpy(page.key, key.data(), key.size());
if (!key_name.empty()) {
auto &ukad {reinterpret_cast<kad&>(*(buffer.get() + sizeof(page_sde) + key.size()))};
ukad.length = htons(key_name.size());
std::memcpy(ukad.descriptor, key_name.data(), key_name.size());
}
return buffer;
}
void write_sde(const std::string& device, const std::uint8_t *sde_buffer)
{
auto& page {reinterpret_cast<const page_sde&>(*sde_buffer)};
std::size_t length {sizeof(page_header) + ntohs(page.length)};
const uint8_t spout_sde_command[] {
SSP_SPOUT_OPCODE,
SSP_SP_PROTOCOL_TDE,
0,
0X10,
0,
0,
BSINTTOCHAR(length),
0,
0
};
scsi_execute(device, spout_sde_command, sizeof(spout_sde_command),
sde_buffer, length, scsi_direction::to_device);
}
void print_sense_data(std::ostream& os, const sense_data& sd) {
os << std::left << std::setw(25) << "Sense Code: ";
auto sense_key {static_cast<unsigned int>(sd.flags & sense_data::flags_sense_key_mask)};
switch (sense_key) {
case 0u:
os << "No specific error";
break;
case 2u:
os << "Device not ready";
break;
case 3u:
os << "Medium Error";
break;
case 4u:
os << "Hardware Error";
break;
case 5u:
os << "Illegal Request";
break;
case 6u:
os << "Unit Attention";
break;
case 7u:
os << "Data protect";
break;
case 8u:
os << "Blank tape";
break;
}
os << " (0x" << HEX(sense_key) << ")\n";
os << std::left << std::setw(25) << " ASC:"
<< "0x" << HEX(sd.additional_sense_code) << "\n";
os << std::left << std::setw(25) << " ASCQ:"
<< "0x" << HEX(sd.additional_sense_qualifier) << "\n";
if (sd.additional_sense_length > 0) {
os << std::left << std::setw(25) << " Additional data: " << "0x";
for (int i = 0; i < sd.additional_sense_length; i++) {
os << HEX(sd.additional_sense_bytes[i]);
}
os << "\n";
}
#ifdef DEBUGSCSI
os << std::left << std::setw(25) << " Raw Sense:"
<< "0x";
char *rawsense = (char *)&sd;
for (int i = 0; i < sense_data::maximum_size; i++) {
os << HEX(rawsense[i]);
}
os << "\n";
#endif
}
}