testmgr.c 51 KB

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