1/* 2 * xfrm algorithm interface 3 * 4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 5 * 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. 10 */ 11 12#include <linux/module.h> 13#include <linux/kernel.h> 14#include <linux/pfkeyv2.h> 15#include <linux/crypto.h> 16#include <net/xfrm.h> 17#if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || \ 18 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) || \ 22 defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE) 23#include <net/esp.h> 24#endif 25#include <asm/scatterlist.h> 26 27/* 28 * Algorithms supported by IPsec. These entries contain properties which 29 * are used in key negotiation and xfrm processing, and are used to verify 30 * that instantiated crypto transforms have correct parameters for IPsec 31 * purposes. 32 */ 33static struct xfrm_algo_desc aalg_list[] = { 34{ 35 .name = "hmac(digest_null)", 36 .compat = "digest_null", 37 38 .uinfo = { 39 .auth = { 40 .icv_truncbits = 0, 41 .icv_fullbits = 0, 42 } 43 }, 44 45 .desc = { 46 .sadb_alg_id = SADB_X_AALG_NULL, 47 .sadb_alg_ivlen = 0, 48 .sadb_alg_minbits = 0, 49 .sadb_alg_maxbits = 0 50 } 51}, 52{ 53 .name = "hmac(md5)", 54 .compat = "md5", 55 56 .uinfo = { 57 .auth = { 58 .icv_truncbits = 96, 59 .icv_fullbits = 128, 60 } 61 }, 62 63 .desc = { 64 .sadb_alg_id = SADB_AALG_MD5HMAC, 65 .sadb_alg_ivlen = 0, 66 .sadb_alg_minbits = 128, 67 .sadb_alg_maxbits = 128 68 } 69}, 70{ 71 .name = "hmac(sha1)", 72 .compat = "sha1", 73 74 .uinfo = { 75 .auth = { 76 .icv_truncbits = 96, 77 .icv_fullbits = 160, 78 } 79 }, 80 81 .desc = { 82 .sadb_alg_id = SADB_AALG_SHA1HMAC, 83 .sadb_alg_ivlen = 0, 84 .sadb_alg_minbits = 160, 85 .sadb_alg_maxbits = 160 86 } 87}, 88{ 89 .name = "hmac(sha256)", 90 .compat = "sha256", 91 92 .uinfo = { 93 .auth = { 94 .icv_truncbits = 96, 95 .icv_fullbits = 256, 96 } 97 }, 98 99 .desc = { 100 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC, 101 .sadb_alg_ivlen = 0, 102 .sadb_alg_minbits = 256, 103 .sadb_alg_maxbits = 256 104 } 105}, 106{ 107 .name = "hmac(ripemd160)", 108 .compat = "ripemd160", 109 110 .uinfo = { 111 .auth = { 112 .icv_truncbits = 96, 113 .icv_fullbits = 160, 114 } 115 }, 116 117 .desc = { 118 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC, 119 .sadb_alg_ivlen = 0, 120 .sadb_alg_minbits = 160, 121 .sadb_alg_maxbits = 160 122 } 123}, 124{ 125 .name = "xcbc(aes)", 126 127 .uinfo = { 128 .auth = { 129 .icv_truncbits = 96, 130 .icv_fullbits = 128, 131 } 132 }, 133 134 .desc = { 135 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC, 136 .sadb_alg_ivlen = 0, 137 .sadb_alg_minbits = 128, 138 .sadb_alg_maxbits = 128 139 } 140}, 141}; 142 143static struct xfrm_algo_desc ealg_list[] = { 144{ 145 .name = "ecb(cipher_null)", 146 .compat = "cipher_null", 147 148 .uinfo = { 149 .encr = { 150 .blockbits = 8, 151 .defkeybits = 0, 152 } 153 }, 154 155 .desc = { 156 .sadb_alg_id = SADB_EALG_NULL, 157 .sadb_alg_ivlen = 0, 158 .sadb_alg_minbits = 0, 159 .sadb_alg_maxbits = 0 160 } 161}, 162{ 163 .name = "cbc(des)", 164 .compat = "des", 165 166 .uinfo = { 167 .encr = { 168 .blockbits = 64, 169 .defkeybits = 64, 170 } 171 }, 172 173 .desc = { 174 .sadb_alg_id = SADB_EALG_DESCBC, 175 .sadb_alg_ivlen = 8, 176 .sadb_alg_minbits = 64, 177 .sadb_alg_maxbits = 64 178 } 179}, 180{ 181 .name = "cbc(des3_ede)", 182 .compat = "des3_ede", 183 184 .uinfo = { 185 .encr = { 186 .blockbits = 64, 187 .defkeybits = 192, 188 } 189 }, 190 191 .desc = { 192 .sadb_alg_id = SADB_EALG_3DESCBC, 193 .sadb_alg_ivlen = 8, 194 .sadb_alg_minbits = 192, 195 .sadb_alg_maxbits = 192 196 } 197}, 198{ 199 .name = "cbc(cast128)", 200 .compat = "cast128", 201 202 .uinfo = { 203 .encr = { 204 .blockbits = 64, 205 .defkeybits = 128, 206 } 207 }, 208 209 .desc = { 210 .sadb_alg_id = SADB_X_EALG_CASTCBC, 211 .sadb_alg_ivlen = 8, 212 .sadb_alg_minbits = 40, 213 .sadb_alg_maxbits = 128 214 } 215}, 216{ 217 .name = "cbc(blowfish)", 218 .compat = "blowfish", 219 220 .uinfo = { 221 .encr = { 222 .blockbits = 64, 223 .defkeybits = 128, 224 } 225 }, 226 227 .desc = { 228 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC, 229 .sadb_alg_ivlen = 8, 230 .sadb_alg_minbits = 40, 231 .sadb_alg_maxbits = 448 232 } 233}, 234{ 235 .name = "cbc(aes)", 236 .compat = "aes", 237 238 .uinfo = { 239 .encr = { 240 .blockbits = 128, 241 .defkeybits = 128, 242 } 243 }, 244 245 .desc = { 246 .sadb_alg_id = SADB_X_EALG_AESCBC, 247 .sadb_alg_ivlen = 8, 248 .sadb_alg_minbits = 128, 249 .sadb_alg_maxbits = 256 250 } 251}, 252{ 253 .name = "cbc(serpent)", 254 .compat = "serpent", 255 256 .uinfo = { 257 .encr = { 258 .blockbits = 128, 259 .defkeybits = 128, 260 } 261 }, 262 263 .desc = { 264 .sadb_alg_id = SADB_X_EALG_SERPENTCBC, 265 .sadb_alg_ivlen = 8, 266 .sadb_alg_minbits = 128, 267 .sadb_alg_maxbits = 256, 268 } 269}, 270{ 271 .name = "cbc(camellia)", 272 273 .uinfo = { 274 .encr = { 275 .blockbits = 128, 276 .defkeybits = 128, 277 } 278 }, 279 280 .desc = { 281 .sadb_alg_id = SADB_X_EALG_CAMELLIACBC, 282 .sadb_alg_ivlen = 8, 283 .sadb_alg_minbits = 128, 284 .sadb_alg_maxbits = 256 285 } 286}, 287{ 288 .name = "cbc(twofish)", 289 .compat = "twofish", 290 291 .uinfo = { 292 .encr = { 293 .blockbits = 128, 294 .defkeybits = 128, 295 } 296 }, 297 298 .desc = { 299 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC, 300 .sadb_alg_ivlen = 8, 301 .sadb_alg_minbits = 128, 302 .sadb_alg_maxbits = 256 303 } 304}, 305}; 306 307static struct xfrm_algo_desc calg_list[] = { 308{ 309 .name = "deflate", 310 .uinfo = { 311 .comp = { 312 .threshold = 90, 313 } 314 }, 315 .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE } 316}, 317{ 318 .name = "lzs", 319 .uinfo = { 320 .comp = { 321 .threshold = 90, 322 } 323 }, 324 .desc = { .sadb_alg_id = SADB_X_CALG_LZS } 325}, 326{ 327 .name = "lzjh", 328 .uinfo = { 329 .comp = { 330 .threshold = 50, 331 } 332 }, 333 .desc = { .sadb_alg_id = SADB_X_CALG_LZJH } 334}, 335}; 336 337static inline int aalg_entries(void) 338{ 339 return ARRAY_SIZE(aalg_list); 340} 341 342static inline int ealg_entries(void) 343{ 344 return ARRAY_SIZE(ealg_list); 345} 346 347static inline int calg_entries(void) 348{ 349 return ARRAY_SIZE(calg_list); 350} 351 352struct xfrm_algo_list { 353 struct xfrm_algo_desc *algs; 354 int entries; 355 u32 type; 356 u32 mask; 357}; 358 359static const struct xfrm_algo_list xfrm_aalg_list = { 360 .algs = aalg_list, 361 .entries = ARRAY_SIZE(aalg_list), 362 .type = CRYPTO_ALG_TYPE_HASH, 363 .mask = CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC, 364}; 365 366static const struct xfrm_algo_list xfrm_ealg_list = { 367 .algs = ealg_list, 368 .entries = ARRAY_SIZE(ealg_list), 369 .type = CRYPTO_ALG_TYPE_BLKCIPHER, 370 .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC, 371}; 372 373static const struct xfrm_algo_list xfrm_calg_list = { 374 .algs = calg_list, 375 .entries = ARRAY_SIZE(calg_list), 376 .type = CRYPTO_ALG_TYPE_COMPRESS, 377 .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC, 378}; 379 380static struct xfrm_algo_desc *xfrm_find_algo( 381 const struct xfrm_algo_list *algo_list, 382 int match(const struct xfrm_algo_desc *entry, const void *data), 383 const void *data, int probe) 384{ 385 struct xfrm_algo_desc *list = algo_list->algs; 386 int i, status; 387 388 for (i = 0; i < algo_list->entries; i++) { 389 if (!match(list + i, data)) 390 continue; 391 392 if (list[i].available) 393 return &list[i]; 394 395 if (!probe) 396 break; 397 398 status = crypto_has_alg(list[i].name, algo_list->type, 399 algo_list->mask); 400 if (!status) 401 break; 402 403 list[i].available = status; 404 return &list[i]; 405 } 406 return NULL; 407} 408 409static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry, 410 const void *data) 411{ 412 return entry->desc.sadb_alg_id == (unsigned long)data; 413} 414 415struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id) 416{ 417 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match, 418 (void *)(unsigned long)alg_id, 1); 419} 420EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid); 421 422struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id) 423{ 424 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match, 425 (void *)(unsigned long)alg_id, 1); 426} 427EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid); 428 429struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id) 430{ 431 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match, 432 (void *)(unsigned long)alg_id, 1); 433} 434EXPORT_SYMBOL_GPL(xfrm_calg_get_byid); 435 436static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry, 437 const void *data) 438{ 439 const char *name = data; 440 441 return name && (!strcmp(name, entry->name) || 442 (entry->compat && !strcmp(name, entry->compat))); 443} 444 445struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe) 446{ 447 return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name, 448 probe); 449} 450EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname); 451 452struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe) 453{ 454 return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name, 455 probe); 456} 457EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname); 458 459struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe) 460{ 461 return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name, 462 probe); 463} 464EXPORT_SYMBOL_GPL(xfrm_calg_get_byname); 465 466struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx) 467{ 468 if (idx >= aalg_entries()) 469 return NULL; 470 471 return &aalg_list[idx]; 472} 473EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx); 474 475struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx) 476{ 477 if (idx >= ealg_entries()) 478 return NULL; 479 480 return &ealg_list[idx]; 481} 482EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx); 483 484/* 485 * Probe for the availability of crypto algorithms, and set the available 486 * flag for any algorithms found on the system. This is typically called by 487 * pfkey during userspace SA add, update or register. 488 */ 489void xfrm_probe_algs(void) 490{ 491#ifdef CONFIG_CRYPTO 492 int i, status; 493 494 BUG_ON(in_softirq()); 495 496 for (i = 0; i < aalg_entries(); i++) { 497 status = crypto_has_hash(aalg_list[i].name, 0, 498 CRYPTO_ALG_ASYNC); 499 if (aalg_list[i].available != status) 500 aalg_list[i].available = status; 501 } 502 503 for (i = 0; i < ealg_entries(); i++) { 504 status = crypto_has_blkcipher(ealg_list[i].name, 0, 505 CRYPTO_ALG_ASYNC); 506 if (ealg_list[i].available != status) 507 ealg_list[i].available = status; 508 } 509 510 for (i = 0; i < calg_entries(); i++) { 511 status = crypto_has_comp(calg_list[i].name, 0, 512 CRYPTO_ALG_ASYNC); 513 if (calg_list[i].available != status) 514 calg_list[i].available = status; 515 } 516#endif 517} 518EXPORT_SYMBOL_GPL(xfrm_probe_algs); 519 520int xfrm_count_auth_supported(void) 521{ 522 int i, n; 523 524 for (i = 0, n = 0; i < aalg_entries(); i++) 525 if (aalg_list[i].available) 526 n++; 527 return n; 528} 529EXPORT_SYMBOL_GPL(xfrm_count_auth_supported); 530 531int xfrm_count_enc_supported(void) 532{ 533 int i, n; 534 535 for (i = 0, n = 0; i < ealg_entries(); i++) 536 if (ealg_list[i].available) 537 n++; 538 return n; 539} 540EXPORT_SYMBOL_GPL(xfrm_count_enc_supported); 541 542/* Move to common area: it is shared with AH. */ 543 544int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc, 545 int offset, int len, icv_update_fn_t icv_update) 546{ 547 int start = skb_headlen(skb); 548 int i, copy = start - offset; 549 int err; 550 struct scatterlist sg; 551 552 /* Checksum header. */ 553 if (copy > 0) { 554 if (copy > len) 555 copy = len; 556 557 sg.page = virt_to_page(skb->data + offset); 558 sg.offset = (unsigned long)(skb->data + offset) % PAGE_SIZE; 559 sg.length = copy; 560 561 err = icv_update(desc, &sg, copy); 562 if (unlikely(err)) 563 return err; 564 565 if ((len -= copy) == 0) 566 return 0; 567 offset += copy; 568 } 569 570 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 571 int end; 572 573 BUG_TRAP(start <= offset + len); 574 575 end = start + skb_shinfo(skb)->frags[i].size; 576 if ((copy = end - offset) > 0) { 577 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 578 579 if (copy > len) 580 copy = len; 581 582 sg.page = frag->page; 583 sg.offset = frag->page_offset + offset-start; 584 sg.length = copy; 585 586 err = icv_update(desc, &sg, copy); 587 if (unlikely(err)) 588 return err; 589 590 if (!(len -= copy)) 591 return 0; 592 offset += copy; 593 } 594 start = end; 595 } 596 597 if (skb_shinfo(skb)->frag_list) { 598 struct sk_buff *list = skb_shinfo(skb)->frag_list; 599 600 for (; list; list = list->next) { 601 int end; 602 603 BUG_TRAP(start <= offset + len); 604 605 end = start + list->len; 606 if ((copy = end - offset) > 0) { 607 if (copy > len) 608 copy = len; 609 err = skb_icv_walk(list, desc, offset-start, 610 copy, icv_update); 611 if (unlikely(err)) 612 return err; 613 if ((len -= copy) == 0) 614 return 0; 615 offset += copy; 616 } 617 start = end; 618 } 619 } 620 BUG_ON(len); 621 return 0; 622} 623EXPORT_SYMBOL_GPL(skb_icv_walk); 624 625#if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || \ 626 defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE) 627 628void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 629{ 630 if (tail != skb) { 631 skb->data_len += len; 632 skb->len += len; 633 } 634 return skb_put(tail, len); 635} 636EXPORT_SYMBOL_GPL(pskb_put); 637#endif 638