1 /*-
2 * Copyright (c) 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
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #ifdef _KERNEL
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/sysctl.h>
35 #include <sys/systm.h>
36 #endif /* _KERNEL */
37 #include <sys/queue.h>
38 #include <sys/tree.h>
39
40 #include <geom/geom.h>
41
42 #include <geom/eli/g_eli.h>
43
44 #ifdef _KERNEL
45 MALLOC_DECLARE(M_ELI);
46
47 SYSCTL_DECL(_kern_geom_eli);
48 /*
49 * The default limit (8192 keys) will allow to cache all keys for 4TB
50 * provider with 512 bytes sectors and will take around 1MB of memory.
51 */
52 static u_int g_eli_key_cache_limit = 8192;
53 SYSCTL_UINT(_kern_geom_eli, OID_AUTO, key_cache_limit, CTLFLAG_RDTUN,
54 &g_eli_key_cache_limit, 0, "Maximum number of encryption keys to cache");
55 static uint64_t g_eli_key_cache_hits;
56 SYSCTL_UQUAD(_kern_geom_eli, OID_AUTO, key_cache_hits, CTLFLAG_RW,
57 &g_eli_key_cache_hits, 0, "Key cache hits");
58 static uint64_t g_eli_key_cache_misses;
59 SYSCTL_UQUAD(_kern_geom_eli, OID_AUTO, key_cache_misses, CTLFLAG_RW,
60 &g_eli_key_cache_misses, 0, "Key cache misses");
61
62 static int
g_eli_key_cmp(const struct g_eli_key * a,const struct g_eli_key * b)63 g_eli_key_cmp(const struct g_eli_key *a, const struct g_eli_key *b)
64 {
65
66 if (a->gek_keyno > b->gek_keyno)
67 return (1);
68 else if (a->gek_keyno < b->gek_keyno)
69 return (-1);
70 return (0);
71 }
72 #endif /* _KERNEL */
73
74 void
g_eli_key_fill(struct g_eli_softc * sc,struct g_eli_key * key,uint64_t keyno)75 g_eli_key_fill(struct g_eli_softc *sc, struct g_eli_key *key, uint64_t keyno)
76 {
77 const uint8_t *ekey;
78 struct {
79 char magic[4];
80 uint8_t keyno[8];
81 } __packed hmacdata;
82
83 if ((sc->sc_flags & G_ELI_FLAG_ENC_IVKEY) != 0)
84 ekey = sc->sc_mkey;
85 else
86 ekey = sc->sc_ekey;
87
88 bcopy("ekey", hmacdata.magic, 4);
89 le64enc(hmacdata.keyno, keyno);
90 g_eli_crypto_hmac(ekey, G_ELI_MAXKEYLEN, (uint8_t *)&hmacdata,
91 sizeof(hmacdata), key->gek_key, 0);
92 key->gek_keyno = keyno;
93 key->gek_count = 0;
94 key->gek_magic = G_ELI_KEY_MAGIC;
95 }
96
97 #ifdef _KERNEL
98 RB_PROTOTYPE(g_eli_key_tree, g_eli_key, gek_link, g_eli_key_cmp);
99 RB_GENERATE(g_eli_key_tree, g_eli_key, gek_link, g_eli_key_cmp);
100
101 static struct g_eli_key *
g_eli_key_allocate(struct g_eli_softc * sc,uint64_t keyno)102 g_eli_key_allocate(struct g_eli_softc *sc, uint64_t keyno)
103 {
104 struct g_eli_key *key, *ekey, keysearch;
105
106 mtx_assert(&sc->sc_ekeys_lock, MA_OWNED);
107 mtx_unlock(&sc->sc_ekeys_lock);
108
109 key = malloc(sizeof(*key), M_ELI, M_WAITOK);
110 g_eli_key_fill(sc, key, keyno);
111
112 mtx_lock(&sc->sc_ekeys_lock);
113 /*
114 * Recheck if the key wasn't added while we weren't holding the lock.
115 */
116 keysearch.gek_keyno = keyno;
117 ekey = RB_FIND(g_eli_key_tree, &sc->sc_ekeys_tree, &keysearch);
118 if (ekey != NULL) {
119 explicit_bzero(key, sizeof(*key));
120 free(key, M_ELI);
121 key = ekey;
122 TAILQ_REMOVE(&sc->sc_ekeys_queue, key, gek_next);
123 } else {
124 RB_INSERT(g_eli_key_tree, &sc->sc_ekeys_tree, key);
125 sc->sc_ekeys_allocated++;
126 }
127 TAILQ_INSERT_TAIL(&sc->sc_ekeys_queue, key, gek_next);
128
129 return (key);
130 }
131
132 static struct g_eli_key *
g_eli_key_find_last(struct g_eli_softc * sc)133 g_eli_key_find_last(struct g_eli_softc *sc)
134 {
135 struct g_eli_key *key;
136
137 mtx_assert(&sc->sc_ekeys_lock, MA_OWNED);
138
139 TAILQ_FOREACH(key, &sc->sc_ekeys_queue, gek_next) {
140 if (key->gek_count == 0)
141 break;
142 }
143
144 return (key);
145 }
146
147 static void
g_eli_key_replace(struct g_eli_softc * sc,struct g_eli_key * key,uint64_t keyno)148 g_eli_key_replace(struct g_eli_softc *sc, struct g_eli_key *key, uint64_t keyno)
149 {
150
151 mtx_assert(&sc->sc_ekeys_lock, MA_OWNED);
152 KASSERT(key->gek_magic == G_ELI_KEY_MAGIC, ("Invalid magic."));
153
154 RB_REMOVE(g_eli_key_tree, &sc->sc_ekeys_tree, key);
155 TAILQ_REMOVE(&sc->sc_ekeys_queue, key, gek_next);
156
157 KASSERT(key->gek_count == 0, ("gek_count=%d", key->gek_count));
158
159 g_eli_key_fill(sc, key, keyno);
160
161 RB_INSERT(g_eli_key_tree, &sc->sc_ekeys_tree, key);
162 TAILQ_INSERT_TAIL(&sc->sc_ekeys_queue, key, gek_next);
163 }
164
165 static void
g_eli_key_remove(struct g_eli_softc * sc,struct g_eli_key * key)166 g_eli_key_remove(struct g_eli_softc *sc, struct g_eli_key *key)
167 {
168
169 mtx_assert(&sc->sc_ekeys_lock, MA_OWNED);
170 KASSERT(key->gek_magic == G_ELI_KEY_MAGIC, ("Invalid magic."));
171 KASSERT(key->gek_count == 0, ("gek_count=%d", key->gek_count));
172
173 RB_REMOVE(g_eli_key_tree, &sc->sc_ekeys_tree, key);
174 TAILQ_REMOVE(&sc->sc_ekeys_queue, key, gek_next);
175 sc->sc_ekeys_allocated--;
176 explicit_bzero(key, sizeof(*key));
177 free(key, M_ELI);
178 }
179
180 void
g_eli_key_init(struct g_eli_softc * sc)181 g_eli_key_init(struct g_eli_softc *sc)
182 {
183 uint8_t *mkey;
184
185 mtx_lock(&sc->sc_ekeys_lock);
186
187 mkey = sc->sc_mkey + sizeof(sc->sc_ivkey);
188 if ((sc->sc_flags & G_ELI_FLAG_AUTH) == 0)
189 bcopy(mkey, sc->sc_ekey, G_ELI_DATAKEYLEN);
190 else {
191 /*
192 * The encryption key is: ekey = HMAC_SHA512(Data-Key, 0x10)
193 */
194 g_eli_crypto_hmac(mkey, G_ELI_MAXKEYLEN, "\x10", 1,
195 sc->sc_ekey, 0);
196 }
197
198 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) != 0) {
199 sc->sc_ekeys_total = 1;
200 sc->sc_ekeys_allocated = 0;
201 } else {
202 off_t mediasize;
203 size_t blocksize;
204
205 if ((sc->sc_flags & G_ELI_FLAG_AUTH) != 0) {
206 struct g_provider *pp;
207
208 pp = LIST_FIRST(&sc->sc_geom->consumer)->provider;
209 mediasize = pp->mediasize;
210 blocksize = pp->sectorsize;
211 } else {
212 mediasize = sc->sc_mediasize;
213 blocksize = sc->sc_sectorsize;
214 }
215 sc->sc_ekeys_total =
216 ((mediasize - 1) >> G_ELI_KEY_SHIFT) / blocksize + 1;
217 sc->sc_ekeys_allocated = 0;
218 TAILQ_INIT(&sc->sc_ekeys_queue);
219 RB_INIT(&sc->sc_ekeys_tree);
220 if (sc->sc_ekeys_total <= g_eli_key_cache_limit) {
221 uint64_t keyno;
222
223 for (keyno = 0; keyno < sc->sc_ekeys_total; keyno++)
224 (void)g_eli_key_allocate(sc, keyno);
225 KASSERT(sc->sc_ekeys_total == sc->sc_ekeys_allocated,
226 ("sc_ekeys_total=%ju != sc_ekeys_allocated=%ju",
227 (uintmax_t)sc->sc_ekeys_total,
228 (uintmax_t)sc->sc_ekeys_allocated));
229 }
230 }
231
232 mtx_unlock(&sc->sc_ekeys_lock);
233 }
234
235 void
g_eli_key_destroy(struct g_eli_softc * sc)236 g_eli_key_destroy(struct g_eli_softc *sc)
237 {
238
239 mtx_lock(&sc->sc_ekeys_lock);
240 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) != 0) {
241 explicit_bzero(sc->sc_ekey, sizeof(sc->sc_ekey));
242 } else {
243 struct g_eli_key *key;
244
245 while ((key = TAILQ_FIRST(&sc->sc_ekeys_queue)) != NULL)
246 g_eli_key_remove(sc, key);
247 TAILQ_INIT(&sc->sc_ekeys_queue);
248 RB_INIT(&sc->sc_ekeys_tree);
249 }
250 mtx_unlock(&sc->sc_ekeys_lock);
251 }
252
253 /*
254 * Select encryption key. If G_ELI_FLAG_SINGLE_KEY is present we only have one
255 * key available for all the data. If the flag is not present select the key
256 * based on data offset.
257 */
258 uint8_t *
g_eli_key_hold(struct g_eli_softc * sc,off_t offset,size_t blocksize)259 g_eli_key_hold(struct g_eli_softc *sc, off_t offset, size_t blocksize)
260 {
261 struct g_eli_key *key, keysearch;
262 uint64_t keyno;
263
264 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) != 0)
265 return (sc->sc_ekey);
266
267 /* We switch key every 2^G_ELI_KEY_SHIFT blocks. */
268 keyno = (offset >> G_ELI_KEY_SHIFT) / blocksize;
269
270 KASSERT(keyno < sc->sc_ekeys_total,
271 ("%s: keyno=%ju >= sc_ekeys_total=%ju",
272 __func__, (uintmax_t)keyno, (uintmax_t)sc->sc_ekeys_total));
273
274 keysearch.gek_keyno = keyno;
275
276 if (sc->sc_ekeys_total == sc->sc_ekeys_allocated) {
277 /* We have all the keys, so avoid some overhead. */
278 key = RB_FIND(g_eli_key_tree, &sc->sc_ekeys_tree, &keysearch);
279 KASSERT(key != NULL, ("No key %ju found.", (uintmax_t)keyno));
280 KASSERT(key->gek_magic == G_ELI_KEY_MAGIC,
281 ("Invalid key magic."));
282 return (key->gek_key);
283 }
284
285 mtx_lock(&sc->sc_ekeys_lock);
286 key = RB_FIND(g_eli_key_tree, &sc->sc_ekeys_tree, &keysearch);
287 if (key != NULL) {
288 g_eli_key_cache_hits++;
289 TAILQ_REMOVE(&sc->sc_ekeys_queue, key, gek_next);
290 TAILQ_INSERT_TAIL(&sc->sc_ekeys_queue, key, gek_next);
291 } else {
292 /*
293 * No key in cache, find the least recently unreferenced key
294 * or allocate one if we haven't reached our limit yet.
295 */
296 if (sc->sc_ekeys_allocated < g_eli_key_cache_limit) {
297 key = g_eli_key_allocate(sc, keyno);
298 } else {
299 g_eli_key_cache_misses++;
300 key = g_eli_key_find_last(sc);
301 if (key != NULL) {
302 g_eli_key_replace(sc, key, keyno);
303 } else {
304 /* All keys are referenced? Allocate one. */
305 key = g_eli_key_allocate(sc, keyno);
306 }
307 }
308 }
309 key->gek_count++;
310 mtx_unlock(&sc->sc_ekeys_lock);
311
312 KASSERT(key->gek_magic == G_ELI_KEY_MAGIC, ("Invalid key magic."));
313
314 return (key->gek_key);
315 }
316
317 void
g_eli_key_drop(struct g_eli_softc * sc,uint8_t * rawkey)318 g_eli_key_drop(struct g_eli_softc *sc, uint8_t *rawkey)
319 {
320 struct g_eli_key *key = (struct g_eli_key *)rawkey;
321
322 if ((sc->sc_flags & G_ELI_FLAG_SINGLE_KEY) != 0)
323 return;
324
325 KASSERT(key->gek_magic == G_ELI_KEY_MAGIC, ("Invalid key magic."));
326
327 if (sc->sc_ekeys_total == sc->sc_ekeys_allocated)
328 return;
329
330 mtx_lock(&sc->sc_ekeys_lock);
331 KASSERT(key->gek_count > 0, ("key->gek_count=%d", key->gek_count));
332 key->gek_count--;
333 while (sc->sc_ekeys_allocated > g_eli_key_cache_limit) {
334 key = g_eli_key_find_last(sc);
335 if (key == NULL)
336 break;
337 g_eli_key_remove(sc, key);
338 }
339 mtx_unlock(&sc->sc_ekeys_lock);
340 }
341 #endif /* _KERNEL */
342