1 #ifndef _BCACHE_CHECKSUM_H
2 #define _BCACHE_CHECKSUM_H
7 #include <crypto/chacha20.h>
9 u64 bch_crc64_update(u64, const void *, size_t);
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)
17 struct bch_csum bch_checksum(struct cache_set *, unsigned, struct nonce,
18 const void *, size_t);
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
24 #define csum_vstruct(_c, _type, _nonce, _i) \
26 const void *start = ((const void *) (_i)) + sizeof((_i)->csum); \
27 const void *end = vstruct_end(_i); \
29 bch_checksum(_c, _type, _nonce, start, end - start); \
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 *);
35 void bch_encrypt(struct cache_set *, unsigned, struct nonce,
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 *);
43 int bch_disable_encryption(struct cache_set *);
44 int bch_enable_encryption(struct cache_set *, bool);
46 void bch_cache_set_encryption_free(struct cache_set *);
47 int bch_cache_set_encryption_init(struct cache_set *);
49 static inline unsigned bch_data_checksum_type(struct cache_set *c)
51 if (c->sb.encryption_type)
52 return c->opts.wide_macs
53 ? BCH_CSUM_CHACHA20_POLY1305_128
54 : BCH_CSUM_CHACHA20_POLY1305_80;
56 return c->opts.data_checksum;
59 static inline unsigned bch_meta_checksum_type(struct cache_set *c)
61 return c->sb.encryption_type
62 ? BCH_CSUM_CHACHA20_POLY1305_128
63 : c->opts.metadata_checksum;
66 static inline bool bch_checksum_type_valid(const struct cache_set *c,
69 if (type >= BCH_CSUM_NR)
72 if (bch_csum_type_is_encryption(type) && !c->chacha20)
78 static const unsigned bch_crc_bytes[] = {
80 [BCH_CSUM_CRC32C] = 4,
82 [BCH_CSUM_CHACHA20_POLY1305_80] = 10,
83 [BCH_CSUM_CHACHA20_POLY1305_128] = 16,
86 static inline bool bch_crc_cmp(struct bch_csum l, struct bch_csum r)
89 * XXX: need some way of preventing the compiler from optimizing this
90 * into a form that isn't constant time..
92 return ((l.lo ^ r.lo) | (l.hi ^ r.hi)) != 0;
95 /* for skipping ahead and encrypting/decrypting at an offset: */
96 static inline struct nonce nonce_add(struct nonce nonce, unsigned offset)
98 EBUG_ON(offset & (CHACHA20_BLOCK_SIZE - 1));
100 le32_add_cpu(&nonce.d[0], offset / CHACHA20_BLOCK_SIZE);
104 static inline bool bch_key_is_encrypted(struct bch_encrypted_key *key)
106 return le64_to_cpu(key->magic) != BCH_KEY_MAGIC;
109 static inline struct nonce __bch_sb_key_nonce(struct bch_sb *sb)
111 __le64 magic = __bch_sb_magic(sb);
113 return (struct nonce) {{
116 [2] = ((__le32 *) &magic)[0],
117 [3] = ((__le32 *) &magic)[1],
121 static inline struct nonce bch_sb_key_nonce(struct cache_set *c)
123 __le64 magic = bch_sb_magic(c);
125 return (struct nonce) {{
128 [2] = ((__le32 *) &magic)[0],
129 [3] = ((__le32 *) &magic)[1],
133 #endif /* _BCACHE_CHECKSUM_H */