1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * xfrm algorithm interface
5 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 #include <crypto/aead.h>
9 #include <crypto/hash.h>
10 #include <crypto/skcipher.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/pfkeyv2.h>
14 #include <linux/crypto.h>
15 #include <linux/scatterlist.h>
17 #if IS_ENABLED(CONFIG_INET_ESP) || IS_ENABLED(CONFIG_INET6_ESP)
22 * Algorithms supported by IPsec. These entries contain properties which
23 * are used in key negotiation and xfrm processing, and are used to verify
24 * that instantiated crypto transforms have correct parameters for IPsec
27 static struct xfrm_algo_desc aead_list[] = {
29 .name = "rfc4106(gcm(aes))",
41 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
43 .sadb_alg_minbits = 128,
44 .sadb_alg_maxbits = 256
48 .name = "rfc4106(gcm(aes))",
60 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
62 .sadb_alg_minbits = 128,
63 .sadb_alg_maxbits = 256
67 .name = "rfc4106(gcm(aes))",
79 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
81 .sadb_alg_minbits = 128,
82 .sadb_alg_maxbits = 256
86 .name = "rfc4309(ccm(aes))",
98 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
100 .sadb_alg_minbits = 128,
101 .sadb_alg_maxbits = 256
105 .name = "rfc4309(ccm(aes))",
114 .pfkey_supported = 1,
117 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
119 .sadb_alg_minbits = 128,
120 .sadb_alg_maxbits = 256
124 .name = "rfc4309(ccm(aes))",
129 .icv_truncbits = 128,
133 .pfkey_supported = 1,
136 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
138 .sadb_alg_minbits = 128,
139 .sadb_alg_maxbits = 256
143 .name = "rfc4543(gcm(aes))",
148 .icv_truncbits = 128,
152 .pfkey_supported = 1,
155 .sadb_alg_id = SADB_X_EALG_NULL_AES_GMAC,
157 .sadb_alg_minbits = 128,
158 .sadb_alg_maxbits = 256
162 .name = "rfc7539esp(chacha20,poly1305)",
167 .icv_truncbits = 128,
171 .pfkey_supported = 0,
175 static struct xfrm_algo_desc aalg_list[] = {
177 .name = "digest_null",
186 .pfkey_supported = 1,
189 .sadb_alg_id = SADB_X_AALG_NULL,
191 .sadb_alg_minbits = 0,
192 .sadb_alg_maxbits = 0
206 .pfkey_supported = 1,
209 .sadb_alg_id = SADB_AALG_MD5HMAC,
211 .sadb_alg_minbits = 128,
212 .sadb_alg_maxbits = 128
216 .name = "hmac(sha1)",
226 .pfkey_supported = 1,
229 .sadb_alg_id = SADB_AALG_SHA1HMAC,
231 .sadb_alg_minbits = 160,
232 .sadb_alg_maxbits = 160
236 .name = "hmac(sha256)",
246 .pfkey_supported = 1,
249 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
251 .sadb_alg_minbits = 256,
252 .sadb_alg_maxbits = 256
256 .name = "hmac(sha384)",
260 .icv_truncbits = 192,
265 .pfkey_supported = 1,
268 .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
270 .sadb_alg_minbits = 384,
271 .sadb_alg_maxbits = 384
275 .name = "hmac(sha512)",
279 .icv_truncbits = 256,
284 .pfkey_supported = 1,
287 .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
289 .sadb_alg_minbits = 512,
290 .sadb_alg_maxbits = 512
294 .name = "hmac(rmd160)",
304 .pfkey_supported = 1,
307 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
309 .sadb_alg_minbits = 160,
310 .sadb_alg_maxbits = 160
323 .pfkey_supported = 1,
326 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
328 .sadb_alg_minbits = 128,
329 .sadb_alg_maxbits = 128
343 .pfkey_supported = 0,
351 .icv_truncbits = 256,
356 .pfkey_supported = 1,
359 .sadb_alg_id = SADB_X_AALG_SM3_256HMAC,
361 .sadb_alg_minbits = 256,
362 .sadb_alg_maxbits = 256
367 static struct xfrm_algo_desc ealg_list[] = {
369 .name = "ecb(cipher_null)",
370 .compat = "cipher_null",
379 .pfkey_supported = 1,
382 .sadb_alg_id = SADB_EALG_NULL,
384 .sadb_alg_minbits = 0,
385 .sadb_alg_maxbits = 0
400 .pfkey_supported = 1,
403 .sadb_alg_id = SADB_EALG_DESCBC,
405 .sadb_alg_minbits = 64,
406 .sadb_alg_maxbits = 64
410 .name = "cbc(des3_ede)",
411 .compat = "des3_ede",
421 .pfkey_supported = 1,
424 .sadb_alg_id = SADB_EALG_3DESCBC,
426 .sadb_alg_minbits = 192,
427 .sadb_alg_maxbits = 192
431 .name = "cbc(cast5)",
442 .pfkey_supported = 1,
445 .sadb_alg_id = SADB_X_EALG_CASTCBC,
447 .sadb_alg_minbits = 40,
448 .sadb_alg_maxbits = 128
452 .name = "cbc(blowfish)",
453 .compat = "blowfish",
463 .pfkey_supported = 1,
466 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
468 .sadb_alg_minbits = 40,
469 .sadb_alg_maxbits = 448
484 .pfkey_supported = 1,
487 .sadb_alg_id = SADB_X_EALG_AESCBC,
489 .sadb_alg_minbits = 128,
490 .sadb_alg_maxbits = 256
494 .name = "cbc(serpent)",
505 .pfkey_supported = 1,
508 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
510 .sadb_alg_minbits = 128,
511 .sadb_alg_maxbits = 256,
515 .name = "cbc(camellia)",
516 .compat = "camellia",
526 .pfkey_supported = 1,
529 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
531 .sadb_alg_minbits = 128,
532 .sadb_alg_maxbits = 256
536 .name = "cbc(twofish)",
547 .pfkey_supported = 1,
550 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
552 .sadb_alg_minbits = 128,
553 .sadb_alg_maxbits = 256
557 .name = "rfc3686(ctr(aes))",
563 .defkeybits = 160, /* 128-bit key + 32-bit nonce */
567 .pfkey_supported = 1,
570 .sadb_alg_id = SADB_X_EALG_AESCTR,
572 .sadb_alg_minbits = 160,
573 .sadb_alg_maxbits = 288
588 .pfkey_supported = 1,
591 .sadb_alg_id = SADB_X_EALG_SM4CBC,
592 .sadb_alg_ivlen = 16,
593 .sadb_alg_minbits = 128,
594 .sadb_alg_maxbits = 256
599 static struct xfrm_algo_desc calg_list[] = {
607 .pfkey_supported = 1,
608 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
617 .pfkey_supported = 1,
618 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
627 .pfkey_supported = 1,
628 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
632 static inline int aalg_entries(void)
634 return ARRAY_SIZE(aalg_list);
637 static inline int ealg_entries(void)
639 return ARRAY_SIZE(ealg_list);
642 static inline int calg_entries(void)
644 return ARRAY_SIZE(calg_list);
647 struct xfrm_algo_list {
648 int (*find)(const char *name, u32 type, u32 mask);
649 struct xfrm_algo_desc *algs;
653 static const struct xfrm_algo_list xfrm_aead_list = {
654 .find = crypto_has_aead,
656 .entries = ARRAY_SIZE(aead_list),
659 static const struct xfrm_algo_list xfrm_aalg_list = {
660 .find = crypto_has_ahash,
662 .entries = ARRAY_SIZE(aalg_list),
665 static const struct xfrm_algo_list xfrm_ealg_list = {
666 .find = crypto_has_skcipher,
668 .entries = ARRAY_SIZE(ealg_list),
671 static const struct xfrm_algo_list xfrm_calg_list = {
672 .find = crypto_has_comp,
674 .entries = ARRAY_SIZE(calg_list),
677 static struct xfrm_algo_desc *xfrm_find_algo(
678 const struct xfrm_algo_list *algo_list,
679 int match(const struct xfrm_algo_desc *entry, const void *data),
680 const void *data, int probe)
682 struct xfrm_algo_desc *list = algo_list->algs;
685 for (i = 0; i < algo_list->entries; i++) {
686 if (!match(list + i, data))
689 if (list[i].available)
695 status = algo_list->find(list[i].name, 0, 0);
699 list[i].available = status;
705 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
708 return entry->desc.sadb_alg_id == (unsigned long)data;
711 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
713 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
714 (void *)(unsigned long)alg_id, 1);
716 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
718 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
720 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
721 (void *)(unsigned long)alg_id, 1);
723 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
725 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
727 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
728 (void *)(unsigned long)alg_id, 1);
730 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
732 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
735 const char *name = data;
737 return name && (!strcmp(name, entry->name) ||
738 (entry->compat && !strcmp(name, entry->compat)));
741 struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe)
743 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
746 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
748 struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe)
750 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
753 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
755 struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe)
757 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
760 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
762 struct xfrm_aead_name {
767 static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
770 const struct xfrm_aead_name *aead = data;
771 const char *name = aead->name;
773 return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
774 !strcmp(name, entry->name);
777 struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len, int probe)
779 struct xfrm_aead_name data = {
784 return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
787 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
789 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
791 if (idx >= aalg_entries())
794 return &aalg_list[idx];
796 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
798 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
800 if (idx >= ealg_entries())
803 return &ealg_list[idx];
805 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
808 * Probe for the availability of crypto algorithms, and set the available
809 * flag for any algorithms found on the system. This is typically called by
810 * pfkey during userspace SA add, update or register.
812 void xfrm_probe_algs(void)
816 BUG_ON(in_softirq());
818 for (i = 0; i < aalg_entries(); i++) {
819 status = crypto_has_ahash(aalg_list[i].name, 0, 0);
820 if (aalg_list[i].available != status)
821 aalg_list[i].available = status;
824 for (i = 0; i < ealg_entries(); i++) {
825 status = crypto_has_skcipher(ealg_list[i].name, 0, 0);
826 if (ealg_list[i].available != status)
827 ealg_list[i].available = status;
830 for (i = 0; i < calg_entries(); i++) {
831 status = crypto_has_comp(calg_list[i].name, 0,
833 if (calg_list[i].available != status)
834 calg_list[i].available = status;
837 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
839 int xfrm_count_pfkey_auth_supported(void)
843 for (i = 0, n = 0; i < aalg_entries(); i++)
844 if (aalg_list[i].available && aalg_list[i].pfkey_supported)
848 EXPORT_SYMBOL_GPL(xfrm_count_pfkey_auth_supported);
850 int xfrm_count_pfkey_enc_supported(void)
854 for (i = 0, n = 0; i < ealg_entries(); i++)
855 if (ealg_list[i].available && ealg_list[i].pfkey_supported)
859 EXPORT_SYMBOL_GPL(xfrm_count_pfkey_enc_supported);
861 MODULE_DESCRIPTION("XFRM Algorithm interface");
862 MODULE_LICENSE("GPL");