]> git.sesse.net Git - bcachefs-tools-debian/blob - include/crypto/hash.h
bcache in userspace; userspace fsck
[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 hash_alg_common {
20         unsigned int digestsize;
21         unsigned int statesize;
22
23         struct crypto_alg base;
24 };
25
26 struct shash_desc {
27         struct crypto_shash *tfm;
28         u32 flags;
29
30         void *__ctx[] CRYPTO_MINALIGN_ATTR;
31 };
32
33 #define SHASH_DESC_ON_STACK(shash, ctx)                           \
34         char __##shash##_desc[sizeof(struct shash_desc) +         \
35                 crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \
36         struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
37
38 struct shash_alg {
39         int (*init)(struct shash_desc *desc);
40         int (*update)(struct shash_desc *desc, const u8 *data,
41                       unsigned int len);
42         int (*final)(struct shash_desc *desc, u8 *out);
43         int (*finup)(struct shash_desc *desc, const u8 *data,
44                      unsigned int len, u8 *out);
45         int (*digest)(struct shash_desc *desc, const u8 *data,
46                       unsigned int len, u8 *out);
47         int (*export)(struct shash_desc *desc, void *out);
48         int (*import)(struct shash_desc *desc, const void *in);
49         int (*setkey)(struct crypto_shash *tfm, const u8 *key,
50                       unsigned int keylen);
51
52         unsigned int descsize;
53
54         /* These fields must match hash_alg_common. */
55         unsigned int digestsize
56                 __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
57         unsigned int statesize;
58
59         struct crypto_alg base;
60 };
61
62 struct crypto_shash {
63         unsigned int descsize;
64         struct crypto_tfm base;
65 };
66
67 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
68                                         u32 mask);
69
70 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
71 {
72         return &tfm->base;
73 }
74
75 static inline void crypto_free_shash(struct crypto_shash *tfm)
76 {
77         crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
78 }
79
80 static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
81 {
82         return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
83 }
84
85 static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
86 {
87         return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
88 }
89
90 static inline unsigned int crypto_shash_alignmask(
91         struct crypto_shash *tfm)
92 {
93         return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
94 }
95
96 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
97 {
98         return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
99 }
100
101 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
102 {
103         return container_of(alg, struct shash_alg, base);
104 }
105
106 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
107 {
108         return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
109 }
110
111 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
112 {
113         return crypto_shash_alg(tfm)->digestsize;
114 }
115
116 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
117 {
118         return crypto_shash_alg(tfm)->statesize;
119 }
120
121 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
122 {
123         return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
124 }
125
126 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
127 {
128         crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
129 }
130
131 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
132 {
133         crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
134 }
135
136 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
137 {
138         return tfm->descsize;
139 }
140
141 static inline void *shash_desc_ctx(struct shash_desc *desc)
142 {
143         return desc->__ctx;
144 }
145
146 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
147                         unsigned int keylen);
148
149 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
150                         unsigned int len, u8 *out);
151
152 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
153 {
154         return crypto_shash_alg(desc->tfm)->export(desc, out);
155 }
156
157 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
158 {
159         return crypto_shash_alg(desc->tfm)->import(desc, in);
160 }
161
162 static inline int crypto_shash_init(struct shash_desc *desc)
163 {
164         return crypto_shash_alg(desc->tfm)->init(desc);
165 }
166
167 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
168                         unsigned int len);
169
170 int crypto_shash_final(struct shash_desc *desc, u8 *out);
171
172 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
173                        unsigned int len, u8 *out);
174
175 static inline void shash_desc_zero(struct shash_desc *desc)
176 {
177         memzero_explicit(desc,
178                          sizeof(*desc) + crypto_shash_descsize(desc->tfm));
179 }
180
181 #endif  /* _CRYPTO_HASH_H */