testmgr.c 54 KB

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