]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/checksum.h
a9a1758791a92ab332ae94c28f6c5fa29daaa09d
[bcachefs-tools-debian] / libbcache / checksum.h
1 #ifndef _BCACHE_CHECKSUM_H
2 #define _BCACHE_CHECKSUM_H
3
4 #include "bcache.h"
5 #include "super-io.h"
6
7 #include <crypto/chacha20.h>
8
9 u64 bch_crc64_update(u64, const void *, size_t);
10
11 #define BCH_NONCE_EXTENT        cpu_to_le32(1 << 28)
12 #define BCH_NONCE_BTREE         cpu_to_le32(2 << 28)
13 #define BCH_NONCE_JOURNAL       cpu_to_le32(3 << 28)
14 #define BCH_NONCE_PRIO          cpu_to_le32(4 << 28)
15 #define BCH_NONCE_POLY          cpu_to_le32(1 << 31)
16
17 struct bch_csum bch_checksum(struct cache_set *, unsigned, struct nonce,
18                              const void *, size_t);
19
20 /*
21  * This is used for various on disk data structures - bch_sb, prio_set, bset,
22  * jset: The checksum is _always_ the first field of these structs
23  */
24 #define csum_vstruct(_c, _type, _nonce, _i)                             \
25 ({                                                                      \
26         const void *start = ((const void *) (_i)) + sizeof((_i)->csum); \
27         const void *end = vstruct_end(_i);                              \
28                                                                         \
29         bch_checksum(_c, _type, _nonce, start, end - start);            \
30 })
31
32 int bch_chacha_encrypt_key(struct bch_key *, struct nonce, void *, size_t);
33 int bch_request_key(struct bch_sb *, struct bch_key *);
34
35 void bch_encrypt(struct cache_set *, unsigned, struct nonce,
36                  void *data, size_t);
37
38 struct bch_csum bch_checksum_bio(struct cache_set *, unsigned,
39                                  struct nonce, struct bio *);
40 void bch_encrypt_bio(struct cache_set *, unsigned,
41                     struct nonce, struct bio *);
42
43 int bch_disable_encryption(struct cache_set *);
44 int bch_enable_encryption(struct cache_set *, bool);
45
46 void bch_cache_set_encryption_free(struct cache_set *);
47 int bch_cache_set_encryption_init(struct cache_set *);
48
49 static inline unsigned bch_data_checksum_type(struct cache_set *c)
50 {
51         if (c->sb.encryption_type)
52                 return c->opts.wide_macs
53                         ? BCH_CSUM_CHACHA20_POLY1305_128
54                         : BCH_CSUM_CHACHA20_POLY1305_80;
55
56         return c->opts.data_checksum;
57 }
58
59 static inline unsigned bch_meta_checksum_type(struct cache_set *c)
60 {
61         return c->sb.encryption_type
62                 ? BCH_CSUM_CHACHA20_POLY1305_128
63                 : c->opts.metadata_checksum;
64 }
65
66 static inline bool bch_checksum_type_valid(const struct cache_set *c,
67                                            unsigned type)
68 {
69         if (type >= BCH_CSUM_NR)
70                 return false;
71
72         if (bch_csum_type_is_encryption(type) && !c->chacha20)
73                 return false;
74
75         return true;
76 }
77
78 static const unsigned bch_crc_bytes[] = {
79         [BCH_CSUM_NONE]                         = 0,
80         [BCH_CSUM_CRC32C]                       = 4,
81         [BCH_CSUM_CRC64]                        = 8,
82         [BCH_CSUM_CHACHA20_POLY1305_80]         = 10,
83         [BCH_CSUM_CHACHA20_POLY1305_128]        = 16,
84 };
85
86 static inline bool bch_crc_cmp(struct bch_csum l, struct bch_csum r)
87 {
88         /*
89          * XXX: need some way of preventing the compiler from optimizing this
90          * into a form that isn't constant time..
91          */
92         return ((l.lo ^ r.lo) | (l.hi ^ r.hi)) != 0;
93 }
94
95 /* for skipping ahead and encrypting/decrypting at an offset: */
96 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
97 {
98         EBUG_ON(offset & (CHACHA20_BLOCK_SIZE - 1));
99
100         le32_add_cpu(&nonce.d[0], offset / CHACHA20_BLOCK_SIZE);
101         return nonce;
102 }
103
104 static inline bool bch_key_is_encrypted(struct bch_encrypted_key *key)
105 {
106         return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
107 }
108
109 static inline struct nonce __bch_sb_key_nonce(struct bch_sb *sb)
110 {
111         __le64 magic = __bch_sb_magic(sb);
112
113         return (struct nonce) {{
114                 [0] = 0,
115                 [1] = 0,
116                 [2] = ((__le32 *) &magic)[0],
117                 [3] = ((__le32 *) &magic)[1],
118         }};
119 }
120
121 static inline struct nonce bch_sb_key_nonce(struct cache_set *c)
122 {
123         __le64 magic = bch_sb_magic(c);
124
125         return (struct nonce) {{
126                 [0] = 0,
127                 [1] = 0,
128                 [2] = ((__le32 *) &magic)[0],
129                 [3] = ((__le32 *) &magic)[1],
130         }};
131 }
132
133 #endif /* _BCACHE_CHECKSUM_H */