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
= "hmac(digest_null)",
133 .compat
= "digest_null",
143 .sadb_alg_id
= SADB_X_AALG_NULL
,
145 .sadb_alg_minbits
= 0,
146 .sadb_alg_maxbits
= 0
161 .sadb_alg_id
= SADB_AALG_MD5HMAC
,
163 .sadb_alg_minbits
= 128,
164 .sadb_alg_maxbits
= 128
168 .name
= "hmac(sha1)",
179 .sadb_alg_id
= SADB_AALG_SHA1HMAC
,
181 .sadb_alg_minbits
= 160,
182 .sadb_alg_maxbits
= 160
186 .name
= "hmac(sha256)",
197 .sadb_alg_id
= SADB_X_AALG_SHA2_256HMAC
,
199 .sadb_alg_minbits
= 256,
200 .sadb_alg_maxbits
= 256
204 .name
= "hmac(ripemd160)",
205 .compat
= "ripemd160",
215 .sadb_alg_id
= SADB_X_AALG_RIPEMD160HMAC
,
217 .sadb_alg_minbits
= 160,
218 .sadb_alg_maxbits
= 160
232 .sadb_alg_id
= SADB_X_AALG_AES_XCBC_MAC
,
234 .sadb_alg_minbits
= 128,
235 .sadb_alg_maxbits
= 128
240 static struct xfrm_algo_desc ealg_list
[] = {
242 .name
= "ecb(cipher_null)",
243 .compat
= "cipher_null",
253 .sadb_alg_id
= SADB_EALG_NULL
,
255 .sadb_alg_minbits
= 0,
256 .sadb_alg_maxbits
= 0
271 .sadb_alg_id
= SADB_EALG_DESCBC
,
273 .sadb_alg_minbits
= 64,
274 .sadb_alg_maxbits
= 64
278 .name
= "cbc(des3_ede)",
279 .compat
= "des3_ede",
289 .sadb_alg_id
= SADB_EALG_3DESCBC
,
291 .sadb_alg_minbits
= 192,
292 .sadb_alg_maxbits
= 192
296 .name
= "cbc(cast128)",
307 .sadb_alg_id
= SADB_X_EALG_CASTCBC
,
309 .sadb_alg_minbits
= 40,
310 .sadb_alg_maxbits
= 128
314 .name
= "cbc(blowfish)",
315 .compat
= "blowfish",
325 .sadb_alg_id
= SADB_X_EALG_BLOWFISHCBC
,
327 .sadb_alg_minbits
= 40,
328 .sadb_alg_maxbits
= 448
343 .sadb_alg_id
= SADB_X_EALG_AESCBC
,
345 .sadb_alg_minbits
= 128,
346 .sadb_alg_maxbits
= 256
350 .name
= "cbc(serpent)",
361 .sadb_alg_id
= SADB_X_EALG_SERPENTCBC
,
363 .sadb_alg_minbits
= 128,
364 .sadb_alg_maxbits
= 256,
368 .name
= "cbc(camellia)",
378 .sadb_alg_id
= SADB_X_EALG_CAMELLIACBC
,
380 .sadb_alg_minbits
= 128,
381 .sadb_alg_maxbits
= 256
385 .name
= "cbc(twofish)",
396 .sadb_alg_id
= SADB_X_EALG_TWOFISHCBC
,
398 .sadb_alg_minbits
= 128,
399 .sadb_alg_maxbits
= 256
403 .name
= "rfc3686(ctr(aes))",
408 .defkeybits
= 160, /* 128-bit key + 32-bit nonce */
413 .sadb_alg_id
= SADB_X_EALG_AESCTR
,
415 .sadb_alg_minbits
= 128,
416 .sadb_alg_maxbits
= 256
421 static struct xfrm_algo_desc calg_list
[] = {
429 .desc
= { .sadb_alg_id
= SADB_X_CALG_DEFLATE
}
438 .desc
= { .sadb_alg_id
= SADB_X_CALG_LZS
}
447 .desc
= { .sadb_alg_id
= SADB_X_CALG_LZJH
}
451 static inline int aead_entries(void)
453 return ARRAY_SIZE(aead_list
);
456 static inline int aalg_entries(void)
458 return ARRAY_SIZE(aalg_list
);
461 static inline int ealg_entries(void)
463 return ARRAY_SIZE(ealg_list
);
466 static inline int calg_entries(void)
468 return ARRAY_SIZE(calg_list
);
471 struct xfrm_algo_list
{
472 struct xfrm_algo_desc
*algs
;
478 static const struct xfrm_algo_list xfrm_aead_list
= {
480 .entries
= ARRAY_SIZE(aead_list
),
481 .type
= CRYPTO_ALG_TYPE_AEAD
,
482 .mask
= CRYPTO_ALG_TYPE_MASK
,
485 static const struct xfrm_algo_list xfrm_aalg_list
= {
487 .entries
= ARRAY_SIZE(aalg_list
),
488 .type
= CRYPTO_ALG_TYPE_HASH
,
489 .mask
= CRYPTO_ALG_TYPE_HASH_MASK
,
492 static const struct xfrm_algo_list xfrm_ealg_list
= {
494 .entries
= ARRAY_SIZE(ealg_list
),
495 .type
= CRYPTO_ALG_TYPE_BLKCIPHER
,
496 .mask
= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
,
499 static const struct xfrm_algo_list xfrm_calg_list
= {
501 .entries
= ARRAY_SIZE(calg_list
),
502 .type
= CRYPTO_ALG_TYPE_COMPRESS
,
503 .mask
= CRYPTO_ALG_TYPE_MASK
,
506 static struct xfrm_algo_desc
*xfrm_find_algo(
507 const struct xfrm_algo_list
*algo_list
,
508 int match(const struct xfrm_algo_desc
*entry
, const void *data
),
509 const void *data
, int probe
)
511 struct xfrm_algo_desc
*list
= algo_list
->algs
;
514 for (i
= 0; i
< algo_list
->entries
; i
++) {
515 if (!match(list
+ i
, data
))
518 if (list
[i
].available
)
524 status
= crypto_has_alg(list
[i
].name
, algo_list
->type
,
529 list
[i
].available
= status
;
535 static int xfrm_alg_id_match(const struct xfrm_algo_desc
*entry
,
538 return entry
->desc
.sadb_alg_id
== (unsigned long)data
;
541 struct xfrm_algo_desc
*xfrm_aalg_get_byid(int alg_id
)
543 return xfrm_find_algo(&xfrm_aalg_list
, xfrm_alg_id_match
,
544 (void *)(unsigned long)alg_id
, 1);
546 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid
);
548 struct xfrm_algo_desc
*xfrm_ealg_get_byid(int alg_id
)
550 return xfrm_find_algo(&xfrm_ealg_list
, xfrm_alg_id_match
,
551 (void *)(unsigned long)alg_id
, 1);
553 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid
);
555 struct xfrm_algo_desc
*xfrm_calg_get_byid(int alg_id
)
557 return xfrm_find_algo(&xfrm_calg_list
, xfrm_alg_id_match
,
558 (void *)(unsigned long)alg_id
, 1);
560 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid
);
562 static int xfrm_alg_name_match(const struct xfrm_algo_desc
*entry
,
565 const char *name
= data
;
567 return name
&& (!strcmp(name
, entry
->name
) ||
568 (entry
->compat
&& !strcmp(name
, entry
->compat
)));
571 struct xfrm_algo_desc
*xfrm_aalg_get_byname(char *name
, int probe
)
573 return xfrm_find_algo(&xfrm_aalg_list
, xfrm_alg_name_match
, name
,
576 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname
);
578 struct xfrm_algo_desc
*xfrm_ealg_get_byname(char *name
, int probe
)
580 return xfrm_find_algo(&xfrm_ealg_list
, xfrm_alg_name_match
, name
,
583 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname
);
585 struct xfrm_algo_desc
*xfrm_calg_get_byname(char *name
, int probe
)
587 return xfrm_find_algo(&xfrm_calg_list
, xfrm_alg_name_match
, name
,
590 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname
);
592 struct xfrm_aead_name
{
597 static int xfrm_aead_name_match(const struct xfrm_algo_desc
*entry
,
600 const struct xfrm_aead_name
*aead
= data
;
601 const char *name
= aead
->name
;
603 return aead
->icvbits
== entry
->uinfo
.aead
.icv_truncbits
&& name
&&
604 !strcmp(name
, entry
->name
);
607 struct xfrm_algo_desc
*xfrm_aead_get_byname(char *name
, int icv_len
, int probe
)
609 struct xfrm_aead_name data
= {
614 return xfrm_find_algo(&xfrm_aead_list
, xfrm_aead_name_match
, &data
,
617 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname
);
619 struct xfrm_algo_desc
*xfrm_aalg_get_byidx(unsigned int idx
)
621 if (idx
>= aalg_entries())
624 return &aalg_list
[idx
];
626 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx
);
628 struct xfrm_algo_desc
*xfrm_ealg_get_byidx(unsigned int idx
)
630 if (idx
>= ealg_entries())
633 return &ealg_list
[idx
];
635 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx
);
638 * Probe for the availability of crypto algorithms, and set the available
639 * flag for any algorithms found on the system. This is typically called by
640 * pfkey during userspace SA add, update or register.
642 void xfrm_probe_algs(void)
646 BUG_ON(in_softirq());
648 for (i
= 0; i
< aalg_entries(); i
++) {
649 status
= crypto_has_hash(aalg_list
[i
].name
, 0,
651 if (aalg_list
[i
].available
!= status
)
652 aalg_list
[i
].available
= status
;
655 for (i
= 0; i
< ealg_entries(); i
++) {
656 status
= crypto_has_blkcipher(ealg_list
[i
].name
, 0,
658 if (ealg_list
[i
].available
!= status
)
659 ealg_list
[i
].available
= status
;
662 for (i
= 0; i
< calg_entries(); i
++) {
663 status
= crypto_has_comp(calg_list
[i
].name
, 0,
665 if (calg_list
[i
].available
!= status
)
666 calg_list
[i
].available
= status
;
669 EXPORT_SYMBOL_GPL(xfrm_probe_algs
);
671 int xfrm_count_auth_supported(void)
675 for (i
= 0, n
= 0; i
< aalg_entries(); i
++)
676 if (aalg_list
[i
].available
)
680 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported
);
682 int xfrm_count_enc_supported(void)
686 for (i
= 0, n
= 0; i
< ealg_entries(); i
++)
687 if (ealg_list
[i
].available
)
691 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported
);
693 /* Move to common area: it is shared with AH. */
695 int skb_icv_walk(const struct sk_buff
*skb
, struct hash_desc
*desc
,
696 int offset
, int len
, icv_update_fn_t icv_update
)
698 int start
= skb_headlen(skb
);
699 int i
, copy
= start
- offset
;
701 struct scatterlist sg
;
703 /* Checksum header. */
708 sg_init_one(&sg
, skb
->data
+ offset
, copy
);
710 err
= icv_update(desc
, &sg
, copy
);
714 if ((len
-= copy
) == 0)
719 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
722 BUG_TRAP(start
<= offset
+ len
);
724 end
= start
+ skb_shinfo(skb
)->frags
[i
].size
;
725 if ((copy
= end
- offset
) > 0) {
726 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
731 sg_init_table(&sg
, 1);
732 sg_set_page(&sg
, frag
->page
, copy
,
733 frag
->page_offset
+ offset
-start
);
735 err
= icv_update(desc
, &sg
, copy
);
746 if (skb_shinfo(skb
)->frag_list
) {
747 struct sk_buff
*list
= skb_shinfo(skb
)->frag_list
;
749 for (; list
; list
= list
->next
) {
752 BUG_TRAP(start
<= offset
+ len
);
754 end
= start
+ list
->len
;
755 if ((copy
= end
- offset
) > 0) {
758 err
= skb_icv_walk(list
, desc
, offset
-start
,
762 if ((len
-= copy
) == 0)
772 EXPORT_SYMBOL_GPL(skb_icv_walk
);
774 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
776 void *pskb_put(struct sk_buff
*skb
, struct sk_buff
*tail
, int len
)
779 skb
->data_len
+= len
;
782 return skb_put(tail
, len
);
784 EXPORT_SYMBOL_GPL(pskb_put
);