testmgr.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  1. /*
  2. * Algorithm testing framework and tests.
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
  6. * Copyright (c) 2007 Nokia Siemens Networks
  7. * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/hash.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include "internal.h"
  22. #include "testmgr.h"
  23. /*
  24. * Need slab memory for testing (size in number of pages).
  25. */
  26. #define XBUFSIZE 8
  27. /*
  28. * Indexes into the xbuf to simulate cross-page access.
  29. */
  30. #define IDX1 32
  31. #define IDX2 32400
  32. #define IDX3 1
  33. #define IDX4 8193
  34. #define IDX5 22222
  35. #define IDX6 17101
  36. #define IDX7 27333
  37. #define IDX8 3000
  38. /*
  39. * Used by test_cipher()
  40. */
  41. #define ENCRYPT 1
  42. #define DECRYPT 0
  43. struct tcrypt_result {
  44. struct completion completion;
  45. int err;
  46. };
  47. struct aead_test_suite {
  48. struct {
  49. struct aead_testvec *vecs;
  50. unsigned int count;
  51. } enc, dec;
  52. };
  53. struct cipher_test_suite {
  54. struct {
  55. struct cipher_testvec *vecs;
  56. unsigned int count;
  57. } enc, dec;
  58. };
  59. struct comp_test_suite {
  60. struct {
  61. struct comp_testvec *vecs;
  62. unsigned int count;
  63. } comp, decomp;
  64. };
  65. struct pcomp_test_suite {
  66. struct {
  67. struct pcomp_testvec *vecs;
  68. unsigned int count;
  69. } comp, decomp;
  70. };
  71. struct hash_test_suite {
  72. struct hash_testvec *vecs;
  73. unsigned int count;
  74. };
  75. struct alg_test_desc {
  76. const char *alg;
  77. int (*test)(const struct alg_test_desc *desc, const char *driver,
  78. u32 type, u32 mask);
  79. union {
  80. struct aead_test_suite aead;
  81. struct cipher_test_suite cipher;
  82. struct comp_test_suite comp;
  83. struct pcomp_test_suite pcomp;
  84. struct hash_test_suite hash;
  85. } suite;
  86. };
  87. static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
  88. static char *xbuf[XBUFSIZE];
  89. static char *axbuf[XBUFSIZE];
  90. static void hexdump(unsigned char *buf, unsigned int len)
  91. {
  92. print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
  93. 16, 1,
  94. buf, len, false);
  95. }
  96. static void tcrypt_complete(struct crypto_async_request *req, int err)
  97. {
  98. struct tcrypt_result *res = req->data;
  99. if (err == -EINPROGRESS)
  100. return;
  101. res->err = err;
  102. complete(&res->completion);
  103. }
  104. static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
  105. unsigned int tcount)
  106. {
  107. const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
  108. unsigned int i, j, k, temp;
  109. struct scatterlist sg[8];
  110. char result[64];
  111. struct ahash_request *req;
  112. struct tcrypt_result tresult;
  113. int ret;
  114. void *hash_buff;
  115. init_completion(&tresult.completion);
  116. req = ahash_request_alloc(tfm, GFP_KERNEL);
  117. if (!req) {
  118. printk(KERN_ERR "alg: hash: Failed to allocate request for "
  119. "%s\n", algo);
  120. ret = -ENOMEM;
  121. goto out_noreq;
  122. }
  123. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  124. tcrypt_complete, &tresult);
  125. for (i = 0; i < tcount; i++) {
  126. memset(result, 0, 64);
  127. hash_buff = xbuf[0];
  128. memcpy(hash_buff, template[i].plaintext, template[i].psize);
  129. sg_init_one(&sg[0], hash_buff, template[i].psize);
  130. if (template[i].ksize) {
  131. crypto_ahash_clear_flags(tfm, ~0);
  132. ret = crypto_ahash_setkey(tfm, template[i].key,
  133. template[i].ksize);
  134. if (ret) {
  135. printk(KERN_ERR "alg: hash: setkey failed on "
  136. "test %d for %s: ret=%d\n", i + 1, algo,
  137. -ret);
  138. goto out;
  139. }
  140. }
  141. ahash_request_set_crypt(req, sg, result, template[i].psize);
  142. ret = crypto_ahash_digest(req);
  143. switch (ret) {
  144. case 0:
  145. break;
  146. case -EINPROGRESS:
  147. case -EBUSY:
  148. ret = wait_for_completion_interruptible(
  149. &tresult.completion);
  150. if (!ret && !(ret = tresult.err)) {
  151. INIT_COMPLETION(tresult.completion);
  152. break;
  153. }
  154. /* fall through */
  155. default:
  156. printk(KERN_ERR "alg: hash: digest failed on test %d "
  157. "for %s: ret=%d\n", i + 1, algo, -ret);
  158. goto out;
  159. }
  160. if (memcmp(result, template[i].digest,
  161. crypto_ahash_digestsize(tfm))) {
  162. printk(KERN_ERR "alg: hash: Test %d failed for %s\n",
  163. i + 1, algo);
  164. hexdump(result, crypto_ahash_digestsize(tfm));
  165. ret = -EINVAL;
  166. goto out;
  167. }
  168. }
  169. j = 0;
  170. for (i = 0; i < tcount; i++) {
  171. if (template[i].np) {
  172. j++;
  173. memset(result, 0, 64);
  174. temp = 0;
  175. sg_init_table(sg, template[i].np);
  176. for (k = 0; k < template[i].np; k++) {
  177. sg_set_buf(&sg[k],
  178. memcpy(xbuf[IDX[k] >> PAGE_SHIFT] +
  179. offset_in_page(IDX[k]),
  180. template[i].plaintext + temp,
  181. template[i].tap[k]),
  182. template[i].tap[k]);
  183. temp += template[i].tap[k];
  184. }
  185. if (template[i].ksize) {
  186. crypto_ahash_clear_flags(tfm, ~0);
  187. ret = crypto_ahash_setkey(tfm, template[i].key,
  188. template[i].ksize);
  189. if (ret) {
  190. printk(KERN_ERR "alg: hash: setkey "
  191. "failed on chunking test %d "
  192. "for %s: ret=%d\n", j, algo,
  193. -ret);
  194. goto out;
  195. }
  196. }
  197. ahash_request_set_crypt(req, sg, result,
  198. template[i].psize);
  199. ret = crypto_ahash_digest(req);
  200. switch (ret) {
  201. case 0:
  202. break;
  203. case -EINPROGRESS:
  204. case -EBUSY:
  205. ret = wait_for_completion_interruptible(
  206. &tresult.completion);
  207. if (!ret && !(ret = tresult.err)) {
  208. INIT_COMPLETION(tresult.completion);
  209. break;
  210. }
  211. /* fall through */
  212. default:
  213. printk(KERN_ERR "alg: hash: digest failed "
  214. "on chunking test %d for %s: "
  215. "ret=%d\n", j, algo, -ret);
  216. goto out;
  217. }
  218. if (memcmp(result, template[i].digest,
  219. crypto_ahash_digestsize(tfm))) {
  220. printk(KERN_ERR "alg: hash: Chunking test %d "
  221. "failed for %s\n", j, algo);
  222. hexdump(result, crypto_ahash_digestsize(tfm));
  223. ret = -EINVAL;
  224. goto out;
  225. }
  226. }
  227. }
  228. ret = 0;
  229. out:
  230. ahash_request_free(req);
  231. out_noreq:
  232. return ret;
  233. }
  234. static int test_aead(struct crypto_aead *tfm, int enc,
  235. struct aead_testvec *template, unsigned int tcount)
  236. {
  237. const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  238. unsigned int i, j, k, n, temp;
  239. int ret = 0;
  240. char *q;
  241. char *key;
  242. struct aead_request *req;
  243. struct scatterlist sg[8];
  244. struct scatterlist asg[8];
  245. const char *e;
  246. struct tcrypt_result result;
  247. unsigned int authsize;
  248. void *input;
  249. void *assoc;
  250. char iv[MAX_IVLEN];
  251. if (enc == ENCRYPT)
  252. e = "encryption";
  253. else
  254. e = "decryption";
  255. init_completion(&result.completion);
  256. req = aead_request_alloc(tfm, GFP_KERNEL);
  257. if (!req) {
  258. printk(KERN_ERR "alg: aead: Failed to allocate request for "
  259. "%s\n", algo);
  260. ret = -ENOMEM;
  261. goto out;
  262. }
  263. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  264. tcrypt_complete, &result);
  265. for (i = 0, j = 0; i < tcount; i++) {
  266. if (!template[i].np) {
  267. j++;
  268. /* some tepmplates have no input data but they will
  269. * touch input
  270. */
  271. input = xbuf[0];
  272. assoc = axbuf[0];
  273. memcpy(input, template[i].input, template[i].ilen);
  274. memcpy(assoc, template[i].assoc, template[i].alen);
  275. if (template[i].iv)
  276. memcpy(iv, template[i].iv, MAX_IVLEN);
  277. else
  278. memset(iv, 0, MAX_IVLEN);
  279. crypto_aead_clear_flags(tfm, ~0);
  280. if (template[i].wk)
  281. crypto_aead_set_flags(
  282. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  283. key = template[i].key;
  284. ret = crypto_aead_setkey(tfm, key,
  285. template[i].klen);
  286. if (!ret == template[i].fail) {
  287. printk(KERN_ERR "alg: aead: setkey failed on "
  288. "test %d for %s: flags=%x\n", j, algo,
  289. crypto_aead_get_flags(tfm));
  290. goto out;
  291. } else if (ret)
  292. continue;
  293. authsize = abs(template[i].rlen - template[i].ilen);
  294. ret = crypto_aead_setauthsize(tfm, authsize);
  295. if (ret) {
  296. printk(KERN_ERR "alg: aead: Failed to set "
  297. "authsize to %u on test %d for %s\n",
  298. authsize, j, algo);
  299. goto out;
  300. }
  301. sg_init_one(&sg[0], input,
  302. template[i].ilen + (enc ? authsize : 0));
  303. sg_init_one(&asg[0], assoc, template[i].alen);
  304. aead_request_set_crypt(req, sg, sg,
  305. template[i].ilen, iv);
  306. aead_request_set_assoc(req, asg, template[i].alen);
  307. ret = enc ?
  308. crypto_aead_encrypt(req) :
  309. crypto_aead_decrypt(req);
  310. switch (ret) {
  311. case 0:
  312. break;
  313. case -EINPROGRESS:
  314. case -EBUSY:
  315. ret = wait_for_completion_interruptible(
  316. &result.completion);
  317. if (!ret && !(ret = result.err)) {
  318. INIT_COMPLETION(result.completion);
  319. break;
  320. }
  321. /* fall through */
  322. default:
  323. printk(KERN_ERR "alg: aead: %s failed on test "
  324. "%d for %s: ret=%d\n", e, j, algo, -ret);
  325. goto out;
  326. }
  327. q = input;
  328. if (memcmp(q, template[i].result, template[i].rlen)) {
  329. printk(KERN_ERR "alg: aead: Test %d failed on "
  330. "%s for %s\n", j, e, algo);
  331. hexdump(q, template[i].rlen);
  332. ret = -EINVAL;
  333. goto out;
  334. }
  335. }
  336. }
  337. for (i = 0, j = 0; i < tcount; i++) {
  338. if (template[i].np) {
  339. j++;
  340. if (template[i].iv)
  341. memcpy(iv, template[i].iv, MAX_IVLEN);
  342. else
  343. memset(iv, 0, MAX_IVLEN);
  344. crypto_aead_clear_flags(tfm, ~0);
  345. if (template[i].wk)
  346. crypto_aead_set_flags(
  347. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  348. key = template[i].key;
  349. ret = crypto_aead_setkey(tfm, key, template[i].klen);
  350. if (!ret == template[i].fail) {
  351. printk(KERN_ERR "alg: aead: setkey failed on "
  352. "chunk test %d for %s: flags=%x\n", j,
  353. algo, crypto_aead_get_flags(tfm));
  354. goto out;
  355. } else if (ret)
  356. continue;
  357. authsize = abs(template[i].rlen - template[i].ilen);
  358. ret = -EINVAL;
  359. sg_init_table(sg, template[i].np);
  360. for (k = 0, temp = 0; k < template[i].np; k++) {
  361. if (WARN_ON(offset_in_page(IDX[k]) +
  362. template[i].tap[k] > PAGE_SIZE))
  363. goto out;
  364. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  365. offset_in_page(IDX[k]);
  366. memcpy(q, template[i].input + temp,
  367. template[i].tap[k]);
  368. n = template[i].tap[k];
  369. if (k == template[i].np - 1 && enc)
  370. n += authsize;
  371. if (offset_in_page(q) + n < PAGE_SIZE)
  372. q[n] = 0;
  373. sg_set_buf(&sg[k], q, template[i].tap[k]);
  374. temp += template[i].tap[k];
  375. }
  376. ret = crypto_aead_setauthsize(tfm, authsize);
  377. if (ret) {
  378. printk(KERN_ERR "alg: aead: Failed to set "
  379. "authsize to %u on chunk test %d for "
  380. "%s\n", authsize, j, algo);
  381. goto out;
  382. }
  383. if (enc) {
  384. if (WARN_ON(sg[k - 1].offset +
  385. sg[k - 1].length + authsize >
  386. PAGE_SIZE)) {
  387. ret = -EINVAL;
  388. goto out;
  389. }
  390. sg[k - 1].length += authsize;
  391. }
  392. sg_init_table(asg, template[i].anp);
  393. for (k = 0, temp = 0; k < template[i].anp; k++) {
  394. sg_set_buf(&asg[k],
  395. memcpy(axbuf[IDX[k] >> PAGE_SHIFT] +
  396. offset_in_page(IDX[k]),
  397. template[i].assoc + temp,
  398. template[i].atap[k]),
  399. template[i].atap[k]);
  400. temp += template[i].atap[k];
  401. }
  402. aead_request_set_crypt(req, sg, sg,
  403. template[i].ilen,
  404. iv);
  405. aead_request_set_assoc(req, asg, template[i].alen);
  406. ret = enc ?
  407. crypto_aead_encrypt(req) :
  408. crypto_aead_decrypt(req);
  409. switch (ret) {
  410. case 0:
  411. break;
  412. case -EINPROGRESS:
  413. case -EBUSY:
  414. ret = wait_for_completion_interruptible(
  415. &result.completion);
  416. if (!ret && !(ret = result.err)) {
  417. INIT_COMPLETION(result.completion);
  418. break;
  419. }
  420. /* fall through */
  421. default:
  422. printk(KERN_ERR "alg: aead: %s failed on "
  423. "chunk test %d for %s: ret=%d\n", e, j,
  424. algo, -ret);
  425. goto out;
  426. }
  427. ret = -EINVAL;
  428. for (k = 0, temp = 0; k < template[i].np; k++) {
  429. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  430. offset_in_page(IDX[k]);
  431. n = template[i].tap[k];
  432. if (k == template[i].np - 1)
  433. n += enc ? authsize : -authsize;
  434. if (memcmp(q, template[i].result + temp, n)) {
  435. printk(KERN_ERR "alg: aead: Chunk "
  436. "test %d failed on %s at page "
  437. "%u for %s\n", j, e, k, algo);
  438. hexdump(q, n);
  439. goto out;
  440. }
  441. q += n;
  442. if (k == template[i].np - 1 && !enc) {
  443. if (memcmp(q, template[i].input +
  444. temp + n, authsize))
  445. n = authsize;
  446. else
  447. n = 0;
  448. } else {
  449. for (n = 0; offset_in_page(q + n) &&
  450. q[n]; n++)
  451. ;
  452. }
  453. if (n) {
  454. printk(KERN_ERR "alg: aead: Result "
  455. "buffer corruption in chunk "
  456. "test %d on %s at page %u for "
  457. "%s: %u bytes:\n", j, e, k,
  458. algo, n);
  459. hexdump(q, n);
  460. goto out;
  461. }
  462. temp += template[i].tap[k];
  463. }
  464. }
  465. }
  466. ret = 0;
  467. out:
  468. aead_request_free(req);
  469. return ret;
  470. }
  471. static int test_cipher(struct crypto_cipher *tfm, int enc,
  472. struct cipher_testvec *template, unsigned int tcount)
  473. {
  474. const char *algo = crypto_tfm_alg_driver_name(crypto_cipher_tfm(tfm));
  475. unsigned int i, j, k;
  476. int ret;
  477. char *q;
  478. const char *e;
  479. void *data;
  480. if (enc == ENCRYPT)
  481. e = "encryption";
  482. else
  483. e = "decryption";
  484. j = 0;
  485. for (i = 0; i < tcount; i++) {
  486. if (template[i].np)
  487. continue;
  488. j++;
  489. data = xbuf[0];
  490. memcpy(data, template[i].input, template[i].ilen);
  491. crypto_cipher_clear_flags(tfm, ~0);
  492. if (template[i].wk)
  493. crypto_cipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  494. ret = crypto_cipher_setkey(tfm, template[i].key,
  495. template[i].klen);
  496. if (!ret == template[i].fail) {
  497. printk(KERN_ERR "alg: cipher: setkey failed "
  498. "on test %d for %s: flags=%x\n", j,
  499. algo, crypto_cipher_get_flags(tfm));
  500. goto out;
  501. } else if (ret)
  502. continue;
  503. for (k = 0; k < template[i].ilen;
  504. k += crypto_cipher_blocksize(tfm)) {
  505. if (enc)
  506. crypto_cipher_encrypt_one(tfm, data + k,
  507. data + k);
  508. else
  509. crypto_cipher_decrypt_one(tfm, data + k,
  510. data + k);
  511. }
  512. q = data;
  513. if (memcmp(q, template[i].result, template[i].rlen)) {
  514. printk(KERN_ERR "alg: cipher: Test %d failed "
  515. "on %s for %s\n", j, e, algo);
  516. hexdump(q, template[i].rlen);
  517. ret = -EINVAL;
  518. goto out;
  519. }
  520. }
  521. ret = 0;
  522. out:
  523. return ret;
  524. }
  525. static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
  526. struct cipher_testvec *template, unsigned int tcount)
  527. {
  528. const char *algo =
  529. crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
  530. unsigned int i, j, k, n, temp;
  531. int ret;
  532. char *q;
  533. struct ablkcipher_request *req;
  534. struct scatterlist sg[8];
  535. const char *e;
  536. struct tcrypt_result result;
  537. void *data;
  538. char iv[MAX_IVLEN];
  539. if (enc == ENCRYPT)
  540. e = "encryption";
  541. else
  542. e = "decryption";
  543. init_completion(&result.completion);
  544. req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
  545. if (!req) {
  546. printk(KERN_ERR "alg: skcipher: Failed to allocate request "
  547. "for %s\n", algo);
  548. ret = -ENOMEM;
  549. goto out;
  550. }
  551. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  552. tcrypt_complete, &result);
  553. j = 0;
  554. for (i = 0; i < tcount; i++) {
  555. if (template[i].iv)
  556. memcpy(iv, template[i].iv, MAX_IVLEN);
  557. else
  558. memset(iv, 0, MAX_IVLEN);
  559. if (!(template[i].np)) {
  560. j++;
  561. data = xbuf[0];
  562. memcpy(data, template[i].input, template[i].ilen);
  563. crypto_ablkcipher_clear_flags(tfm, ~0);
  564. if (template[i].wk)
  565. crypto_ablkcipher_set_flags(
  566. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  567. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  568. template[i].klen);
  569. if (!ret == template[i].fail) {
  570. printk(KERN_ERR "alg: skcipher: setkey failed "
  571. "on test %d for %s: flags=%x\n", j,
  572. algo, crypto_ablkcipher_get_flags(tfm));
  573. goto out;
  574. } else if (ret)
  575. continue;
  576. sg_init_one(&sg[0], data, template[i].ilen);
  577. ablkcipher_request_set_crypt(req, sg, sg,
  578. template[i].ilen, iv);
  579. ret = enc ?
  580. crypto_ablkcipher_encrypt(req) :
  581. crypto_ablkcipher_decrypt(req);
  582. switch (ret) {
  583. case 0:
  584. break;
  585. case -EINPROGRESS:
  586. case -EBUSY:
  587. ret = wait_for_completion_interruptible(
  588. &result.completion);
  589. if (!ret && !((ret = result.err))) {
  590. INIT_COMPLETION(result.completion);
  591. break;
  592. }
  593. /* fall through */
  594. default:
  595. printk(KERN_ERR "alg: skcipher: %s failed on "
  596. "test %d for %s: ret=%d\n", e, j, algo,
  597. -ret);
  598. goto out;
  599. }
  600. q = data;
  601. if (memcmp(q, template[i].result, template[i].rlen)) {
  602. printk(KERN_ERR "alg: skcipher: Test %d "
  603. "failed on %s for %s\n", j, e, algo);
  604. hexdump(q, template[i].rlen);
  605. ret = -EINVAL;
  606. goto out;
  607. }
  608. }
  609. }
  610. j = 0;
  611. for (i = 0; i < tcount; i++) {
  612. if (template[i].iv)
  613. memcpy(iv, template[i].iv, MAX_IVLEN);
  614. else
  615. memset(iv, 0, MAX_IVLEN);
  616. if (template[i].np) {
  617. j++;
  618. crypto_ablkcipher_clear_flags(tfm, ~0);
  619. if (template[i].wk)
  620. crypto_ablkcipher_set_flags(
  621. tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  622. ret = crypto_ablkcipher_setkey(tfm, template[i].key,
  623. template[i].klen);
  624. if (!ret == template[i].fail) {
  625. printk(KERN_ERR "alg: skcipher: setkey failed "
  626. "on chunk test %d for %s: flags=%x\n",
  627. j, algo,
  628. crypto_ablkcipher_get_flags(tfm));
  629. goto out;
  630. } else if (ret)
  631. continue;
  632. temp = 0;
  633. ret = -EINVAL;
  634. sg_init_table(sg, template[i].np);
  635. for (k = 0; k < template[i].np; k++) {
  636. if (WARN_ON(offset_in_page(IDX[k]) +
  637. template[i].tap[k] > PAGE_SIZE))
  638. goto out;
  639. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  640. offset_in_page(IDX[k]);
  641. memcpy(q, template[i].input + temp,
  642. template[i].tap[k]);
  643. if (offset_in_page(q) + template[i].tap[k] <
  644. PAGE_SIZE)
  645. q[template[i].tap[k]] = 0;
  646. sg_set_buf(&sg[k], q, template[i].tap[k]);
  647. temp += template[i].tap[k];
  648. }
  649. ablkcipher_request_set_crypt(req, sg, sg,
  650. template[i].ilen, iv);
  651. ret = enc ?
  652. crypto_ablkcipher_encrypt(req) :
  653. crypto_ablkcipher_decrypt(req);
  654. switch (ret) {
  655. case 0:
  656. break;
  657. case -EINPROGRESS:
  658. case -EBUSY:
  659. ret = wait_for_completion_interruptible(
  660. &result.completion);
  661. if (!ret && !((ret = result.err))) {
  662. INIT_COMPLETION(result.completion);
  663. break;
  664. }
  665. /* fall through */
  666. default:
  667. printk(KERN_ERR "alg: skcipher: %s failed on "
  668. "chunk test %d for %s: ret=%d\n", e, j,
  669. algo, -ret);
  670. goto out;
  671. }
  672. temp = 0;
  673. ret = -EINVAL;
  674. for (k = 0; k < template[i].np; k++) {
  675. q = xbuf[IDX[k] >> PAGE_SHIFT] +
  676. offset_in_page(IDX[k]);
  677. if (memcmp(q, template[i].result + temp,
  678. template[i].tap[k])) {
  679. printk(KERN_ERR "alg: skcipher: Chunk "
  680. "test %d failed on %s at page "
  681. "%u for %s\n", j, e, k, algo);
  682. hexdump(q, template[i].tap[k]);
  683. goto out;
  684. }
  685. q += template[i].tap[k];
  686. for (n = 0; offset_in_page(q + n) && q[n]; n++)
  687. ;
  688. if (n) {
  689. printk(KERN_ERR "alg: skcipher: "
  690. "Result buffer corruption in "
  691. "chunk test %d on %s at page "
  692. "%u for %s: %u bytes:\n", j, e,
  693. k, algo, n);
  694. hexdump(q, n);
  695. goto out;
  696. }
  697. temp += template[i].tap[k];
  698. }
  699. }
  700. }
  701. ret = 0;
  702. out:
  703. ablkcipher_request_free(req);
  704. return ret;
  705. }
  706. static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,
  707. struct comp_testvec *dtemplate, int ctcount, int dtcount)
  708. {
  709. const char *algo = crypto_tfm_alg_driver_name(crypto_comp_tfm(tfm));
  710. unsigned int i;
  711. char result[COMP_BUF_SIZE];
  712. int ret;
  713. for (i = 0; i < ctcount; i++) {
  714. int ilen, dlen = COMP_BUF_SIZE;
  715. memset(result, 0, sizeof (result));
  716. ilen = ctemplate[i].inlen;
  717. ret = crypto_comp_compress(tfm, ctemplate[i].input,
  718. ilen, result, &dlen);
  719. if (ret) {
  720. printk(KERN_ERR "alg: comp: compression failed "
  721. "on test %d for %s: ret=%d\n", i + 1, algo,
  722. -ret);
  723. goto out;
  724. }
  725. if (dlen != ctemplate[i].outlen) {
  726. printk(KERN_ERR "alg: comp: Compression test %d "
  727. "failed for %s: output len = %d\n", i + 1, algo,
  728. dlen);
  729. ret = -EINVAL;
  730. goto out;
  731. }
  732. if (memcmp(result, ctemplate[i].output, dlen)) {
  733. printk(KERN_ERR "alg: comp: Compression test %d "
  734. "failed for %s\n", i + 1, algo);
  735. hexdump(result, dlen);
  736. ret = -EINVAL;
  737. goto out;
  738. }
  739. }
  740. for (i = 0; i < dtcount; i++) {
  741. int ilen, dlen = COMP_BUF_SIZE;
  742. memset(result, 0, sizeof (result));
  743. ilen = dtemplate[i].inlen;
  744. ret = crypto_comp_decompress(tfm, dtemplate[i].input,
  745. ilen, result, &dlen);
  746. if (ret) {
  747. printk(KERN_ERR "alg: comp: decompression failed "
  748. "on test %d for %s: ret=%d\n", i + 1, algo,
  749. -ret);
  750. goto out;
  751. }
  752. if (dlen != dtemplate[i].outlen) {
  753. printk(KERN_ERR "alg: comp: Decompression test %d "
  754. "failed for %s: output len = %d\n", i + 1, algo,
  755. dlen);
  756. ret = -EINVAL;
  757. goto out;
  758. }
  759. if (memcmp(result, dtemplate[i].output, dlen)) {
  760. printk(KERN_ERR "alg: comp: Decompression test %d "
  761. "failed for %s\n", i + 1, algo);
  762. hexdump(result, dlen);
  763. ret = -EINVAL;
  764. goto out;
  765. }
  766. }
  767. ret = 0;
  768. out:
  769. return ret;
  770. }
  771. static int test_pcomp(struct crypto_pcomp *tfm,
  772. struct pcomp_testvec *ctemplate,
  773. struct pcomp_testvec *dtemplate, int ctcount,
  774. int dtcount)
  775. {
  776. const char *algo = crypto_tfm_alg_driver_name(crypto_pcomp_tfm(tfm));
  777. unsigned int i;
  778. char result[COMP_BUF_SIZE];
  779. int error;
  780. for (i = 0; i < ctcount; i++) {
  781. struct comp_request req;
  782. error = crypto_compress_setup(tfm, ctemplate[i].params,
  783. ctemplate[i].paramsize);
  784. if (error) {
  785. pr_err("alg: pcomp: compression setup failed on test "
  786. "%d for %s: error=%d\n", i + 1, algo, error);
  787. return error;
  788. }
  789. error = crypto_compress_init(tfm);
  790. if (error) {
  791. pr_err("alg: pcomp: compression init failed on test "
  792. "%d for %s: error=%d\n", i + 1, algo, error);
  793. return error;
  794. }
  795. memset(result, 0, sizeof(result));
  796. req.next_in = ctemplate[i].input;
  797. req.avail_in = ctemplate[i].inlen / 2;
  798. req.next_out = result;
  799. req.avail_out = ctemplate[i].outlen / 2;
  800. error = crypto_compress_update(tfm, &req);
  801. if (error && (error != -EAGAIN || req.avail_in)) {
  802. pr_err("alg: pcomp: compression update failed on test "
  803. "%d for %s: error=%d\n", i + 1, algo, error);
  804. return error;
  805. }
  806. /* Add remaining input data */
  807. req.avail_in += (ctemplate[i].inlen + 1) / 2;
  808. error = crypto_compress_update(tfm, &req);
  809. if (error && (error != -EAGAIN || req.avail_in)) {
  810. pr_err("alg: pcomp: compression update failed on test "
  811. "%d for %s: error=%d\n", i + 1, algo, error);
  812. return error;
  813. }
  814. /* Provide remaining output space */
  815. req.avail_out += COMP_BUF_SIZE - ctemplate[i].outlen / 2;
  816. error = crypto_compress_final(tfm, &req);
  817. if (error) {
  818. pr_err("alg: pcomp: compression final failed on test "
  819. "%d for %s: error=%d\n", i + 1, algo, error);
  820. return error;
  821. }
  822. if (COMP_BUF_SIZE - req.avail_out != ctemplate[i].outlen) {
  823. pr_err("alg: comp: Compression test %d failed for %s: "
  824. "output len = %d (expected %d)\n", i + 1, algo,
  825. COMP_BUF_SIZE - req.avail_out,
  826. ctemplate[i].outlen);
  827. return -EINVAL;
  828. }
  829. if (memcmp(result, ctemplate[i].output, ctemplate[i].outlen)) {
  830. pr_err("alg: pcomp: Compression test %d failed for "
  831. "%s\n", i + 1, algo);
  832. hexdump(result, ctemplate[i].outlen);
  833. return -EINVAL;
  834. }
  835. }
  836. for (i = 0; i < dtcount; i++) {
  837. struct comp_request req;
  838. error = crypto_decompress_setup(tfm, dtemplate[i].params,
  839. dtemplate[i].paramsize);
  840. if (error) {
  841. pr_err("alg: pcomp: decompression setup failed on "
  842. "test %d for %s: error=%d\n", i + 1, algo,
  843. error);
  844. return error;
  845. }
  846. error = crypto_decompress_init(tfm);
  847. if (error) {
  848. pr_err("alg: pcomp: decompression init failed on test "
  849. "%d for %s: error=%d\n", i + 1, algo, error);
  850. return error;
  851. }
  852. memset(result, 0, sizeof(result));
  853. req.next_in = dtemplate[i].input;
  854. req.avail_in = dtemplate[i].inlen / 2;
  855. req.next_out = result;
  856. req.avail_out = dtemplate[i].outlen / 2;
  857. error = crypto_decompress_update(tfm, &req);
  858. if (error && (error != -EAGAIN || req.avail_in)) {
  859. pr_err("alg: pcomp: decompression update failed on "
  860. "test %d for %s: error=%d\n", i + 1, algo,
  861. error);
  862. return error;
  863. }
  864. /* Add remaining input data */
  865. req.avail_in += (dtemplate[i].inlen + 1) / 2;
  866. error = crypto_decompress_update(tfm, &req);
  867. if (error && (error != -EAGAIN || req.avail_in)) {
  868. pr_err("alg: pcomp: decompression update failed on "
  869. "test %d for %s: error=%d\n", i + 1, algo,
  870. error);
  871. return error;
  872. }
  873. /* Provide remaining output space */
  874. req.avail_out += COMP_BUF_SIZE - dtemplate[i].outlen / 2;
  875. error = crypto_decompress_final(tfm, &req);
  876. if (error && (error != -EAGAIN || req.avail_in)) {
  877. pr_err("alg: pcomp: decompression final failed on "
  878. "test %d for %s: error=%d\n", i + 1, algo,
  879. error);
  880. return error;
  881. }
  882. if (COMP_BUF_SIZE - req.avail_out != dtemplate[i].outlen) {
  883. pr_err("alg: comp: Decompression test %d failed for "
  884. "%s: output len = %d (expected %d)\n", i + 1,
  885. algo, COMP_BUF_SIZE - req.avail_out,
  886. dtemplate[i].outlen);
  887. return -EINVAL;
  888. }
  889. if (memcmp(result, dtemplate[i].output, dtemplate[i].outlen)) {
  890. pr_err("alg: pcomp: Decompression test %d failed for "
  891. "%s\n", i + 1, algo);
  892. hexdump(result, dtemplate[i].outlen);
  893. return -EINVAL;
  894. }
  895. }
  896. return 0;
  897. }
  898. static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
  899. u32 type, u32 mask)
  900. {
  901. struct crypto_aead *tfm;
  902. int err = 0;
  903. tfm = crypto_alloc_aead(driver, type, mask);
  904. if (IS_ERR(tfm)) {
  905. printk(KERN_ERR "alg: aead: Failed to load transform for %s: "
  906. "%ld\n", driver, PTR_ERR(tfm));
  907. return PTR_ERR(tfm);
  908. }
  909. if (desc->suite.aead.enc.vecs) {
  910. err = test_aead(tfm, ENCRYPT, desc->suite.aead.enc.vecs,
  911. desc->suite.aead.enc.count);
  912. if (err)
  913. goto out;
  914. }
  915. if (!err && desc->suite.aead.dec.vecs)
  916. err = test_aead(tfm, DECRYPT, desc->suite.aead.dec.vecs,
  917. desc->suite.aead.dec.count);
  918. out:
  919. crypto_free_aead(tfm);
  920. return err;
  921. }
  922. static int alg_test_cipher(const struct alg_test_desc *desc,
  923. const char *driver, u32 type, u32 mask)
  924. {
  925. struct crypto_cipher *tfm;
  926. int err = 0;
  927. tfm = crypto_alloc_cipher(driver, type, mask);
  928. if (IS_ERR(tfm)) {
  929. printk(KERN_ERR "alg: cipher: Failed to load transform for "
  930. "%s: %ld\n", driver, PTR_ERR(tfm));
  931. return PTR_ERR(tfm);
  932. }
  933. if (desc->suite.cipher.enc.vecs) {
  934. err = test_cipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  935. desc->suite.cipher.enc.count);
  936. if (err)
  937. goto out;
  938. }
  939. if (desc->suite.cipher.dec.vecs)
  940. err = test_cipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  941. desc->suite.cipher.dec.count);
  942. out:
  943. crypto_free_cipher(tfm);
  944. return err;
  945. }
  946. static int alg_test_skcipher(const struct alg_test_desc *desc,
  947. const char *driver, u32 type, u32 mask)
  948. {
  949. struct crypto_ablkcipher *tfm;
  950. int err = 0;
  951. tfm = crypto_alloc_ablkcipher(driver, type, mask);
  952. if (IS_ERR(tfm)) {
  953. printk(KERN_ERR "alg: skcipher: Failed to load transform for "
  954. "%s: %ld\n", driver, PTR_ERR(tfm));
  955. return PTR_ERR(tfm);
  956. }
  957. if (desc->suite.cipher.enc.vecs) {
  958. err = test_skcipher(tfm, ENCRYPT, desc->suite.cipher.enc.vecs,
  959. desc->suite.cipher.enc.count);
  960. if (err)
  961. goto out;
  962. }
  963. if (desc->suite.cipher.dec.vecs)
  964. err = test_skcipher(tfm, DECRYPT, desc->suite.cipher.dec.vecs,
  965. desc->suite.cipher.dec.count);
  966. out:
  967. crypto_free_ablkcipher(tfm);
  968. return err;
  969. }
  970. static int alg_test_comp(const struct alg_test_desc *desc, const char *driver,
  971. u32 type, u32 mask)
  972. {
  973. struct crypto_comp *tfm;
  974. int err;
  975. tfm = crypto_alloc_comp(driver, type, mask);
  976. if (IS_ERR(tfm)) {
  977. printk(KERN_ERR "alg: comp: Failed to load transform for %s: "
  978. "%ld\n", driver, PTR_ERR(tfm));
  979. return PTR_ERR(tfm);
  980. }
  981. err = test_comp(tfm, desc->suite.comp.comp.vecs,
  982. desc->suite.comp.decomp.vecs,
  983. desc->suite.comp.comp.count,
  984. desc->suite.comp.decomp.count);
  985. crypto_free_comp(tfm);
  986. return err;
  987. }
  988. static int alg_test_pcomp(const struct alg_test_desc *desc, const char *driver,
  989. u32 type, u32 mask)
  990. {
  991. struct crypto_pcomp *tfm;
  992. int err;
  993. tfm = crypto_alloc_pcomp(driver, type, mask);
  994. if (IS_ERR(tfm)) {
  995. pr_err("alg: pcomp: Failed to load transform for %s: %ld\n",
  996. driver, PTR_ERR(tfm));
  997. return PTR_ERR(tfm);
  998. }
  999. err = test_pcomp(tfm, desc->suite.pcomp.comp.vecs,
  1000. desc->suite.pcomp.decomp.vecs,
  1001. desc->suite.pcomp.comp.count,
  1002. desc->suite.pcomp.decomp.count);
  1003. crypto_free_pcomp(tfm);
  1004. return err;
  1005. }
  1006. static int alg_test_hash(const struct alg_test_desc *desc, const char *driver,
  1007. u32 type, u32 mask)
  1008. {
  1009. struct crypto_ahash *tfm;
  1010. int err;
  1011. tfm = crypto_alloc_ahash(driver, type, mask);
  1012. if (IS_ERR(tfm)) {
  1013. printk(KERN_ERR "alg: hash: Failed to load transform for %s: "
  1014. "%ld\n", driver, PTR_ERR(tfm));
  1015. return PTR_ERR(tfm);
  1016. }
  1017. err = test_hash(tfm, desc->suite.hash.vecs, desc->suite.hash.count);
  1018. crypto_free_ahash(tfm);
  1019. return err;
  1020. }
  1021. static int alg_test_crc32c(const struct alg_test_desc *desc,
  1022. const char *driver, u32 type, u32 mask)
  1023. {
  1024. struct crypto_shash *tfm;
  1025. u32 val;
  1026. int err;
  1027. err = alg_test_hash(desc, driver, type, mask);
  1028. if (err)
  1029. goto out;
  1030. tfm = crypto_alloc_shash(driver, type, mask);
  1031. if (IS_ERR(tfm)) {
  1032. printk(KERN_ERR "alg: crc32c: Failed to load transform for %s: "
  1033. "%ld\n", driver, PTR_ERR(tfm));
  1034. err = PTR_ERR(tfm);
  1035. goto out;
  1036. }
  1037. do {
  1038. struct {
  1039. struct shash_desc shash;
  1040. char ctx[crypto_shash_descsize(tfm)];
  1041. } sdesc;
  1042. sdesc.shash.tfm = tfm;
  1043. sdesc.shash.flags = 0;
  1044. *(u32 *)sdesc.ctx = le32_to_cpu(420553207);
  1045. err = crypto_shash_final(&sdesc.shash, (u8 *)&val);
  1046. if (err) {
  1047. printk(KERN_ERR "alg: crc32c: Operation failed for "
  1048. "%s: %d\n", driver, err);
  1049. break;
  1050. }
  1051. if (val != ~420553207) {
  1052. printk(KERN_ERR "alg: crc32c: Test failed for %s: "
  1053. "%d\n", driver, val);
  1054. err = -EINVAL;
  1055. }
  1056. } while (0);
  1057. crypto_free_shash(tfm);
  1058. out:
  1059. return err;
  1060. }
  1061. /* Please keep this list sorted by algorithm name. */
  1062. static const struct alg_test_desc alg_test_descs[] = {
  1063. {
  1064. .alg = "cbc(aes)",
  1065. .test = alg_test_skcipher,
  1066. .suite = {
  1067. .cipher = {
  1068. .enc = {
  1069. .vecs = aes_cbc_enc_tv_template,
  1070. .count = AES_CBC_ENC_TEST_VECTORS
  1071. },
  1072. .dec = {
  1073. .vecs = aes_cbc_dec_tv_template,
  1074. .count = AES_CBC_DEC_TEST_VECTORS
  1075. }
  1076. }
  1077. }
  1078. }, {
  1079. .alg = "cbc(anubis)",
  1080. .test = alg_test_skcipher,
  1081. .suite = {
  1082. .cipher = {
  1083. .enc = {
  1084. .vecs = anubis_cbc_enc_tv_template,
  1085. .count = ANUBIS_CBC_ENC_TEST_VECTORS
  1086. },
  1087. .dec = {
  1088. .vecs = anubis_cbc_dec_tv_template,
  1089. .count = ANUBIS_CBC_DEC_TEST_VECTORS
  1090. }
  1091. }
  1092. }
  1093. }, {
  1094. .alg = "cbc(blowfish)",
  1095. .test = alg_test_skcipher,
  1096. .suite = {
  1097. .cipher = {
  1098. .enc = {
  1099. .vecs = bf_cbc_enc_tv_template,
  1100. .count = BF_CBC_ENC_TEST_VECTORS
  1101. },
  1102. .dec = {
  1103. .vecs = bf_cbc_dec_tv_template,
  1104. .count = BF_CBC_DEC_TEST_VECTORS
  1105. }
  1106. }
  1107. }
  1108. }, {
  1109. .alg = "cbc(camellia)",
  1110. .test = alg_test_skcipher,
  1111. .suite = {
  1112. .cipher = {
  1113. .enc = {
  1114. .vecs = camellia_cbc_enc_tv_template,
  1115. .count = CAMELLIA_CBC_ENC_TEST_VECTORS
  1116. },
  1117. .dec = {
  1118. .vecs = camellia_cbc_dec_tv_template,
  1119. .count = CAMELLIA_CBC_DEC_TEST_VECTORS
  1120. }
  1121. }
  1122. }
  1123. }, {
  1124. .alg = "cbc(des)",
  1125. .test = alg_test_skcipher,
  1126. .suite = {
  1127. .cipher = {
  1128. .enc = {
  1129. .vecs = des_cbc_enc_tv_template,
  1130. .count = DES_CBC_ENC_TEST_VECTORS
  1131. },
  1132. .dec = {
  1133. .vecs = des_cbc_dec_tv_template,
  1134. .count = DES_CBC_DEC_TEST_VECTORS
  1135. }
  1136. }
  1137. }
  1138. }, {
  1139. .alg = "cbc(des3_ede)",
  1140. .test = alg_test_skcipher,
  1141. .suite = {
  1142. .cipher = {
  1143. .enc = {
  1144. .vecs = des3_ede_cbc_enc_tv_template,
  1145. .count = DES3_EDE_CBC_ENC_TEST_VECTORS
  1146. },
  1147. .dec = {
  1148. .vecs = des3_ede_cbc_dec_tv_template,
  1149. .count = DES3_EDE_CBC_DEC_TEST_VECTORS
  1150. }
  1151. }
  1152. }
  1153. }, {
  1154. .alg = "cbc(twofish)",
  1155. .test = alg_test_skcipher,
  1156. .suite = {
  1157. .cipher = {
  1158. .enc = {
  1159. .vecs = tf_cbc_enc_tv_template,
  1160. .count = TF_CBC_ENC_TEST_VECTORS
  1161. },
  1162. .dec = {
  1163. .vecs = tf_cbc_dec_tv_template,
  1164. .count = TF_CBC_DEC_TEST_VECTORS
  1165. }
  1166. }
  1167. }
  1168. }, {
  1169. .alg = "ccm(aes)",
  1170. .test = alg_test_aead,
  1171. .suite = {
  1172. .aead = {
  1173. .enc = {
  1174. .vecs = aes_ccm_enc_tv_template,
  1175. .count = AES_CCM_ENC_TEST_VECTORS
  1176. },
  1177. .dec = {
  1178. .vecs = aes_ccm_dec_tv_template,
  1179. .count = AES_CCM_DEC_TEST_VECTORS
  1180. }
  1181. }
  1182. }
  1183. }, {
  1184. .alg = "crc32c",
  1185. .test = alg_test_crc32c,
  1186. .suite = {
  1187. .hash = {
  1188. .vecs = crc32c_tv_template,
  1189. .count = CRC32C_TEST_VECTORS
  1190. }
  1191. }
  1192. }, {
  1193. .alg = "cts(cbc(aes))",
  1194. .test = alg_test_skcipher,
  1195. .suite = {
  1196. .cipher = {
  1197. .enc = {
  1198. .vecs = cts_mode_enc_tv_template,
  1199. .count = CTS_MODE_ENC_TEST_VECTORS
  1200. },
  1201. .dec = {
  1202. .vecs = cts_mode_dec_tv_template,
  1203. .count = CTS_MODE_DEC_TEST_VECTORS
  1204. }
  1205. }
  1206. }
  1207. }, {
  1208. .alg = "deflate",
  1209. .test = alg_test_comp,
  1210. .suite = {
  1211. .comp = {
  1212. .comp = {
  1213. .vecs = deflate_comp_tv_template,
  1214. .count = DEFLATE_COMP_TEST_VECTORS
  1215. },
  1216. .decomp = {
  1217. .vecs = deflate_decomp_tv_template,
  1218. .count = DEFLATE_DECOMP_TEST_VECTORS
  1219. }
  1220. }
  1221. }
  1222. }, {
  1223. .alg = "ecb(aes)",
  1224. .test = alg_test_skcipher,
  1225. .suite = {
  1226. .cipher = {
  1227. .enc = {
  1228. .vecs = aes_enc_tv_template,
  1229. .count = AES_ENC_TEST_VECTORS
  1230. },
  1231. .dec = {
  1232. .vecs = aes_dec_tv_template,
  1233. .count = AES_DEC_TEST_VECTORS
  1234. }
  1235. }
  1236. }
  1237. }, {
  1238. .alg = "ecb(anubis)",
  1239. .test = alg_test_skcipher,
  1240. .suite = {
  1241. .cipher = {
  1242. .enc = {
  1243. .vecs = anubis_enc_tv_template,
  1244. .count = ANUBIS_ENC_TEST_VECTORS
  1245. },
  1246. .dec = {
  1247. .vecs = anubis_dec_tv_template,
  1248. .count = ANUBIS_DEC_TEST_VECTORS
  1249. }
  1250. }
  1251. }
  1252. }, {
  1253. .alg = "ecb(arc4)",
  1254. .test = alg_test_skcipher,
  1255. .suite = {
  1256. .cipher = {
  1257. .enc = {
  1258. .vecs = arc4_enc_tv_template,
  1259. .count = ARC4_ENC_TEST_VECTORS
  1260. },
  1261. .dec = {
  1262. .vecs = arc4_dec_tv_template,
  1263. .count = ARC4_DEC_TEST_VECTORS
  1264. }
  1265. }
  1266. }
  1267. }, {
  1268. .alg = "ecb(blowfish)",
  1269. .test = alg_test_skcipher,
  1270. .suite = {
  1271. .cipher = {
  1272. .enc = {
  1273. .vecs = bf_enc_tv_template,
  1274. .count = BF_ENC_TEST_VECTORS
  1275. },
  1276. .dec = {
  1277. .vecs = bf_dec_tv_template,
  1278. .count = BF_DEC_TEST_VECTORS
  1279. }
  1280. }
  1281. }
  1282. }, {
  1283. .alg = "ecb(camellia)",
  1284. .test = alg_test_skcipher,
  1285. .suite = {
  1286. .cipher = {
  1287. .enc = {
  1288. .vecs = camellia_enc_tv_template,
  1289. .count = CAMELLIA_ENC_TEST_VECTORS
  1290. },
  1291. .dec = {
  1292. .vecs = camellia_dec_tv_template,
  1293. .count = CAMELLIA_DEC_TEST_VECTORS
  1294. }
  1295. }
  1296. }
  1297. }, {
  1298. .alg = "ecb(cast5)",
  1299. .test = alg_test_skcipher,
  1300. .suite = {
  1301. .cipher = {
  1302. .enc = {
  1303. .vecs = cast5_enc_tv_template,
  1304. .count = CAST5_ENC_TEST_VECTORS
  1305. },
  1306. .dec = {
  1307. .vecs = cast5_dec_tv_template,
  1308. .count = CAST5_DEC_TEST_VECTORS
  1309. }
  1310. }
  1311. }
  1312. }, {
  1313. .alg = "ecb(cast6)",
  1314. .test = alg_test_skcipher,
  1315. .suite = {
  1316. .cipher = {
  1317. .enc = {
  1318. .vecs = cast6_enc_tv_template,
  1319. .count = CAST6_ENC_TEST_VECTORS
  1320. },
  1321. .dec = {
  1322. .vecs = cast6_dec_tv_template,
  1323. .count = CAST6_DEC_TEST_VECTORS
  1324. }
  1325. }
  1326. }
  1327. }, {
  1328. .alg = "ecb(des)",
  1329. .test = alg_test_skcipher,
  1330. .suite = {
  1331. .cipher = {
  1332. .enc = {
  1333. .vecs = des_enc_tv_template,
  1334. .count = DES_ENC_TEST_VECTORS
  1335. },
  1336. .dec = {
  1337. .vecs = des_dec_tv_template,
  1338. .count = DES_DEC_TEST_VECTORS
  1339. }
  1340. }
  1341. }
  1342. }, {
  1343. .alg = "ecb(des3_ede)",
  1344. .test = alg_test_skcipher,
  1345. .suite = {
  1346. .cipher = {
  1347. .enc = {
  1348. .vecs = des3_ede_enc_tv_template,
  1349. .count = DES3_EDE_ENC_TEST_VECTORS
  1350. },
  1351. .dec = {
  1352. .vecs = des3_ede_dec_tv_template,
  1353. .count = DES3_EDE_DEC_TEST_VECTORS
  1354. }
  1355. }
  1356. }
  1357. }, {
  1358. .alg = "ecb(khazad)",
  1359. .test = alg_test_skcipher,
  1360. .suite = {
  1361. .cipher = {
  1362. .enc = {
  1363. .vecs = khazad_enc_tv_template,
  1364. .count = KHAZAD_ENC_TEST_VECTORS
  1365. },
  1366. .dec = {
  1367. .vecs = khazad_dec_tv_template,
  1368. .count = KHAZAD_DEC_TEST_VECTORS
  1369. }
  1370. }
  1371. }
  1372. }, {
  1373. .alg = "ecb(seed)",
  1374. .test = alg_test_skcipher,
  1375. .suite = {
  1376. .cipher = {
  1377. .enc = {
  1378. .vecs = seed_enc_tv_template,
  1379. .count = SEED_ENC_TEST_VECTORS
  1380. },
  1381. .dec = {
  1382. .vecs = seed_dec_tv_template,
  1383. .count = SEED_DEC_TEST_VECTORS
  1384. }
  1385. }
  1386. }
  1387. }, {
  1388. .alg = "ecb(serpent)",
  1389. .test = alg_test_skcipher,
  1390. .suite = {
  1391. .cipher = {
  1392. .enc = {
  1393. .vecs = serpent_enc_tv_template,
  1394. .count = SERPENT_ENC_TEST_VECTORS
  1395. },
  1396. .dec = {
  1397. .vecs = serpent_dec_tv_template,
  1398. .count = SERPENT_DEC_TEST_VECTORS
  1399. }
  1400. }
  1401. }
  1402. }, {
  1403. .alg = "ecb(tea)",
  1404. .test = alg_test_skcipher,
  1405. .suite = {
  1406. .cipher = {
  1407. .enc = {
  1408. .vecs = tea_enc_tv_template,
  1409. .count = TEA_ENC_TEST_VECTORS
  1410. },
  1411. .dec = {
  1412. .vecs = tea_dec_tv_template,
  1413. .count = TEA_DEC_TEST_VECTORS
  1414. }
  1415. }
  1416. }
  1417. }, {
  1418. .alg = "ecb(tnepres)",
  1419. .test = alg_test_skcipher,
  1420. .suite = {
  1421. .cipher = {
  1422. .enc = {
  1423. .vecs = tnepres_enc_tv_template,
  1424. .count = TNEPRES_ENC_TEST_VECTORS
  1425. },
  1426. .dec = {
  1427. .vecs = tnepres_dec_tv_template,
  1428. .count = TNEPRES_DEC_TEST_VECTORS
  1429. }
  1430. }
  1431. }
  1432. }, {
  1433. .alg = "ecb(twofish)",
  1434. .test = alg_test_skcipher,
  1435. .suite = {
  1436. .cipher = {
  1437. .enc = {
  1438. .vecs = tf_enc_tv_template,
  1439. .count = TF_ENC_TEST_VECTORS
  1440. },
  1441. .dec = {
  1442. .vecs = tf_dec_tv_template,
  1443. .count = TF_DEC_TEST_VECTORS
  1444. }
  1445. }
  1446. }
  1447. }, {
  1448. .alg = "ecb(xeta)",
  1449. .test = alg_test_skcipher,
  1450. .suite = {
  1451. .cipher = {
  1452. .enc = {
  1453. .vecs = xeta_enc_tv_template,
  1454. .count = XETA_ENC_TEST_VECTORS
  1455. },
  1456. .dec = {
  1457. .vecs = xeta_dec_tv_template,
  1458. .count = XETA_DEC_TEST_VECTORS
  1459. }
  1460. }
  1461. }
  1462. }, {
  1463. .alg = "ecb(xtea)",
  1464. .test = alg_test_skcipher,
  1465. .suite = {
  1466. .cipher = {
  1467. .enc = {
  1468. .vecs = xtea_enc_tv_template,
  1469. .count = XTEA_ENC_TEST_VECTORS
  1470. },
  1471. .dec = {
  1472. .vecs = xtea_dec_tv_template,
  1473. .count = XTEA_DEC_TEST_VECTORS
  1474. }
  1475. }
  1476. }
  1477. }, {
  1478. .alg = "gcm(aes)",
  1479. .test = alg_test_aead,
  1480. .suite = {
  1481. .aead = {
  1482. .enc = {
  1483. .vecs = aes_gcm_enc_tv_template,
  1484. .count = AES_GCM_ENC_TEST_VECTORS
  1485. },
  1486. .dec = {
  1487. .vecs = aes_gcm_dec_tv_template,
  1488. .count = AES_GCM_DEC_TEST_VECTORS
  1489. }
  1490. }
  1491. }
  1492. }, {
  1493. .alg = "hmac(md5)",
  1494. .test = alg_test_hash,
  1495. .suite = {
  1496. .hash = {
  1497. .vecs = hmac_md5_tv_template,
  1498. .count = HMAC_MD5_TEST_VECTORS
  1499. }
  1500. }
  1501. }, {
  1502. .alg = "hmac(rmd128)",
  1503. .test = alg_test_hash,
  1504. .suite = {
  1505. .hash = {
  1506. .vecs = hmac_rmd128_tv_template,
  1507. .count = HMAC_RMD128_TEST_VECTORS
  1508. }
  1509. }
  1510. }, {
  1511. .alg = "hmac(rmd160)",
  1512. .test = alg_test_hash,
  1513. .suite = {
  1514. .hash = {
  1515. .vecs = hmac_rmd160_tv_template,
  1516. .count = HMAC_RMD160_TEST_VECTORS
  1517. }
  1518. }
  1519. }, {
  1520. .alg = "hmac(sha1)",
  1521. .test = alg_test_hash,
  1522. .suite = {
  1523. .hash = {
  1524. .vecs = hmac_sha1_tv_template,
  1525. .count = HMAC_SHA1_TEST_VECTORS
  1526. }
  1527. }
  1528. }, {
  1529. .alg = "hmac(sha224)",
  1530. .test = alg_test_hash,
  1531. .suite = {
  1532. .hash = {
  1533. .vecs = hmac_sha224_tv_template,
  1534. .count = HMAC_SHA224_TEST_VECTORS
  1535. }
  1536. }
  1537. }, {
  1538. .alg = "hmac(sha256)",
  1539. .test = alg_test_hash,
  1540. .suite = {
  1541. .hash = {
  1542. .vecs = hmac_sha256_tv_template,
  1543. .count = HMAC_SHA256_TEST_VECTORS
  1544. }
  1545. }
  1546. }, {
  1547. .alg = "hmac(sha384)",
  1548. .test = alg_test_hash,
  1549. .suite = {
  1550. .hash = {
  1551. .vecs = hmac_sha384_tv_template,
  1552. .count = HMAC_SHA384_TEST_VECTORS
  1553. }
  1554. }
  1555. }, {
  1556. .alg = "hmac(sha512)",
  1557. .test = alg_test_hash,
  1558. .suite = {
  1559. .hash = {
  1560. .vecs = hmac_sha512_tv_template,
  1561. .count = HMAC_SHA512_TEST_VECTORS
  1562. }
  1563. }
  1564. }, {
  1565. .alg = "lrw(aes)",
  1566. .test = alg_test_skcipher,
  1567. .suite = {
  1568. .cipher = {
  1569. .enc = {
  1570. .vecs = aes_lrw_enc_tv_template,
  1571. .count = AES_LRW_ENC_TEST_VECTORS
  1572. },
  1573. .dec = {
  1574. .vecs = aes_lrw_dec_tv_template,
  1575. .count = AES_LRW_DEC_TEST_VECTORS
  1576. }
  1577. }
  1578. }
  1579. }, {
  1580. .alg = "lzo",
  1581. .test = alg_test_comp,
  1582. .suite = {
  1583. .comp = {
  1584. .comp = {
  1585. .vecs = lzo_comp_tv_template,
  1586. .count = LZO_COMP_TEST_VECTORS
  1587. },
  1588. .decomp = {
  1589. .vecs = lzo_decomp_tv_template,
  1590. .count = LZO_DECOMP_TEST_VECTORS
  1591. }
  1592. }
  1593. }
  1594. }, {
  1595. .alg = "md4",
  1596. .test = alg_test_hash,
  1597. .suite = {
  1598. .hash = {
  1599. .vecs = md4_tv_template,
  1600. .count = MD4_TEST_VECTORS
  1601. }
  1602. }
  1603. }, {
  1604. .alg = "md5",
  1605. .test = alg_test_hash,
  1606. .suite = {
  1607. .hash = {
  1608. .vecs = md5_tv_template,
  1609. .count = MD5_TEST_VECTORS
  1610. }
  1611. }
  1612. }, {
  1613. .alg = "michael_mic",
  1614. .test = alg_test_hash,
  1615. .suite = {
  1616. .hash = {
  1617. .vecs = michael_mic_tv_template,
  1618. .count = MICHAEL_MIC_TEST_VECTORS
  1619. }
  1620. }
  1621. }, {
  1622. .alg = "pcbc(fcrypt)",
  1623. .test = alg_test_skcipher,
  1624. .suite = {
  1625. .cipher = {
  1626. .enc = {
  1627. .vecs = fcrypt_pcbc_enc_tv_template,
  1628. .count = FCRYPT_ENC_TEST_VECTORS
  1629. },
  1630. .dec = {
  1631. .vecs = fcrypt_pcbc_dec_tv_template,
  1632. .count = FCRYPT_DEC_TEST_VECTORS
  1633. }
  1634. }
  1635. }
  1636. }, {
  1637. .alg = "rfc3686(ctr(aes))",
  1638. .test = alg_test_skcipher,
  1639. .suite = {
  1640. .cipher = {
  1641. .enc = {
  1642. .vecs = aes_ctr_enc_tv_template,
  1643. .count = AES_CTR_ENC_TEST_VECTORS
  1644. },
  1645. .dec = {
  1646. .vecs = aes_ctr_dec_tv_template,
  1647. .count = AES_CTR_DEC_TEST_VECTORS
  1648. }
  1649. }
  1650. }
  1651. }, {
  1652. .alg = "rmd128",
  1653. .test = alg_test_hash,
  1654. .suite = {
  1655. .hash = {
  1656. .vecs = rmd128_tv_template,
  1657. .count = RMD128_TEST_VECTORS
  1658. }
  1659. }
  1660. }, {
  1661. .alg = "rmd160",
  1662. .test = alg_test_hash,
  1663. .suite = {
  1664. .hash = {
  1665. .vecs = rmd160_tv_template,
  1666. .count = RMD160_TEST_VECTORS
  1667. }
  1668. }
  1669. }, {
  1670. .alg = "rmd256",
  1671. .test = alg_test_hash,
  1672. .suite = {
  1673. .hash = {
  1674. .vecs = rmd256_tv_template,
  1675. .count = RMD256_TEST_VECTORS
  1676. }
  1677. }
  1678. }, {
  1679. .alg = "rmd320",
  1680. .test = alg_test_hash,
  1681. .suite = {
  1682. .hash = {
  1683. .vecs = rmd320_tv_template,
  1684. .count = RMD320_TEST_VECTORS
  1685. }
  1686. }
  1687. }, {
  1688. .alg = "salsa20",
  1689. .test = alg_test_skcipher,
  1690. .suite = {
  1691. .cipher = {
  1692. .enc = {
  1693. .vecs = salsa20_stream_enc_tv_template,
  1694. .count = SALSA20_STREAM_ENC_TEST_VECTORS
  1695. }
  1696. }
  1697. }
  1698. }, {
  1699. .alg = "sha1",
  1700. .test = alg_test_hash,
  1701. .suite = {
  1702. .hash = {
  1703. .vecs = sha1_tv_template,
  1704. .count = SHA1_TEST_VECTORS
  1705. }
  1706. }
  1707. }, {
  1708. .alg = "sha224",
  1709. .test = alg_test_hash,
  1710. .suite = {
  1711. .hash = {
  1712. .vecs = sha224_tv_template,
  1713. .count = SHA224_TEST_VECTORS
  1714. }
  1715. }
  1716. }, {
  1717. .alg = "sha256",
  1718. .test = alg_test_hash,
  1719. .suite = {
  1720. .hash = {
  1721. .vecs = sha256_tv_template,
  1722. .count = SHA256_TEST_VECTORS
  1723. }
  1724. }
  1725. }, {
  1726. .alg = "sha384",
  1727. .test = alg_test_hash,
  1728. .suite = {
  1729. .hash = {
  1730. .vecs = sha384_tv_template,
  1731. .count = SHA384_TEST_VECTORS
  1732. }
  1733. }
  1734. }, {
  1735. .alg = "sha512",
  1736. .test = alg_test_hash,
  1737. .suite = {
  1738. .hash = {
  1739. .vecs = sha512_tv_template,
  1740. .count = SHA512_TEST_VECTORS
  1741. }
  1742. }
  1743. }, {
  1744. .alg = "tgr128",
  1745. .test = alg_test_hash,
  1746. .suite = {
  1747. .hash = {
  1748. .vecs = tgr128_tv_template,
  1749. .count = TGR128_TEST_VECTORS
  1750. }
  1751. }
  1752. }, {
  1753. .alg = "tgr160",
  1754. .test = alg_test_hash,
  1755. .suite = {
  1756. .hash = {
  1757. .vecs = tgr160_tv_template,
  1758. .count = TGR160_TEST_VECTORS
  1759. }
  1760. }
  1761. }, {
  1762. .alg = "tgr192",
  1763. .test = alg_test_hash,
  1764. .suite = {
  1765. .hash = {
  1766. .vecs = tgr192_tv_template,
  1767. .count = TGR192_TEST_VECTORS
  1768. }
  1769. }
  1770. }, {
  1771. .alg = "wp256",
  1772. .test = alg_test_hash,
  1773. .suite = {
  1774. .hash = {
  1775. .vecs = wp256_tv_template,
  1776. .count = WP256_TEST_VECTORS
  1777. }
  1778. }
  1779. }, {
  1780. .alg = "wp384",
  1781. .test = alg_test_hash,
  1782. .suite = {
  1783. .hash = {
  1784. .vecs = wp384_tv_template,
  1785. .count = WP384_TEST_VECTORS
  1786. }
  1787. }
  1788. }, {
  1789. .alg = "wp512",
  1790. .test = alg_test_hash,
  1791. .suite = {
  1792. .hash = {
  1793. .vecs = wp512_tv_template,
  1794. .count = WP512_TEST_VECTORS
  1795. }
  1796. }
  1797. }, {
  1798. .alg = "xcbc(aes)",
  1799. .test = alg_test_hash,
  1800. .suite = {
  1801. .hash = {
  1802. .vecs = aes_xcbc128_tv_template,
  1803. .count = XCBC_AES_TEST_VECTORS
  1804. }
  1805. }
  1806. }, {
  1807. .alg = "xts(aes)",
  1808. .test = alg_test_skcipher,
  1809. .suite = {
  1810. .cipher = {
  1811. .enc = {
  1812. .vecs = aes_xts_enc_tv_template,
  1813. .count = AES_XTS_ENC_TEST_VECTORS
  1814. },
  1815. .dec = {
  1816. .vecs = aes_xts_dec_tv_template,
  1817. .count = AES_XTS_DEC_TEST_VECTORS
  1818. }
  1819. }
  1820. }
  1821. }, {
  1822. .alg = "zlib",
  1823. .test = alg_test_pcomp,
  1824. .suite = {
  1825. .pcomp = {
  1826. .comp = {
  1827. .vecs = zlib_comp_tv_template,
  1828. .count = ZLIB_COMP_TEST_VECTORS
  1829. },
  1830. .decomp = {
  1831. .vecs = zlib_decomp_tv_template,
  1832. .count = ZLIB_DECOMP_TEST_VECTORS
  1833. }
  1834. }
  1835. }
  1836. }
  1837. };
  1838. static int alg_find_test(const char *alg)
  1839. {
  1840. int start = 0;
  1841. int end = ARRAY_SIZE(alg_test_descs);
  1842. while (start < end) {
  1843. int i = (start + end) / 2;
  1844. int diff = strcmp(alg_test_descs[i].alg, alg);
  1845. if (diff > 0) {
  1846. end = i;
  1847. continue;
  1848. }
  1849. if (diff < 0) {
  1850. start = i + 1;
  1851. continue;
  1852. }
  1853. return i;
  1854. }
  1855. return -1;
  1856. }
  1857. int alg_test(const char *driver, const char *alg, u32 type, u32 mask)
  1858. {
  1859. int i;
  1860. int rc;
  1861. if ((type & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_CIPHER) {
  1862. char nalg[CRYPTO_MAX_ALG_NAME];
  1863. if (snprintf(nalg, sizeof(nalg), "ecb(%s)", alg) >=
  1864. sizeof(nalg))
  1865. return -ENAMETOOLONG;
  1866. i = alg_find_test(nalg);
  1867. if (i < 0)
  1868. goto notest;
  1869. return alg_test_cipher(alg_test_descs + i, driver, type, mask);
  1870. }
  1871. i = alg_find_test(alg);
  1872. if (i < 0)
  1873. goto notest;
  1874. rc = alg_test_descs[i].test(alg_test_descs + i, driver,
  1875. type, mask);
  1876. if (fips_enabled && rc)
  1877. panic("%s: %s alg self test failed in fips mode!\n", driver, alg);
  1878. return rc;
  1879. notest:
  1880. printk(KERN_INFO "alg: No test for %s (%s)\n", alg, driver);
  1881. return 0;
  1882. }
  1883. EXPORT_SYMBOL_GPL(alg_test);
  1884. int __init testmgr_init(void)
  1885. {
  1886. int i;
  1887. for (i = 0; i < XBUFSIZE; i++) {
  1888. xbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  1889. if (!xbuf[i])
  1890. goto err_free_xbuf;
  1891. }
  1892. for (i = 0; i < XBUFSIZE; i++) {
  1893. axbuf[i] = (void *)__get_free_page(GFP_KERNEL);
  1894. if (!axbuf[i])
  1895. goto err_free_axbuf;
  1896. }
  1897. return 0;
  1898. err_free_axbuf:
  1899. for (i = 0; i < XBUFSIZE && axbuf[i]; i++)
  1900. free_page((unsigned long)axbuf[i]);
  1901. err_free_xbuf:
  1902. for (i = 0; i < XBUFSIZE && xbuf[i]; i++)
  1903. free_page((unsigned long)xbuf[i]);
  1904. return -ENOMEM;
  1905. }
  1906. void testmgr_exit(void)
  1907. {
  1908. int i;
  1909. for (i = 0; i < XBUFSIZE; i++)
  1910. free_page((unsigned long)axbuf[i]);
  1911. for (i = 0; i < XBUFSIZE; i++)
  1912. free_page((unsigned long)xbuf[i]);
  1913. }