Deleted Added
full compact
g_eli_key.c (202976) g_eli_key.c (213067)
1/*-
2 * Copyright (c) 2005 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

--- 11 unchanged lines hidden (view full) ---

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
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2005 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

--- 11 unchanged lines hidden (view full) ---

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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_key.c 202976 2010-01-25 16:58:58Z trasz $");
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_key.c 213067 2010-09-23 11:49:47Z pjd $");
29
30#include <sys/param.h>
31#ifdef _KERNEL
32#include <sys/malloc.h>
33#include <sys/systm.h>
34#include <geom/geom.h>
35#else
36#include <stdio.h>
37#include <stdint.h>
38#include <stdlib.h>
39#include <string.h>
40#include <strings.h>
41#include <errno.h>
42#endif
43
44#include <geom/eli/g_eli.h>
45
29
30#include <sys/param.h>
31#ifdef _KERNEL
32#include <sys/malloc.h>
33#include <sys/systm.h>
34#include <geom/geom.h>
35#else
36#include <stdio.h>
37#include <stdint.h>
38#include <stdlib.h>
39#include <string.h>
40#include <strings.h>
41#include <errno.h>
42#endif
43
44#include <geom/eli/g_eli.h>
45
46#ifdef _KERNEL
47MALLOC_DECLARE(M_ELI);
48#endif
46
47/*
48 * Verify if the given 'key' is correct.
49 * Return 1 if it is correct and 0 otherwise.
50 */
51static int
52g_eli_mkey_verify(const unsigned char *mkey, const unsigned char *key)
53{

--- 119 unchanged lines hidden (view full) ---

173 error = g_eli_crypto_encrypt(algo, mkey, G_ELI_MKEYLEN, enckey, keylen);
174
175 bzero(enckey, sizeof(enckey));
176
177 return (error);
178}
179
180#ifdef _KERNEL
49
50/*
51 * Verify if the given 'key' is correct.
52 * Return 1 if it is correct and 0 otherwise.
53 */
54static int
55g_eli_mkey_verify(const unsigned char *mkey, const unsigned char *key)
56{

--- 119 unchanged lines hidden (view full) ---

176 error = g_eli_crypto_encrypt(algo, mkey, G_ELI_MKEYLEN, enckey, keylen);
177
178 bzero(enckey, sizeof(enckey));
179
180 return (error);
181}
182
183#ifdef _KERNEL
184static void
185g_eli_ekeys_generate(struct g_eli_softc *sc)
186{
187 uint8_t *keys;
188 u_int kno;
189 off_t mediasize;
190 size_t blocksize;
191 struct {
192 char magic[4];
193 uint8_t keyno[8];
194 } __packed hmacdata;
195
196 KASSERT((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) == 0,
197 ("%s: G_ELI_FLAG_SINGLE_KEY flag present", __func__));
198
199 if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
200 struct g_provider *pp;
201
202 pp = LIST_FIRST(&sc->sc_geom->consumer)->provider;
203 mediasize = pp->mediasize;
204 blocksize = pp->sectorsize;
205 } else {
206 mediasize = sc->sc_mediasize;
207 blocksize = sc->sc_sectorsize;
208 }
209 sc->sc_nekeys = ((mediasize - 1) >> G_ELI_KEY_SHIFT) / blocksize + 1;
210 sc->sc_ekeys =
211 malloc(sc->sc_nekeys * (sizeof(uint8_t *) + G_ELI_DATAKEYLEN),
212 M_ELI, M_WAITOK);
213 keys = (uint8_t *)(sc->sc_ekeys + sc->sc_nekeys);
214 bcopy("ekey", hmacdata.magic, 4);
215 for (kno = 0; kno < sc->sc_nekeys; kno++, keys += G_ELI_DATAKEYLEN) {
216 sc->sc_ekeys[kno] = keys;
217 le64enc(hmacdata.keyno, (uint64_t)kno);
218 g_eli_crypto_hmac(sc->sc_mkey, G_ELI_MAXKEYLEN,
219 (uint8_t *)&hmacdata, sizeof(hmacdata),
220 sc->sc_ekeys[kno], 0);
221 }
222}
223
181/*
182 * When doing encryption only, copy IV key and encryption key.
183 * When doing encryption and authentication, copy IV key, generate encryption
184 * key and generate authentication key.
185 */
186void
187g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey)
188{
189
190 /* Remember the Master Key. */
191 bcopy(mkey, sc->sc_mkey, sizeof(sc->sc_mkey));
192
193 bcopy(mkey, sc->sc_ivkey, sizeof(sc->sc_ivkey));
194 mkey += sizeof(sc->sc_ivkey);
195
224/*
225 * When doing encryption only, copy IV key and encryption key.
226 * When doing encryption and authentication, copy IV key, generate encryption
227 * key and generate authentication key.
228 */
229void
230g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey)
231{
232
233 /* Remember the Master Key. */
234 bcopy(mkey, sc->sc_mkey, sizeof(sc->sc_mkey));
235
236 bcopy(mkey, sc->sc_ivkey, sizeof(sc->sc_ivkey));
237 mkey += sizeof(sc->sc_ivkey);
238
196 if (!(sc->sc_flags & G_ELI_FLAG_AUTH)) {
197 bcopy(mkey, sc->sc_ekey, sizeof(sc->sc_ekey));
239 /*
240 * The authentication key is: akey = HMAC_SHA512(Master-Key, 0x11)
241 */
242 if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
243 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x11", 1,
244 sc->sc_akey, 0);
198 } else {
245 } else {
199 /*
200 * The encryption key is: ekey = HMAC_SHA512(Master-Key, 0x10)
201 * The authentication key is: akey = HMAC_SHA512(Master-Key, 0x11)
202 */
203 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x10", 1, sc->sc_ekey, 0);
204 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x11", 1, sc->sc_akey, 0);
246 arc4rand(sc->sc_akey, sizeof(sc->sc_akey), 0);
205 }
206
247 }
248
249 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) != 0) {
250 sc->sc_nekeys = 1;
251 sc->sc_ekeys = malloc(sc->sc_nekeys *
252 (sizeof(uint8_t *) + G_ELI_DATAKEYLEN), M_ELI, M_WAITOK);
253 sc->sc_ekeys[0] = (uint8_t *)(sc->sc_ekeys + sc->sc_nekeys);
254 if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0)
255 bcopy(mkey, sc->sc_ekeys[0], G_ELI_DATAKEYLEN);
256 else {
257 /*
258 * The encryption key is: ekey = HMAC_SHA512(Master-Key, 0x10)
259 */
260 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x10", 1,
261 sc->sc_ekeys[0], 0);
262 }
263 } else {
264 /* Generate all encryption keys. */
265 g_eli_ekeys_generate(sc);
266 }
207}
208#endif
267}
268#endif