Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / net / xfrm / xfrm_algo.c
blob6249a9405bb83bc1b579e178fd4632d52cccff0a
1 /*
2 * xfrm algorithm interface
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
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.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pfkeyv2.h>
15 #include <linux/crypto.h>
16 #include <net/xfrm.h>
17 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
18 #include <net/ah.h>
19 #endif
20 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
21 #include <net/esp.h>
22 #endif
23 #include <asm/scatterlist.h>
26 * Algorithms supported by IPsec. These entries contain properties which
27 * are used in key negotiation and xfrm processing, and are used to verify
28 * that instantiated crypto transforms have correct parameters for IPsec
29 * purposes.
31 static struct xfrm_algo_desc aalg_list[] = {
33 .name = "hmac(digest_null)",
34 .compat = "digest_null",
36 .uinfo = {
37 .auth = {
38 .icv_truncbits = 0,
39 .icv_fullbits = 0,
43 .desc = {
44 .sadb_alg_id = SADB_X_AALG_NULL,
45 .sadb_alg_ivlen = 0,
46 .sadb_alg_minbits = 0,
47 .sadb_alg_maxbits = 0
51 .name = "hmac(md5)",
52 .compat = "md5",
54 .uinfo = {
55 .auth = {
56 .icv_truncbits = 96,
57 .icv_fullbits = 128,
61 .desc = {
62 .sadb_alg_id = SADB_AALG_MD5HMAC,
63 .sadb_alg_ivlen = 0,
64 .sadb_alg_minbits = 128,
65 .sadb_alg_maxbits = 128
69 .name = "hmac(sha1)",
70 .compat = "sha1",
72 .uinfo = {
73 .auth = {
74 .icv_truncbits = 96,
75 .icv_fullbits = 160,
79 .desc = {
80 .sadb_alg_id = SADB_AALG_SHA1HMAC,
81 .sadb_alg_ivlen = 0,
82 .sadb_alg_minbits = 160,
83 .sadb_alg_maxbits = 160
87 .name = "hmac(sha256)",
88 .compat = "sha256",
90 .uinfo = {
91 .auth = {
92 .icv_truncbits = 96,
93 .icv_fullbits = 256,
97 .desc = {
98 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
99 .sadb_alg_ivlen = 0,
100 .sadb_alg_minbits = 256,
101 .sadb_alg_maxbits = 256
105 .name = "hmac(ripemd160)",
106 .compat = "ripemd160",
108 .uinfo = {
109 .auth = {
110 .icv_truncbits = 96,
111 .icv_fullbits = 160,
115 .desc = {
116 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
117 .sadb_alg_ivlen = 0,
118 .sadb_alg_minbits = 160,
119 .sadb_alg_maxbits = 160
123 .name = "xcbc(aes)",
125 .uinfo = {
126 .auth = {
127 .icv_truncbits = 96,
128 .icv_fullbits = 128,
132 .desc = {
133 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
134 .sadb_alg_ivlen = 0,
135 .sadb_alg_minbits = 128,
136 .sadb_alg_maxbits = 128
141 static struct xfrm_algo_desc ealg_list[] = {
143 .name = "ecb(cipher_null)",
144 .compat = "cipher_null",
146 .uinfo = {
147 .encr = {
148 .blockbits = 8,
149 .defkeybits = 0,
153 .desc = {
154 .sadb_alg_id = SADB_EALG_NULL,
155 .sadb_alg_ivlen = 0,
156 .sadb_alg_minbits = 0,
157 .sadb_alg_maxbits = 0
161 .name = "cbc(des)",
162 .compat = "des",
164 .uinfo = {
165 .encr = {
166 .blockbits = 64,
167 .defkeybits = 64,
171 .desc = {
172 .sadb_alg_id = SADB_EALG_DESCBC,
173 .sadb_alg_ivlen = 8,
174 .sadb_alg_minbits = 64,
175 .sadb_alg_maxbits = 64
179 .name = "cbc(des3_ede)",
180 .compat = "des3_ede",
182 .uinfo = {
183 .encr = {
184 .blockbits = 64,
185 .defkeybits = 192,
189 .desc = {
190 .sadb_alg_id = SADB_EALG_3DESCBC,
191 .sadb_alg_ivlen = 8,
192 .sadb_alg_minbits = 192,
193 .sadb_alg_maxbits = 192
197 .name = "cbc(cast128)",
198 .compat = "cast128",
200 .uinfo = {
201 .encr = {
202 .blockbits = 64,
203 .defkeybits = 128,
207 .desc = {
208 .sadb_alg_id = SADB_X_EALG_CASTCBC,
209 .sadb_alg_ivlen = 8,
210 .sadb_alg_minbits = 40,
211 .sadb_alg_maxbits = 128
215 .name = "cbc(blowfish)",
216 .compat = "blowfish",
218 .uinfo = {
219 .encr = {
220 .blockbits = 64,
221 .defkeybits = 128,
225 .desc = {
226 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
227 .sadb_alg_ivlen = 8,
228 .sadb_alg_minbits = 40,
229 .sadb_alg_maxbits = 448
233 .name = "cbc(aes)",
234 .compat = "aes",
236 .uinfo = {
237 .encr = {
238 .blockbits = 128,
239 .defkeybits = 128,
243 .desc = {
244 .sadb_alg_id = SADB_X_EALG_AESCBC,
245 .sadb_alg_ivlen = 8,
246 .sadb_alg_minbits = 128,
247 .sadb_alg_maxbits = 256
251 .name = "cbc(serpent)",
252 .compat = "serpent",
254 .uinfo = {
255 .encr = {
256 .blockbits = 128,
257 .defkeybits = 128,
261 .desc = {
262 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
263 .sadb_alg_ivlen = 8,
264 .sadb_alg_minbits = 128,
265 .sadb_alg_maxbits = 256,
269 .name = "cbc(camellia)",
271 .uinfo = {
272 .encr = {
273 .blockbits = 128,
274 .defkeybits = 128,
278 .desc = {
279 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
280 .sadb_alg_ivlen = 8,
281 .sadb_alg_minbits = 128,
282 .sadb_alg_maxbits = 256
286 .name = "cbc(twofish)",
287 .compat = "twofish",
289 .uinfo = {
290 .encr = {
291 .blockbits = 128,
292 .defkeybits = 128,
296 .desc = {
297 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
298 .sadb_alg_ivlen = 8,
299 .sadb_alg_minbits = 128,
300 .sadb_alg_maxbits = 256
305 static struct xfrm_algo_desc calg_list[] = {
307 .name = "deflate",
308 .uinfo = {
309 .comp = {
310 .threshold = 90,
313 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
316 .name = "lzs",
317 .uinfo = {
318 .comp = {
319 .threshold = 90,
322 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
325 .name = "lzjh",
326 .uinfo = {
327 .comp = {
328 .threshold = 50,
331 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
335 static inline int aalg_entries(void)
337 return ARRAY_SIZE(aalg_list);
340 static inline int ealg_entries(void)
342 return ARRAY_SIZE(ealg_list);
345 static inline int calg_entries(void)
347 return ARRAY_SIZE(calg_list);
350 /* Todo: generic iterators */
351 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
353 int i;
355 for (i = 0; i < aalg_entries(); i++) {
356 if (aalg_list[i].desc.sadb_alg_id == alg_id) {
357 if (aalg_list[i].available)
358 return &aalg_list[i];
359 else
360 break;
363 return NULL;
365 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
367 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
369 int i;
371 for (i = 0; i < ealg_entries(); i++) {
372 if (ealg_list[i].desc.sadb_alg_id == alg_id) {
373 if (ealg_list[i].available)
374 return &ealg_list[i];
375 else
376 break;
379 return NULL;
381 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
383 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
385 int i;
387 for (i = 0; i < calg_entries(); i++) {
388 if (calg_list[i].desc.sadb_alg_id == alg_id) {
389 if (calg_list[i].available)
390 return &calg_list[i];
391 else
392 break;
395 return NULL;
397 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
399 static struct xfrm_algo_desc *xfrm_get_byname(struct xfrm_algo_desc *list,
400 int entries, u32 type, u32 mask,
401 char *name, int probe)
403 int i, status;
405 if (!name)
406 return NULL;
408 for (i = 0; i < entries; i++) {
409 if (strcmp(name, list[i].name) &&
410 (!list[i].compat || strcmp(name, list[i].compat)))
411 continue;
413 if (list[i].available)
414 return &list[i];
416 if (!probe)
417 break;
419 status = crypto_has_alg(list[i].name, type,
420 mask | CRYPTO_ALG_ASYNC);
421 if (!status)
422 break;
424 list[i].available = status;
425 return &list[i];
427 return NULL;
430 struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
432 return xfrm_get_byname(aalg_list, aalg_entries(),
433 CRYPTO_ALG_TYPE_HASH, CRYPTO_ALG_TYPE_HASH_MASK,
434 name, probe);
436 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
438 struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
440 return xfrm_get_byname(ealg_list, ealg_entries(),
441 CRYPTO_ALG_TYPE_BLKCIPHER, CRYPTO_ALG_TYPE_MASK,
442 name, probe);
444 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
446 struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
448 return xfrm_get_byname(calg_list, calg_entries(),
449 CRYPTO_ALG_TYPE_COMPRESS, CRYPTO_ALG_TYPE_MASK,
450 name, probe);
452 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
454 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
456 if (idx >= aalg_entries())
457 return NULL;
459 return &aalg_list[idx];
461 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
463 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
465 if (idx >= ealg_entries())
466 return NULL;
468 return &ealg_list[idx];
470 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
473 * Probe for the availability of crypto algorithms, and set the available
474 * flag for any algorithms found on the system. This is typically called by
475 * pfkey during userspace SA add, update or register.
477 void xfrm_probe_algs(void)
479 #ifdef CONFIG_CRYPTO
480 int i, status;
482 BUG_ON(in_softirq());
484 for (i = 0; i < aalg_entries(); i++) {
485 status = crypto_has_hash(aalg_list[i].name, 0,
486 CRYPTO_ALG_ASYNC);
487 if (aalg_list[i].available != status)
488 aalg_list[i].available = status;
491 for (i = 0; i < ealg_entries(); i++) {
492 status = crypto_has_blkcipher(ealg_list[i].name, 0,
493 CRYPTO_ALG_ASYNC);
494 if (ealg_list[i].available != status)
495 ealg_list[i].available = status;
498 for (i = 0; i < calg_entries(); i++) {
499 status = crypto_has_comp(calg_list[i].name, 0,
500 CRYPTO_ALG_ASYNC);
501 if (calg_list[i].available != status)
502 calg_list[i].available = status;
504 #endif
506 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
508 int xfrm_count_auth_supported(void)
510 int i, n;
512 for (i = 0, n = 0; i < aalg_entries(); i++)
513 if (aalg_list[i].available)
514 n++;
515 return n;
517 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
519 int xfrm_count_enc_supported(void)
521 int i, n;
523 for (i = 0, n = 0; i < ealg_entries(); i++)
524 if (ealg_list[i].available)
525 n++;
526 return n;
528 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
530 /* Move to common area: it is shared with AH. */
532 int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc,
533 int offset, int len, icv_update_fn_t icv_update)
535 int start = skb_headlen(skb);
536 int i, copy = start - offset;
537 int err;
538 struct scatterlist sg;
540 /* Checksum header. */
541 if (copy > 0) {
542 if (copy > len)
543 copy = len;
545 sg.page = virt_to_page(skb->data + offset);
546 sg.offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
547 sg.length = copy;
549 err = icv_update(desc, &sg, copy);
550 if (unlikely(err))
551 return err;
553 if ((len -= copy) == 0)
554 return 0;
555 offset += copy;
558 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
559 int end;
561 BUG_TRAP(start <= offset + len);
563 end = start + skb_shinfo(skb)->frags[i].size;
564 if ((copy = end - offset) > 0) {
565 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
567 if (copy > len)
568 copy = len;
570 sg.page = frag->page;
571 sg.offset = frag->page_offset + offset-start;
572 sg.length = copy;
574 err = icv_update(desc, &sg, copy);
575 if (unlikely(err))
576 return err;
578 if (!(len -= copy))
579 return 0;
580 offset += copy;
582 start = end;
585 if (skb_shinfo(skb)->frag_list) {
586 struct sk_buff *list = skb_shinfo(skb)->frag_list;
588 for (; list; list = list->next) {
589 int end;
591 BUG_TRAP(start <= offset + len);
593 end = start + list->len;
594 if ((copy = end - offset) > 0) {
595 if (copy > len)
596 copy = len;
597 err = skb_icv_walk(list, desc, offset-start,
598 copy, icv_update);
599 if (unlikely(err))
600 return err;
601 if ((len -= copy) == 0)
602 return 0;
603 offset += copy;
605 start = end;
608 BUG_ON(len);
609 return 0;
611 EXPORT_SYMBOL_GPL(skb_icv_walk);
613 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
615 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
617 if (tail != skb) {
618 skb->data_len += len;
619 skb->len += len;
621 return skb_put(tail, len);
623 EXPORT_SYMBOL_GPL(pskb_put);
624 #endif