Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 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/errno.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 16 | #include <linux/list.h> |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/string.h> |
| 19 | |
| 20 | #include "internal.h" |
| 21 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 22 | static LIST_HEAD(crypto_template_list); |
| 23 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 24 | static inline int crypto_set_driver_name(struct crypto_alg *alg) |
| 25 | { |
| 26 | static const char suffix[] = "-generic"; |
| 27 | char *driver_name = alg->cra_driver_name; |
| 28 | int len; |
| 29 | |
| 30 | if (*driver_name) |
| 31 | return 0; |
| 32 | |
| 33 | len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); |
| 34 | if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME) |
| 35 | return -ENAMETOOLONG; |
| 36 | |
| 37 | memcpy(driver_name + len, suffix, sizeof(suffix)); |
| 38 | return 0; |
| 39 | } |
| 40 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 41 | static int crypto_check_alg(struct crypto_alg *alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 42 | { |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 43 | if (alg->cra_alignmask & (alg->cra_alignmask + 1)) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | if (alg->cra_alignmask & alg->cra_blocksize) |
| 47 | return -EINVAL; |
| 48 | |
| 49 | if (alg->cra_blocksize > PAGE_SIZE / 8) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | if (alg->cra_priority < 0) |
| 53 | return -EINVAL; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 54 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 55 | return crypto_set_driver_name(alg); |
| 56 | } |
| 57 | |
| 58 | static int __crypto_register_alg(struct crypto_alg *alg) |
| 59 | { |
| 60 | struct crypto_alg *q; |
| 61 | int ret = -EEXIST; |
| 62 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 63 | list_for_each_entry(q, &crypto_alg_list, cra_list) { |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 64 | if (q == alg) |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 65 | goto out; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | list_add(&alg->cra_list, &crypto_alg_list); |
| 69 | atomic_set(&alg->cra_refcnt, 1); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 70 | ret = 0; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 71 | out: |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 72 | return ret; |
| 73 | } |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 74 | |
| 75 | int crypto_register_alg(struct crypto_alg *alg) |
| 76 | { |
| 77 | int err; |
| 78 | |
| 79 | err = crypto_check_alg(alg); |
| 80 | if (err) |
| 81 | return err; |
| 82 | |
| 83 | down_write(&crypto_alg_sem); |
| 84 | err = __crypto_register_alg(alg); |
| 85 | up_write(&crypto_alg_sem); |
| 86 | |
| 87 | return err; |
| 88 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(crypto_register_alg); |
| 90 | |
| 91 | int crypto_unregister_alg(struct crypto_alg *alg) |
| 92 | { |
| 93 | int ret = -ENOENT; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 94 | |
| 95 | down_write(&crypto_alg_sem); |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 96 | if (likely(!list_empty(&alg->cra_list))) { |
| 97 | list_del_init(&alg->cra_list); |
| 98 | ret = 0; |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 99 | } |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 100 | up_write(&crypto_alg_sem); |
| 101 | |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | BUG_ON(atomic_read(&alg->cra_refcnt) != 1); |
| 106 | if (alg->cra_destroy) |
| 107 | alg->cra_destroy(alg); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | EXPORT_SYMBOL_GPL(crypto_unregister_alg); |
| 112 | |
Herbert Xu | 4cc7720 | 2006-08-06 21:16:34 +1000 | [diff] [blame^] | 113 | int crypto_register_template(struct crypto_template *tmpl) |
| 114 | { |
| 115 | struct crypto_template *q; |
| 116 | int err = -EEXIST; |
| 117 | |
| 118 | down_write(&crypto_alg_sem); |
| 119 | |
| 120 | list_for_each_entry(q, &crypto_template_list, list) { |
| 121 | if (q == tmpl) |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | list_add(&tmpl->list, &crypto_template_list); |
| 126 | err = 0; |
| 127 | out: |
| 128 | up_write(&crypto_alg_sem); |
| 129 | return err; |
| 130 | } |
| 131 | EXPORT_SYMBOL_GPL(crypto_register_template); |
| 132 | |
| 133 | void crypto_unregister_template(struct crypto_template *tmpl) |
| 134 | { |
| 135 | struct crypto_instance *inst; |
| 136 | struct hlist_node *p, *n; |
| 137 | struct hlist_head *list; |
| 138 | |
| 139 | down_write(&crypto_alg_sem); |
| 140 | |
| 141 | BUG_ON(list_empty(&tmpl->list)); |
| 142 | list_del_init(&tmpl->list); |
| 143 | |
| 144 | list = &tmpl->instances; |
| 145 | hlist_for_each_entry(inst, p, list, list) { |
| 146 | BUG_ON(list_empty(&inst->alg.cra_list)); |
| 147 | list_del_init(&inst->alg.cra_list); |
| 148 | } |
| 149 | |
| 150 | up_write(&crypto_alg_sem); |
| 151 | |
| 152 | hlist_for_each_entry_safe(inst, p, n, list, list) { |
| 153 | BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); |
| 154 | tmpl->free(inst); |
| 155 | } |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(crypto_unregister_template); |
| 158 | |
| 159 | static struct crypto_template *__crypto_lookup_template(const char *name) |
| 160 | { |
| 161 | struct crypto_template *q, *tmpl = NULL; |
| 162 | |
| 163 | down_read(&crypto_alg_sem); |
| 164 | list_for_each_entry(q, &crypto_template_list, list) { |
| 165 | if (strcmp(q->name, name)) |
| 166 | continue; |
| 167 | if (unlikely(!crypto_tmpl_get(q))) |
| 168 | continue; |
| 169 | |
| 170 | tmpl = q; |
| 171 | break; |
| 172 | } |
| 173 | up_read(&crypto_alg_sem); |
| 174 | |
| 175 | return tmpl; |
| 176 | } |
| 177 | |
| 178 | struct crypto_template *crypto_lookup_template(const char *name) |
| 179 | { |
| 180 | return try_then_request_module(__crypto_lookup_template(name), name); |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(crypto_lookup_template); |
| 183 | |
| 184 | int crypto_register_instance(struct crypto_template *tmpl, |
| 185 | struct crypto_instance *inst) |
| 186 | { |
| 187 | int err = -EINVAL; |
| 188 | |
| 189 | if (inst->alg.cra_destroy) |
| 190 | goto err; |
| 191 | |
| 192 | err = crypto_check_alg(&inst->alg); |
| 193 | if (err) |
| 194 | goto err; |
| 195 | |
| 196 | inst->alg.cra_module = tmpl->module; |
| 197 | |
| 198 | down_write(&crypto_alg_sem); |
| 199 | |
| 200 | err = __crypto_register_alg(&inst->alg); |
| 201 | if (err) |
| 202 | goto unlock; |
| 203 | |
| 204 | hlist_add_head(&inst->list, &tmpl->instances); |
| 205 | inst->tmpl = tmpl; |
| 206 | |
| 207 | unlock: |
| 208 | up_write(&crypto_alg_sem); |
| 209 | |
| 210 | err: |
| 211 | return err; |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(crypto_register_instance); |
| 214 | |
Herbert Xu | cce9e06 | 2006-08-21 21:08:13 +1000 | [diff] [blame] | 215 | static int __init crypto_algapi_init(void) |
| 216 | { |
| 217 | crypto_init_proc(); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static void __exit crypto_algapi_exit(void) |
| 222 | { |
| 223 | crypto_exit_proc(); |
| 224 | } |
| 225 | |
| 226 | module_init(crypto_algapi_init); |
| 227 | module_exit(crypto_algapi_exit); |
| 228 | |
| 229 | MODULE_LICENSE("GPL"); |
| 230 | MODULE_DESCRIPTION("Cryptographic algorithms API"); |