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