Blame view

crypto/skcipher.c 12 KB
7a7ffe65c   Herbert Xu   crypto: skcipher ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   * Symmetric key cipher operations.
   *
   * Generic encrypt/decrypt wrapper for ciphers, handles operations across
   * multiple page boundaries by using temporary blocks.  In user context,
   * the kernel is given a chance to schedule us once per page.
   *
   * Copyright (c) 2015 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.
   *
   */
  
  #include <crypto/internal/skcipher.h>
  #include <linux/bug.h>
4e6c3df4d   Herbert Xu   crypto: skcipher ...
19
  #include <linux/cryptouser.h>
7a7ffe65c   Herbert Xu   crypto: skcipher ...
20
  #include <linux/module.h>
4e6c3df4d   Herbert Xu   crypto: skcipher ...
21
22
23
  #include <linux/rtnetlink.h>
  #include <linux/seq_file.h>
  #include <net/netlink.h>
7a7ffe65c   Herbert Xu   crypto: skcipher ...
24
25
26
27
28
29
30
  
  #include "internal.h"
  
  static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
  {
  	if (alg->cra_type == &crypto_blkcipher_type)
  		return sizeof(struct crypto_blkcipher *);
4e6c3df4d   Herbert Xu   crypto: skcipher ...
31
32
33
  	if (alg->cra_type == &crypto_ablkcipher_type ||
  	    alg->cra_type == &crypto_givcipher_type)
  		return sizeof(struct crypto_ablkcipher *);
7a7ffe65c   Herbert Xu   crypto: skcipher ...
34

4e6c3df4d   Herbert Xu   crypto: skcipher ...
35
  	return crypto_alg_extsize(alg);
7a7ffe65c   Herbert Xu   crypto: skcipher ...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  }
  
  static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
  				     const u8 *key, unsigned int keylen)
  {
  	struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  	struct crypto_blkcipher *blkcipher = *ctx;
  	int err;
  
  	crypto_blkcipher_clear_flags(blkcipher, ~0);
  	crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
  					      CRYPTO_TFM_REQ_MASK);
  	err = crypto_blkcipher_setkey(blkcipher, key, keylen);
  	crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
  				       CRYPTO_TFM_RES_MASK);
  
  	return err;
  }
  
  static int skcipher_crypt_blkcipher(struct skcipher_request *req,
  				    int (*crypt)(struct blkcipher_desc *,
  						 struct scatterlist *,
  						 struct scatterlist *,
  						 unsigned int))
  {
  	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  	struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
  	struct blkcipher_desc desc = {
  		.tfm = *ctx,
  		.info = req->iv,
  		.flags = req->base.flags,
  	};
  
  
  	return crypt(&desc, req->dst, req->src, req->cryptlen);
  }
  
  static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
  {
  	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  	struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  
  	return skcipher_crypt_blkcipher(req, alg->encrypt);
  }
  
  static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
  {
  	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  	struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
  
  	return skcipher_crypt_blkcipher(req, alg->decrypt);
  }
  
  static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
  {
  	struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  
  	crypto_free_blkcipher(*ctx);
  }
ecdd6bed2   Geliang Tang   crypto: skcipher ...
97
  static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
7a7ffe65c   Herbert Xu   crypto: skcipher ...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  {
  	struct crypto_alg *calg = tfm->__crt_alg;
  	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  	struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
  	struct crypto_blkcipher *blkcipher;
  	struct crypto_tfm *btfm;
  
  	if (!crypto_mod_get(calg))
  		return -EAGAIN;
  
  	btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
  					CRYPTO_ALG_TYPE_MASK);
  	if (IS_ERR(btfm)) {
  		crypto_mod_put(calg);
  		return PTR_ERR(btfm);
  	}
  
  	blkcipher = __crypto_blkcipher_cast(btfm);
  	*ctx = blkcipher;
  	tfm->exit = crypto_exit_skcipher_ops_blkcipher;
  
  	skcipher->setkey = skcipher_setkey_blkcipher;
  	skcipher->encrypt = skcipher_encrypt_blkcipher;
  	skcipher->decrypt = skcipher_decrypt_blkcipher;
  
  	skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
973fb3fb5   Herbert Xu   crypto: skcipher ...
124
  	skcipher->keysize = calg->cra_blkcipher.max_keysize;
