2005-07-27 21:43:37 +00:00
|
|
|
/*-
|
2011-04-21 13:31:43 +00:00
|
|
|
* Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
|
2005-07-27 21:43:37 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2006-02-01 12:06:01 +00:00
|
|
|
*
|
2005-07-27 21:43:37 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _G_ELI_H_
|
|
|
|
#define _G_ELI_H_
|
|
|
|
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <crypto/sha2/sha2.h>
|
|
|
|
#include <opencrypto/cryptodev.h>
|
|
|
|
#ifdef _KERNEL
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/libkern.h>
|
2011-04-21 13:31:43 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/tree.h>
|
2005-07-27 21:43:37 +00:00
|
|
|
#include <geom/geom.h>
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2005-08-08 19:40:38 +00:00
|
|
|
#include <strings.h>
|
2005-07-27 21:43:37 +00:00
|
|
|
#endif
|
|
|
|
#ifndef _OpenSSL_
|
|
|
|
#include <sys/md5.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define G_ELI_CLASS_NAME "ELI"
|
|
|
|
#define G_ELI_MAGIC "GEOM::ELI"
|
|
|
|
#define G_ELI_SUFFIX ".eli"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version history:
|
|
|
|
* 0 - Initial version number.
|
2006-06-05 21:38:54 +00:00
|
|
|
* 1 - Added data authentication support (md_aalgo field and
|
|
|
|
* G_ELI_FLAG_AUTH flag).
|
2006-08-11 18:39:58 +00:00
|
|
|
* 2 - Added G_ELI_FLAG_READONLY.
|
2006-09-16 10:43:17 +00:00
|
|
|
* 3 - Added 'configure' subcommand.
|
2010-08-28 08:30:20 +00:00
|
|
|
* 4 - IV is generated from offset converted to little-endian
|
2011-05-08 09:17:56 +00:00
|
|
|
* (the G_ELI_FLAG_NATIVE_BYTE_ORDER flag will be set for older versions).
|
2010-09-23 11:58:36 +00:00
|
|
|
* 5 - Added multiple encrypton keys and AES-XTS support.
|
2011-05-08 09:17:56 +00:00
|
|
|
* 6 - Fixed usage of multiple keys for authenticated providers (the
|
|
|
|
* G_ELI_FLAG_FIRST_KEY flag will be set for older versions).
|
2005-07-27 21:43:37 +00:00
|
|
|
*/
|
2011-05-08 09:17:56 +00:00
|
|
|
#define G_ELI_VERSION_00 0
|
|
|
|
#define G_ELI_VERSION_01 1
|
|
|
|
#define G_ELI_VERSION_02 2
|
|
|
|
#define G_ELI_VERSION_03 3
|
|
|
|
#define G_ELI_VERSION_04 4
|
|
|
|
#define G_ELI_VERSION_05 5
|
|
|
|
#define G_ELI_VERSION_06 6
|
|
|
|
#define G_ELI_VERSION G_ELI_VERSION_06
|
2005-07-27 21:43:37 +00:00
|
|
|
|
2006-08-09 18:11:14 +00:00
|
|
|
/* ON DISK FLAGS. */
|
2005-07-27 21:43:37 +00:00
|
|
|
/* Use random, onetime keys. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_ONETIME 0x00000001
|
2005-07-27 21:43:37 +00:00
|
|
|
/* Ask for the passphrase from the kernel, before mounting root. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_BOOT 0x00000002
|
2005-07-27 21:43:37 +00:00
|
|
|
/* Detach on last close, if we were open for writing. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_WO_DETACH 0x00000004
|
2005-07-27 21:43:37 +00:00
|
|
|
/* Detach on last close. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_RW_DETACH 0x00000008
|
2006-06-05 21:38:54 +00:00
|
|
|
/* Provide data authentication. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_AUTH 0x00000010
|
2006-08-09 18:11:14 +00:00
|
|
|
/* Provider is read-only, we should deny all write attempts. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_RO 0x00000020
|
2006-08-09 18:11:14 +00:00
|
|
|
/* RUNTIME FLAGS. */
|
2005-07-27 21:43:37 +00:00
|
|
|
/* Provider was open for writing. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_WOPEN 0x00010000
|
2005-07-27 21:43:37 +00:00
|
|
|
/* Destroy device. */
|
2006-08-11 19:09:12 +00:00
|
|
|
#define G_ELI_FLAG_DESTROY 0x00020000
|
|
|
|
/* Provider uses native byte-order for IV generation. */
|
|
|
|
#define G_ELI_FLAG_NATIVE_BYTE_ORDER 0x00040000
|
2010-09-23 11:49:47 +00:00
|
|
|
/* Provider uses single encryption key. */
|
|
|
|
#define G_ELI_FLAG_SINGLE_KEY 0x00080000
|
2010-10-20 20:50:55 +00:00
|
|
|
/* Device suspended. */
|
|
|
|
#define G_ELI_FLAG_SUSPEND 0x00100000
|
2011-05-08 09:17:56 +00:00
|
|
|
/* Provider uses first encryption key. */
|
|
|
|
#define G_ELI_FLAG_FIRST_KEY 0x00200000
|
2010-10-20 20:50:55 +00:00
|
|
|
|
|
|
|
#define G_ELI_NEW_BIO 255
|
2005-07-27 21:43:37 +00:00
|
|
|
|
|
|
|
#define SHA512_MDLEN 64
|
2006-06-05 21:38:54 +00:00
|
|
|
#define G_ELI_AUTH_SECKEYLEN SHA256_DIGEST_LENGTH
|
2005-07-27 21:43:37 +00:00
|
|
|
|
|
|
|
#define G_ELI_MAXMKEYS 2
|
|
|
|
#define G_ELI_MAXKEYLEN 64
|
|
|
|
#define G_ELI_USERKEYLEN G_ELI_MAXKEYLEN
|
|
|
|
#define G_ELI_DATAKEYLEN G_ELI_MAXKEYLEN
|
2006-06-05 21:38:54 +00:00
|
|
|
#define G_ELI_AUTHKEYLEN G_ELI_MAXKEYLEN
|
2005-07-27 21:43:37 +00:00
|
|
|
#define G_ELI_IVKEYLEN G_ELI_MAXKEYLEN
|
|
|
|
#define G_ELI_SALTLEN 64
|
|
|
|
#define G_ELI_DATAIVKEYLEN (G_ELI_DATAKEYLEN + G_ELI_IVKEYLEN)
|
|
|
|
/* Data-Key, IV-Key, HMAC_SHA512(Derived-Key, Data-Key+IV-Key) */
|
|
|
|
#define G_ELI_MKEYLEN (G_ELI_DATAIVKEYLEN + SHA512_MDLEN)
|
2010-09-23 11:19:48 +00:00
|
|
|
#define G_ELI_OVERWRITES 5
|
2010-09-23 11:49:47 +00:00
|
|
|
/* Switch data encryption key every 2^20 blocks. */
|
|
|
|
#define G_ELI_KEY_SHIFT 20
|
2005-07-27 21:43:37 +00:00
|
|
|
|
|
|
|
#ifdef _KERNEL
|
2010-09-25 10:32:04 +00:00
|
|
|
extern int g_eli_debug;
|
2005-07-27 21:43:37 +00:00
|
|
|
extern u_int g_eli_overwrites;
|
2006-06-05 21:38:54 +00:00
|
|
|
extern u_int g_eli_batch;
|
2005-07-27 21:43:37 +00:00
|
|
|
|
2010-10-21 19:44:28 +00:00
|
|
|
#define G_ELI_CRYPTO_UNKNOWN 0
|
2005-07-27 21:43:37 +00:00
|
|
|
#define G_ELI_CRYPTO_HW 1
|
|
|
|
#define G_ELI_CRYPTO_SW 2
|
|
|
|
|
|
|
|
#define G_ELI_DEBUG(lvl, ...) do { \
|
|
|
|
if (g_eli_debug >= (lvl)) { \
|
|
|
|
printf("GEOM_ELI"); \
|
|
|
|
if (g_eli_debug > 0) \
|
|
|
|
printf("[%u]", lvl); \
|
|
|
|
printf(": "); \
|
|
|
|
printf(__VA_ARGS__); \
|
|
|
|
printf("\n"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define G_ELI_LOGREQ(lvl, bp, ...) do { \
|
|
|
|
if (g_eli_debug >= (lvl)) { \
|
|
|
|
printf("GEOM_ELI"); \
|
|
|
|
if (g_eli_debug > 0) \
|
|
|
|
printf("[%u]", lvl); \
|
|
|
|
printf(": "); \
|
|
|
|
printf(__VA_ARGS__); \
|
|
|
|
printf(" "); \
|
|
|
|
g_print_bio(bp); \
|
|
|
|
printf("\n"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
struct g_eli_worker {
|
|
|
|
struct g_eli_softc *w_softc;
|
|
|
|
struct proc *w_proc;
|
|
|
|
u_int w_number;
|
|
|
|
uint64_t w_sid;
|
2010-10-20 20:50:55 +00:00
|
|
|
boolean_t w_active;
|
2005-07-27 21:43:37 +00:00
|
|
|
LIST_ENTRY(g_eli_worker) w_next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct g_eli_softc {
|
2011-04-21 13:31:43 +00:00
|
|
|
struct g_geom *sc_geom;
|
|
|
|
u_int sc_crypto;
|
|
|
|
uint8_t sc_mkey[G_ELI_DATAIVKEYLEN];
|
|
|
|
uint8_t sc_ekey[G_ELI_DATAKEYLEN];
|
|
|
|
TAILQ_HEAD(, g_eli_key) sc_ekeys_queue;
|
|
|
|
RB_HEAD(g_eli_key_tree, g_eli_key) sc_ekeys_tree;
|
|
|
|
struct mtx sc_ekeys_lock;
|
|
|
|
uint64_t sc_ekeys_total;
|
|
|
|
uint64_t sc_ekeys_allocated;
|
|
|
|
u_int sc_ealgo;
|
|
|
|
u_int sc_ekeylen;
|
|
|
|
uint8_t sc_akey[G_ELI_AUTHKEYLEN];
|
|
|
|
u_int sc_aalgo;
|
|
|
|
u_int sc_akeylen;
|
|
|
|
u_int sc_alen;
|
|
|
|
SHA256_CTX sc_akeyctx;
|
|
|
|
uint8_t sc_ivkey[G_ELI_IVKEYLEN];
|
|
|
|
SHA256_CTX sc_ivctx;
|
|
|
|
int sc_nkey;
|
|
|
|
uint32_t sc_flags;
|
|
|
|
int sc_inflight;
|
|
|
|
off_t sc_mediasize;
|
|
|
|
size_t sc_sectorsize;
|
|
|
|
u_int sc_bytes_per_sector;
|
|
|
|
u_int sc_data_per_sector;
|
2005-07-27 21:43:37 +00:00
|
|
|
|
|
|
|
/* Only for software cryptography. */
|
|
|
|
struct bio_queue_head sc_queue;
|
2011-04-21 13:31:43 +00:00
|
|
|
struct mtx sc_queue_mtx;
|
2005-07-27 21:43:37 +00:00
|
|
|
LIST_HEAD(, g_eli_worker) sc_workers;
|
|
|
|
};
|
|
|
|
#define sc_name sc_geom->name
|
|
|
|
#endif /* _KERNEL */
|
|
|
|
|
|
|
|
struct g_eli_metadata {
|
|
|
|
char md_magic[16]; /* Magic value. */
|
|
|
|
uint32_t md_version; /* Version number. */
|
|
|
|
uint32_t md_flags; /* Additional flags. */
|
2006-06-05 21:38:54 +00:00
|
|
|
uint16_t md_ealgo; /* Encryption algorithm. */
|
2005-07-27 21:43:37 +00:00
|
|
|
uint16_t md_keylen; /* Key length. */
|
2006-06-05 21:38:54 +00:00
|
|
|
uint16_t md_aalgo; /* Authentication algorithm. */
|
2005-07-27 21:43:37 +00:00
|
|
|
uint64_t md_provsize; /* Provider's size. */
|
|
|
|
uint32_t md_sectorsize; /* Sector size. */
|
|
|
|
uint8_t md_keys; /* Available keys. */
|
2005-08-19 22:16:26 +00:00
|
|
|
int32_t md_iterations; /* Number of iterations for PKCS#5v2. */
|
2005-07-27 21:43:37 +00:00
|
|
|
uint8_t md_salt[G_ELI_SALTLEN]; /* Salt. */
|
|
|
|
/* Encrypted master key (IV-key, Data-key, HMAC). */
|
|
|
|
uint8_t md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN];
|
|
|
|
u_char md_hash[16]; /* MD5 hash. */
|
2005-08-20 10:43:03 +00:00
|
|
|
} __packed;
|
2005-07-27 21:43:37 +00:00
|
|
|
#ifndef _OpenSSL_
|
|
|
|
static __inline void
|
|
|
|
eli_metadata_encode(struct g_eli_metadata *md, u_char *data)
|
|
|
|
{
|
|
|
|
MD5_CTX ctx;
|
|
|
|
u_char *p;
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
bcopy(md->md_magic, p, sizeof(md->md_magic)); p += sizeof(md->md_magic);
|
|
|
|
le32enc(p, md->md_version); p += sizeof(md->md_version);
|
|
|
|
le32enc(p, md->md_flags); p += sizeof(md->md_flags);
|
2006-06-05 21:38:54 +00:00
|
|
|
le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo);
|
2005-07-27 21:43:37 +00:00
|
|
|
le16enc(p, md->md_keylen); p += sizeof(md->md_keylen);
|
2006-06-05 21:38:54 +00:00
|
|
|
le16enc(p, md->md_aalgo); p += sizeof(md->md_aalgo);
|
2005-07-27 21:43:37 +00:00
|
|
|
le64enc(p, md->md_provsize); p += sizeof(md->md_provsize);
|
|
|
|
le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize);
|
|
|
|
*p = md->md_keys; p += sizeof(md->md_keys);
|
|
|
|
le32enc(p, md->md_iterations); p += sizeof(md->md_iterations);
|
|
|
|
bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
|
|
|
|
bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
|
|
|
|
MD5Init(&ctx);
|
|
|
|
MD5Update(&ctx, data, p - data);
|
2006-03-30 18:50:00 +00:00
|
|
|
MD5Final(md->md_hash, &ctx);
|
2005-07-27 21:43:37 +00:00
|
|
|
bcopy(md->md_hash, p, sizeof(md->md_hash));
|
|
|
|
}
|
|
|
|
static __inline int
|
|
|
|
eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md)
|
|
|
|
{
|
|
|
|
MD5_CTX ctx;
|
|
|
|
const u_char *p;
|
|
|
|
|
|
|
|
p = data + sizeof(md->md_magic) + sizeof(md->md_version);
|
|
|
|
md->md_flags = le32dec(p); p += sizeof(md->md_flags);
|
2006-06-05 21:38:54 +00:00
|
|
|
md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo);
|
2005-07-27 21:43:37 +00:00
|
|
|
md->md_keylen = le16dec(p); p += sizeof(md->md_keylen);
|
|
|
|
md->md_provsize = le64dec(p); p += sizeof(md->md_provsize);
|
|
|
|
md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize);
|
|
|
|
md->md_keys = *p; p += sizeof(md->md_keys);
|
|
|
|
md->md_iterations = le32dec(p); p += sizeof(md->md_iterations);
|
|
|
|
bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
|
|
|
|
bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
|
|
|
|
MD5Init(&ctx);
|
|
|
|
MD5Update(&ctx, data, p - data);
|
2006-03-30 18:50:00 +00:00
|
|
|
MD5Final(md->md_hash, &ctx);
|
2005-07-27 21:43:37 +00:00
|
|
|
if (bcmp(md->md_hash, p, 16) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
return (0);
|
|
|
|
}
|
2010-09-19 10:45:53 +00:00
|
|
|
|
2005-07-27 21:43:37 +00:00
|
|
|
static __inline int
|
2011-05-08 09:25:16 +00:00
|
|
|
eli_metadata_decode_v1v2v3v4v5v6(const u_char *data, struct g_eli_metadata *md)
|
2006-06-05 21:38:54 +00:00
|
|
|
{
|
|
|
|
MD5_CTX ctx;
|
|
|
|
const u_char *p;
|
|
|
|
|
|
|
|
p = data + sizeof(md->md_magic) + sizeof(md->md_version);
|
|
|
|
md->md_flags = le32dec(p); p += sizeof(md->md_flags);
|
|
|
|
md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo);
|
|
|
|
md->md_keylen = le16dec(p); p += sizeof(md->md_keylen);
|
|
|
|
md->md_aalgo = le16dec(p); p += sizeof(md->md_aalgo);
|
|
|
|
md->md_provsize = le64dec(p); p += sizeof(md->md_provsize);
|
|
|
|
md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize);
|
|
|
|
md->md_keys = *p; p += sizeof(md->md_keys);
|
|
|
|
md->md_iterations = le32dec(p); p += sizeof(md->md_iterations);
|
|
|
|
bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
|
|
|
|
bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
|
|
|
|
MD5Init(&ctx);
|
|
|
|
MD5Update(&ctx, data, p - data);
|
|
|
|
MD5Final(md->md_hash, &ctx);
|
|
|
|
if (bcmp(md->md_hash, p, 16) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
static __inline int
|
2005-07-27 21:43:37 +00:00
|
|
|
eli_metadata_decode(const u_char *data, struct g_eli_metadata *md)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
bcopy(data, md->md_magic, sizeof(md->md_magic));
|
|
|
|
md->md_version = le32dec(data + sizeof(md->md_magic));
|
|
|
|
switch (md->md_version) {
|
2011-05-08 09:25:16 +00:00
|
|
|
case G_ELI_VERSION_00:
|
2005-07-27 21:43:37 +00:00
|
|
|
error = eli_metadata_decode_v0(data, md);
|
|
|
|
break;
|
2011-05-08 09:25:16 +00:00
|
|
|
case G_ELI_VERSION_01:
|
|
|
|
case G_ELI_VERSION_02:
|
|
|
|
case G_ELI_VERSION_03:
|
|
|
|
case G_ELI_VERSION_04:
|
|
|
|
case G_ELI_VERSION_05:
|
|
|
|
case G_ELI_VERSION_06:
|
|
|
|
error = eli_metadata_decode_v1v2v3v4v5v6(data, md);
|
2006-06-05 21:38:54 +00:00
|
|
|
break;
|
2005-07-27 21:43:37 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
#endif /* !_OpenSSL */
|
|
|
|
|
|
|
|
static __inline u_int
|
2006-06-05 21:38:54 +00:00
|
|
|
g_eli_str2ealgo(const char *name)
|
2005-07-27 21:43:37 +00:00
|
|
|
{
|
|
|
|
|
2005-08-08 19:40:38 +00:00
|
|
|
if (strcasecmp("null", name) == 0)
|
2005-07-27 21:43:37 +00:00
|
|
|
return (CRYPTO_NULL_CBC);
|
2010-09-23 11:58:36 +00:00
|
|
|
else if (strcasecmp("null-cbc", name) == 0)
|
|
|
|
return (CRYPTO_NULL_CBC);
|
2005-08-08 19:40:38 +00:00
|
|
|
else if (strcasecmp("aes", name) == 0)
|
2010-09-23 11:58:36 +00:00
|
|
|
return (CRYPTO_AES_XTS);
|
|
|
|
else if (strcasecmp("aes-cbc", name) == 0)
|
2005-07-27 21:43:37 +00:00
|
|
|
return (CRYPTO_AES_CBC);
|
2010-09-23 11:58:36 +00:00
|
|
|
else if (strcasecmp("aes-xts", name) == 0)
|
|
|
|
return (CRYPTO_AES_XTS);
|
2005-08-08 19:40:38 +00:00
|
|
|
else if (strcasecmp("blowfish", name) == 0)
|
2005-07-27 21:43:37 +00:00
|
|
|
return (CRYPTO_BLF_CBC);
|
2010-09-23 11:58:36 +00:00
|
|
|
else if (strcasecmp("blowfish-cbc", name) == 0)
|
|
|
|
return (CRYPTO_BLF_CBC);
|
2007-09-01 06:33:02 +00:00
|
|
|
else if (strcasecmp("camellia", name) == 0)
|
|
|
|
return (CRYPTO_CAMELLIA_CBC);
|
2010-09-23 11:58:36 +00:00
|
|
|
else if (strcasecmp("camellia-cbc", name) == 0)
|
|
|
|
return (CRYPTO_CAMELLIA_CBC);
|
2005-08-08 19:40:38 +00:00
|
|
|
else if (strcasecmp("3des", name) == 0)
|
2005-07-27 21:43:37 +00:00
|
|
|
return (CRYPTO_3DES_CBC);
|
2010-09-23 11:58:36 +00:00
|
|
|
else if (strcasecmp("3des-cbc", name) == 0)
|
|
|
|
return (CRYPTO_3DES_CBC);
|
2005-07-27 21:43:37 +00:00
|
|
|
return (CRYPTO_ALGORITHM_MIN - 1);
|
|
|
|
}
|
|
|
|
|
2006-06-05 21:38:54 +00:00
|
|
|
static __inline u_int
|
|
|
|
g_eli_str2aalgo(const char *name)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (strcasecmp("hmac/md5", name) == 0)
|
|
|
|
return (CRYPTO_MD5_HMAC);
|
|
|
|
else if (strcasecmp("hmac/sha1", name) == 0)
|
|
|
|
return (CRYPTO_SHA1_HMAC);
|
|
|
|
else if (strcasecmp("hmac/ripemd160", name) == 0)
|
|
|
|
return (CRYPTO_RIPEMD160_HMAC);
|
|
|
|
else if (strcasecmp("hmac/sha256", name) == 0)
|
|
|
|
return (CRYPTO_SHA2_256_HMAC);
|
|
|
|
else if (strcasecmp("hmac/sha384", name) == 0)
|
|
|
|
return (CRYPTO_SHA2_384_HMAC);
|
|
|
|
else if (strcasecmp("hmac/sha512", name) == 0)
|
|
|
|
return (CRYPTO_SHA2_512_HMAC);
|
|
|
|
return (CRYPTO_ALGORITHM_MIN - 1);
|
|
|
|
}
|
|
|
|
|
2005-07-27 21:43:37 +00:00
|
|
|
static __inline const char *
|
|
|
|
g_eli_algo2str(u_int algo)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (algo) {
|
|
|
|
case CRYPTO_NULL_CBC:
|
|
|
|
return ("NULL");
|
|
|
|
case CRYPTO_AES_CBC:
|
2006-06-05 21:38:54 +00:00
|
|
|
return ("AES-CBC");
|
2010-09-23 11:58:36 +00:00
|
|
|
case CRYPTO_AES_XTS:
|
|
|
|
return ("AES-XTS");
|
2005-07-27 21:43:37 +00:00
|
|
|
case CRYPTO_BLF_CBC:
|
2006-06-05 21:38:54 +00:00
|
|
|
return ("Blowfish-CBC");
|
2007-09-01 06:33:02 +00:00
|
|
|
case CRYPTO_CAMELLIA_CBC:
|
|
|
|
return ("CAMELLIA-CBC");
|
2005-07-27 21:43:37 +00:00
|
|
|
case CRYPTO_3DES_CBC:
|
2006-06-05 21:38:54 +00:00
|
|
|
return ("3DES-CBC");
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
return ("HMAC/MD5");
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
return ("HMAC/SHA1");
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
return ("HMAC/RIPEMD160");
|
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
return ("HMAC/SHA256");
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
return ("HMAC/SHA384");
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
return ("HMAC/SHA512");
|
2005-07-27 21:43:37 +00:00
|
|
|
}
|
|
|
|
return ("unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
eli_metadata_dump(const struct g_eli_metadata *md)
|
|
|
|
{
|
|
|
|
static const char hex[] = "0123456789abcdef";
|
|
|
|
char str[sizeof(md->md_mkeys) * 2 + 1];
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
printf(" magic: %s\n", md->md_magic);
|
|
|
|
printf(" version: %u\n", (u_int)md->md_version);
|
|
|
|
printf(" flags: 0x%x\n", (u_int)md->md_flags);
|
2006-06-05 21:38:54 +00:00
|
|
|
printf(" ealgo: %s\n", g_eli_algo2str(md->md_ealgo));
|
2005-07-27 21:43:37 +00:00
|
|
|
printf(" keylen: %u\n", (u_int)md->md_keylen);
|
2006-06-05 21:38:54 +00:00
|
|
|
if (md->md_flags & G_ELI_FLAG_AUTH)
|
|
|
|
printf(" aalgo: %s\n", g_eli_algo2str(md->md_aalgo));
|
2005-07-27 21:43:37 +00:00
|
|
|
printf(" provsize: %ju\n", (uintmax_t)md->md_provsize);
|
|
|
|
printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
|
|
|
|
printf(" keys: 0x%02x\n", (u_int)md->md_keys);
|
|
|
|
printf("iterations: %u\n", (u_int)md->md_iterations);
|
|
|
|
bzero(str, sizeof(str));
|
|
|
|
for (i = 0; i < sizeof(md->md_salt); i++) {
|
|
|
|
str[i * 2] = hex[md->md_salt[i] >> 4];
|
|
|
|
str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f];
|
|
|
|
}
|
2005-08-19 22:16:26 +00:00
|
|
|
printf(" Salt: %s\n", str);
|
2005-07-27 21:43:37 +00:00
|
|
|
bzero(str, sizeof(str));
|
|
|
|
for (i = 0; i < sizeof(md->md_mkeys); i++) {
|
|
|
|
str[i * 2] = hex[md->md_mkeys[i] >> 4];
|
|
|
|
str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f];
|
|
|
|
}
|
|
|
|
printf("Master Key: %s\n", str);
|
|
|
|
bzero(str, sizeof(str));
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
str[i * 2] = hex[md->md_hash[i] >> 4];
|
|
|
|
str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
|
|
|
|
}
|
2005-08-19 22:16:26 +00:00
|
|
|
printf(" MD5 hash: %s\n", str);
|
2005-07-27 21:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline u_int
|
|
|
|
g_eli_keylen(u_int algo, u_int keylen)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (algo) {
|
|
|
|
case CRYPTO_NULL_CBC:
|
|
|
|
if (keylen == 0)
|
|
|
|
keylen = 64 * 8;
|
|
|
|
else {
|
|
|
|
if (keylen > 64 * 8)
|
|
|
|
keylen = 0;
|
|
|
|
}
|
|
|
|
return (keylen);
|
2010-08-28 08:30:20 +00:00
|
|
|
case CRYPTO_AES_CBC:
|
2007-09-01 06:33:02 +00:00
|
|
|
case CRYPTO_CAMELLIA_CBC:
|
2005-07-27 21:43:37 +00:00
|
|
|
switch (keylen) {
|
|
|
|
case 0:
|
|
|
|
return (128);
|
|
|
|
case 128:
|
|
|
|
case 192:
|
|
|
|
case 256:
|
|
|
|
return (keylen);
|
|
|
|
default:
|
|
|
|
return (0);
|
|
|
|
}
|
2010-09-23 11:58:36 +00:00
|
|
|
case CRYPTO_AES_XTS:
|
|
|
|
switch (keylen) {
|
|
|
|
case 0:
|
|
|
|
return (128);
|
|
|
|
case 128:
|
|
|
|
case 256:
|
|
|
|
return (keylen);
|
|
|
|
default:
|
|
|
|
return (0);
|
|
|
|
}
|
2005-07-27 21:43:37 +00:00
|
|
|
case CRYPTO_BLF_CBC:
|
|
|
|
if (keylen == 0)
|
|
|
|
return (128);
|
|
|
|
if (keylen < 128 || keylen > 448)
|
|
|
|
return (0);
|
|
|
|
if ((keylen % 32) != 0)
|
|
|
|
return (0);
|
|
|
|
return (keylen);
|
|
|
|
case CRYPTO_3DES_CBC:
|
|
|
|
if (keylen == 0 || keylen == 192)
|
|
|
|
return (192);
|
|
|
|
return (0);
|
|
|
|
default:
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-05 21:38:54 +00:00
|
|
|
static __inline u_int
|
|
|
|
g_eli_hashlen(u_int algo)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (algo) {
|
|
|
|
case CRYPTO_MD5_HMAC:
|
|
|
|
return (16);
|
|
|
|
case CRYPTO_SHA1_HMAC:
|
|
|
|
return (20);
|
|
|
|
case CRYPTO_RIPEMD160_HMAC:
|
|
|
|
return (20);
|
|
|
|
case CRYPTO_SHA2_256_HMAC:
|
|
|
|
return (32);
|
|
|
|
case CRYPTO_SHA2_384_HMAC:
|
|
|
|
return (48);
|
|
|
|
case CRYPTO_SHA2_512_HMAC:
|
|
|
|
return (64);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-07-27 21:43:37 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
|
|
|
|
struct g_eli_metadata *md);
|
|
|
|
struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp,
|
|
|
|
struct g_provider *bpp, const struct g_eli_metadata *md,
|
|
|
|
const u_char *mkey, int nkey);
|
|
|
|
int g_eli_destroy(struct g_eli_softc *sc, boolean_t force);
|
|
|
|
|
|
|
|
int g_eli_access(struct g_provider *pp, int dr, int dw, int de);
|
|
|
|
void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb);
|
2006-06-05 21:38:54 +00:00
|
|
|
|
|
|
|
void g_eli_read_done(struct bio *bp);
|
|
|
|
void g_eli_write_done(struct bio *bp);
|
|
|
|
int g_eli_crypto_rerun(struct cryptop *crp);
|
|
|
|
void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
|
|
|
|
size_t size);
|
|
|
|
|
2010-10-20 20:50:55 +00:00
|
|
|
void g_eli_crypto_read(struct g_eli_softc *sc, struct bio *bp, boolean_t fromworker);
|
2006-06-05 21:38:54 +00:00
|
|
|
void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp);
|
|
|
|
|
|
|
|
void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp);
|
|
|
|
void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp);
|
2005-07-27 21:43:37 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key);
|
|
|
|
int g_eli_mkey_decrypt(const struct g_eli_metadata *md,
|
|
|
|
const unsigned char *key, unsigned char *mkey, unsigned *nkeyp);
|
|
|
|
int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
|
|
|
|
unsigned char *mkey);
|
2006-06-05 21:38:54 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey);
|
|
|
|
#endif
|
2005-07-27 21:43:37 +00:00
|
|
|
|
|
|
|
int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
|
|
|
|
const u_char *key, size_t keysize);
|
|
|
|
int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
|
|
|
|
const u_char *key, size_t keysize);
|
|
|
|
|
|
|
|
struct hmac_ctx {
|
|
|
|
SHA512_CTX shactx;
|
|
|
|
u_char k_opad[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey,
|
|
|
|
size_t hkeylen);
|
|
|
|
void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
|
|
|
|
size_t datasize);
|
|
|
|
void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize);
|
|
|
|
void g_eli_crypto_hmac(const uint8_t *hkey, size_t hkeysize,
|
|
|
|
const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize);
|
2011-04-21 13:31:43 +00:00
|
|
|
|
|
|
|
#ifdef _KERNEL
|
|
|
|
void g_eli_key_init(struct g_eli_softc *sc);
|
|
|
|
void g_eli_key_destroy(struct g_eli_softc *sc);
|
|
|
|
uint8_t *g_eli_key_hold(struct g_eli_softc *sc, off_t offset, size_t blocksize);
|
|
|
|
void g_eli_key_drop(struct g_eli_softc *sc, uint8_t *rawkey);
|
|
|
|
#endif
|
2005-07-27 21:43:37 +00:00
|
|
|
#endif /* !_G_ELI_H_ */
|