GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / net / xfrm / xfrm_algo.c
blob065e9f855e52beb27b47ead4a726262e2f8ae4bd
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 <linux/scatterlist.h>
17 #include <net/xfrm.h>
18 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || \
19 defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
20 #include <net/ah.h>
21 #endif
22 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || \
23 defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
24 #include <net/esp.h>
25 #endif
28 * Algorithms supported by IPsec. These entries contain properties which
29 * are used in key negotiation and xfrm processing, and are used to verify
30 * that instantiated crypto transforms have correct parameters for IPsec
31 * purposes.
33 static struct xfrm_algo_desc aead_list[] = {
35 .name = "rfc4106(gcm(aes))",
37 .uinfo = {
38 .aead = {
39 .icv_truncbits = 64,
43 .desc = {
44 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
45 .sadb_alg_ivlen = 8,
46 .sadb_alg_minbits = 128,
47 .sadb_alg_maxbits = 256
51 .name = "rfc4106(gcm(aes))",
53 .uinfo = {
54 .aead = {
55 .icv_truncbits = 96,
59 .desc = {
60 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
61 .sadb_alg_ivlen = 8,
62 .sadb_alg_minbits = 128,
63 .sadb_alg_maxbits = 256
67 .name = "rfc4106(gcm(aes))",
69 .uinfo = {
70 .aead = {
71 .icv_truncbits = 128,
75 .desc = {
76 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
77 .sadb_alg_ivlen = 8,
78 .sadb_alg_minbits = 128,
79 .sadb_alg_maxbits = 256
83 .name = "rfc4309(ccm(aes))",
85 .uinfo = {
86 .aead = {
87 .icv_truncbits = 64,
91 .desc = {
92 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
93 .sadb_alg_ivlen = 8,
94 .sadb_alg_minbits = 128,
95 .sadb_alg_maxbits = 256
99 .name = "rfc4309(ccm(aes))",
101 .uinfo = {
102 .aead = {
103 .icv_truncbits = 96,
107 .desc = {
108 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
109 .sadb_alg_ivlen = 8,
110 .sadb_alg_minbits = 128,
111 .sadb_alg_maxbits = 256
115 .name = "rfc4309(ccm(aes))",
117 .uinfo = {
118 .aead = {
119 .icv_truncbits = 128,
123 .desc = {
124 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
125 .sadb_alg_ivlen = 8,
126 .sadb_alg_minbits = 128,
127 .sadb_alg_maxbits = 256
131 .name = "rfc4543(gcm(aes))",
133 .uinfo = {
134 .aead = {
135 .icv_truncbits = 128,
139 .desc = {
140 .sadb_alg_id = SADB_X_EALG_NULL_AES_GMAC,
141 .sadb_alg_ivlen = 8,
142 .sadb_alg_minbits = 128,
143 .sadb_alg_maxbits = 256
148 static struct xfrm_algo_desc aalg_list[] = {
150 .name = "digest_null",
152 .uinfo = {
153 .auth = {
154 .icv_truncbits = 0,
155 .icv_fullbits = 0,
159 .desc = {
160 .sadb_alg_id = SADB_X_AALG_NULL,
161 .sadb_alg_ivlen = 0,
162 .sadb_alg_minbits = 0,
163 .sadb_alg_maxbits = 0
167 .name = "hmac(md5)",
168 .compat = "md5",
170 .uinfo = {
171 .auth = {
172 .icv_truncbits = 96,
173 .icv_fullbits = 128,
177 .desc = {
178 .sadb_alg_id = SADB_AALG_MD5HMAC,
179 .sadb_alg_ivlen = 0,
180 .sadb_alg_minbits = 128,
181 .sadb_alg_maxbits = 128
185 .name = "hmac(sha1)",
186 .compat = "sha1",
188 .uinfo = {
189 .auth = {
190 .icv_truncbits = 96,
191 .icv_fullbits = 160,
195 .desc = {
196 .sadb_alg_id = SADB_AALG_SHA1HMAC,
197 .sadb_alg_ivlen = 0,
198 .sadb_alg_minbits = 160,
199 .sadb_alg_maxbits = 160
203 .name = "hmac(sha256)",
204 .compat = "sha256",
206 .uinfo = {
207 .auth = {
208 .icv_truncbits = 96,
209 .icv_fullbits = 256,
213 .desc = {
214 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
215 .sadb_alg_ivlen = 0,
216 .sadb_alg_minbits = 256,
217 .sadb_alg_maxbits = 256
221 .name = "hmac(sha384)",
223 .uinfo = {
224 .auth = {
225 .icv_truncbits = 192,
226 .icv_fullbits = 384,
230 .desc = {
231 .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
232 .sadb_alg_ivlen = 0,
233 .sadb_alg_minbits = 384,
234 .sadb_alg_maxbits = 384
238 .name = "hmac(sha512)",
240 .uinfo = {
241 .auth = {
242 .icv_truncbits = 256,
243 .icv_fullbits = 512,
247 .desc = {
248 .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
249 .sadb_alg_ivlen = 0,
250 .sadb_alg_minbits = 512,
251 .sadb_alg_maxbits = 512
255 .name = "hmac(rmd160)",
256 .compat = "rmd160",
258 .uinfo = {
259 .auth = {
260 .icv_truncbits = 96,
261 .icv_fullbits = 160,
265 .desc = {
266 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
267 .sadb_alg_ivlen = 0,
268 .sadb_alg_minbits = 160,
269 .sadb_alg_maxbits = 160
273 .name = "xcbc(aes)",
275 .uinfo = {
276 .auth = {
277 .icv_truncbits = 96,
278 .icv_fullbits = 128,
282 .desc = {
283 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
284 .sadb_alg_ivlen = 0,
285 .sadb_alg_minbits = 128,
286 .sadb_alg_maxbits = 128
291 static struct xfrm_algo_desc ealg_list[] = {
293 .name = "ecb(cipher_null)",
294 .compat = "cipher_null",
296 .uinfo = {
297 .encr = {
298 .blockbits = 8,
299 .defkeybits = 0,
303 .desc = {
304 .sadb_alg_id = SADB_EALG_NULL,
305 .sadb_alg_ivlen = 0,
306 .sadb_alg_minbits = 0,
307 .sadb_alg_maxbits = 0
311 .name = "cbc(des)",
312 .compat = "des",
314 .uinfo = {
315 .encr = {
316 .blockbits = 64,
317 .defkeybits = 64,
321 .desc = {
322 .sadb_alg_id = SADB_EALG_DESCBC,
323 .sadb_alg_ivlen = 8,
324 .sadb_alg_minbits = 64,
325 .sadb_alg_maxbits = 64
329 .name = "cbc(des3_ede)",
330 .compat = "des3_ede",
332 .uinfo = {
333 .encr = {
334 .blockbits = 64,
335 .defkeybits = 192,
339 .desc = {
340 .sadb_alg_id = SADB_EALG_3DESCBC,
341 .sadb_alg_ivlen = 8,
342 .sadb_alg_minbits = 192,
343 .sadb_alg_maxbits = 192
347 .name = "cbc(cast5)",
348 .compat = "cast5",
350 .uinfo = {
351 .encr = {
352 .blockbits = 64,
353 .defkeybits = 128,
357 .desc = {
358 .sadb_alg_id = SADB_X_EALG_CASTCBC,
359 .sadb_alg_ivlen = 8,
360 .sadb_alg_minbits = 40,
361 .sadb_alg_maxbits = 128
365 .name = "cbc(blowfish)",
366 .compat = "blowfish",
368 .uinfo = {
369 .encr = {
370 .blockbits = 64,
371 .defkeybits = 128,
375 .desc = {
376 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
377 .sadb_alg_ivlen = 8,
378 .sadb_alg_minbits = 40,
379 .sadb_alg_maxbits = 448
383 .name = "cbc(aes)",
384 .compat = "aes",
386 .uinfo = {
387 .encr = {
388 .blockbits = 128,
389 .defkeybits = 128,
393 .desc = {
394 .sadb_alg_id = SADB_X_EALG_AESCBC,
395 .sadb_alg_ivlen = 8,
396 .sadb_alg_minbits = 128,
397 .sadb_alg_maxbits = 256
401 .name = "cbc(serpent)",
402 .compat = "serpent",
404 .uinfo = {
405 .encr = {
406 .blockbits = 128,
407 .defkeybits = 128,
411 .desc = {
412 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
413 .sadb_alg_ivlen = 8,
414 .sadb_alg_minbits = 128,
415 .sadb_alg_maxbits = 256,
419 .name = "cbc(camellia)",
420 .compat = "camellia",
422 .uinfo = {
423 .encr = {
424 .blockbits = 128,
425 .defkeybits = 128,
429 .desc = {
430 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
431 .sadb_alg_ivlen = 8,
432 .sadb_alg_minbits = 128,
433 .sadb_alg_maxbits = 256
437 .name = "cbc(twofish)",
438 .compat = "twofish",
440 .uinfo = {
441 .encr = {
442 .blockbits = 128,
443 .defkeybits = 128,
447 .desc = {
448 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
449 .sadb_alg_ivlen = 8,
450 .sadb_alg_minbits = 128,
451 .sadb_alg_maxbits = 256
455 .name = "rfc3686(ctr(aes))",
457 .uinfo = {
458 .encr = {
459 .blockbits = 128,
460 .defkeybits = 160, /* 128-bit key + 32-bit nonce */
464 .desc = {
465 .sadb_alg_id = SADB_X_EALG_AESCTR,
466 .sadb_alg_ivlen = 8,
467 .sadb_alg_minbits = 128,
468 .sadb_alg_maxbits = 256
473 static struct xfrm_algo_desc calg_list[] = {
475 .name = "deflate",
476 .uinfo = {
477 .comp = {
478 .threshold = 90,
481 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
484 .name = "lzs",
485 .uinfo = {
486 .comp = {
487 .threshold = 90,
490 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
493 .name = "lzjh",
494 .uinfo = {
495 .comp = {
496 .threshold = 50,
499 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
503 static inline int aead_entries(void)
505 return ARRAY_SIZE(aead_list);
508 static inline int aalg_entries(void)
510 return ARRAY_SIZE(aalg_list);
513 static inline int ealg_entries(void)
515 return ARRAY_SIZE(ealg_list);
518 static inline int calg_entries(void)
520 return ARRAY_SIZE(calg_list);
523 struct xfrm_algo_list {
524 struct xfrm_algo_desc *algs;
525 int entries;
526 u32 type;
527 u32 mask;
530 static const struct xfrm_algo_list xfrm_aead_list = {
531 .algs = aead_list,
532 .entries = ARRAY_SIZE(aead_list),
533 .type = CRYPTO_ALG_TYPE_AEAD,
534 .mask = CRYPTO_ALG_TYPE_MASK,
537 static const struct xfrm_algo_list xfrm_aalg_list = {
538 .algs = aalg_list,
539 .entries = ARRAY_SIZE(aalg_list),
540 .type = CRYPTO_ALG_TYPE_HASH,
541 .mask = CRYPTO_ALG_TYPE_HASH_MASK,
544 static const struct xfrm_algo_list xfrm_ealg_list = {
545 .algs = ealg_list,
546 .entries = ARRAY_SIZE(ealg_list),
547 .type = CRYPTO_ALG_TYPE_BLKCIPHER,
548 .mask = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
551 static const struct xfrm_algo_list xfrm_calg_list = {
552 .algs = calg_list,
553 .entries = ARRAY_SIZE(calg_list),
554 .type = CRYPTO_ALG_TYPE_COMPRESS,
555 .mask = CRYPTO_ALG_TYPE_MASK,
558 static struct xfrm_algo_desc *xfrm_find_algo(
559 const struct xfrm_algo_list *algo_list,
560 int match(const struct xfrm_algo_desc *entry, const void *data),
561 const void *data, int probe)
563 struct xfrm_algo_desc *list = algo_list->algs;
564 int i, status;
566 for (i = 0; i < algo_list->entries; i++) {
567 if (!match(list + i, data))
568 continue;
570 if (list[i].available)
571 return &list[i];
573 if (!probe)
574 break;
576 status = crypto_has_alg(list[i].name, algo_list->type,
577 algo_list->mask);
578 if (!status)
579 break;
581 list[i].available = status;
582 return &list[i];
584 return NULL;
587 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
588 const void *data)
590 return entry->desc.sadb_alg_id == (unsigned long)data;
593 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
595 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
596 (void *)(unsigned long)alg_id, 1);
598 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
600 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
602 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
603 (void *)(unsigned long)alg_id, 1);
605 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
607 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
609 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
610 (void *)(unsigned long)alg_id, 1);
612 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
614 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
615 const void *data)
617 const char *name = data;
619 return name && (!strcmp(name, entry->name) ||
620 (entry->compat && !strcmp(name, entry->compat)));
623 struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
625 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
626 probe);
628 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
630 struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
632 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
633 probe);
635 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
637 struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
639 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
640 probe);
642 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
644 struct xfrm_aead_name {
645 const char *name;
646 int icvbits;
649 static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
650 const void *data)
652 const struct xfrm_aead_name *aead = data;
653 const char *name = aead->name;
655 return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
656 !strcmp(name, entry->name);
659 struct xfrm_algo_desc *xfrm_aead_get_byname(char *name, int icv_len, int probe)
661 struct xfrm_aead_name data = {
662 .name = name,
663 .icvbits = icv_len,
666 return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
667 probe);
669 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
671 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
673 if (idx >= aalg_entries())
674 return NULL;
676 return &aalg_list[idx];
678 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
680 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
682 if (idx >= ealg_entries())
683 return NULL;
685 return &ealg_list[idx];
687 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
690 * Probe for the availability of crypto algorithms, and set the available
691 * flag for any algorithms found on the system. This is typically called by
692 * pfkey during userspace SA add, update or register.
694 void xfrm_probe_algs(void)
696 int i, status;
698 BUG_ON(in_softirq());
700 for (i = 0; i < aalg_entries(); i++) {
701 status = crypto_has_hash(aalg_list[i].name, 0,
702 CRYPTO_ALG_ASYNC);
703 if (aalg_list[i].available != status)
704 aalg_list[i].available = status;
707 for (i = 0; i < ealg_entries(); i++) {
708 status = crypto_has_blkcipher(ealg_list[i].name, 0,
709 CRYPTO_ALG_ASYNC);
710 if (ealg_list[i].available != status)
711 ealg_list[i].available = status;
714 for (i = 0; i < calg_entries(); i++) {
715 status = crypto_has_comp(calg_list[i].name, 0,
716 CRYPTO_ALG_ASYNC);
717 if (calg_list[i].available != status)
718 calg_list[i].available = status;
721 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
723 int xfrm_count_auth_supported(void)
725 int i, n;
727 for (i = 0, n = 0; i < aalg_entries(); i++)
728 if (aalg_list[i].available)
729 n++;
730 return n;
732 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
734 int xfrm_count_enc_supported(void)
736 int i, n;
738 for (i = 0, n = 0; i < ealg_entries(); i++)
739 if (ealg_list[i].available)
740 n++;
741 return n;
743 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
745 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || \
746 defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
748 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
750 if (tail != skb) {
751 skb->data_len += len;
752 skb->len += len;
754 return skb_put(tail, len);
756 EXPORT_SYMBOL_GPL(pskb_put);
757 #endif