Blame view

crypto/ablkcipher.c 8.75 KB
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  /*
   * Asynchronous block chaining cipher operations.
   * 
   * This is the asynchronous version of blkcipher.c indicating completion
   * via a callback.
   *
   * 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.
   *
   */
378f4f51f   Herbert Xu   [CRYPTO] skcipher...
15
16
  #include <crypto/internal/skcipher.h>
  #include <linux/err.h>
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
17
  #include <linux/init.h>
791b4d5f7   Herbert Xu   [CRYPTO] api: Add...
18
  #include <linux/kernel.h>
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
19
  #include <linux/module.h>
b9c55aa47   Herbert Xu   [CRYPTO] skcipher...
20
21
  #include <linux/rtnetlink.h>
  #include <linux/sched.h>
791b4d5f7   Herbert Xu   [CRYPTO] api: Add...
22
  #include <linux/slab.h>
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
23
  #include <linux/seq_file.h>
378f4f51f   Herbert Xu   [CRYPTO] skcipher...
24
  #include "internal.h"
791b4d5f7   Herbert Xu   [CRYPTO] api: Add...
25
26
  static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  			    unsigned int keylen)
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  {
  	struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  	unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  	int ret;
  	u8 *buffer, *alignbuffer;
  	unsigned long absize;
  
  	absize = keylen + alignmask;
  	buffer = kmalloc(absize, GFP_ATOMIC);
  	if (!buffer)
  		return -ENOMEM;
  
  	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  	memcpy(alignbuffer, key, keylen);
  	ret = cipher->setkey(tfm, alignbuffer, keylen);
068171767   Sebastian Siewior   [CRYPTO] api: fix...
42
  	memset(alignbuffer, 0, keylen);
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
43
44
45
  	kfree(buffer);
  	return ret;
  }
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
46
47
48
49
  static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  		  unsigned int keylen)
  {
  	struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
50
  	unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
51
52
53
54
55
  
  	if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  		crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  		return -EINVAL;
  	}
ca7c39385   Sebastian Siewior   [CRYPTO] api: Han...
56
57
  	if ((unsigned long)key & alignmask)
  		return setkey_unaligned(tfm, key, keylen);
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
58
59
60
61
62
63
64
65
  	return cipher->setkey(tfm, key, keylen);
  }
  
  static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  					      u32 mask)
  {
  	return alg->cra_ctxsize;
  }
