OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [crypto/] [api.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * Scatterlist Cryptographic API.
3
 *
4
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5
 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6
 *
7
 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
8
 * and Nettle, by Niels Möller.
9
 *
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)
13
 * any later version.
14
 *
15
 */
16
#include <linux/init.h>
17
#include <linux/crypto.h>
18
#include <linux/errno.h>
19
#include <linux/rwsem.h>
20
#include <linux/slab.h>
21
#include "internal.h"
22
 
23
LIST_HEAD(crypto_alg_list);
24
DECLARE_RWSEM(crypto_alg_sem);
25
 
26
static inline int crypto_alg_get(struct crypto_alg *alg)
27
{
28
        return try_inc_mod_count(alg->cra_module);
29
}
30
 
31
static inline void crypto_alg_put(struct crypto_alg *alg)
32
{
33
        if (alg->cra_module)
34
                __MOD_DEC_USE_COUNT(alg->cra_module);
35
}
36
 
37
struct crypto_alg *crypto_alg_lookup(const char *name)
38
{
39
        struct crypto_alg *q, *alg = NULL;
40
 
41
        if (!name)
42
                return NULL;
43
 
44
        down_read(&crypto_alg_sem);
45
 
46
        list_for_each_entry(q, &crypto_alg_list, cra_list) {
47
                if (!(strcmp(q->cra_name, name))) {
48
                        if (crypto_alg_get(q))
49
                                alg = q;
50
                        break;
51
                }
52
        }
53
 
54
        up_read(&crypto_alg_sem);
55
        return alg;
56
}
57
 
58
static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
59
{
60
        tfm->crt_flags = 0;
61
 
62
        switch (crypto_tfm_alg_type(tfm)) {
63
        case CRYPTO_ALG_TYPE_CIPHER:
64
                return crypto_init_cipher_flags(tfm, flags);
65
 
66
        case CRYPTO_ALG_TYPE_DIGEST:
67
                return crypto_init_digest_flags(tfm, flags);
68
 
69
        case CRYPTO_ALG_TYPE_COMPRESS:
70
                return crypto_init_compress_flags(tfm, flags);
71
 
72
        default:
73
                break;
74
        }
75
 
76
        BUG();
77
        return -EINVAL;
78
}
79
 
80
static int crypto_init_ops(struct crypto_tfm *tfm)
81
{
82
        switch (crypto_tfm_alg_type(tfm)) {
83
        case CRYPTO_ALG_TYPE_CIPHER:
84
                return crypto_init_cipher_ops(tfm);
85
 
86
        case CRYPTO_ALG_TYPE_DIGEST:
87
                return crypto_init_digest_ops(tfm);
88
 
89
        case CRYPTO_ALG_TYPE_COMPRESS:
90
                return crypto_init_compress_ops(tfm);
91
 
92
        default:
93
                break;
94
        }
95
 
96
        BUG();
97
        return -EINVAL;
98
}
99
 
100
static void crypto_exit_ops(struct crypto_tfm *tfm)
101
{
102
        switch (crypto_tfm_alg_type(tfm)) {
103
        case CRYPTO_ALG_TYPE_CIPHER:
104
                crypto_exit_cipher_ops(tfm);
105
                break;
106
 
107
        case CRYPTO_ALG_TYPE_DIGEST:
108
                crypto_exit_digest_ops(tfm);
109
                break;
110
 
111
        case CRYPTO_ALG_TYPE_COMPRESS:
112
                crypto_exit_compress_ops(tfm);
113
                break;
114
 
115
        default:
116
                BUG();
117
 
118
        }
119
}
120
 
121
struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
122
{
123
        struct crypto_tfm *tfm = NULL;
124
        struct crypto_alg *alg;
125
 
126
        alg = crypto_alg_mod_lookup(name);
127
        if (alg == NULL)
128
                goto out;
129
 
130
        tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
131
        if (tfm == NULL)
132
                goto out_put;
133
 
134
        memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
135
 
136
        tfm->__crt_alg = alg;
137
 
138
        if (crypto_init_flags(tfm, flags))
139
                goto out_free_tfm;
140
 
141
        if (crypto_init_ops(tfm)) {
142
                crypto_exit_ops(tfm);
143
                goto out_free_tfm;
144
        }
145
 
146
        goto out;
147
 
148
out_free_tfm:
149
        kfree(tfm);
150
        tfm = NULL;
151
out_put:
152
        crypto_alg_put(alg);
153
out:
154
        return tfm;
155
}
156
 
157
void crypto_free_tfm(struct crypto_tfm *tfm)
158
{
159
        crypto_exit_ops(tfm);
160
        crypto_alg_put(tfm->__crt_alg);
161
        kfree(tfm);
162
}
163
 
164
int crypto_register_alg(struct crypto_alg *alg)
165
{
166
        int ret = 0;
167
        struct crypto_alg *q;
168
 
169
        down_write(&crypto_alg_sem);
170
 
171
        list_for_each_entry(q, &crypto_alg_list, cra_list) {
172
                if (!(strcmp(q->cra_name, alg->cra_name))) {
173
                        ret = -EEXIST;
174
                        goto out;
175
                }
176
        }
177
 
178
        list_add_tail(&alg->cra_list, &crypto_alg_list);
179
out:
180
        up_write(&crypto_alg_sem);
181
        return ret;
182
}
183
 
184
int crypto_unregister_alg(struct crypto_alg *alg)
185
{
186
        int ret = -ENOENT;
187
        struct crypto_alg *q;
188
 
189
        BUG_ON(!alg->cra_module);
190
 
191
        down_write(&crypto_alg_sem);
192
        list_for_each_entry(q, &crypto_alg_list, cra_list) {
193
                if (alg == q) {
194
                        list_del(&alg->cra_list);
195
                        ret = 0;
196
                        goto out;
197
                }
198
        }
199
out:
200
        up_write(&crypto_alg_sem);
201
        return ret;
202
}
203
 
204
int crypto_alg_available(const char *name, u32 flags)
205
{
206
        int ret = 0;
207
        struct crypto_alg *alg = crypto_alg_mod_lookup(name);
208
 
209
        if (alg) {
210
                crypto_alg_put(alg);
211
                ret = 1;
212
        }
213
 
214
        return ret;
215
}
216
 
217
static int __init init_crypto(void)
218
{
219
        printk(KERN_INFO "Initializing Cryptographic API\n");
220
        crypto_init_proc();
221
        return 0;
222
}
223
 
224
__initcall(init_crypto);
225
 
226
EXPORT_SYMBOL_GPL(crypto_register_alg);
227
EXPORT_SYMBOL_GPL(crypto_unregister_alg);
228
EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
229
EXPORT_SYMBOL_GPL(crypto_free_tfm);
230
EXPORT_SYMBOL_GPL(crypto_alg_available);

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.