Deleted Added
full compact
g_eli_key.c (214225) g_eli_key.c (220922)
1/*-
1/*-
2 * Copyright (c) 2005-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
2 * Copyright (c) 2005-2011 Pawel Jakub Dawidek <pawel@dawidek.net>
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

--- 9 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>
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

--- 9 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 214225 2010-10-22 22:13:11Z pjd $");
28__FBSDID("$FreeBSD: head/sys/geom/eli/g_eli_key.c 220922 2011-04-21 13:31:43Z 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>

--- 139 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
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>

--- 139 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
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{

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

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);
245 } else {
246 arc4rand(sc->sc_akey, sizeof(sc->sc_akey), 0);
247 }
248
184/*
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.
188 */
189void
190g_eli_mkey_propagate(struct g_eli_softc *sc, const unsigned char *mkey)
191{

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

201 */
202 if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
203 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x11", 1,
204 sc->sc_akey, 0);
205 } else {
206 arc4rand(sc->sc_akey, sizeof(sc->sc_akey), 0);
207 }
208
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 }
209 /* Initialize encryption keys. */
210 g_eli_key_init(sc);
267
268 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
269 /*
270 * Precalculate SHA256 for HMAC key generation.
271 * This is expensive operation and we can do it only once now or
272 * for every access to sector, so now will be much better.
273 */
274 SHA256_Init(&sc->sc_akeyctx);

--- 19 unchanged lines hidden ---
211
212 if (sc->sc_flags & G_ELI_FLAG_AUTH) {
213 /*
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.
217 */
218 SHA256_Init(&sc->sc_akeyctx);

--- 19 unchanged lines hidden ---