1 /* 2 BAREOS® - Backup Archiving REcovery Open Sourced 3 4 Copyright (C) 2011-2012 Planets Communications B.V. 5 Copyright (C) 2013-2013 Bareos GmbH & Co. KG 6 7 This program is Free Software; you can redistribute it and/or 8 modify it under the terms of version three of the GNU Affero General Public 9 License as published by the Free Software Foundation and included 10 in the file LICENSE. 11 12 This program is distributed in the hope that it will be useful, but 13 WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Affero General Public License for more details. 16 17 You should have received a copy of the GNU Affero General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20 02110-1301, USA. 21 */ 22 23 /* 24 * Marco van Wieringen, March 2012 25 */ 26 27 #ifndef BAREOS_LIB_SCSI_CRYPTO_H_ 28 #define BAREOS_LIB_SCSI_CRYPTO_H_ 1 29 30 /* clang-format off */ 31 32 /* 33 * Include the SCSI Low Level Interface functions and definitions. 34 */ 35 #include "scsi_lli.h" 36 37 #define SPP_SP_PROTOCOL_TDE 0x20 38 39 #define SPP_KEY_LENGTH 0x20 /* 32 bytes */ 40 #define SPP_DESCRIPTOR_LENGTH 1024 41 #define SPP_PAGE_DES_LENGTH 24 42 #define SPP_PAGE_NBES_LENGTH 16 43 #define SPP_KAD_HEAD_LENGTH 4 44 #define SPP_PAGE_ALLOCATION 8192 45 #define SPP_UKAD_LENGTH 0x1e 46 47 /* 48 * SCSI CDB opcodes 49 */ 50 enum { 51 SCSI_SPIN_OPCODE = 0xa2, 52 SCSI_SPOUT_OPCODE = 0xb5 53 }; 54 55 /* 56 * SCSI SPIN pagecodes. 57 */ 58 enum { 59 SPIN_TAPE_DATA_ENCR_IN_SUP_PAGE = 0x00, /* Tape Data Encryption In Support page */ 60 SPIN_TAPE_DATE_ENCR_OUT_SUP_PAGE = 0x01, /* Tape Data Encryption Out Support page */ 61 SPIN_DATA_ENCR_CAP_PAGE = 0x10, /* Data Encryption Capabilities page */ 62 SPIN_SUP_KEY_FORMATS_PAGE = 0x11, /* Supported Key Formats page */ 63 SPIN_DATA_ENCR_MGMT_CAP_PAGE = 0x12, /* Data Encryption Management Capabilities page */ 64 SPIN_DATA_ENCR_STATUS_PAGE = 0x20, /* Data Encryption Status page */ 65 SPIN_NEXT_BLOCK_ENCR_STATUS_PAGE = 0x21, /* Next Block Encryption Status page */ 66 SPIN_RANDOM_NUM_PAGE = 0x30, /* Random Number page */ 67 SPIN_DEV_SVR_KEY_WRAP_PUB_KEY_PAGE = 0x31 /* Device Server Key Wrapping Public Key page */ 68 }; 69 70 /* 71 * SCSI SPOUT pagecodes. 72 */ 73 enum { 74 SPOUT_SET_DATA_ENCRYPTION_PAGE = 0x10, /* Set Data Encryption page */ 75 SPOUT_SA_ENCAP_PAGE = 0x11 /* SA Encapsulation page */ 76 }; 77 78 /* 79 * SPP SCSI Control Descriptor Block 80 */ 81 typedef struct { 82 uint8_t opcode; /* Operation Code See SCSI_*_OPCODE */ 83 uint8_t scp; /* Security Protocol */ 84 uint8_t scp_specific[2]; /* Security Protocol Specific, 2 bytes MSB/LSB */ 85 uint8_t res_bits_1[2]; /* Reserved, 2 bytes */ 86 uint8_t allocation_length[4]; /* Allocation Length, 4 bytes, 2 bytes MSB and 2 bytes LSB */ 87 uint8_t res_bits_2; /* Reserved, 1 byte */ 88 uint8_t control_byte; /* Control Byte */ 89 } SPP_SCSI_CDB; 90 91 /* 92 * Generic SPP Page Buffer 93 */ 94 typedef struct { 95 uint8_t pageCode[2]; 96 uint8_t length[2]; 97 uint8_t buffer[SPP_PAGE_ALLOCATION]; 98 } SPP_PAGE_BUFFER; 99 100 /* 101 * Nexus Scopes 102 */ 103 enum { 104 SPP_NEXUS_SC_PUBLIC = 0, /* All fields other than the scope field and LOCK bit shall be ignored. 105 The I_T nexus shall use data encryption parameters that are shared 106 by other I_T nexuses. If no I_T nexuses are sharing data encryption 107 parameters, the device server shall use default data encryption parameters. */ 108 SPP_NEXUS_SC_LOCAL = 1, /* The data encryption parameters are unique to the I_T nexus associated 109 with the SECURITY PROTOCOL OUT command and shall not be shared 110 with other I_T nexuses.*/ 111 SPP_NEXUS_SC_ALL_I_T_NEXUS = 2 /* The data encryption parameters shall be shared with all I_T nexuses. */ 112 }; 113 114 /* 115 * Check External Encryption Mode 116 */ 117 enum { 118 SPP_CEEM_VENDOR_SPECIFIC = 0, /* Vendor specific */ 119 SPP_CEEM_NO_ENCR_CHECK = 1, /* Do not check the encryption mode that was in use when the block 120 was written to the medium.*/ 121 SPP_CEEM_CHECK_EXTERNAL = 2, /* On read and verify commands, check the encryption mode that 122 was in use when the block was written to the medium. Report an 123 error if the block was written in EXTERNAL mode */ 124 SPP_CEEM_CHECK_ENCR = 3 /* On read and verify commands, check the encryption mode that 125 was in use when the block was written to the medium. Report 126 an error if the block was written in ENCRYPT mode */ 127 }; 128 129 /* 130 * Raw Decryption Mode Control 131 */ 132 enum { 133 SPP_RDMC_DEFAULT = 0, /* The device server shall mark each encrypted block per the default 134 setting for the algorithm */ 135 SPP_RDMC_UNPROTECT = 2, /* The device server shall mark each encrypted block written to the 136 medium in a format specific manner as enabled for raw decryption 137 mode operations. */ 138 SPP_RDMC_PROTECT = 3 /* The device server shall mark each encrypted block written to the 139 medium in a format specific manner as disabled for raw 140 decryption mode operations. */ 141 }; 142 143 /* 144 * Encryption Modes. 145 */ 146 enum { 147 SPP_ENCR_MODE_DISABLE = 0, /* Data encryption is disabled. */ 148 SPP_ENCR_MODE_EXTERNAL = 1, /* The data associated with the WRITE(6) and WRITE(16) commands has been 149 encrypted by a system that is compatible with the algorithm specified 150 by the ALGORITHM INDEX field. */ 151 SPP_ENCR_MODE_ENCRYPT = 2 /* The device server shall encrypt all data that it receives for a 152 WRITE(6) or WRITE(16) command using the algorithm specified in the 153 ALGORITHM INDEX field and the key specified in the KEY field. */ 154 }; 155 156 /* 157 * Decryption Modes. 158 */ 159 enum { 160 SPP_DECR_MODE_DISABLE = 0, /* Data decryption is disabled. If the device server encounters an 161 encrypted logical block while reading, it shall not allow access 162 to the data. */ 163 SPP_DECR_MODE_RAW = 1, /* Data decryption is disabled. If the device server encounters an 164 encrypted logical block while reading, it shall pass the encrypted 165 block to the host without decrypting it. The encrypted block 166 may contain data that is not user data. */ 167 SPP_DECR_MODE_DECRYPT = 2, /* The device server shall decrypt all data that is read from the medium 168 when processing a READ(6), READ(16), READ REVERSE(6), READ REVERSE(16), 169 or RECOVER BUFFERED DATA command or verified when processing a 170 VERIFY(6) or VERIFY(16) command. The data shall be decrypted 171 using the algorithm specified in the ALGORITHM INDEX field and 172 the key specified in the KEY field */ 173 SPP_DECR_MODE_MIXED = 3 /* The device server shall decrypt all data that is read from the 174 medium that the device server determines was encrypted when processing 175 a READ(6), READ(16), READ REVERSE(6), READ REVERSE(16), or 176 RECOVER BUFFERED DATA command or verified when processing a 177 VERIFY(6) or VERIFY(16) command. The data shall be decrypted 178 using the algorithm specified in the ALGORITHM INDEX 179 field and the key specified in the KEY field. If the device 180 server encounters unencrypted data when processing a READ(6), 181 READ(16), READ REVERSE(6), READ REVERSE(16), RECOVER BUFFERED DATA, 182 VERIFY(6), or VERIFY(16) command, the data shall be processed 183 without decrypting */ 184 }; 185 186 /* 187 * Key Format Types. 188 */ 189 enum { 190 SPP_KAD_KEY_FORMAT_NORMAL = 0, /* The KEY field contains the key to be used to encrypt or decrypt data. */ 191 SPP_KAD_KEY_FORMAT_REFERENCE = 1, /* The KEY field contains a vendor-specific key reference. */ 192 SPP_KAD_KEY_FORMAT_WRAPPED = 2, /* The KEY field contains the key wrapped by the device server public key. */ 193 SPP_KAD_KEY_FORMAT_ESP_SCSI = 3 /* The KEY field contains a key that is encrypted using ESP-SCSI. */ 194 }; 195 196 197 /* 198 * Key Descriptor Types 199 */ 200 enum { 201 SPP_KAD_KEY_DESC_UKAD = 0, /* Unauthenticated key-associated data */ 202 SPP_KAD_KEY_DESC_AKAD = 1, /* Authenticated key-associated data */ 203 SPP_KAD_KEY_DESC_NONCE = 2, /* Nonce value */ 204 SPP_KAD_KEY_DESC_META = 3 /* Metadata key-associated data */ 205 }; 206 207 /* 208 * SPOUT Page Set Data Encryption (0x10) 209 */ 210 typedef struct { 211 uint8_t pageCode[2]; /* Page Code, 2 bytes MSB/LSB */ 212 uint8_t length[2]; /* Page Length, 2 bytes MSB/LSB */ 213 #if HAVE_BIG_ENDIAN 214 uint8_t nexusScope:3; /* Scope, See SPP_NEXUS_SC_* */ 215 uint8_t res_bits_1:4; /* Reserved, 4 bits */ 216 uint8_t lock:1; /* Lock bit */ 217 uint8_t CEEM:2; /* Check External Encryption Mode, See SPP_CEEM_* */ 218 uint8_t RDMC:2; /* Raw Decryption Mode Control, See SPP_RDMC_* */ 219 uint8_t SDK:1; /* Supplemental Decryption Key */ 220 uint8_t CKOD:1; /* Clear Key On Demount */ 221 uint8_t CKORP:1; /* Clear Key On Reservation Preempt */ 222 uint8_t CKORL:1; /* Clear Key On Reservation Lost */ 223 #else 224 uint8_t lock:1; /* Lock bit */ 225 uint8_t res_bits_1:4; /* Reserved, 4 bits */ 226 uint8_t nexusScope:3; /* Scope, See SPP_NEXUS_SC_* */ 227 uint8_t CKORL:1; /* Clear Key On Reservation Lost */ 228 uint8_t CKORP:1; /* Clear Key On Reservation Preempt */ 229 uint8_t CKOD:1; /* Clear Key On Demount */ 230 uint8_t SDK:1; /* Supplemental Decryption Key */ 231 uint8_t RDMC:2; /* Raw Decryption Mode Control, See SPP_RDMC_* */ 232 uint8_t CEEM:2; /* Check External Encryption Mode, See SPP_CEEM_* */ 233 #endif 234 uint8_t encryptionMode; /* Encryption Mode, See SPP_ENCR_MODE_* */ 235 uint8_t decryptionMode; /* Decryption Mode, See SPP_DECR_MODE_* */ 236 uint8_t algorithmIndex; /* Algorithm Index */ 237 uint8_t keyFormat; /* Logical Block Encryption Key Format */ 238 uint8_t kadFormat; /* KAD Format, See SPP_KAD_KEY_FORMAT_* */ 239 uint8_t res_bits_2[7]; /* Reserved, 7 bytes */ 240 uint8_t keyLength[2]; /* Logical Block Encryption Key Length, 2 bytes MSB/LSB */ 241 uint8_t keyData[SPP_KEY_LENGTH]; 242 } SPP_PAGE_SDE; 243 244 enum { 245 SPP_PARM_LOG_BLOCK_ENCR_NONE = 0, /* Logical block encryption parameters control is not reported. */ 246 SPP_PARM_LOG_BLOCK_ENCR_AME = 1, /* Logical Block Encryption Parameters are not exclusively 247 controlled by external data encryption control. */ 248 SPP_PARM_LOG_BLOCK_ENCR_DRIVE = 2, /* Logical block encryption parameters are exclusively 249 controlled by the sequential-access device server. */ 250 SPP_PARM_LOG_BLOCK_LME_ADC = 3, /* Logical block encryption parameters are exclusively 251 controlled by the automation/drive interface device server. */ 252 SPP_PARM_LOG_BLOCK_UNSUP = 4 /* Not supported. */ 253 }; 254 255 /* 256 * Device Encryption Status Page (0x20) 257 */ 258 typedef struct { 259 uint8_t pageCode[2]; /* Page Code, 2 bytes MSB/LSB */ 260 uint8_t length[2]; /* Page Length, 2 bytes MSB/LSB */ 261 #if HAVE_BIG_ENDIAN 262 uint8_t nexusScope:3; /* Scope, See SPP_NEXUS_SC_* */ 263 uint8_t res_bits_1:2; /* Reserved, 2 bits */ 264 uint8_t keyScope:3; /* Logical Block Encryption Scope */ 265 #else 266 uint8_t keyScope:3; /* Logical Block Encryption Scope */ 267 uint8_t res_bits_1:2; /* Reserved, 2 bits */ 268 uint8_t nexusScope:3; /* Scope, See SPP_NEXUS_SC_* */ 269 #endif 270 uint8_t encryptionMode; /* Encryption Mode, See SPP_ENCR_MODE_* */ 271 uint8_t decryptionMode; /* Decryption Mode, See SPP_DECR_MODE_* */ 272 uint8_t algorithmIndex; /* Algorithm Index */ 273 uint8_t keyInstance[4]; /* Key Instance Counter MSB/LSB */ 274 #if HAVE_BIG_ENDIAN 275 uint8_t res_bits_2:1; /* Reserved, 1 bit */ 276 uint8_t parametersControl:3; /* Logical Block encryption parameters, See SPP_PARM_LOG_BLOCK_* */ 277 uint8_t VCELB:1; /* Volume Contains Encrypted Logical Blocks */ 278 uint8_t CEEMS:2; /* Check External Encryption Mode Status, See SPP_CEEM_* */ 279 uint8_t RDMD:1; /* Raw Decryption Mode Disabled */ 280 #else 281 uint8_t RDMD:1; /* Raw Decryption Mode Disabled */ 282 uint8_t CEEMS:2; /* Check External Encryption Mode Status, See SPP_CEEM_* */ 283 uint8_t VCELB:1; /* Volume Contains Encrypted Logical Blocks */ 284 uint8_t parametersControl:3; /* Logical Block encryption parameters, See SPP_PARM_LOG_BLOCK_* */ 285 uint8_t res_bits_2:1; /* Reserved, 1 bit */ 286 #endif 287 uint8_t kadFormat; /* KAD Format, See SPP_KAD_KEY_FORMAT_* */ 288 uint8_t ASDKCount[2]; /* Available Supplemental Decryption Key MSB/LSB */ 289 uint8_t res_bits_4[8]; /* Reserved, 8 bytes */ 290 } SPP_PAGE_DES; 291 292 enum { 293 SPP_COMP_STATUS_UNKNOWN = 0, /* The device server is incapable of determining if the logical 294 object referenced by the LOGICAL OBJECT NUMBER field has been 295 compressed. */ 296 SPP_COMP_STATUS_UNAVAIL = 1, /* The device server is capable of determining if the logical 297 object referenced by the LOGICAL OBJECT NUMBER field has 298 been compressed, but is not able to at this time. 299 Possible reasons are: 300 a) the next logical block has not yet been read into the buffer; 301 b) there was an error reading the next logical block; or 302 c) there are no more logical blocks (i.e., end-of-data). */ 303 SPP_COMP_STATUS_ILLEGAL = 2, /* The device server has determined that the logical object referenced 304 by the LOGICAL OBJECT NUMBER field is not a logical block. */ 305 SPP_COMP_STATUS_UNCOMPRESSED = 3, /* The device server has determined that the logical object referenced 306 by the LOGICAL OBJECT NUMBER field is not compressed. */ 307 SPP_COMP_STATUS_COMPRESSED = 4 /* The device server has determined that the logical object referenced 308 by the LOGICAL OBJECT NUMBER field is compressed. */ 309 }; 310 311 enum { 312 SPP_ENCR_STATUS_UNKNOWN = 0, /* The device server is incapable of determining if the logical object 313 referenced by the LOGICAL OBJECT NUMBER field has been encrypted. */ 314 SPP_ENCR_STATUS_UNAVAIL = 1, /* The device server is capable of determining if the logical object 315 referenced by the LOGICAL OBJECT NUMBER field has been encrypted, 316 but is not able to at this time. Possible reasons are: 317 a) the next logical block has not yet been read into the buffer; 318 b) there was an error reading the next logical block; or 319 c) there are no more logical blocks (i.e., end-of-data). */ 320 SPP_ENCR_STATUS_ILLEGAL = 2, /* The device server has determined that the logical object referenced 321 by the LOGICAL OBJECT NUMBER field is not a logical block. */ 322 SPP_ENCR_STATUS_NOT_ENCRYPTED = 3, /* The device server has determined that the logical object referenced 323 by the LOGICAL OBJECT NUMBER field is not encrypted. */ 324 SPP_ENCR_STATUS_ENCR_ALG_NOT_SUPP = 4, /* The device server has determined that the logical object referenced by 325 the LOGICAL OBJECT NUMBER field is encrypted by an algorithm that is 326 not supported by this device server. The values in the KEY-ASSOCIATED 327 DATA DESCRIPTORS field contain information pertaining to the encrypted block. */ 328 SPP_ENCR_STATUS_ENCRYPTED = 5, /* The device server has determined that the logical object referenced by 329 the LOGICAL OBJECT NUMBER field is encrypted by an algorithm that is 330 supported by this device server. The values in the ALGORITHM INDEX and 331 KEY-ASSOCIATED DATA DESCRIPTORS fields contain information pertaining 332 to the encrypted block. */ 333 SPP_ENCR_STATUS_ENCR_NOT_AVAIL = 6 /* The device server has determined that the logical object referenced by 334 the LOGICAL OBJECT NUMBER field is encrypted by an algorithm that is 335 supported by this device server, but the device server is either not 336 enabled to decrypt or does not have the correct key or nonce value to 337 decrypt the encrypted block. */ 338 }; 339 340 /* 341 * Next Block Encryption Status Page (0x21) 342 */ 343 typedef struct { 344 uint8_t pageCode[2]; /* Page Code, 2 bytes MSB/LSB */ 345 uint8_t length[2]; /* Page Length, 2 bytes MSB/LSB */ 346 uint8_t log_obj_num[8]; /* Logical Object Number */ 347 #if HAVE_BIG_ENDIAN 348 uint8_t compressionStatus:4; /* Compression Status, See SPP_COMPRESS_STATUS_* */ 349 uint8_t encryptionStatus:4; /* Encryption Status, See SPP_ENCR_STATUS_* */ 350 #else 351 uint8_t encryptionStatus:4; /* Encryption Status, See SPP_ENCR_STATUS_* */ 352 uint8_t compressionStatus:4; /* Compression Status, See SPP_COMPRESS_STATUS_* */ 353 #endif 354 uint8_t algorithmIndex; /* Algorithm Index */ 355 #if HAVE_BIG_ENDIAN 356 uint8_t res_bits_1:6; /* Reserved, 6 bits */ 357 uint8_t EMES:1; /* Encryption Mode External Status */ 358 uint8_t RDMDS:1; /* Raw Decryption Mode Disabled Status */ 359 #else 360 uint8_t RDMDS:1; /* Raw Decryption Mode Disabled Status */ 361 uint8_t EMES:1; /* Encryption Mode External Status */ 362 uint8_t res_bits_1:6; /* Reserved, 6 bits */ 363 #endif 364 uint8_t nextBlockKADFormat; /* Next Block KAD Format, See SPP_KAD_KEY_FORMAT_* */ 365 } SPP_PAGE_NBES; 366 367 /* 368 * Key Associated Data (KAD) Descriptors 369 */ 370 typedef struct { 371 uint8_t type; /* Key Descriptor Type, See SPP_KAD_KEY_DESC_* */ 372 #if HAVE_BIG_ENDIAN 373 uint8_t res_bits_1:5; /* Reserved, 5 bits */ 374 uint8_t authenticated:3; 375 #else 376 uint8_t authenticated:3; 377 uint8_t res_bits_1:5; /* Reserved, 5 bits */ 378 #endif 379 uint8_t descriptorLength[2]; /* Key Descriptor Length MSB/LSB */ 380 uint8_t descriptor[SPP_DESCRIPTOR_LENGTH]; 381 } SPP_KAD; 382 383 /* clang-format on */ 384 385 bool ClearScsiEncryptionKey(int fd, const char* device); 386 bool SetScsiEncryptionKey(int fd, const char* device, char* encryption_key); 387 int GetScsiDriveEncryptionStatus(int fd, 388 const char* device_name, 389 POOLMEM*& status, 390 int indent); 391 int GetScsiVolumeEncryptionStatus(int fd, 392 const char* device_name, 393 POOLMEM*& status, 394 int indent); 395 bool NeedScsiCryptoKey(int fd, const char* device_name, bool use_drive_status); 396 bool IsScsiEncryptionEnabled(int fd, const char* device_name); 397 398 #endif /* BAREOS_LIB_SCSI_CRYPTO_H_ */ 399