7a7ffe65c   Herbert Xu   crypto: skcipher ...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  
  	return 0;
  }
  
  static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
  				      const u8 *key, unsigned int keylen)
  {
  	struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  	struct crypto_ablkcipher *ablkcipher = *ctx;
  	int err;
  
  	crypto_ablkcipher_clear_flags(ablkcipher, ~0);
  	crypto_ablkcipher_set_flags(ablkcipher,
  				    crypto_skcipher_get_flags(tfm) &
  				    CRYPTO_TFM_REQ_MASK);
  	err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
  	crypto_skcipher_set_flags(tfm,
  				  crypto_ablkcipher_get_flags(ablkcipher) &
  				  CRYPTO_TFM_RES_MASK);
  
  	return err;
  }
  
  static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
  				     int (*crypt)(struct ablkcipher_request *))
  {
  	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  	struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
  	struct ablkcipher_request *subreq = skcipher_request_ctx(req);
  
  	ablkcipher_request_set_tfm(subreq, *ctx);
  	ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
  					req->base.complete, req->base.data);
  	ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  				     req->iv);
  
  	return crypt(subreq);
  }
  
  static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
  {
  	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  
  	return skcipher_crypt_ablkcipher(req, alg->encrypt);
  }
  
  static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
  {
  	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
  	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
  	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  
  	return skcipher_crypt_ablkcipher(req, alg->decrypt);
  }
  
  static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
  {
  	struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  
  	crypto_free_ablkcipher(*ctx);
  }
ecdd6bed2   Geliang Tang   crypto: skcipher ...
188
  static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
7a7ffe65c   Herbert Xu   crypto: skcipher ...
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
  {
  	struct crypto_alg *calg = tfm->__crt_alg;
  	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  	struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
  	struct crypto_ablkcipher *ablkcipher;
  	struct crypto_tfm *abtfm;
  
  	if (!crypto_mod_get(calg))
  		return -EAGAIN;
  
  	abtfm = __crypto_alloc_tfm(calg, 0, 0);
  	if (IS_ERR(abtfm)) {
  		crypto_mod_put(calg);
  		return PTR_ERR(abtfm);
  	}
  
  	ablkcipher = __crypto_ablkcipher_cast(abtfm);
  	*ctx = ablkcipher;
  	tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
  
  	skcipher->setkey = skcipher_setkey_ablkcipher;
  	skcipher->encrypt = skcipher_encrypt_ablkcipher;
  	skcipher->decrypt = skcipher_decrypt_ablkcipher;
  
  	skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  	skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
  			    sizeof(struct ablkcipher_request);
973fb3fb5   Herbert Xu   crypto: skcipher ...
216
  	skcipher->keysize = calg->cra_ablkcipher.max_keysize;
7a7ffe65c   Herbert Xu   crypto: skcipher ...
217
218
219
  
  	return 0;
  }
4e6c3df4d   Herbert Xu   crypto: skcipher ...
220
221
222
223
224
225
226
  static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
  {
  	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  	struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
  
  	alg->exit(skcipher);
  }
7a7ffe65c   Herbert Xu   crypto: skcipher ...
227
228
  static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
  {
4e6c3df4d   Herbert Xu   crypto: skcipher ...
229
230
  	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
  	struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
7a7ffe65c   Herbert Xu   crypto: skcipher ...
231
232
  	if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
  		return crypto_init_skcipher_ops_blkcipher(tfm);
4e6c3df4d   Herbert Xu   crypto: skcipher ...
233
234
235
236
237
238
239
240
241
242
243
244
  	if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
  	    tfm->__crt_alg->cra_type == &crypto_givcipher_type)
  		return crypto_init_skcipher_ops_ablkcipher(tfm);
  
  	skcipher->setkey = alg->setkey;
  	skcipher->encrypt = alg->encrypt;
  	skcipher->decrypt = alg->decrypt;
  	skcipher->ivsize = alg->ivsize;
  	skcipher->keysize = alg->max_keysize;
  
  	if (alg->exit)
  		skcipher->base.exit = crypto_skcipher_exit_tfm;
7a7ffe65c   Herbert Xu   crypto: skcipher ...
245

