algboss.c 6.25 KB
Newer Older
Herbert Xu's avatar
Herbert Xu committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Create default crypto algorithm instances.
 *
 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */

13
#include <crypto/internal/aead.h>
Herbert Xu's avatar
Herbert Xu committed
14 15 16
#include <linux/ctype.h>
#include <linux/err.h>
#include <linux/init.h>
17
#include <linux/kthread.h>
Herbert Xu's avatar
Herbert Xu committed
18 19 20
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/rtnetlink.h>
Herbert Xu's avatar
Herbert Xu committed
21
#include <linux/sched.h>
22
#include <linux/slab.h>
Herbert Xu's avatar
Herbert Xu committed
23 24 25 26 27
#include <linux/string.h>

#include "internal.h"

struct cryptomgr_param {
28
	struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
29 30 31 32 33 34

	struct {
		struct rtattr attr;
		struct crypto_attr_type data;
	} type;

35
	union {
Herbert Xu's avatar
Herbert Xu committed
36
		struct rtattr attr;
37 38 39 40 41 42 43 44 45 46 47
		struct {
			struct rtattr attr;
			struct crypto_attr_alg data;
		} alg;
		struct {
			struct rtattr attr;
			struct crypto_attr_u32 data;
		} nu32;
	} attrs[CRYPTO_MAX_ATTRS];

	char larval[CRYPTO_MAX_ALG_NAME];
Herbert Xu's avatar
Herbert Xu committed
48
	char template[CRYPTO_MAX_ALG_NAME];
49 50 51 52 53 54 55 56 57

	u32 otype;
	u32 omask;
};

struct crypto_test_param {
	char driver[CRYPTO_MAX_ALG_NAME];
	char alg[CRYPTO_MAX_ALG_NAME];
	u32 type;
Herbert Xu's avatar
Herbert Xu committed
58 59
};

60
static int cryptomgr_probe(void *data)
Herbert Xu's avatar
Herbert Xu committed
61
{
62
	struct cryptomgr_param *param = data;
Herbert Xu's avatar
Herbert Xu committed
63 64
	struct crypto_template *tmpl;
	struct crypto_instance *inst;
Herbert Xu's avatar
Herbert Xu committed
65
	int err;
Herbert Xu's avatar
Herbert Xu committed
66 67 68 69 70

	tmpl = crypto_lookup_template(param->template);
	if (!tmpl)
		goto err;

Herbert Xu's avatar
Herbert Xu committed
71
	do {
72 73 74 75 76
		if (tmpl->create) {
			err = tmpl->create(tmpl, param->tb);
			continue;
		}

77
		inst = tmpl->alloc(param->tb);
Herbert Xu's avatar
Herbert Xu committed
78 79 80 81 82
		if (IS_ERR(inst))
			err = PTR_ERR(inst);
		else if ((err = crypto_register_instance(tmpl, inst)))
			tmpl->free(inst);
	} while (err == -EAGAIN && !signal_pending(current));
Herbert Xu's avatar
Herbert Xu committed
83 84 85

	crypto_tmpl_put(tmpl);

Herbert Xu's avatar
Herbert Xu committed
86 87 88
	if (err)
		goto err;

Herbert Xu's avatar
Herbert Xu committed
89 90
out:
	kfree(param);
91
	module_put_and_exit(0);
Herbert Xu's avatar
Herbert Xu committed
92 93

err:
94
	crypto_larval_error(param->larval, param->otype, param->omask);
Herbert Xu's avatar
Herbert Xu committed
95 96 97 98 99
	goto out;
}

