1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/crc32c.h>
8 #include <linux/crypto.h>
10 #include <linux/random.h>
11 #include <linux/scatterlist.h>
12 #include <crypto/algapi.h>
13 #include <crypto/chacha.h>
14 #include <crypto/hash.h>
15 #include <crypto/poly1305.h>
16 #include <keys/user-type.h>
18 static u64 bch2_checksum_init(unsigned type)
23 case BCH_CSUM_CRC32C_NONZERO:
25 case BCH_CSUM_CRC64_NONZERO:
36 static u64 bch2_checksum_final(unsigned type, u64 crc)
41 case BCH_CSUM_CRC32C_NONZERO:
43 case BCH_CSUM_CRC64_NONZERO:
54 static u64 bch2_checksum_update(unsigned type, u64 crc, const void *data, size_t len)
59 case BCH_CSUM_CRC32C_NONZERO:
61 return crc32c(crc, data, len);
62 case BCH_CSUM_CRC64_NONZERO:
64 return crc64_be(crc, data, len);
70 static inline void do_encrypt_sg(struct crypto_sync_skcipher *tfm,
72 struct scatterlist *sg, size_t len)
74 SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
77 skcipher_request_set_sync_tfm(req, tfm);
78 skcipher_request_set_crypt(req, sg, sg, len, nonce.d);
80 ret = crypto_skcipher_encrypt(req);
84 static inline void do_encrypt(struct crypto_sync_skcipher *tfm,
86 void *buf, size_t len)
88 struct scatterlist sg;
90 sg_init_one(&sg, buf, len);
91 do_encrypt_sg(tfm, nonce, &sg, len);
94 int bch2_chacha_encrypt_key(struct bch_key *key, struct nonce nonce,
95 void *buf, size_t len)
97 struct crypto_sync_skcipher *chacha20 =
98 crypto_alloc_sync_skcipher("chacha20", 0, 0);
102 pr_err("error requesting chacha20 module: %li", PTR_ERR(chacha20));
103 return PTR_ERR(chacha20);
106 ret = crypto_skcipher_setkey(&chacha20->base,
107 (void *) key, sizeof(*key));
109 pr_err("crypto_skcipher_setkey() error: %i", ret);
113 do_encrypt(chacha20, nonce, buf, len);
115 crypto_free_sync_skcipher(chacha20);
119 static void gen_poly_key(struct bch_fs *c, struct shash_desc *desc,
122 u8 key[POLY1305_KEY_SIZE];
124 nonce.d[3] ^= BCH_NONCE_POLY;
126 memset(key, 0, sizeof(key));
127 do_encrypt(c->chacha20, nonce, key, sizeof(key));
129 desc->tfm = c->poly1305;
130 crypto_shash_init(desc);
131 crypto_shash_update(desc, key, sizeof(key));
134 struct bch_csum bch2_checksum(struct bch_fs *c, unsigned type,
135 struct nonce nonce, const void *data, size_t len)
139 case BCH_CSUM_CRC32C_NONZERO:
140 case BCH_CSUM_CRC64_NONZERO:
141 case BCH_CSUM_CRC32C:
142 case BCH_CSUM_CRC64: {
143 u64 crc = bch2_checksum_init(type);
145 crc = bch2_checksum_update(type, crc, data, len);
146 crc = bch2_checksum_final(type, crc);
148 return (struct bch_csum) { .lo = cpu_to_le64(crc) };
151 case BCH_CSUM_CHACHA20_POLY1305_80:
152 case BCH_CSUM_CHACHA20_POLY1305_128: {
153 SHASH_DESC_ON_STACK(desc, c->poly1305);
154 u8 digest[POLY1305_DIGEST_SIZE];
155 struct bch_csum ret = { 0 };
157 gen_poly_key(c, desc, nonce);
159 crypto_shash_update(desc, data, len);
160 crypto_shash_final(desc, digest);
162 memcpy(&ret, digest, bch_crc_bytes[type]);
170 void bch2_encrypt(struct bch_fs *c, unsigned type,
171 struct nonce nonce, void *data, size_t len)
173 if (!bch2_csum_type_is_encryption(type))
176 do_encrypt(c->chacha20, nonce, data, len);
179 static struct bch_csum __bch2_checksum_bio(struct bch_fs *c, unsigned type,
180 struct nonce nonce, struct bio *bio,
181 struct bvec_iter *iter)
187 return (struct bch_csum) { 0 };
188 case BCH_CSUM_CRC32C_NONZERO:
189 case BCH_CSUM_CRC64_NONZERO:
190 case BCH_CSUM_CRC32C:
191 case BCH_CSUM_CRC64: {
192 u64 crc = bch2_checksum_init(type);
194 #ifdef CONFIG_HIGHMEM
195 __bio_for_each_segment(bv, bio, *iter, *iter) {
196 void *p = kmap_atomic(bv.bv_page) + bv.bv_offset;
197 crc = bch2_checksum_update(type,
202 __bio_for_each_bvec(bv, bio, *iter, *iter)
203 crc = bch2_checksum_update(type, crc,
204 page_address(bv.bv_page) + bv.bv_offset,
207 crc = bch2_checksum_final(type, crc);
208 return (struct bch_csum) { .lo = cpu_to_le64(crc) };
211 case BCH_CSUM_CHACHA20_POLY1305_80:
212 case BCH_CSUM_CHACHA20_POLY1305_128: {
213 SHASH_DESC_ON_STACK(desc, c->poly1305);
214 u8 digest[POLY1305_DIGEST_SIZE];
215 struct bch_csum ret = { 0 };
217 gen_poly_key(c, desc, nonce);
219 #ifdef CONFIG_HIGHMEM
220 __bio_for_each_segment(bv, bio, *iter, *iter) {
221 void *p = kmap_atomic(bv.bv_page) + bv.bv_offset;
223 crypto_shash_update(desc, p, bv.bv_len);
227 __bio_for_each_bvec(bv, bio, *iter, *iter)
228 crypto_shash_update(desc,
229 page_address(bv.bv_page) + bv.bv_offset,
232 crypto_shash_final(desc, digest);
234 memcpy(&ret, digest, bch_crc_bytes[type]);
242 struct bch_csum bch2_checksum_bio(struct bch_fs *c, unsigned type,
243 struct nonce nonce, struct bio *bio)
245 struct bvec_iter iter = bio->bi_iter;
247 return __bch2_checksum_bio(c, type, nonce, bio, &iter);
250 void bch2_encrypt_bio(struct bch_fs *c, unsigned type,
251 struct nonce nonce, struct bio *bio)
254 struct bvec_iter iter;
255 struct scatterlist sgl[16], *sg = sgl;
258 if (!bch2_csum_type_is_encryption(type))
261 sg_init_table(sgl, ARRAY_SIZE(sgl));
263 bio_for_each_segment(bv, bio, iter) {
264 if (sg == sgl + ARRAY_SIZE(sgl)) {
266 do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
268 nonce = nonce_add(nonce, bytes);
271 sg_init_table(sgl, ARRAY_SIZE(sgl));
275 sg_set_page(sg++, bv.bv_page, bv.bv_len, bv.bv_offset);
280 do_encrypt_sg(c->chacha20, nonce, sgl, bytes);
283 struct bch_csum bch2_checksum_merge(unsigned type, struct bch_csum a,
284 struct bch_csum b, size_t b_len)
286 BUG_ON(!bch2_checksum_mergeable(type));
289 unsigned b = min_t(unsigned, b_len, PAGE_SIZE);
291 a.lo = bch2_checksum_update(type, a.lo,
292 page_address(ZERO_PAGE(0)), b);
301 int bch2_rechecksum_bio(struct bch_fs *c, struct bio *bio,
302 struct bversion version,
303 struct bch_extent_crc_unpacked crc_old,
304 struct bch_extent_crc_unpacked *crc_a,
305 struct bch_extent_crc_unpacked *crc_b,
306 unsigned len_a, unsigned len_b,
307 unsigned new_csum_type)
309 struct bvec_iter iter = bio->bi_iter;
310 struct nonce nonce = extent_nonce(version, crc_old);
311 struct bch_csum merged = { 0 };
313 struct bch_extent_crc_unpacked *crc;
316 struct bch_csum csum;
318 { crc_a, len_a, new_csum_type },
319 { crc_b, len_b, new_csum_type },
320 { NULL, bio_sectors(bio) - len_a - len_b, new_csum_type },
322 bool mergeable = crc_old.csum_type == new_csum_type &&
323 bch2_checksum_mergeable(new_csum_type);
324 unsigned crc_nonce = crc_old.nonce;
326 BUG_ON(len_a + len_b > bio_sectors(bio));
327 BUG_ON(crc_old.uncompressed_size != bio_sectors(bio));
328 BUG_ON(crc_is_compressed(crc_old));
329 BUG_ON(bch2_csum_type_is_encryption(crc_old.csum_type) !=
330 bch2_csum_type_is_encryption(new_csum_type));
332 for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
333 iter.bi_size = i->len << 9;
334 if (mergeable || i->crc)
335 i->csum = __bch2_checksum_bio(c, i->csum_type,
338 bio_advance_iter(bio, &iter, i->len << 9);
339 nonce = nonce_add(nonce, i->len << 9);
343 for (i = splits; i < splits + ARRAY_SIZE(splits); i++)
344 merged = bch2_checksum_merge(new_csum_type, merged,
345 i->csum, i->len << 9);
347 merged = bch2_checksum_bio(c, crc_old.csum_type,
348 extent_nonce(version, crc_old), bio);
350 if (bch2_crc_cmp(merged, crc_old.csum))
353 for (i = splits; i < splits + ARRAY_SIZE(splits); i++) {
355 *i->crc = (struct bch_extent_crc_unpacked) {
356 .csum_type = i->csum_type,
357 .compression_type = crc_old.compression_type,
358 .compressed_size = i->len,
359 .uncompressed_size = i->len,
366 if (bch2_csum_type_is_encryption(new_csum_type))
374 int bch2_request_key(struct bch_sb *sb, struct bch_key *key)
376 char key_description[60];
377 struct key *keyring_key;
378 const struct user_key_payload *ukp;
381 snprintf(key_description, sizeof(key_description),
382 "bcachefs:%pUb", &sb->user_uuid);
384 keyring_key = request_key(&key_type_logon, key_description, NULL);
385 if (IS_ERR(keyring_key))
386 return PTR_ERR(keyring_key);
388 down_read(&keyring_key->sem);
389 ukp = dereference_key_locked(keyring_key);
390 if (ukp->datalen == sizeof(*key)) {
391 memcpy(key, ukp->data, ukp->datalen);
396 up_read(&keyring_key->sem);
397 key_put(keyring_key);
402 #include <keyutils.h>
403 #include <uuid/uuid.h>
405 int bch2_request_key(struct bch_sb *sb, struct bch_key *key)
408 char key_description[60];
411 uuid_unparse_lower(sb->user_uuid.b, uuid);
412 sprintf(key_description, "bcachefs:%s", uuid);
414 key_id = request_key("user", key_description, NULL,
415 KEY_SPEC_USER_KEYRING);
419 if (keyctl_read(key_id, (void *) key, sizeof(*key)) != sizeof(*key))
426 int bch2_decrypt_sb_key(struct bch_fs *c,
427 struct bch_sb_field_crypt *crypt,
430 struct bch_encrypted_key sb_key = crypt->key;
431 struct bch_key user_key;
434 /* is key encrypted? */
435 if (!bch2_key_is_encrypted(&sb_key))
438 ret = bch2_request_key(c->disk_sb.sb, &user_key);
440 bch_err(c, "error requesting encryption key: %i", ret);
444 /* decrypt real key: */
445 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
446 &sb_key, sizeof(sb_key));
450 if (bch2_key_is_encrypted(&sb_key)) {
451 bch_err(c, "incorrect encryption key");
458 memzero_explicit(&sb_key, sizeof(sb_key));
459 memzero_explicit(&user_key, sizeof(user_key));
463 static int bch2_alloc_ciphers(struct bch_fs *c)
466 c->chacha20 = crypto_alloc_sync_skcipher("chacha20", 0, 0);
467 if (IS_ERR(c->chacha20)) {
468 bch_err(c, "error requesting chacha20 module: %li",
469 PTR_ERR(c->chacha20));
470 return PTR_ERR(c->chacha20);
474 c->poly1305 = crypto_alloc_shash("poly1305", 0, 0);
475 if (IS_ERR(c->poly1305)) {
476 bch_err(c, "error requesting poly1305 module: %li",
477 PTR_ERR(c->poly1305));
478 return PTR_ERR(c->poly1305);
484 int bch2_disable_encryption(struct bch_fs *c)
486 struct bch_sb_field_crypt *crypt;
490 mutex_lock(&c->sb_lock);
492 crypt = bch2_sb_get_crypt(c->disk_sb.sb);
496 /* is key encrypted? */
498 if (bch2_key_is_encrypted(&crypt->key))
501 ret = bch2_decrypt_sb_key(c, crypt, &key);
505 crypt->key.magic = BCH_KEY_MAGIC;
506 crypt->key.key = key;
508 SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 0);
511 mutex_unlock(&c->sb_lock);
516 int bch2_enable_encryption(struct bch_fs *c, bool keyed)
518 struct bch_encrypted_key key;
519 struct bch_key user_key;
520 struct bch_sb_field_crypt *crypt;
523 mutex_lock(&c->sb_lock);
525 /* Do we already have an encryption key? */
526 if (bch2_sb_get_crypt(c->disk_sb.sb))
529 ret = bch2_alloc_ciphers(c);
533 key.magic = BCH_KEY_MAGIC;
534 get_random_bytes(&key.key, sizeof(key.key));
537 ret = bch2_request_key(c->disk_sb.sb, &user_key);
539 bch_err(c, "error requesting encryption key: %i", ret);
543 ret = bch2_chacha_encrypt_key(&user_key, bch2_sb_key_nonce(c),
549 ret = crypto_skcipher_setkey(&c->chacha20->base,
550 (void *) &key.key, sizeof(key.key));
554 crypt = bch2_sb_resize_crypt(&c->disk_sb, sizeof(*crypt) / sizeof(u64));
556 ret = -ENOMEM; /* XXX this technically could be -ENOSPC */
562 /* write superblock */
563 SET_BCH_SB_ENCRYPTION_TYPE(c->disk_sb.sb, 1);
566 mutex_unlock(&c->sb_lock);
567 memzero_explicit(&user_key, sizeof(user_key));
568 memzero_explicit(&key, sizeof(key));
572 void bch2_fs_encryption_exit(struct bch_fs *c)
574 if (!IS_ERR_OR_NULL(c->poly1305))
575 crypto_free_shash(c->poly1305);
576 if (!IS_ERR_OR_NULL(c->chacha20))
577 crypto_free_sync_skcipher(c->chacha20);
578 if (!IS_ERR_OR_NULL(c->sha256))
579 crypto_free_shash(c->sha256);
582 int bch2_fs_encryption_init(struct bch_fs *c)
584 struct bch_sb_field_crypt *crypt;
588 pr_verbose_init(c->opts, "");
590 c->sha256 = crypto_alloc_shash("sha256", 0, 0);
591 if (IS_ERR(c->sha256)) {
592 bch_err(c, "error requesting sha256 module");
593 ret = PTR_ERR(c->sha256);
597 crypt = bch2_sb_get_crypt(c->disk_sb.sb);
601 ret = bch2_alloc_ciphers(c);
605 ret = bch2_decrypt_sb_key(c, crypt, &key);
609 ret = crypto_skcipher_setkey(&c->chacha20->base,
610 (void *) &key.key, sizeof(key.key));
614 memzero_explicit(&key, sizeof(key));
615 pr_verbose_init(c->opts, "ret %i", ret);