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