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