xref: /freebsd/sys/geom/eli/g_eli_key.c (revision eaa3b91996d80ab0c2d5574e70773119f0f13dfc)
1c58794deSPawel Jakub Dawidek /*-
2c58794deSPawel Jakub Dawidek  * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3c58794deSPawel Jakub Dawidek  * All rights reserved.
4c58794deSPawel Jakub Dawidek  *
5c58794deSPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
6c58794deSPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
7c58794deSPawel Jakub Dawidek  * are met:
8c58794deSPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
9c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
10c58794deSPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
11c58794deSPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
12c58794deSPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
13c58794deSPawel Jakub Dawidek  *
14c58794deSPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15c58794deSPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c58794deSPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c58794deSPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18c58794deSPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c58794deSPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c58794deSPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c58794deSPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c58794deSPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c58794deSPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c58794deSPawel Jakub Dawidek  * SUCH DAMAGE.
25c58794deSPawel Jakub Dawidek  */
26c58794deSPawel Jakub Dawidek 
27c58794deSPawel Jakub Dawidek #include <sys/cdefs.h>
28c58794deSPawel Jakub Dawidek __FBSDID("$FreeBSD$");
29c58794deSPawel Jakub Dawidek 
30c58794deSPawel Jakub Dawidek #include <sys/param.h>
31c58794deSPawel Jakub Dawidek #ifdef _KERNEL
32c58794deSPawel Jakub Dawidek #include <sys/malloc.h>
33c58794deSPawel Jakub Dawidek #include <sys/systm.h>
34c58794deSPawel Jakub Dawidek #include <geom/geom.h>
35c58794deSPawel Jakub Dawidek #else
36c58794deSPawel Jakub Dawidek #include <stdio.h>
37c58794deSPawel Jakub Dawidek #include <stdint.h>
38c58794deSPawel Jakub Dawidek #include <stdlib.h>
39c58794deSPawel Jakub Dawidek #include <string.h>
40c58794deSPawel Jakub Dawidek #include <strings.h>
41c58794deSPawel Jakub Dawidek #include <errno.h>
42c58794deSPawel Jakub Dawidek #endif
43c58794deSPawel Jakub Dawidek 
44c58794deSPawel Jakub Dawidek #include <geom/eli/g_eli.h>
45c58794deSPawel Jakub Dawidek 
46c58794deSPawel Jakub Dawidek 
47c58794deSPawel Jakub Dawidek /*
48c58794deSPawel Jakub Dawidek  * Verify if the given 'key' is correct.
49c58794deSPawel Jakub Dawidek  * Return 1 if it is correct and 0 otherwise.
50c58794deSPawel Jakub Dawidek  */
51c58794deSPawel Jakub Dawidek static int
52c58794deSPawel Jakub Dawidek g_eli_mkey_verify(const unsigned char *mkey, const unsigned char *key)
53c58794deSPawel Jakub Dawidek {
54c58794deSPawel Jakub Dawidek 	const unsigned char *odhmac;	/* On-disk HMAC. */
55c58794deSPawel Jakub Dawidek 	unsigned char chmac[SHA512_MDLEN];	/* Calculated HMAC. */
56c58794deSPawel Jakub Dawidek 	unsigned char hmkey[SHA512_MDLEN];	/* Key for HMAC. */
57c58794deSPawel Jakub Dawidek 
58c58794deSPawel Jakub Dawidek 	/*
59c58794deSPawel Jakub Dawidek 	 * The key for HMAC calculations is: hmkey = HMAC_SHA512(Derived-Key, 0)
60c58794deSPawel Jakub Dawidek 	 */
61c58794deSPawel Jakub Dawidek 	g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x00", 1, hmkey, 0);
62c58794deSPawel Jakub Dawidek 
63c58794deSPawel Jakub Dawidek 	odhmac = mkey + G_ELI_DATAIVKEYLEN;
64c58794deSPawel Jakub Dawidek 
65c58794deSPawel Jakub Dawidek 	/* Calculate HMAC from Data-Key and IV-Key. */
66c58794deSPawel Jakub Dawidek 	g_eli_crypto_hmac(hmkey, sizeof(hmkey), mkey, G_ELI_DATAIVKEYLEN,
67c58794deSPawel Jakub Dawidek 	    chmac, 0);
68c58794deSPawel Jakub Dawidek 
69c58794deSPawel Jakub Dawidek 	bzero(hmkey, sizeof(hmkey));
70c58794deSPawel Jakub Dawidek 
71c58794deSPawel Jakub Dawidek 	/*
72c58794deSPawel Jakub Dawidek 	 * Compare calculated HMAC with HMAC from metadata.
73c58794deSPawel Jakub Dawidek 	 * If two HMACs are equal, 'key' is correct.
74c58794deSPawel Jakub Dawidek 	 */
75c58794deSPawel Jakub Dawidek 	return (!bcmp(odhmac, chmac, SHA512_MDLEN));
76c58794deSPawel Jakub Dawidek }
77c58794deSPawel Jakub Dawidek 
78c58794deSPawel Jakub Dawidek /*
79c58794deSPawel Jakub Dawidek  * Calculate HMAC from Data-Key and IV-Key.
80c58794deSPawel Jakub Dawidek  */
81c58794deSPawel Jakub Dawidek void
82c58794deSPawel Jakub Dawidek g_eli_mkey_hmac(unsigned char *mkey, const unsigned char *key)
83c58794deSPawel Jakub Dawidek {
84c58794deSPawel Jakub Dawidek 	unsigned char hmkey[SHA512_MDLEN];	/* Key for HMAC. */
85c58794deSPawel Jakub Dawidek 	unsigned char *odhmac;	/* On-disk HMAC. */
86c58794deSPawel Jakub Dawidek 
87c58794deSPawel Jakub Dawidek 	/*
88c58794deSPawel Jakub Dawidek 	 * The key for HMAC calculations is: hmkey = HMAC_SHA512(Derived-Key, 0)
89c58794deSPawel Jakub Dawidek 	 */
90c58794deSPawel Jakub Dawidek 	g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x00", 1, hmkey, 0);
91c58794deSPawel Jakub Dawidek 
92c58794deSPawel Jakub Dawidek 	odhmac = mkey + G_ELI_DATAIVKEYLEN;
93c58794deSPawel Jakub Dawidek 	/* Calculate HMAC from Data-Key and IV-Key. */
94c58794deSPawel Jakub Dawidek 	g_eli_crypto_hmac(hmkey, sizeof(hmkey), mkey, G_ELI_DATAIVKEYLEN,
95c58794deSPawel Jakub Dawidek 	    odhmac, 0);
96c58794deSPawel Jakub Dawidek 
97c58794deSPawel Jakub Dawidek 	bzero(hmkey, sizeof(hmkey));
98c58794deSPawel Jakub Dawidek }
99c58794deSPawel Jakub Dawidek 
100c58794deSPawel Jakub Dawidek /*
101c58794deSPawel Jakub Dawidek  * Find and decrypt Master Key encrypted with 'key'.
102c58794deSPawel Jakub Dawidek  * Return decrypted Master Key number in 'nkeyp' if not NULL.
103c58794deSPawel Jakub Dawidek  * Return 0 on success, > 0 on failure, -1 on bad key.
104c58794deSPawel Jakub Dawidek  */
105c58794deSPawel Jakub Dawidek int
106c58794deSPawel Jakub Dawidek g_eli_mkey_decrypt(const struct g_eli_metadata *md, const unsigned char *key,
107c58794deSPawel Jakub Dawidek     unsigned char *mkey, unsigned *nkeyp)
108c58794deSPawel Jakub Dawidek {
109c58794deSPawel Jakub Dawidek 	unsigned char tmpmkey[G_ELI_MKEYLEN];
110c58794deSPawel Jakub Dawidek 	unsigned char enckey[SHA512_MDLEN];	/* Key for encryption. */
111c58794deSPawel Jakub Dawidek 	const unsigned char *mmkey;
112c58794deSPawel Jakub Dawidek 	int bit, error, nkey;
113c58794deSPawel Jakub Dawidek 
114c58794deSPawel Jakub Dawidek 	if (nkeyp != NULL)
115c58794deSPawel Jakub Dawidek 		*nkeyp = -1;
116c58794deSPawel Jakub Dawidek 
117c58794deSPawel Jakub Dawidek 	/*
118c58794deSPawel Jakub Dawidek 	 * The key for encryption is: enckey = HMAC_SHA512(Derived-Key, 1)
119c58794deSPawel Jakub Dawidek 	 */
120c58794deSPawel Jakub Dawidek 	g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x01", 1, enckey, 0);
121c58794deSPawel Jakub Dawidek 
122c58794deSPawel Jakub Dawidek 	mmkey = md->md_mkeys;
123c58794deSPawel Jakub Dawidek 	nkey = 0;
124c58794deSPawel Jakub Dawidek 	for (nkey = 0; nkey < G_ELI_MAXMKEYS; nkey++, mmkey += G_ELI_MKEYLEN) {
125c58794deSPawel Jakub Dawidek 		bit = (1 << nkey);
126eaa3b919SPawel Jakub Dawidek 		if (!(md->md_keys & bit))
127c58794deSPawel Jakub Dawidek 			continue;
128c58794deSPawel Jakub Dawidek 		bcopy(mmkey, tmpmkey, G_ELI_MKEYLEN);
129eaa3b919SPawel Jakub Dawidek 		error = g_eli_crypto_decrypt(md->md_ealgo, tmpmkey,
130c58794deSPawel Jakub Dawidek 		    G_ELI_MKEYLEN, enckey, md->md_keylen);
131c58794deSPawel Jakub Dawidek 		if (error != 0) {
132c58794deSPawel Jakub Dawidek 			bzero(tmpmkey, sizeof(tmpmkey));
133c58794deSPawel Jakub Dawidek 			bzero(enckey, sizeof(enckey));
134c58794deSPawel Jakub Dawidek 			return (error);
135c58794deSPawel Jakub Dawidek 		}
136c58794deSPawel Jakub Dawidek 		if (g_eli_mkey_verify(tmpmkey, key)) {
137c58794deSPawel Jakub Dawidek 			bcopy(tmpmkey, mkey, G_ELI_DATAIVKEYLEN);
138c58794deSPawel Jakub Dawidek 			bzero(tmpmkey, sizeof(tmpmkey));
139c58794deSPawel Jakub Dawidek 			bzero(enckey, sizeof(enckey));
140c58794deSPawel Jakub Dawidek 			if (nkeyp != NULL)
141c58794deSPawel Jakub Dawidek 				*nkeyp = nkey;
142c58794deSPawel Jakub Dawidek 			return (0);
143c58794deSPawel Jakub Dawidek 		}
144c58794deSPawel Jakub Dawidek 	}
145c58794deSPawel Jakub Dawidek 	bzero(enckey, sizeof(enckey));
146c58794deSPawel Jakub Dawidek 	bzero(tmpmkey, sizeof(tmpmkey));
147c58794deSPawel Jakub Dawidek 	return (-1);
148c58794deSPawel Jakub Dawidek }
149c58794deSPawel Jakub Dawidek 
150c58794deSPawel Jakub Dawidek /*
151c58794deSPawel Jakub Dawidek  * Encrypt the Master-Key and calculate HMAC to be able to verify it in the
152c58794deSPawel Jakub Dawidek  * future.
153c58794deSPawel Jakub Dawidek  */
154c58794deSPawel Jakub Dawidek int
155c58794deSPawel Jakub Dawidek g_eli_mkey_encrypt(unsigned algo, const unsigned char *key, unsigned keylen,
156c58794deSPawel Jakub Dawidek     unsigned char *mkey)
157c58794deSPawel Jakub Dawidek {
158c58794deSPawel Jakub Dawidek 	unsigned char enckey[SHA512_MDLEN];	/* Key for encryption. */
159c58794deSPawel Jakub Dawidek 	int error;
160c58794deSPawel Jakub Dawidek 
161c58794deSPawel Jakub Dawidek 	/*
162c58794deSPawel Jakub Dawidek 	 * To calculate HMAC, the whole key (G_ELI_USERKEYLEN bytes long) will
163c58794deSPawel Jakub Dawidek 	 * be used.
164c58794deSPawel Jakub Dawidek 	 */
165c58794deSPawel Jakub Dawidek 	g_eli_mkey_hmac(mkey, key);
166c58794deSPawel Jakub Dawidek 	/*
167c58794deSPawel Jakub Dawidek 	 * The key for encryption is: enckey = HMAC_SHA512(Derived-Key, 1)
168c58794deSPawel Jakub Dawidek 	 */
169c58794deSPawel Jakub Dawidek 	g_eli_crypto_hmac(key, G_ELI_USERKEYLEN, "\x01", 1, enckey, 0);
170c58794deSPawel Jakub Dawidek 	/*
171c58794deSPawel Jakub Dawidek 	 * Encrypt the Master-Key and HMAC() result with the given key (this
172c58794deSPawel Jakub Dawidek 	 * time only 'keylen' bits from the key are used).
173c58794deSPawel Jakub Dawidek 	 */
174c58794deSPawel Jakub Dawidek 	error = g_eli_crypto_encrypt(algo, mkey, G_ELI_MKEYLEN, enckey, keylen);
175c58794deSPawel Jakub Dawidek 
176c58794deSPawel Jakub Dawidek 	bzero(enckey, sizeof(enckey));
177c58794deSPawel Jakub Dawidek 
178c58794deSPawel Jakub Dawidek 	return (error);
179c58794deSPawel Jakub Dawidek }
180eaa3b919SPawel Jakub Dawidek 
181eaa3b919SPawel Jakub Dawidek #ifdef _KERNEL
182eaa3b919SPawel Jakub Dawidek /*
183eaa3b919SPawel Jakub Dawidek  * When doing encryption only, copy IV key and encryption key.
184eaa3b919SPawel Jakub Dawidek  * When doing encryption and authentication, copy IV key, generate encryption
185eaa3b919SPawel Jakub Dawidek  * key and generate authentication key.
186eaa3b919SPawel Jakub Dawidek  */
187eaa3b919SPawel Jakub Dawidek void
188eaa3b919SPawel Jakub Dawidek g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey)
189eaa3b919SPawel Jakub Dawidek {
190eaa3b919SPawel Jakub Dawidek 
191eaa3b919SPawel Jakub Dawidek 	/* Remember the Master Key. */
192eaa3b919SPawel Jakub Dawidek 	bcopy(mkey, sc->sc_mkey, sizeof(sc->sc_mkey));
193eaa3b919SPawel Jakub Dawidek 
194eaa3b919SPawel Jakub Dawidek 	bcopy(mkey, sc->sc_ivkey, sizeof(sc->sc_ivkey));
195eaa3b919SPawel Jakub Dawidek 	mkey += sizeof(sc->sc_ivkey);
196eaa3b919SPawel Jakub Dawidek 
197eaa3b919SPawel Jakub Dawidek 	if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
198eaa3b919SPawel Jakub Dawidek 		bcopy(mkey, sc->sc_ekey, sizeof(sc->sc_ekey));
199eaa3b919SPawel Jakub Dawidek 	} else {
200eaa3b919SPawel Jakub Dawidek 		/*
201eaa3b919SPawel Jakub Dawidek 		 * The encryption key is: ekey = HMAC_SHA512(Master-Key, 0x10)
202eaa3b919SPawel Jakub Dawidek 		 * The authentication key is: akey = HMAC_SHA512(Master-Key, 0x11)
203eaa3b919SPawel Jakub Dawidek 		 */
204eaa3b919SPawel Jakub Dawidek 		g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x10", 1, sc->sc_ekey, 0);
205eaa3b919SPawel Jakub Dawidek 		g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x11", 1, sc->sc_akey, 0);
206eaa3b919SPawel Jakub Dawidek 	}
207eaa3b919SPawel Jakub Dawidek 
208eaa3b919SPawel Jakub Dawidek }
209eaa3b919SPawel Jakub Dawidek #endif
210