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 #define G_ELI_CRYPTO_SW_ACCEL 3 149 150 #ifdef _KERNEL 151 #if (MAX_KEY_BYTES < G_ELI_DATAIVKEYLEN) 152 #error "MAX_KEY_BYTES is less than G_ELI_DATAKEYLEN" 153 #endif 154 155 extern int g_eli_debug; 156 extern u_int g_eli_overwrites; 157 extern u_int g_eli_batch; 158 159 #define G_ELI_DEBUG(lvl, ...) \ 160 _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), NULL, __VA_ARGS__) 161 #define G_ELI_LOGREQ(lvl, bp, ...) \ 162 _GEOM_DEBUG("GEOM_ELI", g_eli_debug, (lvl), (bp), __VA_ARGS__) 163 164 struct g_eli_worker { 165 struct g_eli_softc *w_softc; 166 struct proc *w_proc; 167 void *w_first_key; 168 u_int w_number; 169 crypto_session_t w_sid; 170 boolean_t w_active; 171 LIST_ENTRY(g_eli_worker) w_next; 172 }; 173 174 #endif /* _KERNEL */ 175 176 struct g_eli_softc { 177 struct g_geom *sc_geom; 178 u_int sc_version; 179 u_int sc_crypto; 180 uint8_t sc_mkey[G_ELI_DATAIVKEYLEN]; 181 uint8_t sc_ekey[G_ELI_DATAKEYLEN]; 182 TAILQ_HEAD(, g_eli_key) sc_ekeys_queue; 183 RB_HEAD(g_eli_key_tree, g_eli_key) sc_ekeys_tree; 184 #ifndef _STANDALONE 185 struct mtx sc_ekeys_lock; 186 #endif 187 uint64_t sc_ekeys_total; 188 uint64_t sc_ekeys_allocated; 189 u_int sc_ealgo; 190 u_int sc_ekeylen; 191 uint8_t sc_akey[G_ELI_AUTHKEYLEN]; 192 u_int sc_aalgo; 193 u_int sc_akeylen; 194 u_int sc_alen; 195 SHA256_CTX sc_akeyctx; 196 uint8_t sc_ivkey[G_ELI_IVKEYLEN]; 197 SHA256_CTX sc_ivctx; 198 int sc_nkey; 199 uint32_t sc_flags; 200 int sc_inflight; 201 off_t sc_mediasize; 202 size_t sc_sectorsize; 203 off_t sc_provsize; 204 u_int sc_bytes_per_sector; 205 u_int sc_data_per_sector; 206 #ifndef _KERNEL 207 int sc_cpubind; 208 #else /* _KERNEL */ 209 boolean_t sc_cpubind; 210 211 /* Only for software cryptography. */ 212 struct bio_queue_head sc_queue; 213 struct mtx sc_queue_mtx; 214 LIST_HEAD(, g_eli_worker) sc_workers; 215 #endif /* _KERNEL */ 216 }; 217 #define sc_name sc_geom->name 218 219 #define G_ELI_KEY_MAGIC 0xe11341c 220 221 struct g_eli_key { 222 /* Key value, must be first in the structure. */ 223 uint8_t gek_key[G_ELI_DATAKEYLEN]; 224 /* Magic. */ 225 int gek_magic; 226 /* Key number. */ 227 uint64_t gek_keyno; 228 /* Reference counter. */ 229 int gek_count; 230 /* Keeps keys sorted by most recent use. */ 231 TAILQ_ENTRY(g_eli_key) gek_next; 232 /* Keeps keys sorted by number. */ 233 RB_ENTRY(g_eli_key) gek_link; 234 }; 235 236 struct g_eli_metadata { 237 char md_magic[16]; /* Magic value. */ 238 uint32_t md_version; /* Version number. */ 239 uint32_t md_flags; /* Additional flags. */ 240 uint16_t md_ealgo; /* Encryption algorithm. */ 241 uint16_t md_keylen; /* Key length. */ 242 uint16_t md_aalgo; /* Authentication algorithm. */ 243 uint64_t md_provsize; /* Provider's size. */ 244 uint32_t md_sectorsize; /* Sector size. */ 245 uint8_t md_keys; /* Available keys. */ 246 int32_t md_iterations; /* Number of iterations for PKCS#5v2. */ 247 uint8_t md_salt[G_ELI_SALTLEN]; /* Salt. */ 248 /* Encrypted master key (IV-key, Data-key, HMAC). */ 249 uint8_t md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN]; 250 u_char md_hash[16]; /* MD5 hash. */ 251 } __packed; 252 #ifndef _OpenSSL_ 253 static __inline void 254 eli_metadata_encode_v0(struct g_eli_metadata *md, u_char **datap) 255 { 256 u_char *p; 257 258 p = *datap; 259 le32enc(p, md->md_flags); p += sizeof(md->md_flags); 260 le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo); 261 le16enc(p, md->md_keylen); p += sizeof(md->md_keylen); 262 le64enc(p, md->md_provsize); p += sizeof(md->md_provsize); 263 le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize); 264 *p = md->md_keys; p += sizeof(md->md_keys); 265 le32enc(p, md->md_iterations); p += sizeof(md->md_iterations); 266 bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt); 267 bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 268 *datap = p; 269 } 270 static __inline void 271 eli_metadata_encode_v1v2v3v4v5v6v7(struct g_eli_metadata *md, u_char **datap) 272 { 273 u_char *p; 274 275 p = *datap; 276 le32enc(p, md->md_flags); p += sizeof(md->md_flags); 277 le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo); 278 le16enc(p, md->md_keylen); p += sizeof(md->md_keylen); 279 le16enc(p, md->md_aalgo); p += sizeof(md->md_aalgo); 280 le64enc(p, md->md_provsize); p += sizeof(md->md_provsize); 281 le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize); 282 *p = md->md_keys; p += sizeof(md->md_keys); 283 le32enc(p, md->md_iterations); p += sizeof(md->md_iterations); 284 bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt); 285 bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 286 *datap = p; 287 } 288 static __inline void 289 eli_metadata_encode(struct g_eli_metadata *md, u_char *data) 290 { 291 uint32_t hash[4]; 292 MD5_CTX ctx; 293 u_char *p; 294 295 p = data; 296 bcopy(md->md_magic, p, sizeof(md->md_magic)); 297 p += sizeof(md->md_magic); 298 le32enc(p, md->md_version); 299 p += sizeof(md->md_version); 300 switch (md->md_version) { 301 case G_ELI_VERSION_00: 302 eli_metadata_encode_v0(md, &p); 303 break; 304 case G_ELI_VERSION_01: 305 case G_ELI_VERSION_02: 306 case G_ELI_VERSION_03: 307 case G_ELI_VERSION_04: 308 case G_ELI_VERSION_05: 309 case G_ELI_VERSION_06: 310 case G_ELI_VERSION_07: 311 eli_metadata_encode_v1v2v3v4v5v6v7(md, &p); 312 break; 313 default: 314 #ifdef _KERNEL 315 panic("%s: Unsupported version %u.", __func__, 316 (u_int)md->md_version); 317 #else 318 assert(!"Unsupported metadata version."); 319 #endif 320 } 321 MD5Init(&ctx); 322 MD5Update(&ctx, data, p - data); 323 MD5Final((void *)hash, &ctx); 324 bcopy(hash, md->md_hash, sizeof(md->md_hash)); 325 bcopy(md->md_hash, p, sizeof(md->md_hash)); 326 } 327 static __inline int 328 eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md) 329 { 330 uint32_t hash[4]; 331 MD5_CTX ctx; 332 const u_char *p; 333 334 p = data + sizeof(md->md_magic) + sizeof(md->md_version); 335 md->md_flags = le32dec(p); p += sizeof(md->md_flags); 336 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo); 337 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen); 338 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize); 339 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize); 340 md->md_keys = *p; p += sizeof(md->md_keys); 341 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations); 342 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt); 343 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 344 MD5Init(&ctx); 345 MD5Update(&ctx, data, p - data); 346 MD5Final((void *)hash, &ctx); 347 bcopy(hash, md->md_hash, sizeof(md->md_hash)); 348 if (bcmp(md->md_hash, p, 16) != 0) 349 return (EINVAL); 350 return (0); 351 } 352 353 static __inline int 354 eli_metadata_decode_v1v2v3v4v5v6v7(const u_char *data, struct g_eli_metadata *md) 355 { 356 uint32_t hash[4]; 357 MD5_CTX ctx; 358 const u_char *p; 359 360 p = data + sizeof(md->md_magic) + sizeof(md->md_version); 361 md->md_flags = le32dec(p); p += sizeof(md->md_flags); 362 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo); 363 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen); 364 md->md_aalgo = le16dec(p); p += sizeof(md->md_aalgo); 365 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize); 366 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize); 367 md->md_keys = *p; p += sizeof(md->md_keys); 368 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations); 369 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt); 370 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys); 371 MD5Init(&ctx); 372 MD5Update(&ctx, data, p - data); 373 MD5Final((void *)hash, &ctx); 374 bcopy(hash, md->md_hash, sizeof(md->md_hash)); 375 if (bcmp(md->md_hash, p, 16) != 0) 376 return (EINVAL); 377 return (0); 378 } 379 static __inline int 380 eli_metadata_decode(const u_char *data, struct g_eli_metadata *md) 381 { 382 int error; 383 384 bcopy(data, md->md_magic, sizeof(md->md_magic)); 385 if (strcmp(md->md_magic, G_ELI_MAGIC) != 0) 386 return (EINVAL); 387 md->md_version = le32dec(data + sizeof(md->md_magic)); 388 switch (md->md_version) { 389 case G_ELI_VERSION_00: 390 error = eli_metadata_decode_v0(data, md); 391 break; 392 case G_ELI_VERSION_01: 393 case G_ELI_VERSION_02: 394 case G_ELI_VERSION_03: 395 case G_ELI_VERSION_04: 396 case G_ELI_VERSION_05: 397 case G_ELI_VERSION_06: 398 case G_ELI_VERSION_07: 399 error = eli_metadata_decode_v1v2v3v4v5v6v7(data, md); 400 break; 401 default: 402 error = EOPNOTSUPP; 403 break; 404 } 405 return (error); 406 } 407 #endif /* !_OpenSSL */ 408 409 static __inline u_int 410 g_eli_str2ealgo(const char *name) 411 { 412 413 if (strcasecmp("null", name) == 0) 414 return (CRYPTO_NULL_CBC); 415 else if (strcasecmp("null-cbc", name) == 0) 416 return (CRYPTO_NULL_CBC); 417 else if (strcasecmp("aes", name) == 0) 418 return (CRYPTO_AES_XTS); 419 else if (strcasecmp("aes-cbc", name) == 0) 420 return (CRYPTO_AES_CBC); 421 else if (strcasecmp("aes-xts", name) == 0) 422 return (CRYPTO_AES_XTS); 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 return (CRYPTO_ALGORITHM_MIN - 1); 428 } 429 430 static __inline u_int 431 g_eli_str2aalgo(const char *name) 432 { 433 434 if (strcasecmp("hmac/sha1", name) == 0) 435 return (CRYPTO_SHA1_HMAC); 436 else if (strcasecmp("hmac/ripemd160", name) == 0) 437 return (CRYPTO_RIPEMD160_HMAC); 438 else if (strcasecmp("hmac/sha256", name) == 0) 439 return (CRYPTO_SHA2_256_HMAC); 440 else if (strcasecmp("hmac/sha384", name) == 0) 441 return (CRYPTO_SHA2_384_HMAC); 442 else if (strcasecmp("hmac/sha512", name) == 0) 443 return (CRYPTO_SHA2_512_HMAC); 444 return (CRYPTO_ALGORITHM_MIN - 1); 445 } 446 447 static __inline const char * 448 g_eli_algo2str(u_int algo) 449 { 450 451 switch (algo) { 452 case CRYPTO_NULL_CBC: 453 return ("NULL"); 454 case CRYPTO_AES_CBC: 455 return ("AES-CBC"); 456 case CRYPTO_AES_XTS: 457 return ("AES-XTS"); 458 case CRYPTO_CAMELLIA_CBC: 459 return ("CAMELLIA-CBC"); 460 case CRYPTO_SHA1_HMAC: 461 return ("HMAC/SHA1"); 462 case CRYPTO_RIPEMD160_HMAC: 463 return ("HMAC/RIPEMD160"); 464 case CRYPTO_SHA2_256_HMAC: 465 return ("HMAC/SHA256"); 466 case CRYPTO_SHA2_384_HMAC: 467 return ("HMAC/SHA384"); 468 case CRYPTO_SHA2_512_HMAC: 469 return ("HMAC/SHA512"); 470 } 471 return ("unknown"); 472 } 473 474 static __inline void 475 eli_metadata_dump(const struct g_eli_metadata *md) 476 { 477 static const char hex[] = "0123456789abcdef"; 478 char str[sizeof(md->md_mkeys) * 2 + 1]; 479 u_int i; 480 481 printf(" magic: %s\n", md->md_magic); 482 printf(" version: %u\n", (u_int)md->md_version); 483 printf(" flags: 0x%x\n", (u_int)md->md_flags); 484 printf(" ealgo: %s\n", g_eli_algo2str(md->md_ealgo)); 485 printf(" keylen: %u\n", (u_int)md->md_keylen); 486 if (md->md_flags & G_ELI_FLAG_AUTH) 487 printf(" aalgo: %s\n", g_eli_algo2str(md->md_aalgo)); 488 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize); 489 printf("sectorsize: %u\n", (u_int)md->md_sectorsize); 490 printf(" keys: 0x%02x\n", (u_int)md->md_keys); 491 printf("iterations: %d\n", (int)md->md_iterations); 492 bzero(str, sizeof(str)); 493 for (i = 0; i < sizeof(md->md_salt); i++) { 494 str[i * 2] = hex[md->md_salt[i] >> 4]; 495 str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f]; 496 } 497 printf(" Salt: %s\n", str); 498 bzero(str, sizeof(str)); 499 for (i = 0; i < sizeof(md->md_mkeys); i++) { 500 str[i * 2] = hex[md->md_mkeys[i] >> 4]; 501 str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f]; 502 } 503 printf("Master Key: %s\n", str); 504 bzero(str, sizeof(str)); 505 for (i = 0; i < 16; i++) { 506 str[i * 2] = hex[md->md_hash[i] >> 4]; 507 str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f]; 508 } 509 printf(" MD5 hash: %s\n", str); 510 } 511 512 #ifdef _KERNEL 513 static __inline bool 514 eli_metadata_crypto_supported(const struct g_eli_metadata *md) 515 { 516 517 switch (md->md_ealgo) { 518 case CRYPTO_NULL_CBC: 519 case CRYPTO_AES_CBC: 520 case CRYPTO_CAMELLIA_CBC: 521 case CRYPTO_AES_XTS: 522 break; 523 default: 524 return (false); 525 } 526 if (md->md_flags & G_ELI_FLAG_AUTH) { 527 switch (md->md_aalgo) { 528 case CRYPTO_SHA1_HMAC: 529 case CRYPTO_RIPEMD160_HMAC: 530 case CRYPTO_SHA2_256_HMAC: 531 case CRYPTO_SHA2_384_HMAC: 532 case CRYPTO_SHA2_512_HMAC: 533 break; 534 default: 535 return (false); 536 } 537 } 538 return (true); 539 } 540 #endif 541 542 static __inline u_int 543 g_eli_keylen(u_int algo, u_int keylen) 544 { 545 546 switch (algo) { 547 case CRYPTO_NULL_CBC: 548 if (keylen == 0) 549 keylen = 64 * 8; 550 else { 551 if (keylen > 64 * 8) 552 keylen = 0; 553 } 554 return (keylen); 555 case CRYPTO_AES_CBC: 556 case CRYPTO_CAMELLIA_CBC: 557 switch (keylen) { 558 case 0: 559 return (128); 560 case 128: 561 case 192: 562 case 256: 563 return (keylen); 564 default: 565 return (0); 566 } 567 case CRYPTO_AES_XTS: 568 switch (keylen) { 569 case 0: 570 return (128); 571 case 128: 572 case 256: 573 return (keylen); 574 default: 575 return (0); 576 } 577 default: 578 return (0); 579 } 580 } 581 582 static __inline u_int 583 g_eli_ivlen(u_int algo) 584 { 585 586 switch (algo) { 587 case CRYPTO_AES_XTS: 588 return (AES_XTS_IV_LEN); 589 case CRYPTO_AES_CBC: 590 return (AES_BLOCK_LEN); 591 case CRYPTO_CAMELLIA_CBC: 592 return (CAMELLIA_BLOCK_LEN); 593 } 594 return (0); 595 } 596 597 static __inline u_int 598 g_eli_hashlen(u_int algo) 599 { 600 601 switch (algo) { 602 case CRYPTO_SHA1_HMAC: 603 return (20); 604 case CRYPTO_RIPEMD160_HMAC: 605 return (20); 606 case CRYPTO_SHA2_256_HMAC: 607 return (32); 608 case CRYPTO_SHA2_384_HMAC: 609 return (48); 610 case CRYPTO_SHA2_512_HMAC: 611 return (64); 612 } 613 return (0); 614 } 615 616 static __inline off_t 617 eli_mediasize(const struct g_eli_softc *sc, off_t mediasize, u_int sectorsize) 618 { 619 620 if ((sc->sc_flags & G_ELI_FLAG_ONETIME) == 0) { 621 mediasize -= sectorsize; 622 } 623 if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0) { 624 mediasize -= (mediasize % sc->sc_sectorsize); 625 } else { 626 mediasize /= sc->sc_bytes_per_sector; 627 mediasize *= sc->sc_sectorsize; 628 } 629 630 return (mediasize); 631 } 632 633 static __inline void 634 eli_metadata_softc(struct g_eli_softc *sc, const struct g_eli_metadata *md, 635 u_int sectorsize, off_t mediasize) 636 { 637 638 sc->sc_version = md->md_version; 639 sc->sc_inflight = 0; 640 sc->sc_crypto = G_ELI_CRYPTO_UNKNOWN; 641 sc->sc_flags = md->md_flags; 642 /* Backward compatibility. */ 643 if (md->md_version < G_ELI_VERSION_04) 644 sc->sc_flags |= G_ELI_FLAG_NATIVE_BYTE_ORDER; 645 if (md->md_version < G_ELI_VERSION_05) 646 sc->sc_flags |= G_ELI_FLAG_SINGLE_KEY; 647 if (md->md_version < G_ELI_VERSION_06 && 648 (sc->sc_flags & G_ELI_FLAG_AUTH) != 0) { 649 sc->sc_flags |= G_ELI_FLAG_FIRST_KEY; 650 } 651 if (md->md_version < G_ELI_VERSION_07) 652 sc->sc_flags |= G_ELI_FLAG_ENC_IVKEY; 653 sc->sc_ealgo = md->md_ealgo; 654 655 if (sc->sc_flags & G_ELI_FLAG_AUTH) { 656 sc->sc_akeylen = sizeof(sc->sc_akey) * 8; 657 sc->sc_aalgo = md->md_aalgo; 658 sc->sc_alen = g_eli_hashlen(sc->sc_aalgo); 659 660 sc->sc_data_per_sector = sectorsize - sc->sc_alen; 661 /* 662 * Some hash functions (like SHA1 and RIPEMD160) generates hash 663 * which length is not multiple of 128 bits, but we want data 664 * length to be multiple of 128, so we can encrypt without 665 * padding. The line below rounds down data length to multiple 666 * of 128 bits. 667 */ 668 sc->sc_data_per_sector -= sc->sc_data_per_sector % 16; 669 670 sc->sc_bytes_per_sector = 671 (md->md_sectorsize - 1) / sc->sc_data_per_sector + 1; 672 sc->sc_bytes_per_sector *= sectorsize; 673 } 674 sc->sc_provsize = mediasize; 675 sc->sc_sectorsize = md->md_sectorsize; 676 sc->sc_mediasize = eli_mediasize(sc, mediasize, sectorsize); 677 sc->sc_ekeylen = md->md_keylen; 678 } 679 680 #ifdef _KERNEL 681 int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp, 682 struct g_eli_metadata *md); 683 struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp, 684 struct g_provider *bpp, const struct g_eli_metadata *md, 685 const u_char *mkey, int nkey); 686 int g_eli_destroy(struct g_eli_softc *sc, boolean_t force); 687 688 int g_eli_access(struct g_provider *pp, int dr, int dw, int de); 689 void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb); 690 691 void g_eli_read_done(struct bio *bp); 692 void g_eli_write_done(struct bio *bp); 693 int g_eli_crypto_rerun(struct cryptop *crp); 694 695 void g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker); 696 void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp); 697 698 void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp); 699 void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp); 700 #endif 701 void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv, 702 size_t size); 703 704 void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key); 705 int g_eli_mkey_decrypt(const struct g_eli_metadata *md, 706 const unsigned char *key, unsigned char *mkey, unsigned keyp); 707 int g_eli_mkey_decrypt_any(const struct g_eli_metadata *md, 708 const unsigned char *key, unsigned char *mkey, unsigned *nkeyp); 709 int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen, 710 unsigned char *mkey); 711 #ifdef _KERNEL 712 void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey); 713 #endif 714 715 int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize, 716 const u_char *key, size_t keysize); 717 int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize, 718 const u_char *key, size_t keysize); 719 720 struct hmac_ctx { 721 SHA512_CTX innerctx; 722 SHA512_CTX outerctx; 723 }; 724 725 void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const char *hkey, 726 size_t hkeylen); 727 void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data, 728 size_t datasize); 729 void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize); 730 void g_eli_crypto_hmac(const char *hkey, size_t hkeysize, 731 const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize); 732 733 void g_eli_key_fill(struct g_eli_softc *sc, struct g_eli_key *key, 734 uint64_t keyno); 735 #ifdef _KERNEL 736 void g_eli_key_init(struct g_eli_softc *sc); 737 void g_eli_key_destroy(struct g_eli_softc *sc); 738 void g_eli_key_resize(struct g_eli_softc *sc); 739 uint8_t *g_eli_key_hold(struct g_eli_softc *sc, off_t offset, size_t blocksize); 740 void g_eli_key_drop(struct g_eli_softc *sc, uint8_t *rawkey); 741 #endif 742 #endif /* !_G_ELI_H_ */ 743