static int cryptomgr_schedule_probe(struct crypto_larval *larval)
{
100
	struct task_struct *thread;
Herbert Xu's avatar
Herbert Xu committed
101 102 103 104
	struct cryptomgr_param *param;
	const char *name = larval->alg.cra_name;
	const char *p;
	unsigned int len;
105
	int i;
Herbert Xu's avatar
Herbert Xu committed
106

107 108 109
	if (!try_module_get(THIS_MODULE))
		goto err;

110
	param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu's avatar
Herbert Xu committed
111
	if (!param)
112
		goto err_put_module;
Herbert Xu's avatar
Herbert Xu committed
113 114 115 116 117 118 119 120 121 122

	for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
		;

	len = p - name;
	if (!len || *p != '(')
		goto err_free_param;

	memcpy(param->template, name, len);

123 124 125
	i = 0;
	for (;;) {
		int notnum = 0;
Herbert Xu's avatar
Herbert Xu committed
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
		name = ++p;
		len = 0;

		for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
			notnum |= !isdigit(*p);

		if (*p == '(') {
			int recursion = 0;

			for (;;) {
				if (!*++p)
					goto err_free_param;
				if (*p == '(')
					recursion++;
				else if (*p == ')' && !recursion--)
					break;
			}

			notnum = 1;
146
			p++;
147
		}
148 149

		len = p - name;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		if (!len)
			goto err_free_param;

		if (notnum) {
			param->attrs[i].alg.attr.rta_len =
				sizeof(param->attrs[i].alg);
			param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
			memcpy(param->attrs[i].alg.data.name, name, len);
		} else {
			param->attrs[i].nu32.attr.rta_len =
				sizeof(param->attrs[i].nu32);
			param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
			param->attrs[i].nu32.data.num =
				simple_strtol(name, NULL, 0);
		}

		param->tb[i + 1] = &param->attrs[i].attr;
		i++;

169
		if (i >= CRYPTO_MAX_ATTRS)
170 171 172 173 174 175 176
			goto err_free_param;

		if (*p == ')')
			break;

		if (*p != ',')
			goto err_free_param;
177 178
	}

179
	if (!i)
Herbert Xu's avatar
Herbert Xu committed
180 181
		goto err_free_param;

182 183
	param->tb[i + 1] = NULL;

184 185
	param->type.attr.rta_len = sizeof(param->type);
	param->type.attr.rta_type = CRYPTOA_TYPE;
186 187
	param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
	param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
188
	param->tb[0] = &param->type.attr;
Herbert Xu's avatar
Herbert Xu committed
189

190 191 192
	param->otype = larval->alg.cra_flags;
	param->omask = larval->mask;

193
	memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
Herbert Xu's avatar
Herbert Xu committed
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
	if (IS_ERR(thread))
		goto err_free_param;

	return NOTIFY_STOP;

err_free_param:
	kfree(param);
err_put_module:
	module_put(THIS_MODULE);
err:
	return NOTIFY_OK;
}

static int cryptomgr_test(void *data)
{
	struct crypto_test_param *param = data;
	u32 type = param->type;
	int err = 0;

215
	if (type & CRYPTO_ALG_TESTED)
216 217
		goto skiptest;

218
	err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
219 220 221 222 223 224 225 226 227 228 229 230

skiptest:
	crypto_alg_tested(param->driver, err);

	kfree(param);
	module_put_and_exit(0);
}

static int cryptomgr_schedule_test(struct crypto_alg *alg)
{
	struct task_struct *thread;
	struct crypto_test_param *param;
231
	u32 type;
232 233 234 235 236 237 238 239 240 241

	if (!try_module_get(THIS_MODULE))
		goto err;

	param = kzalloc(sizeof(*param), GFP_KERNEL);
	if (!param)
		goto err_put_module;

	memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
	memcpy(param->alg, alg->cra_name, sizeof(param->alg));
242 243 244 245 246 247 248 249 250 251 252 253 254
	type = alg->cra_flags;

	/* This piece of crap needs to disappear into per-type test hooks. */
	if ((!((type ^ CRYPTO_ALG_TYPE_BLKCIPHER) &
	       CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV) &&
	     ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
	      CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
					  alg->cra_ablkcipher.ivsize)) ||
	    (!((type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) &&
	     alg->cra_type == &crypto_nivaead_type && alg->cra_aead.ivsize))
		type |= CRYPTO_ALG_TESTED;

	param->type = type;
255 256

	thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
257
	if (IS_ERR(thread))
258
		goto err_free_param;
Herbert Xu's avatar
Herbert Xu committed
259 260 261 262 263

	return NOTIFY_STOP;

err_free_param:
	kfree(param);
264 265
err_put_module:
	module_put(THIS_MODULE);
Herbert Xu's avatar
Herbert Xu committed
266 267 268 269 270 271 272 273 274 275
err:
	return NOTIFY_OK;
}

static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
			    void *data)
{
	switch (msg) {
	case CRYPTO_MSG_ALG_REQUEST:
		return cryptomgr_schedule_probe(data);
276 277
	case CRYPTO_MSG_ALG_REGISTER:
		return cryptomgr_schedule_test(data);
Herbert Xu's avatar
Herbert Xu committed
278 279 280 281 282 283 284 285 286 287 288
	}

	return NOTIFY_DONE;
}

static struct notifier_block cryptomgr_notifier = {
	.notifier_call = cryptomgr_notify,
};

static int __init cryptomgr_init(void)
{
289
	return crypto_register_notifier(&cryptomgr_notifier);
Herbert Xu's avatar
Herbert Xu committed
290 291 292 293 294 295 296 297
}

static void __exit cryptomgr_exit(void)
{
	int err = crypto_unregister_notifier(&cryptomgr_notifier);
	BUG_ON(err);
}

298
subsys_initcall(cryptomgr_init);
Herbert Xu's avatar
Herbert Xu committed
299 300 301 302
module_exit(cryptomgr_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Crypto Algorithm Manager");