testmgr.c 54 KB

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