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)
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>
18 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
23 * Algorithms supported by IPsec. These entries contain properties which
24 * are used in key negotiation and xfrm processing, and are used to verify
25 * that instantiated crypto transforms have correct parameters for IPsec
28 static struct xfrm_algo_desc aead_list[] = {
30 .name = "rfc4106(gcm(aes))",
39 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
41 .sadb_alg_minbits = 128,
42 .sadb_alg_maxbits = 256
46 .name = "rfc4106(gcm(aes))",
55 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
57 .sadb_alg_minbits = 128,
58 .sadb_alg_maxbits = 256
62 .name = "rfc4106(gcm(aes))",
71 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
73 .sadb_alg_minbits = 128,
74 .sadb_alg_maxbits = 256
78 .name = "rfc4309(ccm(aes))",
87 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
89 .sadb_alg_minbits = 128,
90 .sadb_alg_maxbits = 256
94 .name = "rfc4309(ccm(aes))",
103 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
105 .sadb_alg_minbits = 128,
106 .sadb_alg_maxbits = 256
110 .name = "rfc4309(ccm(aes))",
114 .icv_truncbits = 128,
119 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
121 .sadb_alg_minbits = 128,
122 .sadb_alg_maxbits = 256
126 .name = "rfc4543(gcm(aes))",
130 .icv_truncbits = 128,
135 .sadb_alg_id = SADB_X_EALG_NULL_AES_GMAC,
137 .sadb_alg_minbits = 128,
138 .sadb_alg_maxbits = 256
143 static struct xfrm_algo_desc aalg_list[] = {
145 .name = "digest_null",
155 .sadb_alg_id = SADB_X_AALG_NULL,
157 .sadb_alg_minbits = 0,
158 .sadb_alg_maxbits = 0
173 .sadb_alg_id = SADB_AALG_MD5HMAC,
175 .sadb_alg_minbits = 128,
176 .sadb_alg_maxbits = 128
180 .name = "hmac(sha1)",
191 .sadb_alg_id = SADB_AALG_SHA1HMAC,
193 .sadb_alg_minbits = 160,
194 .sadb_alg_maxbits = 160
198 .name = "hmac(sha256)",
209 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
211 .sadb_alg_minbits = 256,
212 .sadb_alg_maxbits = 256
216 .name = "hmac(sha384)",
220 .icv_truncbits = 192,
226 .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
228 .sadb_alg_minbits = 384,
229 .sadb_alg_maxbits = 384
233 .name = "hmac(sha512)",
237 .icv_truncbits = 256,
243 .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
245 .sadb_alg_minbits = 512,
246 .sadb_alg_maxbits = 512
250 .name = "hmac(rmd160)",
261 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
263 .sadb_alg_minbits = 160,
264 .sadb_alg_maxbits = 160
278 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
280 .sadb_alg_minbits = 128,
281 .sadb_alg_maxbits = 128
286 static struct xfrm_algo_desc ealg_list[] = {
288 .name = "ecb(cipher_null)",
289 .compat = "cipher_null",
299 .sadb_alg_id = SADB_EALG_NULL,
301 .sadb_alg_minbits = 0,
302 .sadb_alg_maxbits = 0
317 .sadb_alg_id = SADB_EALG_DESCBC,
319 .sadb_alg_minbits = 64,
320 .sadb_alg_maxbits = 64
324 .name = "cbc(des3_ede)",
325 .compat = "des3_ede",
335 .sadb_alg_id = SADB_EALG_3DESCBC,
337 .sadb_alg_minbits = 192,
338 .sadb_alg_maxbits = 192
342 .name = "cbc(cast5)",
353 .sadb_alg_id = SADB_X_EALG_CASTCBC,
355 .sadb_alg_minbits = 40,
356 .sadb_alg_maxbits = 128
360 .name = "cbc(blowfish)",
361 .compat = "blowfish",
371 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
373 .sadb_alg_minbits = 40,
374 .sadb_alg_maxbits = 448
389 .sadb_alg_id = SADB_X_EALG_AESCBC,
391 .sadb_alg_minbits = 128,
392 .sadb_alg_maxbits = 256
396 .name = "cbc(serpent)",
407 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
409 .sadb_alg_minbits = 128,
410 .sadb_alg_maxbits = 256,
414 .name = "cbc(camellia)",
415 .compat = "camellia",
425 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
427 .sadb_alg_minbits = 128,
428 .sadb_alg_maxbits = 256
432 .name = "cbc(twofish)",
443 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
445 .sadb_alg_minbits = 128,
446 .sadb_alg_maxbits = 256
450 .name = "rfc3686(ctr(aes))",
455 .defkeybits = 160, /* 128-bit key + 32-bit nonce */
460 .sadb_alg_id = SADB_X_EALG_AESCTR,
462 .sadb_alg_minbits = 160,
463 .sadb_alg_maxbits = 288
468 static struct xfrm_algo_desc calg_list[] = {
476 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
485 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
494 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
498 static inline int aead_entries(void)
500 return ARRAY_SIZE(aead_list);
503 static inline int aalg_entries(void)
505 return ARRAY_SIZE(aalg_list);
508 static inline int ealg_entries(void)
510 return ARRAY_SIZE(ealg_list);
513 static inline int calg_entries(void)
515 return ARRAY_SIZE(calg_list);
518 struct xfrm_algo_list {
519 struct xfrm_algo_desc *algs;
525 static const struct xfrm_algo_list xfrm_aead_list = {
527 .entries = ARRAY_SIZE(aead_list),
528 .type = CRYPTO_ALG_TYPE_AEAD,
529 .mask = CRYPTO_ALG_TYPE_MASK,
532 static const struct xfrm_algo_list xfrm_aalg_list = {
534 .entries = ARRAY_SIZE(aalg_list),
535 .type = CRYPTO_ALG_TYPE_HASH,
536 .mask = CRYPTO_ALG_TYPE_HASH_MASK,
539 static const struct xfrm_algo_list xfrm_ealg_list = {
541 .entries = ARRAY_SIZE(ealg_list),
542 .type = CRYPTO_ALG_TYPE_BLKCIPHER,
543 .mask = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
546 static const struct xfrm_algo_list xfrm_calg_list = {
548 .entries = ARRAY_SIZE(calg_list),
549 .type = CRYPTO_ALG_TYPE_COMPRESS,
550 .mask = CRYPTO_ALG_TYPE_MASK,
553 static struct xfrm_algo_desc *xfrm_find_algo(
554 const struct xfrm_algo_list *algo_list,
555 int match(const struct xfrm_algo_desc *entry, const void *data),
556 const void *data, int probe)
558 struct xfrm_algo_desc *list = algo_list->algs;
561 for (i = 0; i < algo_list->entries; i++) {
562 if (!match(list + i, data))
565 if (list[i].available)
571 status = crypto_has_alg(list[i].name, algo_list->type,
576 list[i].available = status;
582 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
585 return entry->desc.sadb_alg_id == (unsigned long)data;
588 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
590 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
591 (void *)(unsigned long)alg_id, 1);
593 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
595 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
597 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
598 (void *)(unsigned long)alg_id, 1);
600 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
602 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
604 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
605 (void *)(unsigned long)alg_id, 1);
607 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
609 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
612 const char *name = data;
614 return name && (!strcmp(name, entry->name) ||
615 (entry->compat && !strcmp(name, entry->compat)));
618 struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe)
620 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
623 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
625 struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe)
627 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
630 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
632 struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe)
634 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
637 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
639 struct xfrm_aead_name {
644 static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
647 const struct xfrm_aead_name *aead = data;
648 const char *name = aead->name;
650 return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
651 !strcmp(name, entry->name);
654 struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len, int probe)
656 struct xfrm_aead_name data = {
661 return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
664 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
666 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
668 if (idx >= aalg_entries())
671 return &aalg_list[idx];
673 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
675 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
677 if (idx >= ealg_entries())
680 return &ealg_list[idx];
682 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
685 * Probe for the availability of crypto algorithms, and set the available
686 * flag for any algorithms found on the system. This is typically called by
687 * pfkey during userspace SA add, update or register.
689 void xfrm_probe_algs(void)
693 BUG_ON(in_softirq());
695 for (i = 0; i < aalg_entries(); i++) {
696 status = crypto_has_hash(aalg_list[i].name, 0,
698 if (aalg_list[i].available != status)
699 aalg_list[i].available = status;
702 for (i = 0; i < ealg_entries(); i++) {
703 status = crypto_has_ablkcipher(ealg_list[i].name, 0, 0);
704 if (ealg_list[i].available != status)
705 ealg_list[i].available = status;
708 for (i = 0; i < calg_entries(); i++) {
709 status = crypto_has_comp(calg_list[i].name, 0,
711 if (calg_list[i].available != status)
712 calg_list[i].available = status;
715 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
717 int xfrm_count_auth_supported(void)
721 for (i = 0, n = 0; i < aalg_entries(); i++)
722 if (aalg_list[i].available)
726 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
728 int xfrm_count_enc_supported(void)
732 for (i = 0, n = 0; i < ealg_entries(); i++)
733 if (ealg_list[i].available)
737 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
739 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
741 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
744 skb->data_len += len;
747 return skb_put(tail, len);
749 EXPORT_SYMBOL_GPL(pskb_put);
752 MODULE_LICENSE("GPL");