b9c55aa47   Herbert Xu   [CRYPTO] skcipher...
66
67
68
69
70
71
72
73
74
  int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
  {
  	return crypto_ablkcipher_encrypt(&req->creq);
  }
  
  int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
  {
  	return crypto_ablkcipher_decrypt(&req->creq);
  }
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
75
76
77
78
79
80
81
82
83
84
85
86
  static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  				      u32 mask)
  {
  	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  	struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  
  	if (alg->ivsize > PAGE_SIZE / 8)
  		return -EINVAL;
  
  	crt->setkey = setkey;
  	crt->encrypt = alg->encrypt;
  	crt->decrypt = alg->decrypt;
b9c55aa47   Herbert Xu   [CRYPTO] skcipher...
87
88
89
90
  	if (!alg->ivsize) {
  		crt->givencrypt = skcipher_null_givencrypt;
  		crt->givdecrypt = skcipher_null_givdecrypt;
  	}
ecfc43292   Herbert Xu   [CRYPTO] skcipher...
91
  	crt->base = __crypto_ablkcipher_cast(tfm);
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
92
93
94
95
96
97
98
99
100
101
102
103
104
  	crt->ivsize = alg->ivsize;
  
  	return 0;
  }
  
  static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  	__attribute__ ((unused));
  static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  {
  	struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  
  	seq_printf(m, "type         : ablkcipher
  ");
189ed66e9   Herbert Xu   [CRYPTO] api: Sho...
105
106
107
  	seq_printf(m, "async        : %s
  ", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  					     "yes" : "no");
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
108
109
110
111
112
113
114
115
  	seq_printf(m, "blocksize    : %u
  ", alg->cra_blocksize);
  	seq_printf(m, "min keysize  : %u
  ", ablkcipher->min_keysize);
  	seq_printf(m, "max keysize  : %u
  ", ablkcipher->max_keysize);
  	seq_printf(m, "ivsize       : %u
  ", ablkcipher->ivsize);
23508e11a   Herbert Xu   [CRYPTO] skcipher...
116
117
  	seq_printf(m, "geniv        : %s
  ", ablkcipher->geniv ?: "<default>");
b5b7f0886   Herbert Xu   [CRYPTO] api: Add...
118
119
120
121
122
123
124
125
126
127
  }
  
  const struct crypto_type crypto_ablkcipher_type = {
  	.ctxsize = crypto_ablkcipher_ctxsize,
  	.init = crypto_init_ablkcipher_ops,
  #ifdef CONFIG_PROC_FS
  	.show = crypto_ablkcipher_show,
  #endif
  };
  EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
61da88e2b   Herbert Xu   [CRYPTO] skcipher...
128
129
130
131
132
133
134
135
136
137
138
139
140
  static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  {
  	return -ENOSYS;
  }
  
  static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  				      u32 mask)
  {
  	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  	struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  
  	if (alg->ivsize > PAGE_SIZE / 8)
  		return -EINVAL;
ecfc43292   Herbert Xu   [CRYPTO] skcipher...
141
142
  	crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  		      alg->setkey : setkey;
61da88e2b   Herbert Xu   [CRYPTO] skcipher...
143
144
145
146
  	crt->encrypt = alg->encrypt;
  	crt->decrypt = alg->decrypt;
  	crt->givencrypt = alg->givencrypt;
  	crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
ecfc43292   Herbert Xu   [CRYPTO] skcipher...
147
  	crt->base = __crypto_ablkcipher_cast(tfm);
61da88e2b   Herbert Xu   [CRYPTO] skcipher...
148
149
150
151
152
153
154
155
156
157
158
159
160
  	crt->ivsize = alg->ivsize;
  
  	return 0;
  }
  
  static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  	__attribute__ ((unused));
  static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  {
  	struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  
  	seq_printf(m, "type         : givcipher
  ");
189ed66e9   Herbert Xu   [CRYPTO] api: Sho...
161
162
163
  	seq_printf(m, "async        : %s
  ", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  					     "yes" : "no");
61da88e2b   Herbert Xu   [CRYPTO] skcipher...
164
165
166
167
168
169
170
171
  	seq_printf(m, "blocksize    : %u
  ", alg->cra_blocksize);
  	seq_printf(m, "min keysize  : %u
  ", ablkcipher->min_keysize);
  	seq_printf(m, "max keysize  : %u
  ", ablkcipher->max_keysize);
  	seq_printf(m, "ivsize       : %u
  ", ablkcipher->ivsize);
23508e11a   Herbert Xu   [CRYPTO] skcipher...
172
173
  	seq_printf(m, "geniv        : %s
  ", ablkcipher->geniv ?: "<built-in>");
61da88e2b   Herbert Xu   [CRYPTO] skcipher...
174
175
176
177
178
179
180
181
182
183
  }
  
  const struct crypto_type crypto_givcipher_type = {
  	.ctxsize = crypto_ablkcipher_ctxsize,
  	.init = crypto_init_givcipher_ops,
  #ifdef CONFIG_PROC_FS
  	.show = crypto_givcipher_show,
  #endif
  };
  EXPORT_SYMBOL_GPL(crypto_givcipher_type);
ecfc43292   Herbert Xu   [CRYPTO] skcipher...
184
185
186
187
  const char *crypto_default_geniv(const struct crypto_alg *alg)
  {
  	return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
  }
b9c55aa47   Herbert Xu   [CRYPTO] skcipher...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  {
  	struct rtattr *tb[3];
  	struct {
  		struct rtattr attr;
  		struct crypto_attr_type data;
  	} ptype;
  	struct {
  		struct rtattr attr;
  		struct crypto_attr_alg data;
  	} palg;
  	struct crypto_template *tmpl;
  	struct crypto_instance *inst;
  	struct crypto_alg *larval;
  	const char *geniv;
  	int err;
  
  	larval = crypto_larval_lookup(alg->cra_driver_name,
  				      CRYPTO_ALG_TYPE_GIVCIPHER,
  				      CRYPTO_ALG_TYPE_MASK);
  	err = PTR_ERR(larval);
  	if (IS_ERR(larval))
  		goto out;
  
  	err = -EAGAIN;
  	if (!crypto_is_larval(larval))
  		goto drop_larval;
  
  	ptype.attr.rta_len = sizeof(ptype);
  	ptype.attr.rta_type = CRYPTOA_TYPE;
  	ptype.data.type = type | CRYPTO_ALG_GENIV;
  	/* GENIV tells the template that we're making a default geniv. */
  	ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  	tb[0] = &ptype.attr;
  
  	palg.attr.rta_len = sizeof(palg);
  	palg.attr.rta_type = CRYPTOA_ALG;
  	/* Must use the exact name to locate ourselves. */
  	memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  	tb[1] = &palg.attr;
  
  	tb[2] = NULL;
  
  	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  	    CRYPTO_ALG_TYPE_BLKCIPHER)
  		geniv = alg->cra_blkcipher.geniv;
  	else
  		geniv = alg->cra_ablkcipher.geniv;
  
  	if (!geniv)
  		geniv = crypto_default_geniv(alg);
  
  	tmpl = crypto_lookup_template(geniv);
  	err = -ENOENT;
  	if (!tmpl)
  		goto kill_larval;
  
  	inst = tmpl->alloc(tb);
  	err = PTR_ERR(inst);
  	if (IS_ERR(inst))
  		goto put_tmpl;
  
  	if ((err = crypto_register_instance(tmpl, inst))) {
  		tmpl->free(inst);
  		goto put_tmpl;
  	}
  
  	/* Redo the lookup to use the instance we just registered. */
  	err = -EAGAIN;
  
  put_tmpl:
  	crypto_tmpl_put(tmpl);
  kill_larval:
  	crypto_larval_kill(larval);
  drop_larval:
  	crypto_mod_put(larval);
  out:
  	crypto_mod_put(alg);
  	return err;
  }
  
  static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
  						 u32 mask)
  {
  	struct crypto_alg *alg;
  
  	alg = crypto_alg_mod_lookup(name, type, mask);
  	if (IS_ERR(alg))
  		return alg;
  
  	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  	    CRYPTO_ALG_TYPE_GIVCIPHER)
  		return alg;
  
  	if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  	      CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  					  alg->cra_ablkcipher.ivsize))
  		return alg;
  
  	return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  }
378f4f51f   Herbert Xu   [CRYPTO] skcipher...
289
290
291
292
293
294
295
296
  int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  			 u32 type, u32 mask)
  {
  	struct crypto_alg *alg;
  	int err;
  
  	type = crypto_skcipher_type(type);
  	mask = crypto_skcipher_mask(mask);
b9c55aa47   Herbert Xu   [CRYPTO] skcipher...
297
  	alg = crypto_lookup_skcipher(name, type, mask);
378f4f51f   Herbert Xu   [CRYPTO] skcipher...
298
299
300
301
302
303
304
305
  	if (IS_ERR(alg))
  		return PTR_ERR(alg);
  
  	err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  	crypto_mod_put(alg);
  	return err;
  }
  EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
b9c55aa47   Herbert Xu   [CRYPTO] skcipher...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  						  u32 type, u32 mask)
  {
  	struct crypto_tfm *tfm;
  	int err;
  
  	type = crypto_skcipher_type(type);
  	mask = crypto_skcipher_mask(mask);
  
  	for (;;) {
  		struct crypto_alg *alg;
  
  		alg = crypto_lookup_skcipher(alg_name, type, mask);
  		if (IS_ERR(alg)) {
  			err = PTR_ERR(alg);
  			goto err;
  		}
  
  		tfm = __crypto_alloc_tfm(alg, type, mask);
  		if (!IS_ERR(tfm))
  			return __crypto_ablkcipher_cast(tfm);
  
  		crypto_mod_put(alg);
  		err = PTR_ERR(tfm);
  
  err:
  		if (err != -EAGAIN)
  			break;
  		if (signal_pending(current)) {
  			err = -EINTR;
  			break;
  		}
  	}
  
  	return ERR_PTR(err);
  }
  EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);