2
* Scatterlist Cryptographic API.
4
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5
* Copyright (c) 2002 David S. Miller (davem@redhat.com)
7
* Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8
* and Nettle, by Niels M鰈ler.
10
* This program is free software; you can redistribute it and/or modify it
11
* under the terms of the GNU General Public License as published by the Free
12
* Software Foundation; either version 2 of the License, or (at your option)
18
#include <linux/init.h>
19
#include <linux/module.h>
20
#include "rtl_crypto.h"
21
#include <linux/errno.h>
22
#include <linux/rwsem.h>
23
#include <linux/slab.h>
26
LIST_HEAD(crypto_alg_list);
27
DECLARE_RWSEM(crypto_alg_sem);
29
static inline int crypto_alg_get(struct crypto_alg *alg)
31
#ifdef BUILT_IN_CRYPTO
34
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
35
return try_module_get(alg->cra_module);
37
return try_inc_mod_count(alg->cra_module);
42
static inline void crypto_alg_put(struct crypto_alg *alg)
44
#ifndef BUILT_IN_CRYPTO
46
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
47
module_put(alg->cra_module);
49
__MOD_DEC_USE_COUNT(alg->cra_module);
54
struct crypto_alg *crypto_alg_lookup(const char *name)
56
struct crypto_alg *q, *alg = NULL;
61
down_read(&crypto_alg_sem);
63
list_for_each_entry(q, &crypto_alg_list, cra_list) {
64
if (!(strcmp(q->cra_name, name))) {
65
if (crypto_alg_get(q))
71
up_read(&crypto_alg_sem);
75
static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
79
switch (crypto_tfm_alg_type(tfm)) {
80
case CRYPTO_ALG_TYPE_CIPHER:
81
return crypto_init_cipher_flags(tfm, flags);
83
case CRYPTO_ALG_TYPE_DIGEST:
84
return crypto_init_digest_flags(tfm, flags);
86
case CRYPTO_ALG_TYPE_COMPRESS:
87
return crypto_init_compress_flags(tfm, flags);
97
static int crypto_init_ops(struct crypto_tfm *tfm)
99
switch (crypto_tfm_alg_type(tfm)) {
100
case CRYPTO_ALG_TYPE_CIPHER:
101
return crypto_init_cipher_ops(tfm);
103
case CRYPTO_ALG_TYPE_DIGEST:
104
return crypto_init_digest_ops(tfm);
106
case CRYPTO_ALG_TYPE_COMPRESS:
107
return crypto_init_compress_ops(tfm);
117
static void crypto_exit_ops(struct crypto_tfm *tfm)
119
switch (crypto_tfm_alg_type(tfm)) {
120
case CRYPTO_ALG_TYPE_CIPHER:
121
crypto_exit_cipher_ops(tfm);
124
case CRYPTO_ALG_TYPE_DIGEST:
125
crypto_exit_digest_ops(tfm);
128
case CRYPTO_ALG_TYPE_COMPRESS:
129
crypto_exit_compress_ops(tfm);
138
struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
140
struct crypto_tfm *tfm = NULL;
141
struct crypto_alg *alg;
143
alg = crypto_alg_mod_lookup(name);
147
tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
151
memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
153
tfm->__crt_alg = alg;
155
if (crypto_init_flags(tfm, flags))
158
if (crypto_init_ops(tfm)) {
159
crypto_exit_ops(tfm);
174
void crypto_free_tfm(struct crypto_tfm *tfm)
176
struct crypto_alg *alg = tfm->__crt_alg;
177
int size = sizeof(*tfm) + alg->cra_ctxsize;
179
crypto_exit_ops(tfm);
181
memset(tfm, 0, size);
185
int crypto_register_alg(struct crypto_alg *alg)
188
struct crypto_alg *q;
190
down_write(&crypto_alg_sem);
192
list_for_each_entry(q, &crypto_alg_list, cra_list) {
193
if (!(strcmp(q->cra_name, alg->cra_name))) {
199
list_add_tail(&alg->cra_list, &crypto_alg_list);
201
up_write(&crypto_alg_sem);
205
int crypto_unregister_alg(struct crypto_alg *alg)
208
struct crypto_alg *q;
210
BUG_ON(!alg->cra_module);
212
down_write(&crypto_alg_sem);
213
list_for_each_entry(q, &crypto_alg_list, cra_list) {
215
list_del(&alg->cra_list);
221
up_write(&crypto_alg_sem);
225
int crypto_alg_available(const char *name, u32 flags)
228
struct crypto_alg *alg = crypto_alg_mod_lookup(name);
238
#ifndef BUILT_IN_CRYPTO
239
static int __init init_crypto(void)
241
printk(KERN_INFO "Initializing Cryptographic API\n");
246
__initcall(init_crypto);
250
EXPORT_SYMBOL_GPL(crypto_register_alg);
251
EXPORT_SYMBOL_GPL(crypto_unregister_alg);
252
EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
253
EXPORT_SYMBOL_GPL(crypto_free_tfm);
254
EXPORT_SYMBOL_GPL(crypto_alg_available);
256
EXPORT_SYMBOL_RSL(crypto_register_alg);
257
EXPORT_SYMBOL_RSL(crypto_unregister_alg);
258
EXPORT_SYMBOL_RSL(crypto_alloc_tfm);
259
EXPORT_SYMBOL_RSL(crypto_free_tfm);
260
EXPORT_SYMBOL_RSL(crypto_alg_available);