1/*-
2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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: head/sys/geom/eli/g_eli.h 161220 2006-08-11 19:09:12Z pjd $
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/sha2.h>
36#include <opencrypto/cryptodev.h>
37#ifdef _KERNEL
38#include <sys/bio.h>
39#include <sys/libkern.h>
40#include <geom/geom.h>
41#else
42#include <stdio.h>
43#include <string.h>
44#include <strings.h>
45#endif
46#ifndef _OpenSSL_
47#include <sys/md5.h>
48#endif
49
50#define G_ELI_CLASS_NAME "ELI"
51#define G_ELI_MAGIC "GEOM::ELI"
52#define G_ELI_SUFFIX ".eli"
53
54/*
55 * Version history:
56 * 0 - Initial version number.
57 * 1 - Added data authentication support (md_aalgo field and
58 * G_ELI_FLAG_AUTH flag).
59 * 2 - Added G_ELI_FLAG_READONLY.
60 * - IV is generated from offset converted to little-endian
61 * (flag G_ELI_FLAG_NATIVE_BYTE_ORDER will be set for older versions).
62 */
63#define G_ELI_VERSION 2
64
65/* ON DISK FLAGS. */
66/* Use random, onetime keys. */
67#define G_ELI_FLAG_ONETIME 0x00000001
68/* Ask for the passphrase from the kernel, before mounting root. */
69#define G_ELI_FLAG_BOOT 0x00000002
70/* Detach on last close, if we were open for writing. */
71#define G_ELI_FLAG_WO_DETACH 0x00000004
72/* Detach on last close. */
73#define G_ELI_FLAG_RW_DETACH 0x00000008
74/* Provide data authentication. */
75#define G_ELI_FLAG_AUTH 0x00000010
76/* Provider is read-only, we should deny all write attempts. */
77#define G_ELI_FLAG_RO 0x00000020
78/* RUNTIME FLAGS. */
79/* Provider was open for writing. */
80#define G_ELI_FLAG_WOPEN 0x00010000
81/* Destroy device. */
82#define G_ELI_FLAG_DESTROY 0x00020000
83/* Provider uses native byte-order for IV generation. */
84#define G_ELI_FLAG_NATIVE_BYTE_ORDER 0x00040000
85
86#define SHA512_MDLEN 64
87#define G_ELI_AUTH_SECKEYLEN SHA256_DIGEST_LENGTH
88
89#define G_ELI_MAXMKEYS 2
90#define G_ELI_MAXKEYLEN 64
91#define G_ELI_USERKEYLEN G_ELI_MAXKEYLEN
92#define G_ELI_DATAKEYLEN G_ELI_MAXKEYLEN
93#define G_ELI_AUTHKEYLEN G_ELI_MAXKEYLEN
94#define G_ELI_IVKEYLEN G_ELI_MAXKEYLEN
95#define G_ELI_SALTLEN 64
96#define G_ELI_DATAIVKEYLEN (G_ELI_DATAKEYLEN + G_ELI_IVKEYLEN)
97/* Data-Key, IV-Key, HMAC_SHA512(Derived-Key, Data-Key+IV-Key) */
98#define G_ELI_MKEYLEN (G_ELI_DATAIVKEYLEN + SHA512_MDLEN)
99
100#ifdef _KERNEL
101extern u_int g_eli_debug;
102extern u_int g_eli_overwrites;
103extern u_int g_eli_batch;
104
105#define G_ELI_CRYPTO_HW 1
106#define G_ELI_CRYPTO_SW 2
107
108#define G_ELI_DEBUG(lvl, ...) do { \
109 if (g_eli_debug >= (lvl)) { \
110 printf("GEOM_ELI"); \
111 if (g_eli_debug > 0) \
112 printf("[%u]", lvl); \
113 printf(": "); \
114 printf(__VA_ARGS__); \
115 printf("\n"); \
116 } \
117} while (0)
118#define G_ELI_LOGREQ(lvl, bp, ...) do { \
119 if (g_eli_debug >= (lvl)) { \
120 printf("GEOM_ELI"); \
121 if (g_eli_debug > 0) \
122 printf("[%u]", lvl); \
123 printf(": "); \
124 printf(__VA_ARGS__); \
125 printf(" "); \
126 g_print_bio(bp); \
127 printf("\n"); \
128 } \
129} while (0)
130
131struct g_eli_worker {
132 struct g_eli_softc *w_softc;
133 struct proc *w_proc;
134 u_int w_number;
135 uint64_t w_sid;
136 LIST_ENTRY(g_eli_worker) w_next;
137};
138
139struct g_eli_softc {
140 struct g_geom *sc_geom;
141 u_int sc_crypto;
142 uint8_t sc_mkey[G_ELI_DATAIVKEYLEN];
143 uint8_t sc_ekey[G_ELI_DATAKEYLEN];
144 u_int sc_ealgo;
145 u_int sc_ekeylen;
146 uint8_t sc_akey[G_ELI_AUTHKEYLEN];
147 u_int sc_aalgo;
148 u_int sc_akeylen;
149 u_int sc_alen;
150 SHA256_CTX sc_akeyctx;
151 uint8_t sc_ivkey[G_ELI_IVKEYLEN];
152 SHA256_CTX sc_ivctx;
153 int sc_nkey;
154 uint32_t sc_flags;
155 u_int sc_bytes_per_sector;
156 u_int sc_data_per_sector;
157
158 /* Only for software cryptography. */
159 struct bio_queue_head sc_queue;
160 struct mtx sc_queue_mtx;
161 LIST_HEAD(, g_eli_worker) sc_workers;
162};
163#define sc_name sc_geom->name
164#endif /* _KERNEL */
165
166struct g_eli_metadata {
167 char md_magic[16]; /* Magic value. */
168 uint32_t md_version; /* Version number. */
169 uint32_t md_flags; /* Additional flags. */
170 uint16_t md_ealgo; /* Encryption algorithm. */
171 uint16_t md_keylen; /* Key length. */
172 uint16_t md_aalgo; /* Authentication algorithm. */
173 uint64_t md_provsize; /* Provider's size. */
174 uint32_t md_sectorsize; /* Sector size. */
175 uint8_t md_keys; /* Available keys. */
176 int32_t md_iterations; /* Number of iterations for PKCS#5v2. */
177 uint8_t md_salt[G_ELI_SALTLEN]; /* Salt. */
178 /* Encrypted master key (IV-key, Data-key, HMAC). */
179 uint8_t md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN];
180 u_char md_hash[16]; /* MD5 hash. */
181} __packed;
182#ifndef _OpenSSL_
183static __inline void
184eli_metadata_encode(struct g_eli_metadata *md, u_char *data)
185{
186 MD5_CTX ctx;
187 u_char *p;
188
189 p = data;
190 bcopy(md->md_magic, p, sizeof(md->md_magic)); p += sizeof(md->md_magic);
191 le32enc(p, md->md_version); p += sizeof(md->md_version);
192 le32enc(p, md->md_flags); p += sizeof(md->md_flags);
193 le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo);
194 le16enc(p, md->md_keylen); p += sizeof(md->md_keylen);
195 le16enc(p, md->md_aalgo); p += sizeof(md->md_aalgo);
196 le64enc(p, md->md_provsize); p += sizeof(md->md_provsize);
197 le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize);
198 *p = md->md_keys; p += sizeof(md->md_keys);
199 le32enc(p, md->md_iterations); p += sizeof(md->md_iterations);
200 bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
201 bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
202 MD5Init(&ctx);
203 MD5Update(&ctx, data, p - data);
204 MD5Final(md->md_hash, &ctx);
205 bcopy(md->md_hash, p, sizeof(md->md_hash));
206}
207static __inline int
208eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md)
209{
210 MD5_CTX ctx;
211 const u_char *p;
212
213 p = data + sizeof(md->md_magic) + sizeof(md->md_version);
214 md->md_flags = le32dec(p); p += sizeof(md->md_flags);
215 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo);
216 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen);
217 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize);
218 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize);
219 md->md_keys = *p; p += sizeof(md->md_keys);
220 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations);
221 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
222 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
223 MD5Init(&ctx);
224 MD5Update(&ctx, data, p - data);
225 MD5Final(md->md_hash, &ctx);
226 if (bcmp(md->md_hash, p, 16) != 0)
227 return (EINVAL);
228 return (0);
229}
230static __inline int
231eli_metadata_decode_v1v2(const u_char *data, struct g_eli_metadata *md)
232{
233 MD5_CTX ctx;
234 const u_char *p;
235
236 p = data + sizeof(md->md_magic) + sizeof(md->md_version);
237 md->md_flags = le32dec(p); p += sizeof(md->md_flags);
238 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo);
239 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen);
240 md->md_aalgo = le16dec(p); p += sizeof(md->md_aalgo);
241 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize);
242 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize);
243 md->md_keys = *p; p += sizeof(md->md_keys);
244 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations);
245 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
246 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
247 MD5Init(&ctx);
248 MD5Update(&ctx, data, p - data);
249 MD5Final(md->md_hash, &ctx);
250 if (bcmp(md->md_hash, p, 16) != 0)
251 return (EINVAL);
252 return (0);
253}
254static __inline int
255eli_metadata_decode(const u_char *data, struct g_eli_metadata *md)
256{
257 int error;
258
259 bcopy(data, md->md_magic, sizeof(md->md_magic));
260 md->md_version = le32dec(data + sizeof(md->md_magic));
261 switch (md->md_version) {
262 case 0:
263 error = eli_metadata_decode_v0(data, md);
264 break;
265 case 1:
266 case 2:
267 error = eli_metadata_decode_v1v2(data, md);
268 break;
269 default:
270 error = EINVAL;
271 break;
272 }
273 return (error);
274}
275#endif /* !_OpenSSL */
276
277static __inline u_int
278g_eli_str2ealgo(const char *name)
279{
280
281 if (strcasecmp("null", name) == 0)
282 return (CRYPTO_NULL_CBC);
283 else if (strcasecmp("aes", name) == 0)
284 return (CRYPTO_AES_CBC);
285 else if (strcasecmp("blowfish", name) == 0)
286 return (CRYPTO_BLF_CBC);
287 else if (strcasecmp("3des", name) == 0)
288 return (CRYPTO_3DES_CBC);
289 return (CRYPTO_ALGORITHM_MIN - 1);
290}
291
292static __inline u_int
293g_eli_str2aalgo(const char *name)
294{
295
296 if (strcasecmp("hmac/md5", name) == 0)
297 return (CRYPTO_MD5_HMAC);
298 else if (strcasecmp("hmac/sha1", name) == 0)
299 return (CRYPTO_SHA1_HMAC);
300 else if (strcasecmp("hmac/ripemd160", name) == 0)
301 return (CRYPTO_RIPEMD160_HMAC);
302 else if (strcasecmp("hmac/sha256", name) == 0)
303 return (CRYPTO_SHA2_256_HMAC);
304 else if (strcasecmp("hmac/sha384", name) == 0)
305 return (CRYPTO_SHA2_384_HMAC);
306 else if (strcasecmp("hmac/sha512", name) == 0)
307 return (CRYPTO_SHA2_512_HMAC);
308 return (CRYPTO_ALGORITHM_MIN - 1);
309}
310
311static __inline const char *
312g_eli_algo2str(u_int algo)
313{
314
315 switch (algo) {
316 case CRYPTO_NULL_CBC:
317 return ("NULL");
318 case CRYPTO_AES_CBC:
319 return ("AES-CBC");
320 case CRYPTO_BLF_CBC:
321 return ("Blowfish-CBC");
322 case CRYPTO_3DES_CBC:
323 return ("3DES-CBC");
324 case CRYPTO_MD5_HMAC:
325 return ("HMAC/MD5");
326 case CRYPTO_SHA1_HMAC:
327 return ("HMAC/SHA1");
328 case CRYPTO_RIPEMD160_HMAC:
329 return ("HMAC/RIPEMD160");
330 case CRYPTO_SHA2_256_HMAC:
331 return ("HMAC/SHA256");
332 case CRYPTO_SHA2_384_HMAC:
333 return ("HMAC/SHA384");
334 case CRYPTO_SHA2_512_HMAC:
335 return ("HMAC/SHA512");
336 }
337 return ("unknown");
338}
339
340static __inline void
341eli_metadata_dump(const struct g_eli_metadata *md)
342{
343 static const char hex[] = "0123456789abcdef";
344 char str[sizeof(md->md_mkeys) * 2 + 1];
345 u_int i;
346
347 printf(" magic: %s\n", md->md_magic);
348 printf(" version: %u\n", (u_int)md->md_version);
349 printf(" flags: 0x%x\n", (u_int)md->md_flags);
350 printf(" ealgo: %s\n", g_eli_algo2str(md->md_ealgo));
351 printf(" keylen: %u\n", (u_int)md->md_keylen);
352 if (md->md_flags & G_ELI_FLAG_AUTH)
353 printf(" aalgo: %s\n", g_eli_algo2str(md->md_aalgo));
354 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize);
355 printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
356 printf(" keys: 0x%02x\n", (u_int)md->md_keys);
357 printf("iterations: %u\n", (u_int)md->md_iterations);
358 bzero(str, sizeof(str));
359 for (i = 0; i < sizeof(md->md_salt); i++) {
360 str[i * 2] = hex[md->md_salt[i] >> 4];
361 str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f];
362 }
363 printf(" Salt: %s\n", str);
364 bzero(str, sizeof(str));
365 for (i = 0; i < sizeof(md->md_mkeys); i++) {
366 str[i * 2] = hex[md->md_mkeys[i] >> 4];
367 str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f];
368 }
369 printf("Master Key: %s\n", str);
370 bzero(str, sizeof(str));
371 for (i = 0; i < 16; i++) {
372 str[i * 2] = hex[md->md_hash[i] >> 4];
373 str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
374 }
375 printf(" MD5 hash: %s\n", str);
376}
377
378static __inline u_int
379g_eli_keylen(u_int algo, u_int keylen)
380{
381
382 switch (algo) {
383 case CRYPTO_NULL_CBC:
384 if (keylen == 0)
385 keylen = 64 * 8;
386 else {
387 if (keylen > 64 * 8)
388 keylen = 0;
389 }
390 return (keylen);
391 case CRYPTO_AES_CBC:
392 switch (keylen) {
393 case 0:
394 return (128);
395 case 128:
396 case 192:
397 case 256:
398 return (keylen);
399 default:
400 return (0);
401 }
402 case CRYPTO_BLF_CBC:
403 if (keylen == 0)
404 return (128);
405 if (keylen < 128 || keylen > 448)
406 return (0);
407 if ((keylen % 32) != 0)
408 return (0);
409 return (keylen);
410 case CRYPTO_3DES_CBC:
411 if (keylen == 0 || keylen == 192)
412 return (192);
413 return (0);
414 default:
415 return (0);
416 }
417}
418
419static __inline u_int
420g_eli_hashlen(u_int algo)
421{
422
423 switch (algo) {
424 case CRYPTO_MD5_HMAC:
425 return (16);
426 case CRYPTO_SHA1_HMAC:
427 return (20);
428 case CRYPTO_RIPEMD160_HMAC:
429 return (20);
430 case CRYPTO_SHA2_256_HMAC:
431 return (32);
432 case CRYPTO_SHA2_384_HMAC:
433 return (48);
434 case CRYPTO_SHA2_512_HMAC:
435 return (64);
436 }
437 return (0);
438}
439
440#ifdef _KERNEL
441int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
442 struct g_eli_metadata *md);
443struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp,
444 struct g_provider *bpp, const struct g_eli_metadata *md,
445 const u_char *mkey, int nkey);
446int g_eli_destroy(struct g_eli_softc *sc, boolean_t force);
447
448int g_eli_access(struct g_provider *pp, int dr, int dw, int de);
449void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb);
450
451void g_eli_read_done(struct bio *bp);
452void g_eli_write_done(struct bio *bp);
453int g_eli_crypto_rerun(struct cryptop *crp);
454void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
455 size_t size);
456
457void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp);
458
459void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp);
460void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp);
461#endif
462
463void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key);
464int g_eli_mkey_decrypt(const struct g_eli_metadata *md,
465 const unsigned char *key, unsigned char *mkey, unsigned *nkeyp);
466int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
467 unsigned char *mkey);
468#ifdef _KERNEL
469void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey);
470#endif
471
472int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
473 const u_char *key, size_t keysize);
474int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
475 const u_char *key, size_t keysize);
476
477struct hmac_ctx {
478 SHA512_CTX shactx;
479 u_char k_opad[128];
480};
481
482void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey,
483 size_t hkeylen);
484void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
485 size_t datasize);
486void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize);
487void g_eli_crypto_hmac(const uint8_t *hkey, size_t hkeysize,
488 const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize);
489#endif /* !_G_ELI_H_ */
2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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: head/sys/geom/eli/g_eli.h 161220 2006-08-11 19:09:12Z pjd $
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/sha2.h>
36#include <opencrypto/cryptodev.h>
37#ifdef _KERNEL
38#include <sys/bio.h>
39#include <sys/libkern.h>
40#include <geom/geom.h>
41#else
42#include <stdio.h>
43#include <string.h>
44#include <strings.h>
45#endif
46#ifndef _OpenSSL_
47#include <sys/md5.h>
48#endif
49
50#define G_ELI_CLASS_NAME "ELI"
51#define G_ELI_MAGIC "GEOM::ELI"
52#define G_ELI_SUFFIX ".eli"
53
54/*
55 * Version history:
56 * 0 - Initial version number.
57 * 1 - Added data authentication support (md_aalgo field and
58 * G_ELI_FLAG_AUTH flag).
59 * 2 - Added G_ELI_FLAG_READONLY.
60 * - IV is generated from offset converted to little-endian
61 * (flag G_ELI_FLAG_NATIVE_BYTE_ORDER will be set for older versions).
62 */
63#define G_ELI_VERSION 2
64
65/* ON DISK FLAGS. */
66/* Use random, onetime keys. */
67#define G_ELI_FLAG_ONETIME 0x00000001
68/* Ask for the passphrase from the kernel, before mounting root. */
69#define G_ELI_FLAG_BOOT 0x00000002
70/* Detach on last close, if we were open for writing. */
71#define G_ELI_FLAG_WO_DETACH 0x00000004
72/* Detach on last close. */
73#define G_ELI_FLAG_RW_DETACH 0x00000008
74/* Provide data authentication. */
75#define G_ELI_FLAG_AUTH 0x00000010
76/* Provider is read-only, we should deny all write attempts. */
77#define G_ELI_FLAG_RO 0x00000020
78/* RUNTIME FLAGS. */
79/* Provider was open for writing. */
80#define G_ELI_FLAG_WOPEN 0x00010000
81/* Destroy device. */
82#define G_ELI_FLAG_DESTROY 0x00020000
83/* Provider uses native byte-order for IV generation. */
84#define G_ELI_FLAG_NATIVE_BYTE_ORDER 0x00040000
85
86#define SHA512_MDLEN 64
87#define G_ELI_AUTH_SECKEYLEN SHA256_DIGEST_LENGTH
88
89#define G_ELI_MAXMKEYS 2
90#define G_ELI_MAXKEYLEN 64
91#define G_ELI_USERKEYLEN G_ELI_MAXKEYLEN
92#define G_ELI_DATAKEYLEN G_ELI_MAXKEYLEN
93#define G_ELI_AUTHKEYLEN G_ELI_MAXKEYLEN
94#define G_ELI_IVKEYLEN G_ELI_MAXKEYLEN
95#define G_ELI_SALTLEN 64
96#define G_ELI_DATAIVKEYLEN (G_ELI_DATAKEYLEN + G_ELI_IVKEYLEN)
97/* Data-Key, IV-Key, HMAC_SHA512(Derived-Key, Data-Key+IV-Key) */
98#define G_ELI_MKEYLEN (G_ELI_DATAIVKEYLEN + SHA512_MDLEN)
99
100#ifdef _KERNEL
101extern u_int g_eli_debug;
102extern u_int g_eli_overwrites;
103extern u_int g_eli_batch;
104
105#define G_ELI_CRYPTO_HW 1
106#define G_ELI_CRYPTO_SW 2
107
108#define G_ELI_DEBUG(lvl, ...) do { \
109 if (g_eli_debug >= (lvl)) { \
110 printf("GEOM_ELI"); \
111 if (g_eli_debug > 0) \
112 printf("[%u]", lvl); \
113 printf(": "); \
114 printf(__VA_ARGS__); \
115 printf("\n"); \
116 } \
117} while (0)
118#define G_ELI_LOGREQ(lvl, bp, ...) do { \
119 if (g_eli_debug >= (lvl)) { \
120 printf("GEOM_ELI"); \
121 if (g_eli_debug > 0) \
122 printf("[%u]", lvl); \
123 printf(": "); \
124 printf(__VA_ARGS__); \
125 printf(" "); \
126 g_print_bio(bp); \
127 printf("\n"); \
128 } \
129} while (0)
130
131struct g_eli_worker {
132 struct g_eli_softc *w_softc;
133 struct proc *w_proc;
134 u_int w_number;
135 uint64_t w_sid;
136 LIST_ENTRY(g_eli_worker) w_next;
137};
138
139struct g_eli_softc {
140 struct g_geom *sc_geom;
141 u_int sc_crypto;
142 uint8_t sc_mkey[G_ELI_DATAIVKEYLEN];
143 uint8_t sc_ekey[G_ELI_DATAKEYLEN];
144 u_int sc_ealgo;
145 u_int sc_ekeylen;
146 uint8_t sc_akey[G_ELI_AUTHKEYLEN];
147 u_int sc_aalgo;
148 u_int sc_akeylen;
149 u_int sc_alen;
150 SHA256_CTX sc_akeyctx;
151 uint8_t sc_ivkey[G_ELI_IVKEYLEN];
152 SHA256_CTX sc_ivctx;
153 int sc_nkey;
154 uint32_t sc_flags;
155 u_int sc_bytes_per_sector;
156 u_int sc_data_per_sector;
157
158 /* Only for software cryptography. */
159 struct bio_queue_head sc_queue;
160 struct mtx sc_queue_mtx;
161 LIST_HEAD(, g_eli_worker) sc_workers;
162};
163#define sc_name sc_geom->name
164#endif /* _KERNEL */
165
166struct g_eli_metadata {
167 char md_magic[16]; /* Magic value. */
168 uint32_t md_version; /* Version number. */
169 uint32_t md_flags; /* Additional flags. */
170 uint16_t md_ealgo; /* Encryption algorithm. */
171 uint16_t md_keylen; /* Key length. */
172 uint16_t md_aalgo; /* Authentication algorithm. */
173 uint64_t md_provsize; /* Provider's size. */
174 uint32_t md_sectorsize; /* Sector size. */
175 uint8_t md_keys; /* Available keys. */
176 int32_t md_iterations; /* Number of iterations for PKCS#5v2. */
177 uint8_t md_salt[G_ELI_SALTLEN]; /* Salt. */
178 /* Encrypted master key (IV-key, Data-key, HMAC). */
179 uint8_t md_mkeys[G_ELI_MAXMKEYS * G_ELI_MKEYLEN];
180 u_char md_hash[16]; /* MD5 hash. */
181} __packed;
182#ifndef _OpenSSL_
183static __inline void
184eli_metadata_encode(struct g_eli_metadata *md, u_char *data)
185{
186 MD5_CTX ctx;
187 u_char *p;
188
189 p = data;
190 bcopy(md->md_magic, p, sizeof(md->md_magic)); p += sizeof(md->md_magic);
191 le32enc(p, md->md_version); p += sizeof(md->md_version);
192 le32enc(p, md->md_flags); p += sizeof(md->md_flags);
193 le16enc(p, md->md_ealgo); p += sizeof(md->md_ealgo);
194 le16enc(p, md->md_keylen); p += sizeof(md->md_keylen);
195 le16enc(p, md->md_aalgo); p += sizeof(md->md_aalgo);
196 le64enc(p, md->md_provsize); p += sizeof(md->md_provsize);
197 le32enc(p, md->md_sectorsize); p += sizeof(md->md_sectorsize);
198 *p = md->md_keys; p += sizeof(md->md_keys);
199 le32enc(p, md->md_iterations); p += sizeof(md->md_iterations);
200 bcopy(md->md_salt, p, sizeof(md->md_salt)); p += sizeof(md->md_salt);
201 bcopy(md->md_mkeys, p, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
202 MD5Init(&ctx);
203 MD5Update(&ctx, data, p - data);
204 MD5Final(md->md_hash, &ctx);
205 bcopy(md->md_hash, p, sizeof(md->md_hash));
206}
207static __inline int
208eli_metadata_decode_v0(const u_char *data, struct g_eli_metadata *md)
209{
210 MD5_CTX ctx;
211 const u_char *p;
212
213 p = data + sizeof(md->md_magic) + sizeof(md->md_version);
214 md->md_flags = le32dec(p); p += sizeof(md->md_flags);
215 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo);
216 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen);
217 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize);
218 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize);
219 md->md_keys = *p; p += sizeof(md->md_keys);
220 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations);
221 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
222 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
223 MD5Init(&ctx);
224 MD5Update(&ctx, data, p - data);
225 MD5Final(md->md_hash, &ctx);
226 if (bcmp(md->md_hash, p, 16) != 0)
227 return (EINVAL);
228 return (0);
229}
230static __inline int
231eli_metadata_decode_v1v2(const u_char *data, struct g_eli_metadata *md)
232{
233 MD5_CTX ctx;
234 const u_char *p;
235
236 p = data + sizeof(md->md_magic) + sizeof(md->md_version);
237 md->md_flags = le32dec(p); p += sizeof(md->md_flags);
238 md->md_ealgo = le16dec(p); p += sizeof(md->md_ealgo);
239 md->md_keylen = le16dec(p); p += sizeof(md->md_keylen);
240 md->md_aalgo = le16dec(p); p += sizeof(md->md_aalgo);
241 md->md_provsize = le64dec(p); p += sizeof(md->md_provsize);
242 md->md_sectorsize = le32dec(p); p += sizeof(md->md_sectorsize);
243 md->md_keys = *p; p += sizeof(md->md_keys);
244 md->md_iterations = le32dec(p); p += sizeof(md->md_iterations);
245 bcopy(p, md->md_salt, sizeof(md->md_salt)); p += sizeof(md->md_salt);
246 bcopy(p, md->md_mkeys, sizeof(md->md_mkeys)); p += sizeof(md->md_mkeys);
247 MD5Init(&ctx);
248 MD5Update(&ctx, data, p - data);
249 MD5Final(md->md_hash, &ctx);
250 if (bcmp(md->md_hash, p, 16) != 0)
251 return (EINVAL);
252 return (0);
253}
254static __inline int
255eli_metadata_decode(const u_char *data, struct g_eli_metadata *md)
256{
257 int error;
258
259 bcopy(data, md->md_magic, sizeof(md->md_magic));
260 md->md_version = le32dec(data + sizeof(md->md_magic));
261 switch (md->md_version) {
262 case 0:
263 error = eli_metadata_decode_v0(data, md);
264 break;
265 case 1:
266 case 2:
267 error = eli_metadata_decode_v1v2(data, md);
268 break;
269 default:
270 error = EINVAL;
271 break;
272 }
273 return (error);
274}
275#endif /* !_OpenSSL */
276
277static __inline u_int
278g_eli_str2ealgo(const char *name)
279{
280
281 if (strcasecmp("null", name) == 0)
282 return (CRYPTO_NULL_CBC);
283 else if (strcasecmp("aes", name) == 0)
284 return (CRYPTO_AES_CBC);
285 else if (strcasecmp("blowfish", name) == 0)
286 return (CRYPTO_BLF_CBC);
287 else if (strcasecmp("3des", name) == 0)
288 return (CRYPTO_3DES_CBC);
289 return (CRYPTO_ALGORITHM_MIN - 1);
290}
291
292static __inline u_int
293g_eli_str2aalgo(const char *name)
294{
295
296 if (strcasecmp("hmac/md5", name) == 0)
297 return (CRYPTO_MD5_HMAC);
298 else if (strcasecmp("hmac/sha1", name) == 0)
299 return (CRYPTO_SHA1_HMAC);
300 else if (strcasecmp("hmac/ripemd160", name) == 0)
301 return (CRYPTO_RIPEMD160_HMAC);
302 else if (strcasecmp("hmac/sha256", name) == 0)
303 return (CRYPTO_SHA2_256_HMAC);
304 else if (strcasecmp("hmac/sha384", name) == 0)
305 return (CRYPTO_SHA2_384_HMAC);
306 else if (strcasecmp("hmac/sha512", name) == 0)
307 return (CRYPTO_SHA2_512_HMAC);
308 return (CRYPTO_ALGORITHM_MIN - 1);
309}
310
311static __inline const char *
312g_eli_algo2str(u_int algo)
313{
314
315 switch (algo) {
316 case CRYPTO_NULL_CBC:
317 return ("NULL");
318 case CRYPTO_AES_CBC:
319 return ("AES-CBC");
320 case CRYPTO_BLF_CBC:
321 return ("Blowfish-CBC");
322 case CRYPTO_3DES_CBC:
323 return ("3DES-CBC");
324 case CRYPTO_MD5_HMAC:
325 return ("HMAC/MD5");
326 case CRYPTO_SHA1_HMAC:
327 return ("HMAC/SHA1");
328 case CRYPTO_RIPEMD160_HMAC:
329 return ("HMAC/RIPEMD160");
330 case CRYPTO_SHA2_256_HMAC:
331 return ("HMAC/SHA256");
332 case CRYPTO_SHA2_384_HMAC:
333 return ("HMAC/SHA384");
334 case CRYPTO_SHA2_512_HMAC:
335 return ("HMAC/SHA512");
336 }
337 return ("unknown");
338}
339
340static __inline void
341eli_metadata_dump(const struct g_eli_metadata *md)
342{
343 static const char hex[] = "0123456789abcdef";
344 char str[sizeof(md->md_mkeys) * 2 + 1];
345 u_int i;
346
347 printf(" magic: %s\n", md->md_magic);
348 printf(" version: %u\n", (u_int)md->md_version);
349 printf(" flags: 0x%x\n", (u_int)md->md_flags);
350 printf(" ealgo: %s\n", g_eli_algo2str(md->md_ealgo));
351 printf(" keylen: %u\n", (u_int)md->md_keylen);
352 if (md->md_flags & G_ELI_FLAG_AUTH)
353 printf(" aalgo: %s\n", g_eli_algo2str(md->md_aalgo));
354 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize);
355 printf("sectorsize: %u\n", (u_int)md->md_sectorsize);
356 printf(" keys: 0x%02x\n", (u_int)md->md_keys);
357 printf("iterations: %u\n", (u_int)md->md_iterations);
358 bzero(str, sizeof(str));
359 for (i = 0; i < sizeof(md->md_salt); i++) {
360 str[i * 2] = hex[md->md_salt[i] >> 4];
361 str[i * 2 + 1] = hex[md->md_salt[i] & 0x0f];
362 }
363 printf(" Salt: %s\n", str);
364 bzero(str, sizeof(str));
365 for (i = 0; i < sizeof(md->md_mkeys); i++) {
366 str[i * 2] = hex[md->md_mkeys[i] >> 4];
367 str[i * 2 + 1] = hex[md->md_mkeys[i] & 0x0f];
368 }
369 printf("Master Key: %s\n", str);
370 bzero(str, sizeof(str));
371 for (i = 0; i < 16; i++) {
372 str[i * 2] = hex[md->md_hash[i] >> 4];
373 str[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
374 }
375 printf(" MD5 hash: %s\n", str);
376}
377
378static __inline u_int
379g_eli_keylen(u_int algo, u_int keylen)
380{
381
382 switch (algo) {
383 case CRYPTO_NULL_CBC:
384 if (keylen == 0)
385 keylen = 64 * 8;
386 else {
387 if (keylen > 64 * 8)
388 keylen = 0;
389 }
390 return (keylen);
391 case CRYPTO_AES_CBC:
392 switch (keylen) {
393 case 0:
394 return (128);
395 case 128:
396 case 192:
397 case 256:
398 return (keylen);
399 default:
400 return (0);
401 }
402 case CRYPTO_BLF_CBC:
403 if (keylen == 0)
404 return (128);
405 if (keylen < 128 || keylen > 448)
406 return (0);
407 if ((keylen % 32) != 0)
408 return (0);
409 return (keylen);
410 case CRYPTO_3DES_CBC:
411 if (keylen == 0 || keylen == 192)
412 return (192);
413 return (0);
414 default:
415 return (0);
416 }
417}
418
419static __inline u_int
420g_eli_hashlen(u_int algo)
421{
422
423 switch (algo) {
424 case CRYPTO_MD5_HMAC:
425 return (16);
426 case CRYPTO_SHA1_HMAC:
427 return (20);
428 case CRYPTO_RIPEMD160_HMAC:
429 return (20);
430 case CRYPTO_SHA2_256_HMAC:
431 return (32);
432 case CRYPTO_SHA2_384_HMAC:
433 return (48);
434 case CRYPTO_SHA2_512_HMAC:
435 return (64);
436 }
437 return (0);
438}
439
440#ifdef _KERNEL
441int g_eli_read_metadata(struct g_class *mp, struct g_provider *pp,
442 struct g_eli_metadata *md);
443struct g_geom *g_eli_create(struct gctl_req *req, struct g_class *mp,
444 struct g_provider *bpp, const struct g_eli_metadata *md,
445 const u_char *mkey, int nkey);
446int g_eli_destroy(struct g_eli_softc *sc, boolean_t force);
447
448int g_eli_access(struct g_provider *pp, int dr, int dw, int de);
449void g_eli_config(struct gctl_req *req, struct g_class *mp, const char *verb);
450
451void g_eli_read_done(struct bio *bp);
452void g_eli_write_done(struct bio *bp);
453int g_eli_crypto_rerun(struct cryptop *crp);
454void g_eli_crypto_ivgen(struct g_eli_softc *sc, off_t offset, u_char *iv,
455 size_t size);
456
457void g_eli_crypto_run(struct g_eli_worker *wr, struct bio *bp);
458
459void g_eli_auth_read(struct g_eli_softc *sc, struct bio *bp);
460void g_eli_auth_run(struct g_eli_worker *wr, struct bio *bp);
461#endif
462
463void g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key);
464int g_eli_mkey_decrypt(const struct g_eli_metadata *md,
465 const unsigned char *key, unsigned char *mkey, unsigned *nkeyp);
466int g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
467 unsigned char *mkey);
468#ifdef _KERNEL
469void g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey);
470#endif
471
472int g_eli_crypto_encrypt(u_int algo, u_char *data, size_t datasize,
473 const u_char *key, size_t keysize);
474int g_eli_crypto_decrypt(u_int algo, u_char *data, size_t datasize,
475 const u_char *key, size_t keysize);
476
477struct hmac_ctx {
478 SHA512_CTX shactx;
479 u_char k_opad[128];
480};
481
482void g_eli_crypto_hmac_init(struct hmac_ctx *ctx, const uint8_t *hkey,
483 size_t hkeylen);
484void g_eli_crypto_hmac_update(struct hmac_ctx *ctx, const uint8_t *data,
485 size_t datasize);
486void g_eli_crypto_hmac_final(struct hmac_ctx *ctx, uint8_t *md, size_t mdsize);
487void g_eli_crypto_hmac(const uint8_t *hkey, size_t hkeysize,
488 const uint8_t *data, size_t datasize, uint8_t *md, size_t mdsize);
489#endif /* !_G_ELI_H_ */