1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/crc32c.h>
9 #include <linux/crypto.h>
10 #include <linux/xxhash.h>
11 #include <linux/key.h>
12 #include <linux/random.h>
13 #include <linux/scatterlist.h>
14 #include <crypto/algapi.h>
15 #include <crypto/chacha.h>
16 #include <crypto/hash.h>
17 #include <crypto/poly1305.h>
18 #include <crypto/skcipher.h>
19 #include <keys/user-type.h>
22 * bch2_checksum state is an abstraction of the checksum state calculated over different pages.
23 * it features page merging without having the checksum algorithm lose its state.
24 * for native checksum aglorithms (like crc), a default seed value will do.
25 * for hash-like algorithms, a state needs to be stored
28 struct bch2_checksum_state {
31 struct xxh64_state h64state;
36 static void bch2_checksum_init(struct bch2_checksum_state *state)
38 switch (state->type) {
44 case BCH_CSUM_crc32c_nonzero:
45 state->seed = U32_MAX;
47 case BCH_CSUM_crc64_nonzero:
48 state->seed = U64_MAX;
51 xxh64_reset(&state->h64state, 0);
58 static u64 bch2_checksum_final(const struct bch2_checksum_state *state)
60 switch (state->type) {
65 case BCH_CSUM_crc32c_nonzero:
66 return state->seed ^ U32_MAX;
67 case BCH_CSUM_crc64_nonzero:
68 return state->seed ^ U64_MAX;
70 return xxh64_digest(&state->h64state);
76 static void bch2_checksum_update(struct bch2_checksum_state *state, const void *data, size_t len)
78 switch (state->type) {
81 case BCH_CSUM_crc32c_nonzero:
83 state->seed = crc32c(state->seed, data, len);
85 case BCH_CSUM_crc64_nonzero:
87 state->seed = crc64_be(state->seed, data, len);
90 xxh64_update(&state->h64state, data, len);
97 static inline int do_encrypt_sg(struct crypto_sync_skcipher *tfm,
99 struct scatterlist *sg, size_t len)
101 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
104 skcipher_request_set_sync_tfm(req, tfm);
105 skcipher_request_set_crypt(req, sg, sg, len, nonce.d);
107 ret = crypto_skcipher_encrypt(req);
109 pr_err("got error %i from crypto_skcipher_encrypt()", ret);
114 static inline int do_encrypt(struct crypto_sync_skcipher *tfm,
116 void *buf, size_t len)
118 if (!is_vmalloc_addr(buf)) {
119 struct scatterlist sg;
121 sg_init_table(&sg, 1);
124 ? vmalloc_to_page(buf)
126 len, offset_in_page(buf));
127 return do_encrypt_sg(tfm, nonce, &sg, len);
129 unsigned pages = buf_pages(buf, len);
130 struct scatterlist *sg;
131 size_t orig_len = len;
134 sg = kmalloc_array(pages, sizeof(*sg), GFP_KERNEL);
136 return -BCH_ERR_ENOMEM_do_encrypt;
138 sg_init_table(sg, pages);
140 for (i = 0; i < pages; i++) {
141 unsigned offset = offset_in_page(buf);
142 unsigned pg_len = min_t(size_t, len, PAGE_SIZE - offset);
144 sg_set_page(sg + i, vmalloc_to_page(buf), pg_len, offset);
149 ret = do_encrypt_sg(tfm, nonce, sg, orig_len);
155 int bch2_chacha_encrypt_key(struct bch_key *key, struct nonce nonce,
156 void *buf, size_t len)
158 struct crypto_sync_skcipher *chacha20 =
159 crypto_alloc_sync_skcipher("chacha20", 0, 0);
162 ret = PTR_ERR_OR_ZERO(chacha20);
164 pr_err("error requesting chacha20 cipher: %s", bch2_err_str(ret));
168 ret = crypto_skcipher_setkey(&chacha20->base,
169 (void *) key, sizeof(*key));
171 pr_err("error from crypto_skcipher_setkey(): %s", bch2_err_str(ret));
175 ret = do_encrypt(chacha20, nonce, buf, len);
177 crypto_free_sync_skcipher(chacha20);
181 static int gen_poly_key(struct bch_fs *c, struct shash_desc *desc,
184 u8 key[POLY1305_KEY_SIZE];
187 nonce.d[3] ^= BCH_NONCE_POLY;
189 memset(key, 0, sizeof(key));
190 ret = do_encrypt(c->chacha20, nonce, key, sizeof(key));
194 desc->tfm = c->poly1305;
195 crypto_shash_init(desc);
196 crypto_shash_update(desc, key, sizeof(key));
200 struct bch_csum bch2_checksum(struct bch_fs *c, unsigned type,
201 struct nonce nonce, const void *data, size_t len)
205 case BCH_CSUM_crc32c_nonzero:
206 case BCH_CSUM_crc64_nonzero:
207 case BCH_CSUM_crc32c:
208 case BCH_CSUM_xxhash:
209 case BCH_CSUM_crc64: {
210 struct bch2_checksum_state state;
214 bch2_checksum_init(&state);
215 bch2_checksum_update(&state, data, len);
217 return (struct bch_csum) { .lo = cpu_to_le64(bch2_checksum_final(&state)) };
220 case BCH_CSUM_chacha20_poly1305_80:
221 case BCH_CSUM_chacha20_poly1305_128: {
222 SHASH_DESC_ON_STACK(desc, c->poly1305);
223 u8 digest[POLY1305_DIGEST_SIZE];
224 struct bch_csum ret = { 0 };
226 gen_poly_key(c, desc, nonce);
228 crypto_shash_update(desc, data, len);
229 crypto_shash_final(desc, digest);
231 memcpy(&ret, digest, bch_crc_bytes[type]);
239 int bch2_encrypt(struct bch_fs *c, unsigned type,
240 struct nonce nonce, void *data, size_t len)
242 if (!bch2_csum_type_is_encryption(type))
245 return do_encrypt(c->chacha20, nonce, data, len);
248 static struct bch_csum __bch2_checksum_bio(struct bch_fs *c, unsigned type,
249 struct nonce nonce, struct bio *bio,
250 struct bvec_iter *iter)
256 return (struct bch_csum) { 0 };
257 case BCH_CSUM_crc32c_nonzero:
258 case BCH_CSUM_crc64_nonzero:
259 case BCH_CSUM_crc32c:
260 case BCH_CSUM_xxhash:
261 case BCH_CSUM_crc64: {
262 struct bch2_checksum_state state;
265 bch2_checksum_init(&state);
267 #ifdef CONFIG_HIGHMEM
268 __bio_for_each_segment(bv, bio, *iter, *iter) {
269 void *p = kmap_local_page(bv.bv_page) + bv.bv_offset;
271 bch2_checksum_update(&state, p, bv.bv_len);
275 __bio_for_each_bvec(bv, bio, *iter, *iter)
276 bch2_checksum_update(&state, page_address(bv.bv_page) + bv.bv_offset,
279 return (struct bch_csum) { .lo = cpu_to_le64(bch2_checksum_final(&state)) };
282 case BCH_CSUM_chacha20_poly1305_80:
283 case BCH_CSUM_chacha20_poly1305_128: {
284 SHASH_DESC_ON_STACK(desc, c->poly1305);
285 u8 digest[POLY1305_DIGEST_SIZE];
286 struct bch_csum ret = { 0 };
288 gen_poly_key(c, desc, nonce);
290 #ifdef CONFIG_HIGHMEM
291 __bio_for_each_segment(bv, bio, *iter, *iter) {
292 void *p = kmap_local_page(bv.bv_page) + bv.bv_offset;
294 crypto_shash_update(desc, p, bv.bv_len);
298 __bio_for_each_bvec(bv, bio, *iter, *iter)
299 crypto_shash_update(desc,
300 page_address(bv.bv_page) + bv.bv_offset,
303 crypto_shash_final(desc, digest);
305 memcpy(&ret, digest, bch_crc_bytes[type]);
313 struct bch_csum bch2_checksum_bio(struct bch_fs *c, unsigned type,
314 struct nonce nonce, struct bio *bio)
316 struct bvec_iter iter = bio->bi_iter;
318 return __bch2_checksum_bio(c, type, nonce, bio, &iter);
321 int __bch2_encrypt_bio(struct bch_fs *c, unsigned type,
322 struct nonce nonce, struct bio *bio)
325 struct bvec_iter iter;
326 struct scatterlist sgl[16], *sg = sgl;
330 if (!bch2_csum_type_is_encryption(type))
333 sg_init_table(sgl, ARRAY_SIZE(sgl));
335 bio_for_each_segment(bv, bio, iter) {
336 if (sg == sgl + ARRAY_SIZE(sgl)) {
339 ret = do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
343 nonce = nonce_add(nonce, bytes);
346 sg_init_table(sgl, ARRAY_SIZE(sgl));
350 sg_set_page(sg++, bv.bv_page, bv.bv_len, bv.bv_offset);
355 return do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
358 struct bch_csum bch2_checksum_merge(unsigned type, struct bch_csum a,
359 struct bch_csum b, size_t b_len)
361 struct bch2_checksum_state state;
364 bch2_checksum_init(&state);
365 state.seed = le64_to_cpu(a.lo);
367 BUG_ON(!bch2_checksum_mergeable(type));
370 unsigned page_len = min_t(unsigned, b_len, PAGE_SIZE);
372 bch2_checksum_update(&state,
373 page_address(ZERO_PAGE(0)), page_len);
376 a.lo = cpu_to_le64(bch2_checksum_final(&state));
382 int bch2_rechecksum_bio(struct bch_fs *c, struct bio *bio,
383 struct bversion version,
384 struct bch_extent_crc_unpacked crc_old,
385 struct bch_extent_crc_unpacked *crc_a,
386 struct bch_extent_crc_unpacked *crc_b,
387 unsigned len_a, unsigned len_b,
388 unsigned new_csum_type)
390 struct bvec_iter iter = bio->bi_iter;
391 struct nonce nonce = extent_nonce(version, crc_old);
392 struct bch_csum merged = { 0 };
394 struct bch_extent_crc_unpacked *crc;
397 struct bch_csum csum;
399 { crc_a, len_a, new_csum_type, { 0 }},
400 { crc_b, len_b, new_csum_type, { 0 } },
401 { NULL, bio_sectors(bio) - len_a - len_b, new_csum_type, { 0 } },
403 bool mergeable = crc_old.csum_type == new_csum_type &&
404 bch2_checksum_mergeable(new_csum_type);
405 unsigned crc_nonce = crc_old.nonce;
407 BUG_ON(len_a + len_b > bio_sectors(bio));
408 BUG_ON(crc_old.uncompressed_size != bio_sectors(bio));
409 BUG_ON(crc_is_compressed(crc_old));
410 BUG_ON(bch2_csum_type_is_encryption(crc_old.csum_type) !=
411 bch2_csum_type_is_encryption(new_csum_type));
413 for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
414 iter.bi_size = i->len << 9;
415 if (mergeable || i->crc)
416 i->csum = __bch2_checksum_bio(c, i->csum_type,
419 bio_advance_iter(bio, &iter, i->len << 9);
420 nonce = nonce_add(nonce, i->len << 9);
424 for (i = splits; i < splits + ARRAY_SIZE(splits); i++)
425 merged = bch2_checksum_merge(new_csum_type, merged,
426 i->csum, i->len << 9);
428 merged = bch2_checksum_bio(c, crc_old.csum_type,
429 extent_nonce(version, crc_old), bio);
431 if (bch2_crc_cmp(merged, crc_old.csum) && !c->opts.no_data_io) {
432 bch_err(c, "checksum error in %s() (memory corruption or bug?)\n"
433 "expected %0llx:%0llx got %0llx:%0llx (old type %s new type %s)",
439 bch2_csum_types[crc_old.csum_type],
440 bch2_csum_types[new_csum_type]);
444 for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
446 *i->crc = (struct bch_extent_crc_unpacked) {
447 .csum_type = i->csum_type,
448 .compression_type = crc_old.compression_type,
449 .compressed_size = i->len,
450 .uncompressed_size = i->len,
457 if (bch2_csum_type_is_encryption(new_csum_type))
464 /* BCH_SB_FIELD_crypt: */
466 static int bch2_sb_crypt_validate(struct bch_sb *sb,
467 struct bch_sb_field *f,
468 struct printbuf *err)
470 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
472 if (vstruct_bytes(&crypt->field) < sizeof(*crypt)) {
473 prt_printf(err, "wrong size (got %zu should be %zu)",
474 vstruct_bytes(&crypt->field), sizeof(*crypt));
475 return -BCH_ERR_invalid_sb_crypt;
478 if (BCH_CRYPT_KDF_TYPE(crypt)) {
479 prt_printf(err, "bad kdf type %llu", BCH_CRYPT_KDF_TYPE(crypt));
480 return -BCH_ERR_invalid_sb_crypt;
486 static void bch2_sb_crypt_to_text(struct printbuf *out, struct bch_sb *sb,
487 struct bch_sb_field *f)
489 struct bch_sb_field_crypt *crypt = field_to_type(f, crypt);
491 prt_printf(out, "KFD: %llu", BCH_CRYPT_KDF_TYPE(crypt));
493 prt_printf(out, "scrypt n: %llu", BCH_KDF_SCRYPT_N(crypt));
495 prt_printf(out, "scrypt r: %llu", BCH_KDF_SCRYPT_R(crypt));
497 prt_printf(out, "scrypt p: %llu", BCH_KDF_SCRYPT_P(crypt));
501 const struct bch_sb_field_ops bch_sb_field_ops_crypt = {
502 .validate = bch2_sb_crypt_validate,
503 .to_text = bch2_sb_crypt_to_text,
507 static int __bch2_request_key(char *key_description, struct bch_key *key)
509 struct key *keyring_key;
510 const struct user_key_payload *ukp;
513 keyring_key = request_key(&key_type_user, key_description, NULL);
514 if (IS_ERR(keyring_key))
515 return PTR_ERR(keyring_key);
517 down_read(&keyring_key->sem);
518 ukp = dereference_key_locked(keyring_key);
519 if (ukp->datalen == sizeof(*key)) {
520 memcpy(key, ukp->data, ukp->datalen);
525 up_read(&keyring_key->sem);
526 key_put(keyring_key);
531 #include <keyutils.h>
533 static int __bch2_request_key(char *key_description, struct bch_key *key)
537 key_id = request_key("user", key_description, NULL,
538 KEY_SPEC_SESSION_KEYRING);
542 key_id = request_key("user", key_description, NULL,
543 KEY_SPEC_USER_KEYRING);
547 key_id = request_key("user", key_description, NULL,
548 KEY_SPEC_USER_SESSION_KEYRING);
555 if (keyctl_read(key_id, (void *) key, sizeof(*key)) != sizeof(*key))
561 #include "../crypto.h"
564 int bch2_request_key(struct bch_sb *sb, struct bch_key *key)
566 struct printbuf key_description = PRINTBUF;
569 prt_printf(&key_description, "bcachefs:");
570 pr_uuid(&key_description, sb->user_uuid.b);
572 ret = __bch2_request_key(key_description.buf, key);
573 printbuf_exit(&key_description);
577 char *passphrase = read_passphrase("Enter passphrase: ");
578 struct bch_encrypted_key sb_key;
580 bch2_passphrase_check(sb, passphrase,
586 /* stash with memfd, pass memfd fd to mount */
592 int bch2_revoke_key(struct bch_sb *sb)
595 struct printbuf key_description = PRINTBUF;
597 prt_printf(&key_description, "bcachefs:");
598 pr_uuid(&key_description, sb->user_uuid.b);
600 key_id = request_key("user", key_description.buf, NULL, KEY_SPEC_USER_KEYRING);
601 printbuf_exit(&key_description);
605 keyctl_revoke(key_id);
611 int bch2_decrypt_sb_key(struct bch_fs *c,
612 struct bch_sb_field_crypt *crypt,
615 struct bch_encrypted_key sb_key = crypt->key;
616 struct bch_key user_key;
619 /* is key encrypted? */
620 if (!bch2_key_is_encrypted(&sb_key))
623 ret = bch2_request_key(c->disk_sb.sb, &user_key);
625 bch_err(c, "error requesting encryption key: %s", bch2_err_str(ret));
629 /* decrypt real key: */
630 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
631 &sb_key, sizeof(sb_key));
635 if (bch2_key_is_encrypted(&sb_key)) {
636 bch_err(c, "incorrect encryption key");
643 memzero_explicit(&sb_key, sizeof(sb_key));
644 memzero_explicit(&user_key, sizeof(user_key));
648 static int bch2_alloc_ciphers(struct bch_fs *c)
653 c->chacha20 = crypto_alloc_sync_skcipher("chacha20", 0, 0);
654 ret = PTR_ERR_OR_ZERO(c->chacha20);
657 bch_err(c, "error requesting chacha20 module: %s", bch2_err_str(ret));
662 c->poly1305 = crypto_alloc_shash("poly1305", 0, 0);
663 ret = PTR_ERR_OR_ZERO(c->poly1305);
666 bch_err(c, "error requesting poly1305 module: %s", bch2_err_str(ret));
673 int bch2_disable_encryption(struct bch_fs *c)
675 struct bch_sb_field_crypt *crypt;
679 mutex_lock(&c->sb_lock);
681 crypt = bch2_sb_field_get(c->disk_sb.sb, crypt);
685 /* is key encrypted? */
687 if (bch2_key_is_encrypted(&crypt->key))
690 ret = bch2_decrypt_sb_key(c, crypt, &key);
694 crypt->key.magic = cpu_to_le64(BCH_KEY_MAGIC);
695 crypt->key.key = key;
697 SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 0);
700 mutex_unlock(&c->sb_lock);
705 int bch2_enable_encryption(struct bch_fs *c, bool keyed)
707 struct bch_encrypted_key key;
708 struct bch_key user_key;
709 struct bch_sb_field_crypt *crypt;
712 mutex_lock(&c->sb_lock);
714 /* Do we already have an encryption key? */
715 if (bch2_sb_field_get(c->disk_sb.sb, crypt))
718 ret = bch2_alloc_ciphers(c);
722 key.magic = cpu_to_le64(BCH_KEY_MAGIC);
723 get_random_bytes(&key.key, sizeof(key.key));
726 ret = bch2_request_key(c->disk_sb.sb, &user_key);
728 bch_err(c, "error requesting encryption key: %s", bch2_err_str(ret));
732 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
738 ret = crypto_skcipher_setkey(&c->chacha20->base,
739 (void *) &key.key, sizeof(key.key));
743 crypt = bch2_sb_field_resize(&c->disk_sb, crypt,
744 sizeof(*crypt) / sizeof(u64));
746 ret = -BCH_ERR_ENOSPC_sb_crypt;
752 /* write superblock */
753 SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 1);
756 mutex_unlock(&c->sb_lock);
757 memzero_explicit(&user_key, sizeof(user_key));
758 memzero_explicit(&key, sizeof(key));
762 void bch2_fs_encryption_exit(struct bch_fs *c)
764 if (!IS_ERR_OR_NULL(c->poly1305))
765 crypto_free_shash(c->poly1305);
766 if (!IS_ERR_OR_NULL(c->chacha20))
767 crypto_free_sync_skcipher(c->chacha20);
768 if (!IS_ERR_OR_NULL(c->sha256))
769 crypto_free_shash(c->sha256);
772 int bch2_fs_encryption_init(struct bch_fs *c)
774 struct bch_sb_field_crypt *crypt;
778 c->sha256 = crypto_alloc_shash("sha256", 0, 0);
779 ret = PTR_ERR_OR_ZERO(c->sha256);
781 bch_err(c, "error requesting sha256 module: %s", bch2_err_str(ret));
785 crypt = bch2_sb_field_get(c->disk_sb.sb, crypt);
789 ret = bch2_alloc_ciphers(c);
793 ret = bch2_decrypt_sb_key(c, crypt, &key);
797 ret = crypto_skcipher_setkey(&c->chacha20->base,
798 (void *) &key.key, sizeof(key.key));
802 memzero_explicit(&key, sizeof(key));