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))",
42 .sadb_alg_id
= SADB_X_EALG_AES_GCM_ICV8
,
44 .sadb_alg_minbits
= 128,
45 .sadb_alg_maxbits
= 256
49 .name
= "rfc4106(gcm(aes))",
61 .sadb_alg_id
= SADB_X_EALG_AES_GCM_ICV12
,
63 .sadb_alg_minbits
= 128,
64 .sadb_alg_maxbits
= 256
68 .name
= "rfc4106(gcm(aes))",
80 .sadb_alg_id
= SADB_X_EALG_AES_GCM_ICV16
,
82 .sadb_alg_minbits
= 128,
83 .sadb_alg_maxbits
= 256
87 .name
= "rfc4309(ccm(aes))",
99 .sadb_alg_id
= SADB_X_EALG_AES_CCM_ICV8
,
101 .sadb_alg_minbits
= 128,
102 .sadb_alg_maxbits
= 256
106 .name
= "rfc4309(ccm(aes))",
115 .pfkey_supported
= 1,
118 .sadb_alg_id
= SADB_X_EALG_AES_CCM_ICV12
,
120 .sadb_alg_minbits
= 128,
121 .sadb_alg_maxbits
= 256
125 .name
= "rfc4309(ccm(aes))",
130 .icv_truncbits
= 128,
134 .pfkey_supported
= 1,
137 .sadb_alg_id
= SADB_X_EALG_AES_CCM_ICV16
,
139 .sadb_alg_minbits
= 128,
140 .sadb_alg_maxbits
= 256
144 .name
= "rfc4543(gcm(aes))",
149 .icv_truncbits
= 128,
153 .pfkey_supported
= 1,
156 .sadb_alg_id
= SADB_X_EALG_NULL_AES_GMAC
,
158 .sadb_alg_minbits
= 128,
159 .sadb_alg_maxbits
= 256
163 .name
= "rfc7539esp(chacha20,poly1305)",
168 .icv_truncbits
= 128,
172 .pfkey_supported
= 0,
176 static struct xfrm_algo_desc aalg_list
[] = {
178 .name
= "digest_null",
187 .pfkey_supported
= 1,
190 .sadb_alg_id
= SADB_X_AALG_NULL
,
192 .sadb_alg_minbits
= 0,
193 .sadb_alg_maxbits
= 0
207 .pfkey_supported
= 1,
210 .sadb_alg_id
= SADB_AALG_MD5HMAC
,
212 .sadb_alg_minbits
= 128,
213 .sadb_alg_maxbits
= 128
217 .name
= "hmac(sha1)",
227 .pfkey_supported
= 1,
230 .sadb_alg_id
= SADB_AALG_SHA1HMAC
,
232 .sadb_alg_minbits
= 160,
233 .sadb_alg_maxbits
= 160
237 .name
= "hmac(sha256)",
247 .pfkey_supported
= 1,
250 .sadb_alg_id
= SADB_X_AALG_SHA2_256HMAC
,
252 .sadb_alg_minbits
= 256,
253 .sadb_alg_maxbits
= 256
257 .name
= "hmac(sha384)",
261 .icv_truncbits
= 192,
266 .pfkey_supported
= 1,
269 .sadb_alg_id
= SADB_X_AALG_SHA2_384HMAC
,
271 .sadb_alg_minbits
= 384,
272 .sadb_alg_maxbits
= 384
276 .name
= "hmac(sha512)",
280 .icv_truncbits
= 256,
285 .pfkey_supported
= 1,
288 .sadb_alg_id
= SADB_X_AALG_SHA2_512HMAC
,
290 .sadb_alg_minbits
= 512,
291 .sadb_alg_maxbits
= 512
295 .name
= "hmac(rmd160)",
305 .pfkey_supported
= 1,
308 .sadb_alg_id
= SADB_X_AALG_RIPEMD160HMAC
,
310 .sadb_alg_minbits
= 160,
311 .sadb_alg_maxbits
= 160
324 .pfkey_supported
= 1,
327 .sadb_alg_id
= SADB_X_AALG_AES_XCBC_MAC
,
329 .sadb_alg_minbits
= 128,
330 .sadb_alg_maxbits
= 128
344 .pfkey_supported
= 0,
348 static struct xfrm_algo_desc ealg_list
[] = {
350 .name
= "ecb(cipher_null)",
351 .compat
= "cipher_null",
360 .pfkey_supported
= 1,
363 .sadb_alg_id
= SADB_EALG_NULL
,
365 .sadb_alg_minbits
= 0,
366 .sadb_alg_maxbits
= 0
381 .pfkey_supported
= 1,
384 .sadb_alg_id
= SADB_EALG_DESCBC
,
386 .sadb_alg_minbits
= 64,
387 .sadb_alg_maxbits
= 64
391 .name
= "cbc(des3_ede)",
392 .compat
= "des3_ede",
402 .pfkey_supported
= 1,
405 .sadb_alg_id
= SADB_EALG_3DESCBC
,
407 .sadb_alg_minbits
= 192,
408 .sadb_alg_maxbits
= 192
412 .name
= "cbc(cast5)",
423 .pfkey_supported
= 1,
426 .sadb_alg_id
= SADB_X_EALG_CASTCBC
,
428 .sadb_alg_minbits
= 40,
429 .sadb_alg_maxbits
= 128
433 .name
= "cbc(blowfish)",
434 .compat
= "blowfish",
444 .pfkey_supported
= 1,
447 .sadb_alg_id
= SADB_X_EALG_BLOWFISHCBC
,
449 .sadb_alg_minbits
= 40,
450 .sadb_alg_maxbits
= 448
465 .pfkey_supported
= 1,
468 .sadb_alg_id
= SADB_X_EALG_AESCBC
,
470 .sadb_alg_minbits
= 128,
471 .sadb_alg_maxbits
= 256
475 .name
= "cbc(serpent)",
486 .pfkey_supported
= 1,
489 .sadb_alg_id
= SADB_X_EALG_SERPENTCBC
,
491 .sadb_alg_minbits
= 128,
492 .sadb_alg_maxbits
= 256,
496 .name
= "cbc(camellia)",
497 .compat
= "camellia",
507 .pfkey_supported
= 1,
510 .sadb_alg_id
= SADB_X_EALG_CAMELLIACBC
,
512 .sadb_alg_minbits
= 128,
513 .sadb_alg_maxbits
= 256
517 .name
= "cbc(twofish)",
528 .pfkey_supported
= 1,
531 .sadb_alg_id
= SADB_X_EALG_TWOFISHCBC
,
533 .sadb_alg_minbits
= 128,
534 .sadb_alg_maxbits
= 256
538 .name
= "rfc3686(ctr(aes))",
544 .defkeybits
= 160, /* 128-bit key + 32-bit nonce */
548 .pfkey_supported
= 1,
551 .sadb_alg_id
= SADB_X_EALG_AESCTR
,
553 .sadb_alg_minbits
= 160,
554 .sadb_alg_maxbits
= 288
559 static struct xfrm_algo_desc calg_list
[] = {
567 .pfkey_supported
= 1,
568 .desc
= { .sadb_alg_id
= SADB_X_CALG_DEFLATE
}
577 .pfkey_supported
= 1,
578 .desc
= { .sadb_alg_id
= SADB_X_CALG_LZS
}
587 .pfkey_supported
= 1,
588 .desc
= { .sadb_alg_id
= SADB_X_CALG_LZJH
}
592 static inline int aalg_entries(void)
594 return ARRAY_SIZE(aalg_list
);
597 static inline int ealg_entries(void)
599 return ARRAY_SIZE(ealg_list
);
602 static inline int calg_entries(void)
604 return ARRAY_SIZE(calg_list
);
607 struct xfrm_algo_list
{
608 struct xfrm_algo_desc
*algs
;
614 static const struct xfrm_algo_list xfrm_aead_list
= {
616 .entries
= ARRAY_SIZE(aead_list
),
617 .type
= CRYPTO_ALG_TYPE_AEAD
,
618 .mask
= CRYPTO_ALG_TYPE_MASK
,
621 static const struct xfrm_algo_list xfrm_aalg_list
= {
623 .entries
= ARRAY_SIZE(aalg_list
),
624 .type
= CRYPTO_ALG_TYPE_HASH
,
625 .mask
= CRYPTO_ALG_TYPE_HASH_MASK
,
628 static const struct xfrm_algo_list xfrm_ealg_list
= {
630 .entries
= ARRAY_SIZE(ealg_list
),
631 .type
= CRYPTO_ALG_TYPE_BLKCIPHER
,
632 .mask
= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
,
635 static const struct xfrm_algo_list xfrm_calg_list
= {
637 .entries
= ARRAY_SIZE(calg_list
),
638 .type
= CRYPTO_ALG_TYPE_COMPRESS
,
639 .mask
= CRYPTO_ALG_TYPE_MASK
,
642 static struct xfrm_algo_desc
*xfrm_find_algo(
643 const struct xfrm_algo_list
*algo_list
,
644 int match(const struct xfrm_algo_desc
*entry
, const void *data
),
645 const void *data
, int probe
)
647 struct xfrm_algo_desc
*list
= algo_list
->algs
;
650 for (i
= 0; i
< algo_list
->entries
; i
++) {
651 if (!match(list
+ i
, data
))
654 if (list
[i
].available
)
660 status
= crypto_has_alg(list
[i
].name
, algo_list
->type
,
665 list
[i
].available
= status
;
671 static int xfrm_alg_id_match(const struct xfrm_algo_desc
*entry
,
674 return entry
->desc
.sadb_alg_id
== (unsigned long)data
;
677 struct xfrm_algo_desc
*xfrm_aalg_get_byid(int alg_id
)
679 return xfrm_find_algo(&xfrm_aalg_list
, xfrm_alg_id_match
,
680 (void *)(unsigned long)alg_id
, 1);
682 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid
);
684 struct xfrm_algo_desc
*xfrm_ealg_get_byid(int alg_id
)
686 return xfrm_find_algo(&xfrm_ealg_list
, xfrm_alg_id_match
,
687 (void *)(unsigned long)alg_id
, 1);
689 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid
);
691 struct xfrm_algo_desc
*xfrm_calg_get_byid(int alg_id
)
693 return xfrm_find_algo(&xfrm_calg_list
, xfrm_alg_id_match
,
694 (void *)(unsigned long)alg_id
, 1);
696 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid
);
698 static int xfrm_alg_name_match(const struct xfrm_algo_desc
*entry
,
701 const char *name
= data
;
703 return name
&& (!strcmp(name
, entry
->name
) ||
704 (entry
->compat
&& !strcmp(name
, entry
->compat
)));
707 struct xfrm_algo_desc
*xfrm_aalg_get_byname(const char *name
, int probe
)
709 return xfrm_find_algo(&xfrm_aalg_list
, xfrm_alg_name_match
, name
,
712 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname
);
714 struct xfrm_algo_desc
*xfrm_ealg_get_byname(const char *name
, int probe
)
716 return xfrm_find_algo(&xfrm_ealg_list
, xfrm_alg_name_match
, name
,
719 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname
);
721 struct xfrm_algo_desc
*xfrm_calg_get_byname(const char *name
, int probe
)
723 return xfrm_find_algo(&xfrm_calg_list
, xfrm_alg_name_match
, name
,
726 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname
);
728 struct xfrm_aead_name
{
733 static int xfrm_aead_name_match(const struct xfrm_algo_desc
*entry
,
736 const struct xfrm_aead_name
*aead
= data
;
737 const char *name
= aead
->name
;
739 return aead
->icvbits
== entry
->uinfo
.aead
.icv_truncbits
&& name
&&
740 !strcmp(name
, entry
->name
);
743 struct xfrm_algo_desc
*xfrm_aead_get_byname(const char *name
, int icv_len
, int probe
)
745 struct xfrm_aead_name data
= {
750 return xfrm_find_algo(&xfrm_aead_list
, xfrm_aead_name_match
, &data
,
753 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname
);
755 struct xfrm_algo_desc
*xfrm_aalg_get_byidx(unsigned int idx
)
757 if (idx
>= aalg_entries())
760 return &aalg_list
[idx
];
762 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx
);
764 struct xfrm_algo_desc
*xfrm_ealg_get_byidx(unsigned int idx
)
766 if (idx
>= ealg_entries())
769 return &ealg_list
[idx
];
771 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx
);
774 * Probe for the availability of crypto algorithms, and set the available
775 * flag for any algorithms found on the system. This is typically called by
776 * pfkey during userspace SA add, update or register.
778 void xfrm_probe_algs(void)
782 BUG_ON(in_softirq());
784 for (i
= 0; i
< aalg_entries(); i
++) {
785 status
= crypto_has_hash(aalg_list
[i
].name
, 0,
787 if (aalg_list
[i
].available
!= status
)
788 aalg_list
[i
].available
= status
;
791 for (i
= 0; i
< ealg_entries(); i
++) {
792 status
= crypto_has_ablkcipher(ealg_list
[i
].name
, 0, 0);
793 if (ealg_list
[i
].available
!= status
)
794 ealg_list
[i
].available
= status
;
797 for (i
= 0; i
< calg_entries(); i
++) {
798 status
= crypto_has_comp(calg_list
[i
].name
, 0,
800 if (calg_list
[i
].available
!= status
)
801 calg_list
[i
].available
= status
;
804 EXPORT_SYMBOL_GPL(xfrm_probe_algs
);
806 int xfrm_count_pfkey_auth_supported(void)
810 for (i
= 0, n
= 0; i
< aalg_entries(); i
++)
811 if (aalg_list
[i
].available
&& aalg_list
[i
].pfkey_supported
)
815 EXPORT_SYMBOL_GPL(xfrm_count_pfkey_auth_supported
);
817 int xfrm_count_pfkey_enc_supported(void)
821 for (i
= 0, n
= 0; i
< ealg_entries(); i
++)
822 if (ealg_list
[i
].available
&& ealg_list
[i
].pfkey_supported
)
826 EXPORT_SYMBOL_GPL(xfrm_count_pfkey_enc_supported
);
828 MODULE_LICENSE("GPL");