1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2005-2019 Pawel Jakub Dawidek <pawel@dawidek.net> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _G_ELI_H_ 32 #define _G_ELI_H_ 33 34 #include <sys/endian.h> 35 #include <sys/errno.h> 36 #include <sys/malloc.h> 37 #include <crypto/sha2/sha256.h> 38 #include <crypto/sha2/sha512.h> 39 #include <opencrypto/cryptodev.h> 40 #ifdef _KERNEL 41 #include <sys/bio.h> 42 #include <sys/libkern.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <geom/geom.h> 46 #include <crypto/intake.h> 47 #else 48 #include <assert.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <strings.h> 52 #endif 53 #include <sys/queue.h> 54 #include <sys/tree.h> 55 #ifndef _OpenSSL_ 56 #include <sys/md5.h> 57 #endif 58 59 #define G_ELI_CLASS_NAME "ELI" 60 #define G_ELI_MAGIC "GEOM::ELI" 61 #define G_ELI_SUFFIX ".eli" 62 63 /* 64 * Version history: 65 * 0 - Initial version number. 66 * 1 - Added data authentication support (md_aalgo field and 67 * G_ELI_FLAG_AUTH flag). 68 * 2 - Added G_ELI_FLAG_READONLY. 69 * 3 - Added 'configure' subcommand. 70 * 4 - IV is generated from offset converted to little-endian 71 * (the G_ELI_FLAG_NATIVE_BYTE_ORDER flag will be set for older versions). 72 * 5 - Added multiple encrypton keys and AES-XTS support. 73 * 6 - Fixed usage of multiple keys for authenticated providers (the 74 * G_ELI_FLAG_FIRST_KEY flag will be set for older versions). 75 * 7 - Encryption keys are now generated from the Data Key and not from the 76 * IV Key (the G_ELI_FLAG_ENC_IVKEY flag will be set for older versions). 77 */ 78 #define G_ELI_VERSION_00 0 79 #define G_ELI_VERSION_01 1 80 #define G_ELI_VERSION_02 2 81 #define G_ELI_VERSION_03 3 82 #define G_ELI_VERSION_04 4 83 #define G_ELI_VERSION_05 5 84 #define G_ELI_VERSION_06 6 85 #define G_ELI_VERSION_07 7 86 #define G_ELI_VERSION G_ELI_VERSION_07 87 88 /* ON DISK FLAGS. */ 89 /* Use random, onetime keys. */ 90 #define G_ELI_FLAG_ONETIME 0x00000001 91 /* Ask for the passphrase from the kernel, before mounting root. */ 92 #define G_ELI_FLAG_BOOT 0x00000002 93 /* Detach on last close, if we were open for writing. */ 94 #define G_ELI_FLAG_WO_DETACH 0x00000004 95 /* Detach on last close. */ 96 #define G_ELI_FLAG_RW_DETACH 0x00000008 97 /* Provide data authentication. */ 98 #define G_ELI_FLAG_AUTH 0x00000010 99 /* Provider is read-only, we should deny all write attempts. */ 100 #define G_ELI_FLAG_RO 0x00000020 101 /* Don't pass through BIO_DELETE requests. */ 102 #define G_ELI_FLAG_NODELETE 0x00000040 103 /* This GELI supports GELIBoot */ 104 #define G_ELI_FLAG_GELIBOOT 0x00000080 105 /* Hide passphrase length in GELIboot. */ 106 #define G_ELI_FLAG_GELIDISPLAYPASS 0x00000100 107 /* Expand provider automatically. */ 108 #define G_ELI_FLAG_AUTORESIZE 0x00000200 109 110 /* RUNTIME FLAGS. */ 111 /* Provider was open for writing. */ 112 #define G_ELI_FLAG_WOPEN 0x00010000 113 /* Destroy device. */ 114 #define G_ELI_FLAG_DESTROY 0x00020000 115 /* Provider uses native byte-order for IV generation. */ 116 #define G_ELI_FLAG_NATIVE_BYTE_ORDER 0x00040000 117 /* Provider uses single encryption key. */ 118 #define G_ELI_FLAG_SINGLE_KEY 0x00080000 119 /* Device suspended. */ 120 #define G_ELI_FLAG_SUSPEND 0x00100000 121 /* Provider uses first encryption key. */ 122 #define G_ELI_FLAG_FIRST_KEY 0x00200000 123 /* Provider uses IV-Key for encryption key generation. */ 124 #define G_ELI_FLAG_ENC_IVKEY 0x00400000 125 126 #define G_ELI_NEW_BIO 255 127 128 #define SHA512_MDLEN 64 129 #define G_ELI_AUTH_SECKEYLEN SHA256_DIGEST_LENGTH 130 131 #define G_ELI_MAXMKEYS 2 132 #define G_ELI_MAXKEYLEN 64 133 #define G_ELI_USERKEYLEN G_ELI_MAXKEYLEN 134 #define G_ELI_DATAKEYLEN G_ELI_MAXKEYLEN 135 #define G_ELI_AUTHKEYLEN G_ELI_MAXKEYLEN 136 #define G_ELI_IVKEYLEN G_ELI_MAXKEYLEN 137 #define G_ELI_SALTLEN 64 138 #define G_ELI_DATAIVKEYLEN (G_ELI_DATAKEYLEN + G_ELI_IVKEYLEN) 139 /* Data-Key, IV-Key, HMAC_SHA512(Derived-Key, Data-Key+IV-Key) */ 140 #define G_ELI_MKEYLEN (G_ELI_DATAIVKEYLEN + SHA512_MDLEN) 141 #define G_ELI_OVERWRITES 5 142 /* Switch data encryption key every 2^20 blocks. */ 143 #define G_ELI_KEY_SHIFT 20 144 145 #define G_ELI_CRYPTO_UNKNOWN 0 146 #define G_ELI_CRYPTO_HW 1 147 #define G_ELI_CRYPTO_SW 2 148 149 #ifdef _KERNEL 150 #if (MAX_KEY_BYTES < G_ELI_DATAIVKEYLEN) 151 #error "MAX_KEY_BYTES is less than G_ELI_DATAKEYLEN" 152 #endif 153 154 extern int g_eli_debug; 155 extern u_int g_eli_overwrites; 156 extern u_int g_eli_batch; 157 158 #define G_ELI_DEBUG(lvl, ...) \ 159 _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), NULL, __VA_ARGS__) 160 #define G_ELI_LOGREQ(lvl, bp, ...) \ 161 _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), (bp), __VA_ARGS__) 162 163 struct g_eli_worker { 164 struct g_eli_softc *w_softc; 165 struct proc *w_proc; 166 u_int w_number; 167 crypto_session_t w_sid; 168 boolean_t w_active; 169 LIST_ENTRY(g_eli_worker) w_next; 170 }; 171 172 #endif /* _KERNEL */ 173 174 struct g_eli_softc { 175 struct g_geom *sc_geom; 176 u_int sc_version; 177 u_int sc_crypto; 178 uint8_t sc_mkey[G_ELI_DATAIVKEYLEN]; 179 uint8_t sc_ekey[G_ELI_DATAKEYLEN]; 180 TAILQ_HEAD(, g_eli_key) sc_ekeys_queue; 181 RB_HEAD(g_eli_key_tree, g_eli_key) sc_ekeys_tree; 182 struct mtx sc_ekeys_lock; 183 uint64_t sc_ekeys_total; 184 uint64_t sc_ekeys_allocated; 185 u_int sc_ealgo; 186 u_int sc_ekeylen; 187 uint8_t sc_akey[G_ELI_AUTHKEYLEN]; 188 u_int sc_aalgo; 189 u_int sc_akeylen; 190 u_int sc_alen; 191 SHA256_CTX sc_akeyctx; 192 uint8_t sc_ivkey[G_ELI_IVKEYLEN]; 193 SHA256_CTX sc_ivctx; 194 int sc_nkey; 195 uint32_t sc_flags; 196 int sc_inflight; 197 off_t sc_mediasize; 198 size_t sc_sectorsize; 199 off_t sc_provsize; 200 u_int sc_bytes_per_sector; 201 u_int sc_data_per_sector; 202 #ifndef _KERNEL 203 int sc_cpubind; 204 #else /* _KERNEL */ 205 boolean_t sc_cpubind; 206 207 /* Only for software cryptography. */ 208 struct bio_queue_head sc_queue; 209 struct mtx sc_queue_mtx; 210 LIST_HEAD(, g_eli_worker) sc_workers; 211 #endif /* _KERNEL */ 212 }; 213 #define sc_name sc_geom->name 214 215 #define G_ELI_KEY_MAGIC 0xe11341c 216 217 struct g_eli_key { 218 /* Key value, must be first in the structure. */ 219 uint8_t gek_key[G_ELI_DATAKEYLEN]; 220 /* Magic. */ 221 int gek_magic; 222 /* Key number. */ 223 uint64_t gek_keyno; 224 /* Reference counter. */ 225 int gek_count; 226 /* Keeps keys sorted by most recent use. */ 227 TAILQ_ENTRY(g_eli_key) gek_next; 228 /* Keeps keys sorted by number. */ 229 RB_ENTRY(g_eli_key) gek_link; 230 }; 231 232 struct g_eli_metadata { 233 char md_magic[16]; /* Magic value. */ 234 uint32_t md_version; /* Version number. */ 235 uint32_t md_flags; /* Additional flags. */ 236 uint16_t md_ealgo; /* Encryption algorithm. */ 237 uint16_t md_keylen; /* Key length. */ 238 uint16_t md_aalgo; /* Authentication algorithm. */ 239 uint64_t md_provsize; /* Provider's size. */ 240 uint32_t md_sectorsize; /* Sector size. */ 241 uint8_t md_keys; /* Available keys. */ 242 int32_t md_iterations; /* Number of iterations for PKCS#5v2. */ 243 uint8_t md_salt[G_ELI_SALTLEN]; /* Salt. */ 244 /* Encrypted master key (IV-key, Data-key, HMAC). */ 245 uint8_t md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN]; 246 u_char md_hash[16]; /* MD5 hash. */ 247 } __packed; 248 #ifndef _OpenSSL_ 249 static __inline void 250 eli_metadata_encode_v0(struct g_eli_metadata *md, u_char **datap) 251 { 252 u_char *p; 253 254 p = *datap; 255 le32enc(p, md->md_flags); p += sizeof(md->md_flags); 256 le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo); 257 le16enc(p, md->md_keylen); p += sizeof(md->md_keylen); 258 le64enc(p, md->md_provsize); p += sizeof(md->md_provsize); 259 le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize); 260 *p = md->md_keys; p += sizeof(md->md_keys); 261 le32enc(p, md->md_iterations); p += sizeof(md->md_iterations); 262 bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt); 263 bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 264 *datap = p; 265 } 266 static __inline void 267 eli_metadata_encode_v1v2v3v4v5v6v7(struct g_eli_metadata *md, u_char **datap) 268 { 269 u_char *p; 270 271 p = *datap; 272 le32enc(p, md->md_flags); p += sizeof(md->md_flags); 273 le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo); 274 le16enc(p, md->md_keylen); p += sizeof(md->md_keylen); 275 le16enc(p, md->md_aalgo); p += sizeof(md->md_aalgo); 276 le64enc(p, md->md_provsize); p += sizeof(md->md_provsize); 277 le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize); 278 *p = md->md_keys; p += sizeof(md->md_keys); 279 le32enc(p, md->md_iterations); p += sizeof(md->md_iterations); 280 bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt); 281 bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 282 *datap = p; 283 } 284 static __inline void 285 eli_metadata_encode(struct g_eli_metadata *md, u_char *data) 286 { 287 uint32_t hash[4]; 288 MD5_CTX ctx; 289 u_char *p; 290 291 p = data; 292 bcopy(md->md_magic, p, sizeof(md->md_magic)); 293 p += sizeof(md->md_magic); 294 le32enc(p, md->md_version); 295 p += sizeof(md->md_version); 296 switch (md->md_version) { 297 case G_ELI_VERSION_00: 298 eli_metadata_encode_v0(md, &p); 299 break; 300 case G_ELI_VERSION_01: 301 case G_ELI_VERSION_02: 302 case G_ELI_VERSION_03: 303 case G_ELI_VERSION_04: 304 case G_ELI_VERSION_05: 305 case G_ELI_VERSION_06: 306 case G_ELI_VERSION_07: 307 eli_metadata_encode_v1v2v3v4v5v6v7(md, &p); 308 break; 309 default: 310 #ifdef _KERNEL 311 panic("%s: Unsupported version %u.", __func__, 312 (u_int)md->md_version); 313 #else 314 assert(!"Unsupported metadata version."); 315 #endif 316 } 317 MD5Init(&ctx); 318 MD5Update(&ctx, data, p - data); 319 MD5Final((void *)hash, &ctx); 320 bcopy(hash, md->md_hash, sizeof(md->md_hash)); 321 bcopy(md->md_hash, p, sizeof(md->md_hash)); 322 } 323 static __inline int 324 eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md) 325 { 326 uint32_t hash[4]; 327 MD5_CTX ctx; 328 const u_char *p; 329 330 p = data + sizeof(md->md_magic) + sizeof(md->md_version); 331 md->md_flags = le32dec(p); p += sizeof(md->md_flags); 332 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo); 333 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen); 334 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize); 335 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize); 336 md->md_keys = *p; p += sizeof(md->md_keys); 337 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations); 338 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt); 339 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 340 MD5Init(&ctx); 341 MD5Update(&ctx, data, p - data); 342 MD5Final((void *)hash, &ctx); 343 bcopy(hash, md->md_hash, sizeof(md->md_hash)); 344 if (bcmp(md->md_hash, p, 16) != 0) 345 return (EINVAL); 346 return (0); 347 } 348 349 static __inline int 350 eli_metadata_decode_v1v2v3v4v5v6v7(const u_char *data, struct g_eli_metadata *md) 351 { 352 uint32_t hash[4]; 353 MD5_CTX ctx; 354 const u_char *p; 355 356 p = data + sizeof(md->md_magic) + sizeof(md->md_version); 357 md->md_flags = le32dec(p); p += sizeof(md->md_flags); 358 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo); 359 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen); 360 md->md_aalgo = le16dec(p); p += sizeof(md->md_aalgo); 361 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize); 362 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize); 363 md->md_keys = *p; p += sizeof(md->md_keys); 364 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations); 365 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt); 366 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 367 MD5Init(&ctx); 368 MD5Update(&ctx, data, p - data); 369 MD5Final((void *)hash, &ctx); 370 bcopy(hash, md->md_hash, sizeof(md->md_hash)); 371 if (bcmp(md->md_hash, p, 16) != 0) 372 return (EINVAL); 373 return (0); 374 } 375 static __inline int 376 eli_metadata_decode(const u_char *data, struct g_eli_metadata *md) 377 { 378 int error; 379 380 bcopy(data, md->md_magic, sizeof(md->md_magic)); 381 if (strcmp(md->md_magic, G_ELI_MAGIC) != 0) 382 return (EINVAL); 383 md->md_version = le32dec(data + sizeof(md->md_magic)); 384 switch (md->md_version) { 385 case G_ELI_VERSION_00: 386 error = eli_metadata_decode_v0(data, md); 387 break; 388 case G_ELI_VERSION_01: 389 case G_ELI_VERSION_02: 390 case G_ELI_VERSION_03: 391 case G_ELI_VERSION_04: 392 case G_ELI_VERSION_05: 393 case G_ELI_VERSION_06: 394 case G_ELI_VERSION_07: 395 error = eli_metadata_decode_v1v2v3v4v5v6v7(data, md); 396 break; 397 default: 398 error = EOPNOTSUPP; 399 break; 400 } 401 return (error); 402 } 403 #endif /* !_OpenSSL */ 404 405 static __inline u_int 406 g_eli_str2ealgo(const char *name) 407 { 408 409 if (strcasecmp("null", name) == 0) 410 return (CRYPTO_NULL_CBC); 411 else if (strcasecmp("null-cbc", name) == 0) 412 return (CRYPTO_NULL_CBC); 413 else if (strcasecmp("aes", name) == 0) 414 return (CRYPTO_AES_XTS); 415 else if (strcasecmp("aes-cbc", name) == 0) 416 return (CRYPTO_AES_CBC); 417 else if (strcasecmp("aes-xts", name) == 0) 418 return (CRYPTO_AES_XTS); 419 else if (strcasecmp("blowfish", name) == 0) 420 return (CRYPTO_BLF_CBC); 421 else if (strcasecmp("blowfish-cbc", name) == 0) 422 return (CRYPTO_BLF_CBC); 423 else if (strcasecmp("camellia", name) == 0) 424 return (CRYPTO_CAMELLIA_CBC); 425 else if (strcasecmp("camellia-cbc", name) == 0) 426 return (CRYPTO_CAMELLIA_CBC); 427 else if (strcasecmp("3des", name) == 0) 428 return (CRYPTO_3DES_CBC); 429 else if (strcasecmp("3des-cbc", name) == 0) 430 return (CRYPTO_3DES_CBC); 431 return (CRYPTO_ALGORITHM_MIN - 1); 432 } 433 434 static __inline u_int 435 g_eli_str2aalgo(const char *name) 436 { 437 438 if (strcasecmp("hmac/md5", name) == 0) 439 return (CRYPTO_MD5_HMAC); 440 else if (strcasecmp("hmac/sha1", name) == 0) 441 return (CRYPTO_SHA1_HMAC); 442 else if (strcasecmp("hmac/ripemd160", name) == 0) 443 return (CRYPTO_RIPEMD160_HMAC); 444 else if (strcasecmp("hmac/sha256", name) == 0) 445 return (CRYPTO_SHA2_256_HMAC); 446 else if (strcasecmp("hmac/sha384", name) == 0) 447 return (CRYPTO_SHA2_384_HMAC); 448 else if (strcasecmp("hmac/sha512", name) == 0) 449 return (CRYPTO_SHA2_512_HMAC); 450 return (CRYPTO_ALGORITHM_MIN - 1); 451 } 452 453 static __inline const char * 454 g_eli_algo2str(u_int algo) 455 { 456 457 switch (algo) { 458 case CRYPTO_NULL_CBC: 459 return ("NULL"); 460 case CRYPTO_AES_CBC: 461 return ("AES-CBC"); 462 case CRYPTO_AES_XTS: 463 return ("AES-XTS"); 464 case CRYPTO_BLF_CBC: 465 return ("Blowfish-CBC"); 466 case CRYPTO_CAMELLIA_CBC: 467 return ("CAMELLIA-CBC"); 468 case CRYPTO_3DES_CBC: 469 return ("3DES-CBC"); 470 case CRYPTO_MD5_HMAC: 471 return ("HMAC/MD5"); 472 case CRYPTO_SHA1_HMAC: 473 return ("HMAC/SHA1"); 474 case CRYPTO_RIPEMD160_HMAC: 475 return ("HMAC/RIPEMD160"); 476 case CRYPTO_SHA2_256_HMAC: 477 return ("HMAC/SHA256"); 478 case CRYPTO_SHA2_384_HMAC: 479 return ("HMAC/SHA384"); 480 case CRYPTO_SHA2_512_HMAC: 481 return ("HMAC/SHA512"); 482 } 483 return ("unknown"); 484 } 485 486 static __inline void 487 eli_metadata_dump(const struct g_eli_metadata *md) 488 { 489 static const char hex[] = "0123456789abcdef"; 490 char str[sizeof(md->md_mkeys) * 2 + 1]; 491 u_int i; 492 493 printf(" magic: %s\n", md->md_magic); 494 printf(" version: %u\n", (u_int)md->md_version); 495 printf(" flags: 0x%x\n", (u_int)md->md_flags); 496 printf(" ealgo: %s\n", g_eli_algo2str(md->md_ealgo)); 497 printf(" keylen: %u\n", (u_int)md->md_keylen); 498 if (md->md_flags & G_ELI_FLAG_AUTH) 499 printf(" aalgo: %s\n", g_eli_algo2str(md->md_aalgo)); 500 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize); 501 printf("sectorsize: %u\n", (u_int)md->md_sectorsize); 502 printf(" keys: 0x%02x\n", (u_int)md->md_keys); 503 printf("iterations: %d\n", (int)md->md_iterations); 504 bzero(str, sizeof(str)); 505 for (i = 0; i < sizeof(md->md_salt); i++) { 506 str[i * 2] = hex[md->md_salt[i] >> 4]; 507 str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f]; 508 } 509 printf(" Salt: %s\n", str); 510 bzero(str, sizeof(str)); 511 for (i = 0; i < sizeof(md->md_mkeys); i++) { 512 str[i * 2] = hex[md->md_mkeys[i] >> 4]; 513 str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f]; 514 } 515 printf("Master Key: %s\n", str); 516 bzero(str, sizeof(str)); 517 for (i = 0; i < 16; i++) { 518 str[i * 2] = hex[md->md_hash[i] >> 4]; 519 str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 520 } 521 printf(" MD5 hash: %s\n", str); 522 } 523 524 static __inline u_int 525 g_eli_keylen(u_int algo, u_int keylen) 526 { 527 528 switch (algo) { 529 case CRYPTO_NULL_CBC: 530 if (keylen == 0) 531 keylen = 64 * 8; 532 else { 533 if (keylen > 64 * 8) 534 keylen = 0; 535 } 536 return (keylen); 537 case CRYPTO_AES_CBC: 538 case CRYPTO_CAMELLIA_CBC: 539 switch (keylen) { 540 case 0: 541 return (128); 542 case 128: 543 case 192: 544 case 256: 545 return (keylen); 546 default: 547 return (0); 548 } 549 case CRYPTO_AES_XTS: 550 switch (keylen) { 551 case 0: 552 return (128); 553 case 128: 554 case 256: 555 return (keylen); 556 default: 557 return (0); 558 } 559 case CRYPTO_BLF_CBC: 560 if (keylen == 0) 561 return (128); 562 if (keylen < 128 || keylen > 448) 563 return (0); 564 if ((keylen % 32) != 0) 565 return (0); 566 return (keylen); 567 case CRYPTO_3DES_CBC: 568 if (keylen == 0 || keylen == 192) 569 return (192); 570 return (0); 571 default: 572 return (0); 573 } 574 } 575 576 static __inline u_int 577 g_eli_hashlen(u_int algo) 578 { 579 580 switch (algo) { 581 case CRYPTO_MD5_HMAC: 582 return (16); 583 case CRYPTO_SHA1_HMAC: 584 return (20); 585 case CRYPTO_RIPEMD160_HMAC: 586 return (20); 587 case CRYPTO_SHA2_256_HMAC: 588 return (32); 589 case CRYPTO_SHA2_384_HMAC: 590 return (48); 591 case CRYPTO_SHA2_512_HMAC: 592 return (64); 593 } 594 return (0); 595 } 596 597 static __inline off_t 598 eli_mediasize(const struct g_eli_softc *sc, off_t mediasize, u_int sectorsize) 599 { 600 601 if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { 602 mediasize -= sectorsize; 603 } 604 if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) { 605 mediasize -= (mediasize % sc->sc_sectorsize); 606 } else { 607 mediasize /= sc->sc_bytes_per_sector; 608 mediasize *= sc->sc_sectorsize; 609 } 610 611 return (mediasize); 612 } 613 614 static __inline void 615 eli_metadata_softc(struct g_eli_softc *sc, const struct g_eli_metadata *md, 616 u_int sectorsize, off_t mediasize) 617 { 618 619 sc->sc_version = md->md_version; 620 sc->sc_inflight = 0; 621 sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN; 622 sc->sc_flags = md->md_flags; 623 /* Backward compatibility. */ 624 if (md->md_version < G_ELI_VERSION_04) 625 sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER; 626 if (md->md_version < G_ELI_VERSION_05) 627 sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY; 628 if (md->md_version < G_ELI_VERSION_06 && 629 (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) { 630 sc->sc_flags |= G_ELI_FLAG_FIRST_KEY; 631 } 632 if (md->md_version < G_ELI_VERSION_07) 633 sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY; 634 sc->sc_ealgo = md->md_ealgo; 635 636 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 637 sc->sc_akeylen = sizeof(sc->sc_akey) * 8; 638 sc->sc_aalgo = md->md_aalgo; 639 sc->sc_alen = g_eli_hashlen(sc->sc_aalgo); 640 641 sc->sc_data_per_sector = sectorsize - sc->sc_alen; 642 /* 643 * Some hash functions (like SHA1 and RIPEMD160) generates hash 644 * which length is not multiple of 128 bits, but we want data 645 * length to be multiple of 128, so we can encrypt without 646 * padding. The line below rounds down data length to multiple 647 * of 128 bits. 648 */ 649 sc->sc_data_per_sector -= sc->sc_data_per_sector % 16; 650 651 sc->sc_bytes_per_sector = 652 (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1; 653 sc->sc_bytes_per_sector *= sectorsize; 654 } 655 sc->sc_provsize = mediasize; 656 sc->sc_sectorsize = md->md_sectorsize; 657 sc->sc_mediasize = eli_mediasize(sc, mediasize, sectorsize); 658 sc->sc_ekeylen = md->md_keylen; 659 } 660 661 #ifdef _KERNEL 662 int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 663 struct g_eli_metadata *md); 664 struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp, 665 struct g_provider *bpp, const struct g_eli_metadata *md, 666 const u_char *mkey, int nkey); 667 int g_eli_destroy(struct g_eli_softc *sc, boolean_t force); 668 669 int g_eli_access(struct g_provider *pp, int dr, int dw, int de); 670 void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb); 671 672 void g_eli_read_done(struct bio *bp); 673 void g_eli_write_done(struct bio *bp); 674 int g_eli_crypto_rerun(struct cryptop *crp); 675 676 void g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker); 677 void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp); 678 679 void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp); 680 void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp); 681 #endif 682 void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 683 size_t size); 684 685 void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key); 686 int g_eli_mkey_decrypt(const struct g_eli_metadata *md, 687 const unsigned char *key, unsigned char *mkey, unsigned keyp); 688 int g_eli_mkey_decrypt_any(const struct g_eli_metadata *md, 689 const unsigned char *key, unsigned char *mkey, unsigned *nkeyp); 690 int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen, 691 unsigned char *mkey); 692 #ifdef _KERNEL 693 void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey); 694 #endif 695 696 int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize, 697 const u_char *key, size_t keysize); 698 int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize, 699 const u_char *key, size_t keysize); 700 701 struct hmac_ctx { 702 SHA512_CTX innerctx; 703 SHA512_CTX outerctx; 704 }; 705 706 void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const char *hkey, 707 size_t hkeylen); 708 void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data, 709 size_t datasize); 710 void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize); 711 void g_eli_crypto_hmac(const char *hkey, size_t hkeysize, 712 const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize); 713 714 void g_eli_key_fill(struct g_eli_softc *sc, struct g_eli_key *key, 715 uint64_t keyno); 716 #ifdef _KERNEL 717 void g_eli_key_init(struct g_eli_softc *sc); 718 void g_eli_key_destroy(struct g_eli_softc *sc); 719 void g_eli_key_resize(struct g_eli_softc *sc); 720 uint8_t *g_eli_key_hold(struct g_eli_softc *sc, off_t offset, size_t blocksize); 721 void g_eli_key_drop(struct g_eli_softc *sc, uint8_t *rawkey); 722 #endif 723 #endif /* !_G_ELI_H_ */ 724