xfrm_algo.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * xfrm algorithm interface
  4. *
  5. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  6. */
  7. #include <crypto/aead.h>
  8. #include <crypto/hash.h>
  9. #include <crypto/skcipher.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pfkeyv2.h>
  13. #include <linux/crypto.h>
  14. #include <linux/scatterlist.h>
  15. #include <net/xfrm.h>
  16. #if IS_ENABLED(CONFIG_INET_ESP) || IS_ENABLED(CONFIG_INET6_ESP)
  17. #include <net/esp.h>
  18. #endif
  19. /*
  20. * Algorithms supported by IPsec. These entries contain properties which
  21. * are used in key negotiation and xfrm processing, and are used to verify
  22. * that instantiated crypto transforms have correct parameters for IPsec
  23. * purposes.
  24. */
  25. static struct xfrm_algo_desc aead_list[] = {
  26. {
  27. .name = "rfc4106(gcm(aes))",
  28. .uinfo = {
  29. .aead = {
  30. .geniv = "seqiv",
  31. .icv_truncbits = 64,
  32. }
  33. },
  34. .pfkey_supported = 1,
  35. .desc = {
  36. .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV8,
  37. .sadb_alg_ivlen = 8,
  38. .sadb_alg_minbits = 128,
  39. .sadb_alg_maxbits = 256
  40. }
  41. },
  42. {
  43. .name = "rfc4106(gcm(aes))",
  44. .uinfo = {
  45. .aead = {
  46. .geniv = "seqiv",
  47. .icv_truncbits = 96,
  48. }
  49. },
  50. .pfkey_supported = 1,
  51. .desc = {
  52. .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV12,
  53. .sadb_alg_ivlen = 8,
  54. .sadb_alg_minbits = 128,
  55. .sadb_alg_maxbits = 256
  56. }
  57. },
  58. {
  59. .name = "rfc4106(gcm(aes))",
  60. .uinfo = {
  61. .aead = {
  62. .geniv = "seqiv",
  63. .icv_truncbits = 128,
  64. }
  65. },
  66. .pfkey_supported = 1,
  67. .desc = {
  68. .sadb_alg_id = SADB_X_EALG_AES_GCM_ICV16,
  69. .sadb_alg_ivlen = 8,
  70. .sadb_alg_minbits = 128,
  71. .sadb_alg_maxbits = 256
  72. }
  73. },
  74. {
  75. .name = "rfc4309(ccm(aes))",
  76. .uinfo = {
  77. .aead = {
  78. .geniv = "seqiv",
  79. .icv_truncbits = 64,
  80. }
  81. },
  82. .pfkey_supported = 1,
  83. .desc = {
  84. .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV8,
  85. .sadb_alg_ivlen = 8,
  86. .sadb_alg_minbits = 128,
  87. .sadb_alg_maxbits = 256
  88. }
  89. },
  90. {
  91. .name = "rfc4309(ccm(aes))",
  92. .uinfo = {
  93. .aead = {
  94. .geniv = "seqiv",
  95. .icv_truncbits = 96,
  96. }
  97. },
  98. .pfkey_supported = 1,
  99. .desc = {
  100. .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV12,
  101. .sadb_alg_ivlen = 8,
  102. .sadb_alg_minbits = 128,
  103. .sadb_alg_maxbits = 256
  104. }
  105. },
  106. {
  107. .name = "rfc4309(ccm(aes))",
  108. .uinfo = {
  109. .aead = {
  110. .geniv = "seqiv",
  111. .icv_truncbits = 128,
  112. }
  113. },
  114. .pfkey_supported = 1,
  115. .desc = {
  116. .sadb_alg_id = SADB_X_EALG_AES_CCM_ICV16,
  117. .sadb_alg_ivlen = 8,
  118. .sadb_alg_minbits = 128,
  119. .sadb_alg_maxbits = 256
  120. }
  121. },
  122. {
  123. .name = "rfc4543(gcm(aes))",
  124. .uinfo = {
  125. .aead = {
  126. .geniv = "seqiv",
  127. .icv_truncbits = 128,
  128. }
  129. },
  130. .pfkey_supported = 1,
  131. .desc = {
  132. .sadb_alg_id = SADB_X_EALG_NULL_AES_GMAC,
  133. .sadb_alg_ivlen = 8,
  134. .sadb_alg_minbits = 128,
  135. .sadb_alg_maxbits = 256
  136. }
  137. },
  138. {
  139. .name = "rfc7539esp(chacha20,poly1305)",
  140. .uinfo = {
  141. .aead = {
  142. .geniv = "seqiv",
  143. .icv_truncbits = 128,
  144. }
  145. },
  146. .pfkey_supported = 0,
  147. },
  148. };
  149. static struct xfrm_algo_desc aalg_list[] = {
  150. {
  151. .name = "digest_null",
  152. .uinfo = {
  153. .auth = {
  154. .icv_truncbits = 0,
  155. .icv_fullbits = 0,
  156. }
  157. },
  158. .pfkey_supported = 1,
  159. .desc = {
  160. .sadb_alg_id = SADB_X_AALG_NULL,
  161. .sadb_alg_ivlen = 0,
  162. .sadb_alg_minbits = 0,
  163. .sadb_alg_maxbits = 0
  164. }
  165. },
  166. {
  167. .name = "hmac(md5)",
  168. .compat = "md5",
  169. .uinfo = {
  170. .auth = {
  171. .icv_truncbits = 96,
  172. .icv_fullbits = 128,
  173. }
  174. },
  175. .pfkey_supported = 1,
  176. .desc = {
  177. .sadb_alg_id = SADB_AALG_MD5HMAC,
  178. .sadb_alg_ivlen = 0,
  179. .sadb_alg_minbits = 128,
  180. .sadb_alg_maxbits = 128
  181. }
  182. },
  183. {
  184. .name = "hmac(sha1)",
  185. .compat = "sha1",
  186. .uinfo = {
  187. .auth = {
  188. .icv_truncbits = 96,
  189. .icv_fullbits = 160,
  190. }
  191. },
  192. .pfkey_supported = 1,
  193. .desc = {
  194. .sadb_alg_id = SADB_AALG_SHA1HMAC,
  195. .sadb_alg_ivlen = 0,
  196. .sadb_alg_minbits = 160,
  197. .sadb_alg_maxbits = 160
  198. }
  199. },
  200. {
  201. .name = "hmac(sha256)",
  202. .compat = "sha256",
  203. .uinfo = {
  204. .auth = {
  205. .icv_truncbits = 96,
  206. .icv_fullbits = 256,
  207. }
  208. },
  209. .pfkey_supported = 1,
  210. .desc = {
  211. .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
  212. .sadb_alg_ivlen = 0,
  213. .sadb_alg_minbits = 256,
  214. .sadb_alg_maxbits = 256
  215. }
  216. },
  217. {
  218. .name = "hmac(sha384)",
  219. .uinfo = {
  220. .auth = {
  221. .icv_truncbits = 192,
  222. .icv_fullbits = 384,
  223. }
  224. },
  225. .pfkey_supported = 1,
  226. .desc = {
  227. .sadb_alg_id = SADB_X_AALG_SHA2_384HMAC,
  228. .sadb_alg_ivlen = 0,
  229. .sadb_alg_minbits = 384,
  230. .sadb_alg_maxbits = 384
  231. }
  232. },
  233. {
  234. .name = "hmac(sha512)",
  235. .uinfo = {
  236. .auth = {
  237. .icv_truncbits = 256,
  238. .icv_fullbits = 512,
  239. }
  240. },
  241. .pfkey_supported = 1,
  242. .desc = {
  243. .sadb_alg_id = SADB_X_AALG_SHA2_512HMAC,
  244. .sadb_alg_ivlen = 0,
  245. .sadb_alg_minbits = 512,
  246. .sadb_alg_maxbits = 512
  247. }
  248. },
  249. {
  250. .name = "hmac(rmd160)",
  251. .compat = "rmd160",
  252. .uinfo = {
  253. .auth = {
  254. .icv_truncbits = 96,
  255. .icv_fullbits = 160,
  256. }
  257. },
  258. .pfkey_supported = 1,
  259. .desc = {
  260. .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
  261. .sadb_alg_ivlen = 0,
  262. .sadb_alg_minbits = 160,
  263. .sadb_alg_maxbits = 160
  264. }
  265. },
  266. {
  267. .name = "xcbc(aes)",
  268. .uinfo = {
  269. .auth = {
  270. .icv_truncbits = 96,
  271. .icv_fullbits = 128,
  272. }
  273. },
  274. .pfkey_supported = 1,
  275. .desc = {
  276. .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
  277. .sadb_alg_ivlen = 0,
  278. .sadb_alg_minbits = 128,
  279. .sadb_alg_maxbits = 128
  280. }
  281. },
  282. {
  283. /* rfc4494 */
  284. .name = "cmac(aes)",
  285. .uinfo = {
  286. .auth = {
  287. .icv_truncbits = 96,
  288. .icv_fullbits = 128,
  289. }
  290. },
  291. .pfkey_supported = 0,
  292. },
  293. {
  294. .name = "hmac(sm3)",
  295. .compat = "sm3",
  296. .uinfo = {
  297. .auth = {
  298. .icv_truncbits = 256,
  299. .icv_fullbits = 256,
  300. }
  301. },
  302. .pfkey_supported = 1,
  303. .desc = {
  304. .sadb_alg_id = SADB_X_AALG_SM3_256HMAC,
  305. .sadb_alg_ivlen = 0,
  306. .sadb_alg_minbits = 256,
  307. .sadb_alg_maxbits = 256
  308. }
  309. },
  310. };
  311. static struct xfrm_algo_desc ealg_list[] = {
  312. {
  313. .name = "ecb(cipher_null)",
  314. .compat = "cipher_null",
  315. .uinfo = {
  316. .encr = {
  317. .blockbits = 8,
  318. .defkeybits = 0,
  319. }
  320. },
  321. .pfkey_supported = 1,
  322. .desc = {
  323. .sadb_alg_id = SADB_EALG_NULL,
  324. .sadb_alg_ivlen = 0,
  325. .sadb_alg_minbits = 0,
  326. .sadb_alg_maxbits = 0
  327. }
  328. },
  329. {
  330. .name = "cbc(des)",
  331. .compat = "des",
  332. .uinfo = {
  333. .encr = {
  334. .geniv = "echainiv",
  335. .blockbits = 64,
  336. .defkeybits = 64,
  337. }
  338. },
  339. .pfkey_supported = 1,
  340. .desc = {
  341. .sadb_alg_id = SADB_EALG_DESCBC,
  342. .sadb_alg_ivlen = 8,
  343. .sadb_alg_minbits = 64,
  344. .sadb_alg_maxbits = 64
  345. }
  346. },
  347. {
  348. .name = "cbc(des3_ede)",
  349. .compat = "des3_ede",
  350. .uinfo = {
  351. .encr = {
  352. .geniv = "echainiv",
  353. .blockbits = 64,
  354. .defkeybits = 192,
  355. }
  356. },
  357. .pfkey_supported = 1,
  358. .desc = {
  359. .sadb_alg_id = SADB_EALG_3DESCBC,
  360. .sadb_alg_ivlen = 8,
  361. .sadb_alg_minbits = 192,
  362. .sadb_alg_maxbits = 192
  363. }
  364. },
  365. {
  366. .name = "cbc(cast5)",
  367. .compat = "cast5",
  368. .uinfo = {
  369. .encr = {
  370. .geniv = "echainiv",
  371. .blockbits = 64,
  372. .defkeybits = 128,
  373. }
  374. },
  375. .pfkey_supported = 1,
  376. .desc = {
  377. .sadb_alg_id = SADB_X_EALG_CASTCBC,
  378. .sadb_alg_ivlen = 8,
  379. .sadb_alg_minbits = 40,
  380. .sadb_alg_maxbits = 128
  381. }
  382. },
  383. {
  384. .name = "cbc(blowfish)",
  385. .compat = "blowfish",
  386. .uinfo = {
  387. .encr = {
  388. .geniv = "echainiv",
  389. .blockbits = 64,
  390. .defkeybits = 128,
  391. }
  392. },
  393. .pfkey_supported = 1,
  394. .desc = {
  395. .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
  396. .sadb_alg_ivlen = 8,
  397. .sadb_alg_minbits = 40,
  398. .sadb_alg_maxbits = 448
  399. }
  400. },
  401. {
  402. .name = "cbc(aes)",
  403. .compat = "aes",
  404. .uinfo = {
  405. .encr = {
  406. .geniv = "echainiv",
  407. .blockbits = 128,
  408. .defkeybits = 128,
  409. }
  410. },
  411. .pfkey_supported = 1,
  412. .desc = {
  413. .sadb_alg_id = SADB_X_EALG_AESCBC,
  414. .sadb_alg_ivlen = 8,
  415. .sadb_alg_minbits = 128,
  416. .sadb_alg_maxbits = 256
  417. }
  418. },
  419. {
  420. .name = "cbc(serpent)",
  421. .compat = "serpent",
  422. .uinfo = {
  423. .encr = {
  424. .geniv = "echainiv",
  425. .blockbits = 128,
  426. .defkeybits = 128,
  427. }
  428. },
  429. .pfkey_supported = 1,
  430. .desc = {
  431. .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
  432. .sadb_alg_ivlen = 8,
  433. .sadb_alg_minbits = 128,
  434. .sadb_alg_maxbits = 256,
  435. }
  436. },
  437. {
  438. .name = "cbc(camellia)",
  439. .compat = "camellia",
  440. .uinfo = {
  441. .encr = {
  442. .geniv = "echainiv",
  443. .blockbits = 128,
  444. .defkeybits = 128,
  445. }
  446. },
  447. .pfkey_supported = 1,
  448. .desc = {
  449. .sadb_alg_id = SADB_X_EALG_CAMELLIACBC,
  450. .sadb_alg_ivlen = 8,
  451. .sadb_alg_minbits = 128,
  452. .sadb_alg_maxbits = 256
  453. }
  454. },
  455. {
  456. .name = "cbc(twofish)",
  457. .compat = "twofish",
  458. .uinfo = {
  459. .encr = {
  460. .geniv = "echainiv",
  461. .blockbits = 128,
  462. .defkeybits = 128,
  463. }
  464. },
  465. .pfkey_supported = 1,
  466. .desc = {
  467. .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
  468. .sadb_alg_ivlen = 8,
  469. .sadb_alg_minbits = 128,
  470. .sadb_alg_maxbits = 256
  471. }
  472. },
  473. {
  474. .name = "rfc3686(ctr(aes))",
  475. .uinfo = {
  476. .encr = {
  477. .geniv = "seqiv",
  478. .blockbits = 128,
  479. .defkeybits = 160, /* 128-bit key + 32-bit nonce */
  480. }
  481. },
  482. .pfkey_supported = 1,
  483. .desc = {
  484. .sadb_alg_id = SADB_X_EALG_AESCTR,
  485. .sadb_alg_ivlen = 8,
  486. .sadb_alg_minbits = 160,
  487. .sadb_alg_maxbits = 288
  488. }
  489. },
  490. {
  491. .name = "cbc(sm4)",
  492. .compat = "sm4",
  493. .uinfo = {
  494. .encr = {
  495. .geniv = "echainiv",
  496. .blockbits = 128,
  497. .defkeybits = 128,
  498. }
  499. },
  500. .pfkey_supported = 1,
  501. .desc = {
  502. .sadb_alg_id = SADB_X_EALG_SM4CBC,
  503. .sadb_alg_ivlen = 16,
  504. .sadb_alg_minbits = 128,
  505. .sadb_alg_maxbits = 256
  506. }
  507. },
  508. };
  509. static struct xfrm_algo_desc calg_list[] = {
  510. {
  511. .name = "deflate",
  512. .uinfo = {
  513. .comp = {
  514. .threshold = 90,
  515. }
  516. },
  517. .pfkey_supported = 1,
  518. .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
  519. },
  520. {
  521. .name = "lzs",
  522. .uinfo = {
  523. .comp = {
  524. .threshold = 90,
  525. }
  526. },
  527. .pfkey_supported = 1,
  528. .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
  529. },
  530. {
  531. .name = "lzjh",
  532. .uinfo = {
  533. .comp = {
  534. .threshold = 50,
  535. }
  536. },
  537. .pfkey_supported = 1,
  538. .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
  539. },
  540. };
  541. static inline int aalg_entries(void)
  542. {
  543. return ARRAY_SIZE(aalg_list);
  544. }
  545. static inline int ealg_entries(void)
  546. {
  547. return ARRAY_SIZE(ealg_list);
  548. }
  549. static inline int calg_entries(void)
  550. {
  551. return ARRAY_SIZE(calg_list);
  552. }
  553. struct xfrm_algo_list {
  554. int (*find)(const char *name, u32 type, u32 mask);
  555. struct xfrm_algo_desc *algs;
  556. int entries;
  557. };
  558. static const struct xfrm_algo_list xfrm_aead_list = {
  559. .find = crypto_has_aead,
  560. .algs = aead_list,
  561. .entries = ARRAY_SIZE(aead_list),
  562. };
  563. static const struct xfrm_algo_list xfrm_aalg_list = {
  564. .find = crypto_has_ahash,
  565. .algs = aalg_list,
  566. .entries = ARRAY_SIZE(aalg_list),
  567. };
  568. static const struct xfrm_algo_list xfrm_ealg_list = {
  569. .find = crypto_has_skcipher,
  570. .algs = ealg_list,
  571. .entries = ARRAY_SIZE(ealg_list),
  572. };
  573. static const struct xfrm_algo_list xfrm_calg_list = {
  574. .find = crypto_has_comp,
  575. .algs = calg_list,
  576. .entries = ARRAY_SIZE(calg_list),
  577. };
  578. static struct xfrm_algo_desc *xfrm_find_algo(
  579. const struct xfrm_algo_list *algo_list,
  580. int match(const struct xfrm_algo_desc *entry, const void *data),
  581. const void *data, int probe)
  582. {
  583. struct xfrm_algo_desc *list = algo_list->algs;
  584. int i, status;
  585. for (i = 0; i < algo_list->entries; i++) {
  586. if (!match(list + i, data))
  587. continue;
  588. if (list[i].available)
  589. return &list[i];
  590. if (!probe)
  591. break;
  592. status = algo_list->find(list[i].name, 0, 0);
  593. if (!status)
  594. break;
  595. list[i].available = status;
  596. return &list[i];
  597. }
  598. return NULL;
  599. }
  600. static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry,
  601. const void *data)
  602. {
  603. return entry->desc.sadb_alg_id == (unsigned long)data;
  604. }
  605. struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
  606. {
  607. return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match,
  608. (void *)(unsigned long)alg_id, 1);
  609. }
  610. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
  611. struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
  612. {
  613. return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match,
  614. (void *)(unsigned long)alg_id, 1);
  615. }
  616. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
  617. struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
  618. {
  619. return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match,
  620. (void *)(unsigned long)alg_id, 1);
  621. }
  622. EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
  623. static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry,
  624. const void *data)
  625. {
  626. const char *name = data;
  627. return name && (!strcmp(name, entry->name) ||
  628. (entry->compat && !strcmp(name, entry->compat)));
  629. }
  630. struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe)
  631. {
  632. return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name,
  633. probe);
  634. }
  635. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
  636. struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe)
  637. {
  638. return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name,
  639. probe);
  640. }
  641. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
  642. struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe)
  643. {
  644. return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name,
  645. probe);
  646. }
  647. EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
  648. struct xfrm_aead_name {
  649. const char *name;
  650. int icvbits;
  651. };
  652. static int xfrm_aead_name_match(const struct xfrm_algo_desc *entry,
  653. const void *data)
  654. {
  655. const struct xfrm_aead_name *aead = data;
  656. const char *name = aead->name;
  657. return aead->icvbits == entry->uinfo.aead.icv_truncbits && name &&
  658. !strcmp(name, entry->name);
  659. }
  660. struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len, int probe)
  661. {
  662. struct xfrm_aead_name data = {
  663. .name = name,
  664. .icvbits = icv_len,
  665. };
  666. return xfrm_find_algo(&xfrm_aead_list, xfrm_aead_name_match, &data,
  667. probe);
  668. }
  669. EXPORT_SYMBOL_GPL(xfrm_aead_get_byname);
  670. struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
  671. {
  672. if (idx >= aalg_entries())
  673. return NULL;
  674. return &aalg_list[idx];
  675. }
  676. EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
  677. struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
  678. {
  679. if (idx >= ealg_entries())
  680. return NULL;
  681. return &ealg_list[idx];
  682. }
  683. EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
  684. /*
  685. * Probe for the availability of crypto algorithms, and set the available
  686. * flag for any algorithms found on the system. This is typically called by
  687. * pfkey during userspace SA add, update or register.
  688. */
  689. void xfrm_probe_algs(void)
  690. {
  691. int i, status;
  692. BUG_ON(in_softirq());
  693. for (i = 0; i < aalg_entries(); i++) {
  694. status = crypto_has_ahash(aalg_list[i].name, 0, 0);
  695. if (aalg_list[i].available != status)
  696. aalg_list[i].available = status;
  697. }
  698. for (i = 0; i < ealg_entries(); i++) {
  699. status = crypto_has_skcipher(ealg_list[i].name, 0, 0);
  700. if (ealg_list[i].available != status)
  701. ealg_list[i].available = status;
  702. }
  703. for (i = 0; i < calg_entries(); i++) {
  704. status = crypto_has_comp(calg_list[i].name, 0,
  705. CRYPTO_ALG_ASYNC);
  706. if (calg_list[i].available != status)
  707. calg_list[i].available = status;
  708. }
  709. }
  710. EXPORT_SYMBOL_GPL(xfrm_probe_algs);
  711. int xfrm_count_pfkey_auth_supported(void)
  712. {
  713. int i, n;
  714. for (i = 0, n = 0; i < aalg_entries(); i++)
  715. if (aalg_list[i].available && aalg_list[i].pfkey_supported)
  716. n++;
  717. return n;
  718. }
  719. EXPORT_SYMBOL_GPL(xfrm_count_pfkey_auth_supported);
  720. int xfrm_count_pfkey_enc_supported(void)
  721. {
  722. int i, n;
  723. for (i = 0, n = 0; i < ealg_entries(); i++)
  724. if (ealg_list[i].available && ealg_list[i].pfkey_supported)
  725. n++;
  726. return n;
  727. }
  728. EXPORT_SYMBOL_GPL(xfrm_count_pfkey_enc_supported);
  729. MODULE_DESCRIPTION("XFRM Algorithm interface");
  730. MODULE_LICENSE("GPL");