]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/crypto/api.c
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / linux / crypto / api.c
1 /*
2  * Cryptographic API for algorithms (i.e., low-level API).
3  *
4  * Copyright (c) 2006 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 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/rwsem.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21
22 #include <crypto/algapi.h>
23
24 static LIST_HEAD(crypto_alg_list);
25 static DECLARE_RWSEM(crypto_alg_sem);
26
27 struct crypto_type {
28 };
29
30 int crypto_register_alg(struct crypto_alg *alg)
31 {
32         down_write(&crypto_alg_sem);
33         list_add(&alg->cra_list, &crypto_alg_list);
34         up_write(&crypto_alg_sem);
35
36         return 0;
37 }
38
39 static void *crypto_alloc_tfm(const char *name,
40                               const struct crypto_type *type)
41 {
42         struct crypto_alg *alg;
43
44         down_read(&crypto_alg_sem);
45         list_for_each_entry(alg, &crypto_alg_list, cra_list)
46                 if (alg->cra_type == type && !strcmp(alg->cra_name, name))
47                         goto found;
48
49         alg = ERR_PTR(-ENOENT);
50 found:
51         up_read(&crypto_alg_sem);
52
53         if (IS_ERR(alg))
54                 return ERR_CAST(alg);
55
56         return alg->alloc_tfm() ?: ERR_PTR(-ENOMEM);
57 }
58
59 /* skcipher: */
60
61 static const struct crypto_type crypto_skcipher_type2 = {
62 };
63
64 struct crypto_skcipher *crypto_alloc_skcipher(const char *name,
65                                               u32 type, u32 mask)
66 {
67         return crypto_alloc_tfm(name, &crypto_skcipher_type2);
68 }
69
70 int crypto_register_skcipher(struct skcipher_alg *alg)
71 {
72         alg->base.cra_type = &crypto_skcipher_type2;
73
74         return crypto_register_alg(&alg->base);
75 }
76
77 /* shash: */
78
79 #include <crypto/hash.h>
80
81 static int shash_finup(struct shash_desc *desc, const u8 *data,
82                        unsigned len, u8 *out)
83 {
84         return crypto_shash_update(desc, data, len) ?:
85                crypto_shash_final(desc, out);
86 }
87
88 static int shash_digest(struct shash_desc *desc, const u8 *data,
89                                   unsigned len, u8 *out)
90 {
91         return crypto_shash_init(desc) ?:
92                crypto_shash_finup(desc, data, len, out);
93 }
94
95 static const struct crypto_type crypto_shash_type = {
96 };
97
98 struct crypto_shash *crypto_alloc_shash(const char *name,
99                                         u32 type, u32 mask)
100 {
101         return crypto_alloc_tfm(name, &crypto_shash_type);
102 }
103
104 int crypto_register_shash(struct shash_alg *alg)
105 {
106         alg->base.cra_type = &crypto_shash_type;
107
108         if (!alg->finup)
109                 alg->finup = shash_finup;
110         if (!alg->digest)
111                 alg->digest = shash_digest;
112
113         return crypto_register_alg(&alg->base);
114 }