]> git.sesse.net Git - bcachefs-tools-debian/blob - include/crypto/hash.h
New on disk format - encryption
[bcachefs-tools-debian] / include / crypto / hash.h
1 /*
2  * Hash: Hash algorithms under the crypto API
3  * 
4  * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option) 
9  * any later version.
10  *
11  */
12
13 #ifndef _CRYPTO_HASH_H
14 #define _CRYPTO_HASH_H
15
16 #include <linux/crypto.h>
17 #include <linux/string.h>
18
19 struct shash_desc {
20         struct crypto_shash *tfm;
21         u32 flags;
22
23         void *__ctx[] CRYPTO_MINALIGN_ATTR;
24 };
25
26 #define SHASH_DESC_ON_STACK(shash, ctx)                           \
27         char __##shash##_desc[sizeof(struct shash_desc) +         \
28                 crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \
29         struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
30
31 struct shash_alg {
32         int (*init)(struct shash_desc *desc);
33         int (*update)(struct shash_desc *desc, const u8 *data, unsigned len);
34         int (*final)(struct shash_desc *desc, u8 *out);
35         int (*finup)(struct shash_desc *desc, const u8 *data,
36                      unsigned len, u8 *out);
37         int (*digest)(struct shash_desc *desc, const u8 *data,
38                       unsigned len, u8 *out);
39
40         unsigned                descsize;
41         unsigned                digestsize;
42         struct crypto_alg       base;
43 };
44
45 struct crypto_shash {
46         unsigned                descsize;
47         struct crypto_tfm       base;
48 };
49
50 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
51                                         u32 mask);
52
53 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
54 {
55         return &tfm->base;
56 }
57
58 static inline void crypto_free_shash(struct crypto_shash *tfm)
59 {
60         crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
61 }
62
63 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
64 {
65         return container_of(alg, struct shash_alg, base);
66 }
67
68 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
69 {
70         return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
71 }
72
73 static inline unsigned crypto_shash_digestsize(struct crypto_shash *tfm)
74 {
75         return crypto_shash_alg(tfm)->digestsize;
76 }
77
78 static inline unsigned crypto_shash_descsize(struct crypto_shash *tfm)
79 {
80         return tfm->descsize;
81 }
82
83 static inline void *shash_desc_ctx(struct shash_desc *desc)
84 {
85         return desc->__ctx;
86 }
87
88 static inline int crypto_shash_init(struct shash_desc *desc)
89 {
90         return crypto_shash_alg(desc->tfm)->init(desc);
91 }
92
93 static inline int crypto_shash_update(struct shash_desc *desc,
94                                       const u8 *data, unsigned len)
95 {
96         return crypto_shash_alg(desc->tfm)->update(desc, data, len);
97 }
98
99 static inline int crypto_shash_final(struct shash_desc *desc, u8 *out)
100 {
101         return crypto_shash_alg(desc->tfm)->final(desc, out);
102 }
103
104 static inline int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
105                                      unsigned len, u8 *out)
106 {
107         return crypto_shash_alg(desc->tfm)->finup(desc, data, len, out);
108 }
109
110 static inline int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
111                                       unsigned len, u8 *out)
112 {
113         return crypto_shash_alg(desc->tfm)->digest(desc, data, len, out);
114 }
115
116 #endif  /* _CRYPTO_HASH_H */