xfrm: Add SHA384 and SHA512 HMAC authentication algorithms to XFRM
[linux-2.6/kvm.git] / net / xfrm / xfrm_algo.c
blobef8d61d50462e2e5ca7aa21db8607692d97201d8
1 /*
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)
9 * any later version.
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>
17 #include <net/xfrm.h>
18 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
19 #include <net/ah.h>
20 #endif
21 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
22 #include <net/esp.h>
23 #endif
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
29 * purposes.
31 static struct xfrm_algo_desc aead_list[] = {
33 .name = "rfc4106(gcm(aes))",
35 .uinfo = {
36 .aead = {
37 .icv_truncbits = 64,
41 .desc = {
42 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
43 .sadb_alg_ivlen = 8,
44 .sadb_alg_minbits = 128,
45 .sadb_alg_maxbits = 256
49 .name = "rfc4106(gcm(aes))",
51 .uinfo = {
52 .aead = {
53 .icv_truncbits = 96,
57 .desc = {
58 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
59 .sadb_alg_ivlen = 8,
60 .sadb_alg_minbits = 128,
61 .sadb_alg_maxbits = 256
65 .name = "rfc4106(gcm(aes))",
67 .uinfo = {
68 .aead = {
69 .icv_truncbits = 128,
73 .desc = {
74 .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
75 .sadb_alg_ivlen = 8,
76 .sadb_alg_minbits = 128,
77 .sadb_alg_maxbits = 256
81 .name = "rfc4309(ccm(aes))",
83 .uinfo = {
84 .aead = {
85 .icv_truncbits = 64,
89 .desc = {
90 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
91 .sadb_alg_ivlen = 8,
92 .sadb_alg_minbits = 128,
93 .sadb_alg_maxbits = 256
97 .name = "rfc4309(ccm(aes))",
99 .uinfo = {
100 .aead = {
101 .icv_truncbits = 96,
105 .desc = {
106 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
107 .sadb_alg_ivlen = 8,
108 .sadb_alg_minbits = 128,
109 .sadb_alg_maxbits = 256
113 .name = "rfc4309(ccm(aes))",
115 .uinfo = {
116 .aead = {
117 .icv_truncbits = 128,
121 .desc = {
122 .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
123 .sadb_alg_ivlen = 8,
124 .sadb_alg_minbits = 128,
125 .sadb_alg_maxbits = 256
130 static struct xfrm_algo_desc aalg_list[] = {
132 .name = "digest_null",
134 .uinfo = {
135 .auth = {
136 .icv_truncbits = 0,
137 .icv_fullbits = 0,
141 .desc = {
142 .sadb_alg_id = SADB_X_AALG_NULL,
143 .sadb_alg_ivlen = 0,
144 .sadb_alg_minbits = 0,
145 .sadb_alg_maxbits = 0
149 .name = "hmac(md5)",
150 .compat = "md5",
152 .uinfo = {
153 .auth = {
154 .icv_truncbits = 96,
155 .icv_fullbits = 128,
159 .desc = {
160 .sadb_alg_id = SADB_AALG_MD5HMAC,
161 .sadb_alg_ivlen = 0,
162 .sadb_alg_minbits = 128,
163 .sadb_alg_maxbits = 128
167 .name = "hmac(sha1)",
168 .compat = "sha1",
170 .uinfo = {
171 .auth = {
172 .icv_truncbits = 96,
173 .icv_fullbits = 160,
177 .desc = {
178 .sadb_alg_id = SADB_AALG_SHA1HMAC,
179 .sadb_alg_ivlen = 0,
180 .sadb_alg_minbits = 160,
181 .sadb_alg_maxbits = 160
185 .name = "hmac(sha256)",
186 .compat = "sha256",
188 .uinfo = {
189 .auth = {
190 .icv_truncbits = 96,
191 .icv_fullbits = 256,
195 .desc = {
196 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
197 .sadb_alg_ivlen = 0,
198 .sadb_alg_minbits = 256,
199 .sadb_alg_maxbits = 256
203 .name = "hmac(sha384)",
205 .uinfo = {
206 .auth = {
207 .icv_truncbits = 192,
208 .icv_fullbits = 384,
212 .desc = {
213 .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
214 .sadb_alg_ivlen = 0,
215 .sadb_alg_minbits = 384,
216 .sadb_alg_maxbits = 384
220 .name = "hmac(sha512)",
222 .uinfo = {
223 .auth = {
224 .icv_truncbits = 256,
225 .icv_fullbits = 512,
229 .desc = {
230 .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
231 .sadb_alg_ivlen = 0,
232 .sadb_alg_minbits = 512,
233 .sadb_alg_maxbits = 512
237 .name = "hmac(rmd160)",
238 .compat = "rmd160",
240 .uinfo = {
241 .auth = {
242 .icv_truncbits = 96,
243 .icv_fullbits = 160,
247 .desc = {
248 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
249 .sadb_alg_ivlen = 0,
250 .sadb_alg_minbits = 160,
251 .sadb_alg_maxbits = 160
255 .name = "xcbc(aes)",
257 .uinfo = {
258 .auth = {
259 .icv_truncbits = 96,
260 .icv_fullbits = 128,
264 .desc = {
265 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
266 .sadb_alg_ivlen = 0,
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",
278 .uinfo = {
279 .encr = {
280 .blockbits = 8,
281 .defkeybits = 0,
285 .desc = {
286 .sadb_alg_id = SADB_EALG_NULL,
287 .sadb_alg_ivlen = 0,
288 .sadb_alg_minbits = 0,
289 .sadb_alg_maxbits = 0
293 .name = "cbc(des)",
294 .compat = "des",
296 .uinfo = {
297 .encr = {
298 .blockbits = 64,
299 .defkeybits = 64,
303 .desc = {
304 .sadb_alg_id = SADB_EALG_DESCBC,
305 .sadb_alg_ivlen = 8,
306 .sadb_alg_minbits = 64,
307 .sadb_alg_maxbits = 64
311 .name = "cbc(des3_ede)",
312 .compat = "des3_ede",
314 .uinfo = {
315 .encr = {
316 .blockbits = 64,
317 .defkeybits = 192,
321 .desc = {
322 .sadb_alg_id = SADB_EALG_3DESCBC,
323 .sadb_alg_ivlen = 8,
324 .sadb_alg_minbits = 192,
325 .sadb_alg_maxbits = 192
329 .name = "cbc(cast5)",
330 .compat = "cast5",
332 .uinfo = {
333 .encr = {
334 .blockbits = 64,
335 .defkeybits = 128,
339 .desc = {
340 .sadb_alg_id = SADB_X_EALG_CASTCBC,
341 .sadb_alg_ivlen = 8,
342 .sadb_alg_minbits = 40,
343 .sadb_alg_maxbits = 128
347 .name = "cbc(blowfish)",
348 .compat = "blowfish",
350 .uinfo = {
351 .encr = {
352 .blockbits = 64,
353 .defkeybits = 128,
357 .desc = {
358 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
359 .sadb_alg_ivlen = 8,
360 .sadb_alg_minbits = 40,
361 .sadb_alg_maxbits = 448
365 .name = "cbc(aes)",
366 .compat = "aes",
368 .uinfo = {
369 .encr = {
370 .blockbits = 128,
371 .defkeybits = 128,
375 .desc = {
376 .sadb_alg_id = SADB_X_EALG_AESCBC,
377 .sadb_alg_ivlen = 8,
378 .sadb_alg_minbits = 128,
379 .sadb_alg_maxbits = 256
383 .name = "cbc(serpent)",
384 .compat = "serpent",
386 .uinfo = {
387 .encr = {
388 .blockbits = 128,
389 .defkeybits = 128,
393 .desc = {
394 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
395 .sadb_alg_ivlen = 8,
396 .sadb_alg_minbits = 128,
397 .sadb_alg_maxbits = 256,
401 .name = "cbc(camellia)",
403 .uinfo = {
404 .encr = {
405 .blockbits = 128,
406 .defkeybits = 128,
410 .desc = {
411 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
412 .sadb_alg_ivlen = 8,
413 .sadb_alg_minbits = 128,
414 .sadb_alg_maxbits = 256
418 .name = "cbc(twofish)",
419 .compat = "twofish",
421 .uinfo = {
422 .encr = {
423 .blockbits = 128,
424 .defkeybits = 128,
428 .desc = {
429 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
430 .sadb_alg_ivlen = 8,
431 .sadb_alg_minbits = 128,
432 .sadb_alg_maxbits = 256
436 .name = "rfc3686(ctr(aes))",
438 .uinfo = {
439 .encr = {
440 .blockbits = 128,
441 .defkeybits = 160, /* 128-bit key + 32-bit nonce */
445 .desc = {
446 .sadb_alg_id = SADB_X_EALG_AESCTR,
447 .sadb_alg_ivlen = 8,
448 .sadb_alg_minbits = 128,
449 .sadb_alg_maxbits = 256
454 static struct xfrm_algo_desc calg_list[] = {
456 .name = "deflate",
457 .uinfo = {
458 .comp = {
459 .threshold = 90,
462 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
465 .name = "lzs",
466 .uinfo = {
467 .comp = {
468 .threshold = 90,
471 .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
474 .name = "lzjh",
475 .uinfo = {
476 .comp = {
477 .threshold = 50,
480 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
484 static inline int aead_entries(void)
486 return ARRAY_SIZE(aead_list);
489 static inline int aalg_entries(void)
491 return ARRAY_SIZE(aalg_list);
494 static inline int ealg_entries(void)
496 return ARRAY_SIZE(ealg_list);
499 static inline int calg_entries(void)
501 return ARRAY_SIZE(calg_list);
504 struct xfrm_algo_list {
505 struct xfrm_algo_desc *algs;
506 int entries;
507 u32 type;
508 u32 mask;
511 static const struct xfrm_algo_list xfrm_aead_list = {
512 .algs = aead_list,
513 .entries = ARRAY_SIZE(aead_list),
514 .type = CRYPTO_ALG_TYPE_AEAD,
515 .mask = CRYPTO_ALG_TYPE_MASK,
518 static const struct xfrm_algo_list xfrm_aalg_list = {
519 .algs = aalg_list,
520 .entries = ARRAY_SIZE(aalg_list),
521 .type = CRYPTO_ALG_TYPE_HASH,
522 .mask = CRYPTO_ALG_TYPE_HASH_MASK,
525 static const struct xfrm_algo_list xfrm_ealg_list = {
526 .algs = ealg_list,
527 .entries = ARRAY_SIZE(ealg_list),
528 .type = CRYPTO_ALG_TYPE_BLKCIPHER,
529 .mask = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
532 static const struct xfrm_algo_list xfrm_calg_list = {
533 .algs = calg_list,
534 .entries = ARRAY_SIZE(calg_list),
535 .type = CRYPTO_ALG_TYPE_COMPRESS,
536 .mask = CRYPTO_ALG_TYPE_MASK,
539 static struct xfrm_algo_desc *xfrm_find_algo(
540 const struct xfrm_algo_list *algo_list,
541 int match(const struct xfrm_algo_desc *entry, const void *data),
542 const void *data, int probe)
544 struct xfrm_algo_desc *list = algo_list->algs;
545 int i, status;
547 for (i = 0; i < algo_list->entries; i++) {
548 if (!match(list + i, data))
549 continue;
551 if (list[i].available)
552 return &list[i];
554 if (!probe)
555 break;
557 status = crypto_has_alg(list[i].name, algo_list->type,
558 algo_list->mask);
559 if (!status)
560 break;
562 list[i].available = status;
563 return &list[i];
565 return NULL;
568 static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
569 const void *data)
571 return entry->desc.sadb_alg_id == (unsigned long)data;
574 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
576 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
577 (void *)(unsigned long)alg_id, 1);
579 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
581 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
583 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
584 (void *)(unsigned long)alg_id, 1);
586 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
588 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
590 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
591 (void *)(unsigned long)alg_id, 1);
593 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
595 static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
596 const void *data)
598 const char *name = data;
600 return name && (!strcmp(name, entry->name) ||
601 (entry->compat && !strcmp(name, entry->compat)));
604 struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
606 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
607 probe);
609 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
611 struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
613 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
614 probe);
616 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
618 struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
620 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
621 probe);
623 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
625 struct xfrm_aead_name {
626 const char *name;
627 int icvbits;
630 static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
631 const void *data)
633 const struct xfrm_aead_name *aead = data;
634 const char *name = aead->name;
636 return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
637 !strcmp(name, entry->name);
640 struct xfrm_algo_desc *xfrm_aead_get_byname(char *name, int icv_len, int probe)
642 struct xfrm_aead_name data = {
643 .name = name,
644 .icvbits = icv_len,
647 return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
648 probe);
650 EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
652 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
654 if (idx >= aalg_entries())
655 return NULL;
657 return &aalg_list[idx];
659 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
661 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
663 if (idx >= ealg_entries())
664 return NULL;
666 return &ealg_list[idx];
668 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
671 * Probe for the availability of crypto algorithms, and set the available
672 * flag for any algorithms found on the system. This is typically called by
673 * pfkey during userspace SA add, update or register.
675 void xfrm_probe_algs(void)
677 int i, status;
679 BUG_ON(in_softirq());
681 for (i = 0; i < aalg_entries(); i++) {
682 status = crypto_has_hash(aalg_list[i].name, 0,
683 CRYPTO_ALG_ASYNC);
684 if (aalg_list[i].available != status)
685 aalg_list[i].available = status;
688 for (i = 0; i < ealg_entries(); i++) {
689 status = crypto_has_blkcipher(ealg_list[i].name, 0,
690 CRYPTO_ALG_ASYNC);
691 if (ealg_list[i].available != status)
692 ealg_list[i].available = status;
695 for (i = 0; i < calg_entries(); i++) {
696 status = crypto_has_comp(calg_list[i].name, 0,
697 CRYPTO_ALG_ASYNC);
698 if (calg_list[i].available != status)
699 calg_list[i].available = status;
702 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
704 int xfrm_count_auth_supported(void)
706 int i, n;
708 for (i = 0, n = 0; i < aalg_entries(); i++)
709 if (aalg_list[i].available)
710 n++;
711 return n;
713 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
715 int xfrm_count_enc_supported(void)
717 int i, n;
719 for (i = 0, n = 0; i < ealg_entries(); i++)
720 if (ealg_list[i].available)
721 n++;
722 return n;
724 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
726 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
728 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
730 if (tail != skb) {
731 skb->data_len += len;
732 skb->len += len;
734 return skb_put(tail, len);
736 EXPORT_SYMBOL_GPL(pskb_put);
737 #endif