2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34 #include <geom/geom.h>
44 #include <geom/eli/g_eli.h>
47 MALLOC_DECLARE(M_ELI);
51 * Verify if the given 'key' is correct.
52 * Return 1 if it is correct and 0 otherwise.
55 g_eli_mkey_verify(const unsigned char *mkey, const unsigned char *key)
57 const unsigned char *odhmac; /* On-disk HMAC. */
58 unsigned char chmac[SHA512_MDLEN]; /* Calculated HMAC. */
59 unsigned char hmkey[SHA512_MDLEN]; /* Key for HMAC. */
62 * The key for HMAC calculations is: hmkey = HMAC_SHA512(Derived-Key, 0)
64 g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x00", 1, hmkey, 0);
66 odhmac = mkey + G_ELI_DATAIVKEYLEN;
68 /* Calculate HMAC from Data-Key and IV-Key. */
69 g_eli_crypto_hmac(hmkey, sizeof(hmkey), mkey, G_ELI_DATAIVKEYLEN,
72 bzero(hmkey, sizeof(hmkey));
75 * Compare calculated HMAC with HMAC from metadata.
76 * If two HMACs are equal, 'key' is correct.
78 return (!bcmp(odhmac, chmac, SHA512_MDLEN));
82 * Calculate HMAC from Data-Key and IV-Key.
85 g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key)
87 unsigned char hmkey[SHA512_MDLEN]; /* Key for HMAC. */
88 unsigned char *odhmac; /* On-disk HMAC. */
91 * The key for HMAC calculations is: hmkey = HMAC_SHA512(Derived-Key, 0)
93 g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x00", 1, hmkey, 0);
95 odhmac = mkey + G_ELI_DATAIVKEYLEN;
96 /* Calculate HMAC from Data-Key and IV-Key. */
97 g_eli_crypto_hmac(hmkey, sizeof(hmkey), mkey, G_ELI_DATAIVKEYLEN,
100 bzero(hmkey, sizeof(hmkey));
104 * Find and decrypt Master Key encrypted with 'key'.
105 * Return decrypted Master Key number in 'nkeyp' if not NULL.
106 * Return 0 on success, > 0 on failure, -1 on bad key.
109 g_eli_mkey_decrypt(const struct g_eli_metadata *md, const unsigned char *key,
110 unsigned char *mkey, unsigned *nkeyp)
112 unsigned char tmpmkey[G_ELI_MKEYLEN];
113 unsigned char enckey[SHA512_MDLEN]; /* Key for encryption. */
114 const unsigned char *mmkey;
115 int bit, error, nkey;
121 * The key for encryption is: enckey = HMAC_SHA512(Derived-Key, 1)
123 g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x01", 1, enckey, 0);
125 mmkey = md->md_mkeys;
126 for (nkey = 0; nkey < G_ELI_MAXMKEYS; nkey++, mmkey += G_ELI_MKEYLEN) {
128 if (!(md->md_keys & bit))
130 bcopy(mmkey, tmpmkey, G_ELI_MKEYLEN);
131 error = g_eli_crypto_decrypt(md->md_ealgo, tmpmkey,
132 G_ELI_MKEYLEN, enckey, md->md_keylen);
134 bzero(tmpmkey, sizeof(tmpmkey));
135 bzero(enckey, sizeof(enckey));
138 if (g_eli_mkey_verify(tmpmkey, key)) {
139 bcopy(tmpmkey, mkey, G_ELI_DATAIVKEYLEN);
140 bzero(tmpmkey, sizeof(tmpmkey));
141 bzero(enckey, sizeof(enckey));
147 bzero(enckey, sizeof(enckey));
148 bzero(tmpmkey, sizeof(tmpmkey));
153 * Encrypt the Master-Key and calculate HMAC to be able to verify it in the
157 g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
160 unsigned char enckey[SHA512_MDLEN]; /* Key for encryption. */
164 * To calculate HMAC, the whole key (G_ELI_USERKEYLEN bytes long) will
167 g_eli_mkey_hmac(mkey, key);
169 * The key for encryption is: enckey = HMAC_SHA512(Derived-Key, 1)
171 g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x01", 1, enckey, 0);
173 * Encrypt the Master-Key and HMAC() result with the given key (this
174 * time only 'keylen' bits from the key are used).
176 error = g_eli_crypto_encrypt(algo, mkey, G_ELI_MKEYLEN, enckey, keylen);
178 bzero(enckey, sizeof(enckey));
185 * When doing encryption only, copy IV key and encryption key.
186 * When doing encryption and authentication, copy IV key, generate encryption
187 * key and generate authentication key.
190 g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey)
193 /* Remember the Master Key. */
194 bcopy(mkey, sc->sc_mkey, sizeof(sc->sc_mkey));
196 bcopy(mkey, sc->sc_ivkey, sizeof(sc->sc_ivkey));
197 mkey += sizeof(sc->sc_ivkey);
200 * The authentication key is: akey = HMAC_SHA512(Data-Key, 0x11)
202 if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
203 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x11", 1,
206 arc4rand(sc->sc_akey, sizeof(sc->sc_akey), 0);
209 /* Initialize encryption keys. */
212 if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
214 * Precalculate SHA256 for HMAC key generation.
215 * This is expensive operation and we can do it only once now or
216 * for every access to sector, so now will be much better.
218 SHA256_Init(&sc->sc_akeyctx);
219 SHA256_Update(&sc->sc_akeyctx, sc->sc_akey,
220 sizeof(sc->sc_akey));
223 * Precalculate SHA256 for IV generation.
224 * This is expensive operation and we can do it only once now or for
225 * every access to sector, so now will be much better.
227 switch (sc->sc_ealgo) {
231 SHA256_Init(&sc->sc_ivctx);
232 SHA256_Update(&sc->sc_ivctx, sc->sc_ivkey,
233 sizeof(sc->sc_ivkey));