]> git.sesse.net Git - bcachefs-tools-debian/blob - include/crypto/hash.h
rust: bump rpassword to v7.x
[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
18 struct shash_desc;
19
20 struct shash_alg {
21         int (*init)(struct shash_desc *desc);
22         int (*update)(struct shash_desc *desc, const u8 *data, unsigned len);
23         int (*final)(struct shash_desc *desc, u8 *out);
24         int (*finup)(struct shash_desc *desc, const u8 *data,
25                      unsigned len, u8 *out);
26         int (*digest)(struct shash_desc *desc, const u8 *data,
27                       unsigned len, u8 *out);
28
29         unsigned                descsize;
30         unsigned                digestsize;
31         struct crypto_alg       base;
32 };
33
34 int crypto_register_shash(struct shash_alg *alg);
35
36 struct crypto_shash {
37         unsigned                descsize;
38         struct crypto_tfm       base;
39 };
40
41 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
42                                         u32 mask);
43
44 static inline void crypto_free_shash(struct crypto_shash *tfm)
45 {
46         kfree(tfm);
47 }
48
49 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
50 {
51         return container_of(tfm->base.alg, struct shash_alg, base);
52 }
53
54 static inline unsigned crypto_shash_digestsize(struct crypto_shash *tfm)
55 {
56         return crypto_shash_alg(tfm)->digestsize;
57 }
58
59 static inline unsigned crypto_shash_descsize(struct crypto_shash *tfm)
60 {
61         return tfm->descsize;
62 }
63
64 struct shash_desc {
65         struct crypto_shash     *tfm;
66         u32                     flags;
67
68         void                    *ctx[] CRYPTO_MINALIGN_ATTR;
69 };
70
71 #define SHASH_DESC_ON_STACK(shash, tfm)                           \
72         char __##shash##_desc[sizeof(struct shash_desc) +         \
73                 crypto_shash_descsize(tfm)] CRYPTO_MINALIGN_ATTR; \
74         struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
75
76 static inline int crypto_shash_init(struct shash_desc *desc)
77 {
78         return crypto_shash_alg(desc->tfm)->init(desc);
79 }
80
81 static inline int crypto_shash_update(struct shash_desc *desc,
82                                       const u8 *data, unsigned len)
83 {
84         return crypto_shash_alg(desc->tfm)->update(desc, data, len);
85 }
86
87 static inline int crypto_shash_final(struct shash_desc *desc, u8 *out)
88 {
89         return crypto_shash_alg(desc->tfm)->final(desc, out);
90 }
91
92 static inline int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
93                                      unsigned len, u8 *out)
94 {
95         return crypto_shash_alg(desc->tfm)->finup(desc, data, len, out);
96 }
97
98 static inline int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
99                                       unsigned len, u8 *out)
100 {
101         return crypto_shash_alg(desc->tfm)->digest(desc, data, len, out);
102 }
103
104 #endif  /* _CRYPTO_HASH_H */