4e6c3df4d   Herbert Xu   crypto: skcipher ...
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
  	if (alg->init)
  		return alg->init(skcipher);
  
  	return 0;
  }
  
  static void crypto_skcipher_free_instance(struct crypto_instance *inst)
  {
  	struct skcipher_instance *skcipher =
  		container_of(inst, struct skcipher_instance, s.base);
  
  	skcipher->free(skcipher);
  }
  
  static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  	__attribute__ ((unused));
  static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
  {
  	struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  						     base);
  
  	seq_printf(m, "type         : skcipher
  ");
  	seq_printf(m, "async        : %s
  ",
  		   alg->cra_flags & CRYPTO_ALG_ASYNC ?  "yes" : "no");
  	seq_printf(m, "blocksize    : %u
  ", alg->cra_blocksize);
  	seq_printf(m, "min keysize  : %u
  ", skcipher->min_keysize);
  	seq_printf(m, "max keysize  : %u
  ", skcipher->max_keysize);
  	seq_printf(m, "ivsize       : %u
  ", skcipher->ivsize);
  	seq_printf(m, "chunksize    : %u
  ", skcipher->chunksize);
7a7ffe65c   Herbert Xu   crypto: skcipher ...
282
  }
4e6c3df4d   Herbert Xu   crypto: skcipher ...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  #ifdef CONFIG_NET
  static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  {
  	struct crypto_report_blkcipher rblkcipher;
  	struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
  						     base);
  
  	strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
  	strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
  
  	rblkcipher.blocksize = alg->cra_blocksize;
  	rblkcipher.min_keysize = skcipher->min_keysize;
  	rblkcipher.max_keysize = skcipher->max_keysize;
  	rblkcipher.ivsize = skcipher->ivsize;
  
  	if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
  		    sizeof(struct crypto_report_blkcipher), &rblkcipher))
  		goto nla_put_failure;
  	return 0;
  
  nla_put_failure:
  	return -EMSGSIZE;
  }
  #else
  static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  {
  	return -ENOSYS;
  }
  #endif
7a7ffe65c   Herbert Xu   crypto: skcipher ...
312
313
314
  static const struct crypto_type crypto_skcipher_type2 = {
  	.extsize = crypto_skcipher_extsize,
  	.init_tfm = crypto_skcipher_init_tfm,
4e6c3df4d   Herbert Xu   crypto: skcipher ...
315
316
317
318
319
  	.free = crypto_skcipher_free_instance,
  #ifdef CONFIG_PROC_FS
  	.show = crypto_skcipher_show,
  #endif
  	.report = crypto_skcipher_report,
7a7ffe65c   Herbert Xu   crypto: skcipher ...
320
321
  	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
  	.maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
4e6c3df4d   Herbert Xu   crypto: skcipher ...
322
  	.type = CRYPTO_ALG_TYPE_SKCIPHER,
7a7ffe65c   Herbert Xu   crypto: skcipher ...
323
324
  	.tfmsize = offsetof(struct crypto_skcipher, base),
  };
3a01d0ee2   Herbert Xu   crypto: skcipher ...
325
  int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
4e6c3df4d   Herbert Xu   crypto: skcipher ...
326
327
328
329
330
  			  const char *name, u32 type, u32 mask)
  {
  	spawn->base.frontend = &crypto_skcipher_type2;
  	return crypto_grab_spawn(&spawn->base, name, type, mask);
  }
3a01d0ee2   Herbert Xu   crypto: skcipher ...
331
  EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
4e6c3df4d   Herbert Xu   crypto: skcipher ...
332

7a7ffe65c   Herbert Xu   crypto: skcipher ...
333
334
335
336
337
338
  struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
  					      u32 type, u32 mask)
  {
  	return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
  }
  EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
4e6c3df4d   Herbert Xu   crypto: skcipher ...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
  {
  	return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
  				   type, mask);
  }
  EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
  
  static int skcipher_prepare_alg(struct skcipher_alg *alg)
  {
  	struct crypto_alg *base = &alg->base;
  
  	if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
  		return -EINVAL;
  
  	if (!alg->chunksize)
  		alg->chunksize = base->cra_blocksize;
  
  	base->cra_type = &crypto_skcipher_type2;
  	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  	base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
  
  	return 0;
  }
  
  int crypto_register_skcipher(struct skcipher_alg *alg)
  {
  	struct crypto_alg *base = &alg->base;
  	int err;
  
  	err = skcipher_prepare_alg(alg);
  	if (err)
  		return err;
  
  	return crypto_register_alg(base);
  }
  EXPORT_SYMBOL_GPL(crypto_register_skcipher);
  
  void crypto_unregister_skcipher(struct skcipher_alg *alg)
  {
  	crypto_unregister_alg(&alg->base);
  }
  EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
  
  int crypto_register_skciphers(struct skcipher_alg *algs, int count)
  {
  	int i, ret;
  
  	for (i = 0; i < count; i++) {
  		ret = crypto_register_skcipher(&algs[i]);
  		if (ret)
  			goto err;
  	}
  
  	return 0;
  
  err:
  	for (--i; i >= 0; --i)
  		crypto_unregister_skcipher(&algs[i]);
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(crypto_register_skciphers);
  
  void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
  {
  	int i;
  
  	for (i = count - 1; i >= 0; --i)
  		crypto_unregister_skcipher(&algs[i]);
  }
  EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
  
  int skcipher_register_instance(struct crypto_template *tmpl,
  			   struct skcipher_instance *inst)
  {
  	int err;
  
  	err = skcipher_prepare_alg(&inst->alg);
  	if (err)
  		return err;
  
  	return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
  }
  EXPORT_SYMBOL_GPL(skcipher_register_instance);
7a7ffe65c   Herbert Xu   crypto: skcipher ...
423
424
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Symmetric key cipher type");