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/hash.h>
9 #include <crypto/skcipher.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/pfkeyv2.h>
13 #include <linux/crypto.h>
14 #include <linux/scatterlist.h>
16 #if IS_ENABLED(CONFIG_INET_ESP) || IS_ENABLED(CONFIG_INET6_ESP)
21 * Algorithms supported by IPsec. These entries contain properties which
22 * are used in key negotiation and xfrm processing, and are used to verify
23 * that instantiated crypto transforms have correct parameters for IPsec
26 static struct xfrm_algo_desc aead_list[] = {
28 .name = "rfc4106(gcm(aes))",
40 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
42 .sadb_alg_minbits = 128,
43 .sadb_alg_maxbits = 256
47 .name = "rfc4106(gcm(aes))",
59 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
61 .sadb_alg_minbits = 128,
62 .sadb_alg_maxbits = 256
66 .name = "rfc4106(gcm(aes))",
78 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
80 .sadb_alg_minbits = 128,
81 .sadb_alg_maxbits = 256
85 .name = "rfc4309(ccm(aes))",
97 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
99 .sadb_alg_minbits = 128,
100 .sadb_alg_maxbits = 256
104 .name = "rfc4309(ccm(aes))",
113 .pfkey_supported = 1,
116 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
118 .sadb_alg_minbits = 128,
119 .sadb_alg_maxbits = 256
123 .name = "rfc4309(ccm(aes))",
128 .icv_truncbits = 128,
132 .pfkey_supported = 1,
135 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
137 .sadb_alg_minbits = 128,
138 .sadb_alg_maxbits = 256
142 .name = "rfc4543(gcm(aes))",
147 .icv_truncbits = 128,
151 .pfkey_supported = 1,
154 .sadb_alg_id = SADB_X_EALG_NULL_AES_GMAC,
156 .sadb_alg_minbits = 128,
157 .sadb_alg_maxbits = 256
161 .name = "rfc7539esp(chacha20,poly1305)",
166 .icv_truncbits = 128,
170 .pfkey_supported = 0,
174 static struct xfrm_algo_desc aalg_list[] = {
176 .name = "digest_null",
185 .pfkey_supported = 1,
188 .sadb_alg_id = SADB_X_AALG_NULL,
190 .sadb_alg_minbits = 0,
191 .sadb_alg_maxbits = 0
205 .pfkey_supported = 1,
208 .sadb_alg_id = SADB_AALG_MD5HMAC,
210 .sadb_alg_minbits = 128,
211 .sadb_alg_maxbits = 128
215 .name = "hmac(sha1)",
225 .pfkey_supported = 1,
228 .sadb_alg_id = SADB_AALG_SHA1HMAC,
230 .sadb_alg_minbits = 160,
231 .sadb_alg_maxbits = 160
235 .name = "hmac(sha256)",
245 .pfkey_supported = 1,
248 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
250 .sadb_alg_minbits = 256,
251 .sadb_alg_maxbits = 256
255 .name = "hmac(sha384)",
259 .icv_truncbits = 192,
264 .pfkey_supported = 1,
267 .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
269 .sadb_alg_minbits = 384,
270 .sadb_alg_maxbits = 384
274 .name = "hmac(sha512)",
278 .icv_truncbits = 256,
283 .pfkey_supported = 1,
286 .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
288 .sadb_alg_minbits = 512,
289 .sadb_alg_maxbits = 512
293 .name = "hmac(rmd160)",
303 .pfkey_supported = 1,
306 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
308 .sadb_alg_minbits = 160,
309 .sadb_alg_maxbits = 160
322 .pfkey_supported = 1,
325 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
327 .sadb_alg_minbits = 128,
328 .sadb_alg_maxbits = 128
342 .pfkey_supported = 0,
350 .icv_truncbits = 256,
355 .pfkey_supported = 1,
358 .sadb_alg_id = SADB_X_AALG_SM3_256HMAC,
360 .sadb_alg_minbits = 256,
361 .sadb_alg_maxbits = 256
366 static struct xfrm_algo_desc ealg_list[] = {
368 .name = "ecb(cipher_null)",
369 .compat = "cipher_null",
378 .pfkey_supported = 1,
381 .sadb_alg_id = SADB_EALG_NULL,
383 .sadb_alg_minbits = 0,
384 .sadb_alg_maxbits = 0
399 .pfkey_supported = 1,
402 .sadb_alg_id = SADB_EALG_DESCBC,
404 .sadb_alg_minbits = 64,
405 .sadb_alg_maxbits = 64
409 .name = "cbc(des3_ede)",
410 .compat = "des3_ede",
420 .pfkey_supported = 1,
423 .sadb_alg_id = SADB_EALG_3DESCBC,
425 .sadb_alg_minbits = 192,
426 .sadb_alg_maxbits = 192
430 .name = "cbc(cast5)",
441 .pfkey_supported = 1,
444 .sadb_alg_id = SADB_X_EALG_CASTCBC,
446 .sadb_alg_minbits = 40,
447 .sadb_alg_maxbits = 128
451 .name = "cbc(blowfish)",
452 .compat = "blowfish",
462 .pfkey_supported = 1,
465 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
467 .sadb_alg_minbits = 40,
468 .sadb_alg_maxbits = 448
483 .pfkey_supported = 1,
486 .sadb_alg_id = SADB_X_EALG_AESCBC,
488 .sadb_alg_minbits = 128,
489 .sadb_alg_maxbits = 256
493 .name = "cbc(serpent)",
504 .pfkey_supported = 1,
507 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
509 .sadb_alg_minbits = 128,
510 .sadb_alg_maxbits = 256,
514 .name = "cbc(camellia)",
515 .compat = "camellia",
525 .pfkey_supported = 1,
528 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
530 .sadb_alg_minbits = 128,
531 .sadb_alg_maxbits = 256
535 .name = "cbc(twofish)",
546 .pfkey_supported = 1,
549 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
551 .sadb_alg_minbits = 128,
552 .sadb_alg_maxbits = 256
556 .name = "rfc3686(ctr(aes))",
562 .defkeybits = 160, /* 128-bit key + 32-bit nonce */
566 .pfkey_supported = 1,
569 .sadb_alg_id = SADB_X_EALG_AESCTR,
571 .sadb_alg_minbits = 160,
572 .sadb_alg_maxbits = 288
587 .pfkey_supported = 1,
590 .sadb_alg_id = SADB_X_EALG_SM4CBC,
591 .sadb_alg_ivlen = 16,
592 .sadb_alg_minbits = 128,
593 .sadb_alg_maxbits = 256
598 static struct xfrm_algo_desc calg_list[] = {
606 .pfkey_supported = 1,
607 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
616 .pfkey_supported = 1,
617 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
626 .pfkey_supported = 1,
627 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
631 static inline int aalg_entries(void)
633 return ARRAY_SIZE(aalg_list);
636 static inline int ealg_entries(void)
638 return ARRAY_SIZE(ealg_list);
641 static inline int calg_entries(void)
643 return ARRAY_SIZE(calg_list);
646 struct xfrm_algo_list {
647 struct xfrm_algo_desc *algs;
653 static const struct xfrm_algo_list xfrm_aead_list = {
655 .entries = ARRAY_SIZE(aead_list),
656 .type = CRYPTO_ALG_TYPE_AEAD,
657 .mask = CRYPTO_ALG_TYPE_MASK,
660 static const struct xfrm_algo_list xfrm_aalg_list = {
662 .entries = ARRAY_SIZE(aalg_list),
663 .type = CRYPTO_ALG_TYPE_HASH,
664 .mask = CRYPTO_ALG_TYPE_HASH_MASK,
667 static const struct xfrm_algo_list xfrm_ealg_list = {
669 .entries = ARRAY_SIZE(ealg_list),
670 .type = CRYPTO_ALG_TYPE_SKCIPHER,
671 .mask = CRYPTO_ALG_TYPE_MASK,
674 static const struct xfrm_algo_list xfrm_calg_list = {
676 .entries = ARRAY_SIZE(calg_list),
677 .type = CRYPTO_ALG_TYPE_COMPRESS,
678 .mask = CRYPTO_ALG_TYPE_MASK,
681 static struct xfrm_algo_desc *xfrm_find_algo(
682 const struct xfrm_algo_list *algo_list,
683 int match(const struct xfrm_algo_desc *entry, const void *data),
684 const void *data, int probe)
686 struct xfrm_algo_desc *list = algo_list->algs;
689 for (i = 0; i < algo_list->entries; i++) {
690 if (!match(list + i, data))
693 if (list[i].available)
699 status = crypto_has_alg(list[i].name, algo_list->type,
704 list[i].available = status;
710 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
713 return entry->desc.sadb_alg_id == (unsigned long)data;
716 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
718 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
719 (void *)(unsigned long)alg_id, 1);
721 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
723 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
725 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
726 (void *)(unsigned long)alg_id, 1);
728 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
730 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
732 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
733 (void *)(unsigned long)alg_id, 1);
735 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
737 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
740 const char *name = data;
742 return name && (!strcmp(name, entry->name) ||
743 (entry->compat && !strcmp(name, entry->compat)));
746 struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe)
748 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
751 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
753 struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe)
755 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
758 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
760 struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe)
762 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
765 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
767 struct xfrm_aead_name {
772 static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
775 const struct xfrm_aead_name *aead = data;
776 const char *name = aead->name;
778 return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
779 !strcmp(name, entry->name);
782 struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len, int probe)
784 struct xfrm_aead_name data = {
789 return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
792 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
794 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
796 if (idx >= aalg_entries())
799 return &aalg_list[idx];
801 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
803 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
805 if (idx >= ealg_entries())
808 return &ealg_list[idx];
810 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
813 * Probe for the availability of crypto algorithms, and set the available
814 * flag for any algorithms found on the system. This is typically called by
815 * pfkey during userspace SA add, update or register.
817 void xfrm_probe_algs(void)
821 BUG_ON(in_softirq());
823 for (i = 0; i < aalg_entries(); i++) {
824 status = crypto_has_ahash(aalg_list[i].name, 0, 0);
825 if (aalg_list[i].available != status)
826 aalg_list[i].available = status;
829 for (i = 0; i < ealg_entries(); i++) {
830 status = crypto_has_skcipher(ealg_list[i].name, 0, 0);
831 if (ealg_list[i].available != status)
832 ealg_list[i].available = status;
835 for (i = 0; i < calg_entries(); i++) {
836 status = crypto_has_comp(calg_list[i].name, 0,
838 if (calg_list[i].available != status)
839 calg_list[i].available = status;
842 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
844 int xfrm_count_pfkey_auth_supported(void)
848 for (i = 0, n = 0; i < aalg_entries(); i++)
849 if (aalg_list[i].available && aalg_list[i].pfkey_supported)
853 EXPORT_SYMBOL_GPL(xfrm_count_pfkey_auth_supported);
855 int xfrm_count_pfkey_enc_supported(void)
859 for (i = 0, n = 0; i < ealg_entries(); i++)
860 if (ealg_list[i].available && ealg_list[i].pfkey_supported)
864 EXPORT_SYMBOL_GPL(xfrm_count_pfkey_enc_supported);
866 MODULE_LICENSE("GPL");