2 * Cryptographic API for algorithms (i.e., low-level API).
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
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>
22 #include <crypto/algapi.h>
24 static LIST_HEAD(crypto_alg_list);
25 static DECLARE_RWSEM(crypto_alg_sem);
30 int crypto_register_alg(struct crypto_alg *alg)
32 down_write(&crypto_alg_sem);
33 list_add(&alg->cra_list, &crypto_alg_list);
34 up_write(&crypto_alg_sem);
39 static void *crypto_alloc_tfm(const char *name,
40 const struct crypto_type *type)
42 struct crypto_alg *alg;
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))
49 alg = ERR_PTR(-ENOENT);
51 up_read(&crypto_alg_sem);
56 return alg->alloc_tfm() ?: ERR_PTR(-ENOMEM);
61 static const struct crypto_type crypto_skcipher_type2 = {
64 struct crypto_skcipher *crypto_alloc_skcipher(const char *name,
67 return crypto_alloc_tfm(name, &crypto_skcipher_type2);
70 int crypto_register_skcipher(struct skcipher_alg *alg)
72 alg->base.cra_type = &crypto_skcipher_type2;
74 return crypto_register_alg(&alg->base);
79 #include <crypto/hash.h>
81 static int shash_finup(struct shash_desc *desc, const u8 *data,
82 unsigned len, u8 *out)
84 return crypto_shash_update(desc, data, len) ?:
85 crypto_shash_final(desc, out);
88 static int shash_digest(struct shash_desc *desc, const u8 *data,
89 unsigned len, u8 *out)
91 return crypto_shash_init(desc) ?:
92 crypto_shash_finup(desc, data, len, out);
95 static const struct crypto_type crypto_shash_type = {
98 struct crypto_shash *crypto_alloc_shash(const char *name,
101 return crypto_alloc_tfm(name, &crypto_shash_type);
104 int crypto_register_shash(struct shash_alg *alg)
106 alg->base.cra_type = &crypto_shash_type;
109 alg->finup = shash_finup;
111 alg->digest = shash_digest;
113 return crypto_register_alg(&alg->base);