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_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
21 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
26 * Algorithms supported by IPsec. These entries contain properties which
27 * are used in key negotiation and xfrm processing, and are used to verify
28 * that instantiated crypto transforms have correct parameters for IPsec
31 static struct xfrm_algo_desc aead_list
[] = {
33 .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))",
58 .sadb_alg_id
= SADB_X_EALG_AES_GCM_ICV12
,
60 .sadb_alg_minbits
= 128,
61 .sadb_alg_maxbits
= 256
65 .name
= "rfc4106(gcm(aes))",
74 .sadb_alg_id
= SADB_X_EALG_AES_GCM_ICV16
,
76 .sadb_alg_minbits
= 128,
77 .sadb_alg_maxbits
= 256
81 .name
= "rfc4309(ccm(aes))",
90 .sadb_alg_id
= SADB_X_EALG_AES_CCM_ICV8
,
92 .sadb_alg_minbits
= 128,
93 .sadb_alg_maxbits
= 256
97 .name
= "rfc4309(ccm(aes))",
106 .sadb_alg_id
= SADB_X_EALG_AES_CCM_ICV12
,
108 .sadb_alg_minbits
= 128,
109 .sadb_alg_maxbits
= 256
113 .name
= "rfc4309(ccm(aes))",
117 .icv_truncbits
= 128,
122 .sadb_alg_id
= SADB_X_EALG_AES_CCM_ICV16
,
124 .sadb_alg_minbits
= 128,
125 .sadb_alg_maxbits
= 256
130 static struct xfrm_algo_desc aalg_list
[] = {
132 .name
= "digest_null",
142 .sadb_alg_id
= SADB_X_AALG_NULL
,
144 .sadb_alg_minbits
= 0,
145 .sadb_alg_maxbits
= 0
160 .sadb_alg_id
= SADB_AALG_MD5HMAC
,
162 .sadb_alg_minbits
= 128,
163 .sadb_alg_maxbits
= 128
167 .name
= "hmac(sha1)",
178 .sadb_alg_id
= SADB_AALG_SHA1HMAC
,
180 .sadb_alg_minbits
= 160,
181 .sadb_alg_maxbits
= 160
185 .name
= "hmac(sha256)",
196 .sadb_alg_id
= SADB_X_AALG_SHA2_256HMAC
,
198 .sadb_alg_minbits
= 256,
199 .sadb_alg_maxbits
= 256
203 .name
= "hmac(sha384)",
207 .icv_truncbits
= 192,
213 .sadb_alg_id
= SADB_X_AALG_SHA2_384HMAC
,
215 .sadb_alg_minbits
= 384,
216 .sadb_alg_maxbits
= 384
220 .name
= "hmac(sha512)",
224 .icv_truncbits
= 256,
230 .sadb_alg_id
= SADB_X_AALG_SHA2_512HMAC
,
232 .sadb_alg_minbits
= 512,
233 .sadb_alg_maxbits
= 512
237 .name
= "hmac(rmd160)",
248 .sadb_alg_id
= SADB_X_AALG_RIPEMD160HMAC
,
250 .sadb_alg_minbits
= 160,
251 .sadb_alg_maxbits
= 160
265 .sadb_alg_id
= SADB_X_AALG_AES_XCBC_MAC
,
267 .sadb_alg_minbits
= 128,
268 .sadb_alg_maxbits
= 128
273 static struct xfrm_algo_desc ealg_list
[] = {
275 .name
= "ecb(cipher_null)",
276 .compat
= "cipher_null",
286 .sadb_alg_id
= SADB_EALG_NULL
,
288 .sadb_alg_minbits
= 0,
289 .sadb_alg_maxbits
= 0
304 .sadb_alg_id
= SADB_EALG_DESCBC
,
306 .sadb_alg_minbits
= 64,
307 .sadb_alg_maxbits
= 64
311 .name
= "cbc(des3_ede)",
312 .compat
= "des3_ede",
322 .sadb_alg_id
= SADB_EALG_3DESCBC
,
324 .sadb_alg_minbits
= 192,
325 .sadb_alg_maxbits
= 192
329 .name
= "cbc(cast5)",
340 .sadb_alg_id
= SADB_X_EALG_CASTCBC
,
342 .sadb_alg_minbits
= 40,
343 .sadb_alg_maxbits
= 128
347 .name
= "cbc(blowfish)",
348 .compat
= "blowfish",
358 .sadb_alg_id
= SADB_X_EALG_BLOWFISHCBC
,
360 .sadb_alg_minbits
= 40,
361 .sadb_alg_maxbits
= 448
376 .sadb_alg_id
= SADB_X_EALG_AESCBC
,
378 .sadb_alg_minbits
= 128,
379 .sadb_alg_maxbits
= 256
383 .name
= "cbc(serpent)",
394 .sadb_alg_id
= SADB_X_EALG_SERPENTCBC
,
396 .sadb_alg_minbits
= 128,
397 .sadb_alg_maxbits
= 256,
401 .name
= "cbc(camellia)",
402 .compat
= "camellia",
412 .sadb_alg_id
= SADB_X_EALG_CAMELLIACBC
,
414 .sadb_alg_minbits
= 128,
415 .sadb_alg_maxbits
= 256
419 .name
= "cbc(twofish)",
430 .sadb_alg_id
= SADB_X_EALG_TWOFISHCBC
,
432 .sadb_alg_minbits
= 128,
433 .sadb_alg_maxbits
= 256
437 .name
= "rfc3686(ctr(aes))",
442 .defkeybits
= 160, /* 128-bit key + 32-bit nonce */
447 .sadb_alg_id
= SADB_X_EALG_AESCTR
,
449 .sadb_alg_minbits
= 128,
450 .sadb_alg_maxbits
= 256
455 static struct xfrm_algo_desc calg_list
[] = {
463 .desc
= { .sadb_alg_id
= SADB_X_CALG_DEFLATE
}
472 .desc
= { .sadb_alg_id
= SADB_X_CALG_LZS
}
481 .desc
= { .sadb_alg_id
= SADB_X_CALG_LZJH
}
485 static inline int aead_entries(void)
487 return ARRAY_SIZE(aead_list
);
490 static inline int aalg_entries(void)
492 return ARRAY_SIZE(aalg_list
);
495 static inline int ealg_entries(void)
497 return ARRAY_SIZE(ealg_list
);
500 static inline int calg_entries(void)
502 return ARRAY_SIZE(calg_list
);
505 struct xfrm_algo_list
{
506 struct xfrm_algo_desc
*algs
;
512 static const struct xfrm_algo_list xfrm_aead_list
= {
514 .entries
= ARRAY_SIZE(aead_list
),
515 .type
= CRYPTO_ALG_TYPE_AEAD
,
516 .mask
= CRYPTO_ALG_TYPE_MASK
,
519 static const struct xfrm_algo_list xfrm_aalg_list
= {
521 .entries
= ARRAY_SIZE(aalg_list
),
522 .type
= CRYPTO_ALG_TYPE_HASH
,
523 .mask
= CRYPTO_ALG_TYPE_HASH_MASK
,
526 static const struct xfrm_algo_list xfrm_ealg_list
= {
528 .entries
= ARRAY_SIZE(ealg_list
),
529 .type
= CRYPTO_ALG_TYPE_BLKCIPHER
,
530 .mask
= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
,
533 static const struct xfrm_algo_list xfrm_calg_list
= {
535 .entries
= ARRAY_SIZE(calg_list
),
536 .type
= CRYPTO_ALG_TYPE_COMPRESS
,
537 .mask
= CRYPTO_ALG_TYPE_MASK
,
540 static struct xfrm_algo_desc
*xfrm_find_algo(
541 const struct xfrm_algo_list
*algo_list
,
542 int match(const struct xfrm_algo_desc
*entry
, const void *data
),
543 const void *data
, int probe
)
545 struct xfrm_algo_desc
*list
= algo_list
->algs
;
548 for (i
= 0; i
< algo_list
->entries
; i
++) {
549 if (!match(list
+ i
, data
))
552 if (list
[i
].available
)
558 status
= crypto_has_alg(list
[i
].name
, algo_list
->type
,
563 list
[i
].available
= status
;
569 static int xfrm_alg_id_match(const struct xfrm_algo_desc
*entry
,
572 return entry
->desc
.sadb_alg_id
== (unsigned long)data
;
575 struct xfrm_algo_desc
*xfrm_aalg_get_byid(int alg_id
)
577 return xfrm_find_algo(&xfrm_aalg_list
, xfrm_alg_id_match
,
578 (void *)(unsigned long)alg_id
, 1);
580 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid
);
582 struct xfrm_algo_desc
*xfrm_ealg_get_byid(int alg_id
)
584 return xfrm_find_algo(&xfrm_ealg_list
, xfrm_alg_id_match
,
585 (void *)(unsigned long)alg_id
, 1);
587 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid
);
589 struct xfrm_algo_desc
*xfrm_calg_get_byid(int alg_id
)
591 return xfrm_find_algo(&xfrm_calg_list
, xfrm_alg_id_match
,
592 (void *)(unsigned long)alg_id
, 1);
594 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid
);
596 static int xfrm_alg_name_match(const struct xfrm_algo_desc
*entry
,
599 const char *name
= data
;
601 return name
&& (!strcmp(name
, entry
->name
) ||
602 (entry
->compat
&& !strcmp(name
, entry
->compat
)));
605 struct xfrm_algo_desc
*xfrm_aalg_get_byname(char *name
, int probe
)
607 return xfrm_find_algo(&xfrm_aalg_list
, xfrm_alg_name_match
, name
,
610 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname
);
612 struct xfrm_algo_desc
*xfrm_ealg_get_byname(char *name
, int probe
)
614 return xfrm_find_algo(&xfrm_ealg_list
, xfrm_alg_name_match
, name
,
617 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname
);
619 struct xfrm_algo_desc
*xfrm_calg_get_byname(char *name
, int probe
)
621 return xfrm_find_algo(&xfrm_calg_list
, xfrm_alg_name_match
, name
,
624 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname
);
626 struct xfrm_aead_name
{
631 static int xfrm_aead_name_match(const struct xfrm_algo_desc
*entry
,
634 const struct xfrm_aead_name
*aead
= data
;
635 const char *name
= aead
->name
;
637 return aead
->icvbits
== entry
->uinfo
.aead
.icv_truncbits
&& name
&&
638 !strcmp(name
, entry
->name
);
641 struct xfrm_algo_desc
*xfrm_aead_get_byname(char *name
, int icv_len
, int probe
)
643 struct xfrm_aead_name data
= {
648 return xfrm_find_algo(&xfrm_aead_list
, xfrm_aead_name_match
, &data
,
651 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname
);
653 struct xfrm_algo_desc
*xfrm_aalg_get_byidx(unsigned int idx
)
655 if (idx
>= aalg_entries())
658 return &aalg_list
[idx
];
660 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx
);
662 struct xfrm_algo_desc
*xfrm_ealg_get_byidx(unsigned int idx
)
664 if (idx
>= ealg_entries())
667 return &ealg_list
[idx
];
669 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx
);
672 * Probe for the availability of crypto algorithms, and set the available
673 * flag for any algorithms found on the system. This is typically called by
674 * pfkey during userspace SA add, update or register.
676 void xfrm_probe_algs(void)
680 BUG_ON(in_softirq());
682 for (i
= 0; i
< aalg_entries(); i
++) {
683 status
= crypto_has_hash(aalg_list
[i
].name
, 0,
685 if (aalg_list
[i
].available
!= status
)
686 aalg_list
[i
].available
= status
;
689 for (i
= 0; i
< ealg_entries(); i
++) {
690 status
= crypto_has_blkcipher(ealg_list
[i
].name
, 0,
692 if (ealg_list
[i
].available
!= status
)
693 ealg_list
[i
].available
= status
;
696 for (i
= 0; i
< calg_entries(); i
++) {
697 status
= crypto_has_comp(calg_list
[i
].name
, 0,
699 if (calg_list
[i
].available
!= status
)
700 calg_list
[i
].available
= status
;
703 EXPORT_SYMBOL_GPL(xfrm_probe_algs
);
705 int xfrm_count_auth_supported(void)
709 for (i
= 0, n
= 0; i
< aalg_entries(); i
++)
710 if (aalg_list
[i
].available
)
714 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported
);
716 int xfrm_count_enc_supported(void)
720 for (i
= 0, n
= 0; i
< ealg_entries(); i
++)
721 if (ealg_list
[i
].available
)
725 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported
);
727 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
729 void *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
732 skb
->data_len
+= len
;
735 return skb_put(tail
, len
);
737 EXPORT_SYMBOL_GPL(pskb_put
);