1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/crc32c.h>
8 #include <linux/crypto.h>
9 #include <linux/xxhash.h>
10 #include <linux/key.h>
11 #include <linux/random.h>
12 #include <linux/scatterlist.h>
13 #include <crypto/algapi.h>
14 #include <crypto/chacha.h>
15 #include <crypto/hash.h>
16 #include <crypto/poly1305.h>
17 #include <crypto/skcipher.h>
18 #include <keys/user-type.h>
21 * bch2_checksum state is an abstraction of the checksum state calculated over different pages.
22 * it features page merging without having the checksum algorithm lose its state.
23 * for native checksum aglorithms (like crc), a default seed value will do.
24 * for hash-like algorithms, a state needs to be stored
27 struct bch2_checksum_state {
30 struct xxh64_state h64state;
35 static void bch2_checksum_init(struct bch2_checksum_state *state)
37 switch (state->type) {
43 case BCH_CSUM_CRC32C_NONZERO:
44 state->seed = U32_MAX;
46 case BCH_CSUM_CRC64_NONZERO:
47 state->seed = U64_MAX;
50 xxh64_reset(&state->h64state, 0);
57 static u64 bch2_checksum_final(const struct bch2_checksum_state *state)
59 switch (state->type) {
64 case BCH_CSUM_CRC32C_NONZERO:
65 return state->seed ^ U32_MAX;
66 case BCH_CSUM_CRC64_NONZERO:
67 return state->seed ^ U64_MAX;
69 return xxh64_digest(&state->h64state);
75 static void bch2_checksum_update(struct bch2_checksum_state *state, const void *data, size_t len)
77 switch (state->type) {
80 case BCH_CSUM_CRC32C_NONZERO:
82 state->seed = crc32c(state->seed, data, len);
84 case BCH_CSUM_CRC64_NONZERO:
86 state->seed = crc64_be(state->seed, data, len);
89 xxh64_update(&state->h64state, data, len);
96 static inline void do_encrypt_sg(struct crypto_sync_skcipher *tfm,
98 struct scatterlist *sg, size_t len)
100 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
103 skcipher_request_set_sync_tfm(req, tfm);
104 skcipher_request_set_crypt(req, sg, sg, len, nonce.d);
106 ret = crypto_skcipher_encrypt(req);
110 static inline void do_encrypt(struct crypto_sync_skcipher *tfm,
112 void *buf, size_t len)
114 struct scatterlist sg;
116 sg_init_one(&sg, buf, len);
117 do_encrypt_sg(tfm, nonce, &sg, len);
120 int bch2_chacha_encrypt_key(struct bch_key *key, struct nonce nonce,
121 void *buf, size_t len)
123 struct crypto_sync_skcipher *chacha20 =
124 crypto_alloc_sync_skcipher("chacha20", 0, 0);
128 pr_err("error requesting chacha20 module: %li", PTR_ERR(chacha20));
129 return PTR_ERR(chacha20);
132 ret = crypto_skcipher_setkey(&chacha20->base,
133 (void *) key, sizeof(*key));
135 pr_err("crypto_skcipher_setkey() error: %i", ret);
139 do_encrypt(chacha20, nonce, buf, len);
141 crypto_free_sync_skcipher(chacha20);
145 static void gen_poly_key(struct bch_fs *c, struct shash_desc *desc,
148 u8 key[POLY1305_KEY_SIZE];
150 nonce.d[3] ^= BCH_NONCE_POLY;
152 memset(key, 0, sizeof(key));
153 do_encrypt(c->chacha20, nonce, key, sizeof(key));
155 desc->tfm = c->poly1305;
156 crypto_shash_init(desc);
157 crypto_shash_update(desc, key, sizeof(key));
160 struct bch_csum bch2_checksum(struct bch_fs *c, unsigned type,
161 struct nonce nonce, const void *data, size_t len)
165 case BCH_CSUM_CRC32C_NONZERO:
166 case BCH_CSUM_CRC64_NONZERO:
167 case BCH_CSUM_CRC32C:
168 case BCH_CSUM_XXHASH:
169 case BCH_CSUM_CRC64: {
170 struct bch2_checksum_state state;
174 bch2_checksum_init(&state);
175 bch2_checksum_update(&state, data, len);
177 return (struct bch_csum) { .lo = cpu_to_le64(bch2_checksum_final(&state)) };
180 case BCH_CSUM_CHACHA20_POLY1305_80:
181 case BCH_CSUM_CHACHA20_POLY1305_128: {
182 SHASH_DESC_ON_STACK(desc, c->poly1305);
183 u8 digest[POLY1305_DIGEST_SIZE];
184 struct bch_csum ret = { 0 };
186 gen_poly_key(c, desc, nonce);
188 crypto_shash_update(desc, data, len);
189 crypto_shash_final(desc, digest);
191 memcpy(&ret, digest, bch_crc_bytes[type]);
199 void bch2_encrypt(struct bch_fs *c, unsigned type,
200 struct nonce nonce, void *data, size_t len)
202 if (!bch2_csum_type_is_encryption(type))
205 do_encrypt(c->chacha20, nonce, data, len);
208 static struct bch_csum __bch2_checksum_bio(struct bch_fs *c, unsigned type,
209 struct nonce nonce, struct bio *bio,
210 struct bvec_iter *iter)
216 return (struct bch_csum) { 0 };
217 case BCH_CSUM_CRC32C_NONZERO:
218 case BCH_CSUM_CRC64_NONZERO:
219 case BCH_CSUM_CRC32C:
220 case BCH_CSUM_XXHASH:
221 case BCH_CSUM_CRC64: {
222 struct bch2_checksum_state state;
225 bch2_checksum_init(&state);
227 #ifdef CONFIG_HIGHMEM
228 __bio_for_each_segment(bv, bio, *iter, *iter) {
229 void *p = kmap_atomic(bv.bv_page) + bv.bv_offset;
230 bch2_checksum_update(&state, p, bv.bv_len);
234 __bio_for_each_bvec(bv, bio, *iter, *iter)
235 bch2_checksum_update(&state, page_address(bv.bv_page) + bv.bv_offset,
238 return (struct bch_csum) { .lo = cpu_to_le64(bch2_checksum_final(&state)) };
241 case BCH_CSUM_CHACHA20_POLY1305_80:
242 case BCH_CSUM_CHACHA20_POLY1305_128: {
243 SHASH_DESC_ON_STACK(desc, c->poly1305);
244 u8 digest[POLY1305_DIGEST_SIZE];
245 struct bch_csum ret = { 0 };
247 gen_poly_key(c, desc, nonce);
249 #ifdef CONFIG_HIGHMEM
250 __bio_for_each_segment(bv, bio, *iter, *iter) {
251 void *p = kmap_atomic(bv.bv_page) + bv.bv_offset;
253 crypto_shash_update(desc, p, bv.bv_len);
257 __bio_for_each_bvec(bv, bio, *iter, *iter)
258 crypto_shash_update(desc,
259 page_address(bv.bv_page) + bv.bv_offset,
262 crypto_shash_final(desc, digest);
264 memcpy(&ret, digest, bch_crc_bytes[type]);
272 struct bch_csum bch2_checksum_bio(struct bch_fs *c, unsigned type,
273 struct nonce nonce, struct bio *bio)
275 struct bvec_iter iter = bio->bi_iter;
277 return __bch2_checksum_bio(c, type, nonce, bio, &iter);
280 void bch2_encrypt_bio(struct bch_fs *c, unsigned type,
281 struct nonce nonce, struct bio *bio)
284 struct bvec_iter iter;
285 struct scatterlist sgl[16], *sg = sgl;
288 if (!bch2_csum_type_is_encryption(type))
291 sg_init_table(sgl, ARRAY_SIZE(sgl));
293 bio_for_each_segment(bv, bio, iter) {
294 if (sg == sgl + ARRAY_SIZE(sgl)) {
296 do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
298 nonce = nonce_add(nonce, bytes);
301 sg_init_table(sgl, ARRAY_SIZE(sgl));
305 sg_set_page(sg++, bv.bv_page, bv.bv_len, bv.bv_offset);
310 do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
313 struct bch_csum bch2_checksum_merge(unsigned type, struct bch_csum a,
314 struct bch_csum b, size_t b_len)
316 struct bch2_checksum_state state;
319 bch2_checksum_init(&state);
322 BUG_ON(!bch2_checksum_mergeable(type));
325 unsigned b = min_t(unsigned, b_len, PAGE_SIZE);
327 bch2_checksum_update(&state,
328 page_address(ZERO_PAGE(0)), b);
331 a.lo = bch2_checksum_final(&state);
337 int bch2_rechecksum_bio(struct bch_fs *c, struct bio *bio,
338 struct bversion version,
339 struct bch_extent_crc_unpacked crc_old,
340 struct bch_extent_crc_unpacked *crc_a,
341 struct bch_extent_crc_unpacked *crc_b,
342 unsigned len_a, unsigned len_b,
343 unsigned new_csum_type)
345 struct bvec_iter iter = bio->bi_iter;
346 struct nonce nonce = extent_nonce(version, crc_old);
347 struct bch_csum merged = { 0 };
349 struct bch_extent_crc_unpacked *crc;
352 struct bch_csum csum;
354 { crc_a, len_a, new_csum_type },
355 { crc_b, len_b, new_csum_type },
356 { NULL, bio_sectors(bio) - len_a - len_b, new_csum_type },
358 bool mergeable = crc_old.csum_type == new_csum_type &&
359 bch2_checksum_mergeable(new_csum_type);
360 unsigned crc_nonce = crc_old.nonce;
362 BUG_ON(len_a + len_b > bio_sectors(bio));
363 BUG_ON(crc_old.uncompressed_size != bio_sectors(bio));
364 BUG_ON(crc_is_compressed(crc_old));
365 BUG_ON(bch2_csum_type_is_encryption(crc_old.csum_type) !=
366 bch2_csum_type_is_encryption(new_csum_type));
368 for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
369 iter.bi_size = i->len << 9;
370 if (mergeable || i->crc)
371 i->csum = __bch2_checksum_bio(c, i->csum_type,
374 bio_advance_iter(bio, &iter, i->len << 9);
375 nonce = nonce_add(nonce, i->len << 9);
379 for (i = splits; i < splits + ARRAY_SIZE(splits); i++)
380 merged = bch2_checksum_merge(new_csum_type, merged,
381 i->csum, i->len << 9);
383 merged = bch2_checksum_bio(c, crc_old.csum_type,
384 extent_nonce(version, crc_old), bio);
386 if (bch2_crc_cmp(merged, crc_old.csum))
389 for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
391 *i->crc = (struct bch_extent_crc_unpacked) {
392 .csum_type = i->csum_type,
393 .compression_type = crc_old.compression_type,
394 .compressed_size = i->len,
395 .uncompressed_size = i->len,
402 if (bch2_csum_type_is_encryption(new_csum_type))
410 int bch2_request_key(struct bch_sb *sb, struct bch_key *key)
412 char key_description[60];
413 struct key *keyring_key;
414 const struct user_key_payload *ukp;
417 snprintf(key_description, sizeof(key_description),
418 "bcachefs:%pUb", &sb->user_uuid);
420 keyring_key = request_key(&key_type_logon, key_description, NULL);
421 if (IS_ERR(keyring_key))
422 return PTR_ERR(keyring_key);
424 down_read(&keyring_key->sem);
425 ukp = dereference_key_locked(keyring_key);
426 if (ukp->datalen == sizeof(*key)) {
427 memcpy(key, ukp->data, ukp->datalen);
432 up_read(&keyring_key->sem);
433 key_put(keyring_key);
438 #include <keyutils.h>
439 #include <uuid/uuid.h>
441 int bch2_request_key(struct bch_sb *sb, struct bch_key *key)
444 char key_description[60];
447 uuid_unparse_lower(sb->user_uuid.b, uuid);
448 sprintf(key_description, "bcachefs:%s", uuid);
450 key_id = request_key("user", key_description, NULL,
451 KEY_SPEC_USER_KEYRING);
455 if (keyctl_read(key_id, (void *) key, sizeof(*key)) != sizeof(*key))
462 int bch2_decrypt_sb_key(struct bch_fs *c,
463 struct bch_sb_field_crypt *crypt,
466 struct bch_encrypted_key sb_key = crypt->key;
467 struct bch_key user_key;
470 /* is key encrypted? */
471 if (!bch2_key_is_encrypted(&sb_key))
474 ret = bch2_request_key(c->disk_sb.sb, &user_key);
476 bch_err(c, "error requesting encryption key: %i", ret);
480 /* decrypt real key: */
481 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
482 &sb_key, sizeof(sb_key));
486 if (bch2_key_is_encrypted(&sb_key)) {
487 bch_err(c, "incorrect encryption key");
494 memzero_explicit(&sb_key, sizeof(sb_key));
495 memzero_explicit(&user_key, sizeof(user_key));
499 static int bch2_alloc_ciphers(struct bch_fs *c)
502 c->chacha20 = crypto_alloc_sync_skcipher("chacha20", 0, 0);
503 if (IS_ERR(c->chacha20)) {
504 bch_err(c, "error requesting chacha20 module: %li",
505 PTR_ERR(c->chacha20));
506 return PTR_ERR(c->chacha20);
510 c->poly1305 = crypto_alloc_shash("poly1305", 0, 0);
511 if (IS_ERR(c->poly1305)) {
512 bch_err(c, "error requesting poly1305 module: %li",
513 PTR_ERR(c->poly1305));
514 return PTR_ERR(c->poly1305);
520 int bch2_disable_encryption(struct bch_fs *c)
522 struct bch_sb_field_crypt *crypt;
526 mutex_lock(&c->sb_lock);
528 crypt = bch2_sb_get_crypt(c->disk_sb.sb);
532 /* is key encrypted? */
534 if (bch2_key_is_encrypted(&crypt->key))
537 ret = bch2_decrypt_sb_key(c, crypt, &key);
541 crypt->key.magic = BCH_KEY_MAGIC;
542 crypt->key.key = key;
544 SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 0);
547 mutex_unlock(&c->sb_lock);
552 int bch2_enable_encryption(struct bch_fs *c, bool keyed)
554 struct bch_encrypted_key key;
555 struct bch_key user_key;
556 struct bch_sb_field_crypt *crypt;
559 mutex_lock(&c->sb_lock);
561 /* Do we already have an encryption key? */
562 if (bch2_sb_get_crypt(c->disk_sb.sb))
565 ret = bch2_alloc_ciphers(c);
569 key.magic = BCH_KEY_MAGIC;
570 get_random_bytes(&key.key, sizeof(key.key));
573 ret = bch2_request_key(c->disk_sb.sb, &user_key);
575 bch_err(c, "error requesting encryption key: %i", ret);
579 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
585 ret = crypto_skcipher_setkey(&c->chacha20->base,
586 (void *) &key.key, sizeof(key.key));
590 crypt = bch2_sb_resize_crypt(&c->disk_sb, sizeof(*crypt) / sizeof(u64));
592 ret = -ENOMEM; /* XXX this technically could be -ENOSPC */
598 /* write superblock */
599 SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 1);
602 mutex_unlock(&c->sb_lock);
603 memzero_explicit(&user_key, sizeof(user_key));
604 memzero_explicit(&key, sizeof(key));
608 void bch2_fs_encryption_exit(struct bch_fs *c)
610 if (!IS_ERR_OR_NULL(c->poly1305))
611 crypto_free_shash(c->poly1305);
612 if (!IS_ERR_OR_NULL(c->chacha20))
613 crypto_free_sync_skcipher(c->chacha20);
614 if (!IS_ERR_OR_NULL(c->sha256))
615 crypto_free_shash(c->sha256);
618 int bch2_fs_encryption_init(struct bch_fs *c)
620 struct bch_sb_field_crypt *crypt;
624 pr_verbose_init(c->opts, "");
626 c->sha256 = crypto_alloc_shash("sha256", 0, 0);
627 if (IS_ERR(c->sha256)) {
628 bch_err(c, "error requesting sha256 module");
629 ret = PTR_ERR(c->sha256);
633 crypt = bch2_sb_get_crypt(c->disk_sb.sb);
637 ret = bch2_alloc_ciphers(c);
641 ret = bch2_decrypt_sb_key(c, crypt, &key);
645 ret = crypto_skcipher_setkey(&c->chacha20->base,
646 (void *) &key.key, sizeof(key.key));
650 memzero_explicit(&key, sizeof(key));
651 pr_verbose_init(c->opts, "ret %i", ret);