lkcapi_glue.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741
  1. /* lkcapi_glue.c -- glue logic to register wolfCrypt implementations with
  2. * the Linux Kernel Cryptosystem
  3. *
  4. * Copyright (C) 2006-2024 wolfSSL Inc.
  5. *
  6. * This file is part of wolfSSL.
  7. *
  8. * wolfSSL is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * wolfSSL is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  21. */
  22. /* included by linuxkm/module_hooks.c */
  23. #ifndef LINUXKM_LKCAPI_REGISTER
  24. #error lkcapi_glue.c included in non-LINUXKM_LKCAPI_REGISTER project.
  25. #endif
  26. #ifndef WOLFSSL_LINUXKM_LKCAPI_PRIORITY
  27. /* Larger number means higher priority. The highest in-tree priority is 4001,
  28. * in the Cavium driver.
  29. */
  30. #define WOLFSSL_LINUXKM_LKCAPI_PRIORITY 10000
  31. #endif
  32. #ifndef NO_AES
  33. /* note the FIPS code will be returned on failure even in non-FIPS builds. */
  34. #define LINUXKM_LKCAPI_AES_KAT_MISMATCH_E AES_KAT_FIPS_E
  35. #define LINUXKM_LKCAPI_AESGCM_KAT_MISMATCH_E AESGCM_KAT_FIPS_E
  36. #define WOLFKM_AESCBC_NAME "cbc(aes)"
  37. #define WOLFKM_AESCFB_NAME "cfb(aes)"
  38. #define WOLFKM_AESGCM_NAME "gcm(aes)"
  39. #define WOLFKM_AESXTS_NAME "xts(aes)"
  40. #ifdef WOLFSSL_AESNI
  41. #define WOLFKM_DRIVER_ISA_EXT "-aesni"
  42. #else
  43. #define WOLFKM_DRIVER_ISA_EXT ""
  44. #endif
  45. #ifdef HAVE_FIPS
  46. #ifndef HAVE_FIPS_VERSION
  47. #define WOLFKM_DRIVER_FIPS "-fips-140"
  48. #elif HAVE_FIPS_VERSION >= 5
  49. #define WOLFKM_DRIVER_FIPS "-fips-140-3"
  50. #elif HAVE_FIPS_VERSION == 2
  51. #define WOLFKM_DRIVER_FIPS "-fips-140-2"
  52. #else
  53. #define WOLFKM_DRIVER_FIPS "-fips-140"
  54. #endif
  55. #else
  56. #define WOLFKM_DRIVER_FIPS ""
  57. #endif
  58. #define WOLFKM_DRIVER_SUFFIX \
  59. WOLFKM_DRIVER_ISA_EXT WOLFKM_DRIVER_FIPS "-wolfcrypt"
  60. #define WOLFKM_AESCBC_DRIVER ("cbc-aes" WOLFKM_DRIVER_SUFFIX)
  61. #define WOLFKM_AESCFB_DRIVER ("cfb-aes" WOLFKM_DRIVER_SUFFIX)
  62. #define WOLFKM_AESGCM_DRIVER ("gcm-aes" WOLFKM_DRIVER_SUFFIX)
  63. #define WOLFKM_AESXTS_DRIVER ("xts-aes" WOLFKM_DRIVER_SUFFIX)
  64. #if defined(HAVE_AES_CBC) && \
  65. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  66. defined(LINUXKM_LKCAPI_REGISTER_AESCBC))
  67. static int linuxkm_test_aescbc(void);
  68. #endif
  69. #if defined(WOLFSSL_AES_CFB) && \
  70. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  71. defined(LINUXKM_LKCAPI_REGISTER_AESCFB))
  72. static int linuxkm_test_aescfb(void);
  73. #endif
  74. #if defined(HAVE_AESGCM) && \
  75. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  76. defined(LINUXKM_LKCAPI_REGISTER_AESGCM))
  77. static int linuxkm_test_aesgcm(void);
  78. #endif
  79. #if defined(WOLFSSL_AES_XTS) && \
  80. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  81. defined(LINUXKM_LKCAPI_REGISTER_AESXTS))
  82. static int linuxkm_test_aesxts(void);
  83. #endif
  84. /* km_AesX(): wrappers to wolfcrypt wc_AesX functions and
  85. * structures. */
  86. #include <wolfssl/wolfcrypt/aes.h>
  87. struct km_AesCtx {
  88. Aes *aes_encrypt; /* allocated in km_AesInitCommon() to assure
  89. * alignment, needed for AESNI.
  90. */
  91. Aes *aes_decrypt; /* same. */
  92. };
  93. #if defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  94. defined(LINUXKM_LKCAPI_REGISTER_AESCBC) || \
  95. defined(LINUXKM_LKCAPI_REGISTER_AESCFB) || \
  96. defined(LINUXKM_LKCAPI_REGISTER_AESGCM)
  97. static void km_AesExitCommon(struct km_AesCtx * ctx);
  98. static int km_AesInitCommon(
  99. struct km_AesCtx * ctx,
  100. const char * name,
  101. int need_decryption)
  102. {
  103. int err;
  104. ctx->aes_encrypt = (Aes *)malloc(sizeof(*ctx->aes_encrypt));
  105. if (! ctx->aes_encrypt) {
  106. pr_err("%s: allocation of %zu bytes for encryption key failed.\n",
  107. name, sizeof(*ctx->aes_encrypt));
  108. return MEMORY_E;
  109. }
  110. err = wc_AesInit(ctx->aes_encrypt, NULL, INVALID_DEVID);
  111. if (unlikely(err)) {
  112. pr_err("%s: wc_AesInit failed: %d\n", name, err);
  113. free(ctx->aes_encrypt);
  114. ctx->aes_encrypt = NULL;
  115. return -EINVAL;
  116. }
  117. if (! need_decryption) {
  118. ctx->aes_decrypt = NULL;
  119. return 0;
  120. }
  121. ctx->aes_decrypt = (Aes *)malloc(sizeof(*ctx->aes_decrypt));
  122. if (! ctx->aes_decrypt) {
  123. pr_err("%s: allocation of %zu bytes for decryption key failed.\n",
  124. name, sizeof(*ctx->aes_decrypt));
  125. km_AesExitCommon(ctx);
  126. return MEMORY_E;
  127. }
  128. err = wc_AesInit(ctx->aes_decrypt, NULL, INVALID_DEVID);
  129. if (unlikely(err)) {
  130. pr_err("%s: wc_AesInit failed: %d\n", name, err);
  131. free(ctx->aes_decrypt);
  132. ctx->aes_decrypt = NULL;
  133. km_AesExitCommon(ctx);
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static void km_AesExitCommon(struct km_AesCtx * ctx)
  139. {
  140. if (ctx->aes_encrypt) {
  141. wc_AesFree(ctx->aes_encrypt);
  142. free(ctx->aes_encrypt);
  143. ctx->aes_encrypt = NULL;
  144. }
  145. if (ctx->aes_decrypt) {
  146. wc_AesFree(ctx->aes_decrypt);
  147. free(ctx->aes_decrypt);
  148. ctx->aes_decrypt = NULL;
  149. }
  150. }
  151. #if defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  152. defined(LINUXKM_LKCAPI_REGISTER_AESCBC) || \
  153. defined(LINUXKM_LKCAPI_REGISTER_AESCFB)
  154. static int km_AesSetKeyCommon(struct km_AesCtx * ctx, const u8 *in_key,
  155. unsigned int key_len, const char * name)
  156. {
  157. int err;
  158. err = wc_AesSetKey(ctx->aes_encrypt, in_key, key_len, NULL, AES_ENCRYPTION);
  159. if (unlikely(err)) {
  160. pr_err("%s: wc_AesSetKey for encryption key failed: %d\n", name, err);
  161. return -ENOKEY;
  162. }
  163. if (ctx->aes_decrypt) {
  164. err = wc_AesSetKey(ctx->aes_decrypt, in_key, key_len, NULL,
  165. AES_DECRYPTION);
  166. if (unlikely(err)) {
  167. pr_err("%s: wc_AesSetKey for decryption key failed: %d\n",
  168. name, err);
  169. return -ENOKEY;
  170. }
  171. }
  172. return 0;
  173. }
  174. static void km_AesExit(struct crypto_skcipher *tfm)
  175. {
  176. struct km_AesCtx * ctx = crypto_skcipher_ctx(tfm);
  177. km_AesExitCommon(ctx);
  178. }
  179. #endif /* LINUXKM_LKCAPI_REGISTER_ALL ||
  180. * LINUXKM_LKCAPI_REGISTER_AESCBC ||
  181. * LINUXKM_LKCAPI_REGISTER_AESCFB
  182. */
  183. #endif /* LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESCBC ||
  184. * LINUXKM_LKCAPI_REGISTER_AESCFB || LINUXKM_LKCAPI_REGISTER_AESGCM
  185. */
  186. #if defined(HAVE_AES_CBC) && \
  187. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  188. defined(LINUXKM_LKCAPI_REGISTER_AESCBC))
  189. static int km_AesCbcInit(struct crypto_skcipher *tfm)
  190. {
  191. struct km_AesCtx * ctx = crypto_skcipher_ctx(tfm);
  192. return km_AesInitCommon(ctx, WOLFKM_AESCBC_DRIVER, 1);
  193. }
  194. static int km_AesCbcSetKey(struct crypto_skcipher *tfm, const u8 *in_key,
  195. unsigned int key_len)
  196. {
  197. struct km_AesCtx * ctx = crypto_skcipher_ctx(tfm);
  198. return km_AesSetKeyCommon(ctx, in_key, key_len, WOLFKM_AESCBC_DRIVER);
  199. }
  200. static int km_AesCbcEncrypt(struct skcipher_request *req)
  201. {
  202. struct crypto_skcipher * tfm = NULL;
  203. struct km_AesCtx * ctx = NULL;
  204. struct skcipher_walk walk;
  205. unsigned int nbytes = 0;
  206. int err = 0;
  207. tfm = crypto_skcipher_reqtfm(req);
  208. ctx = crypto_skcipher_ctx(tfm);
  209. err = skcipher_walk_virt(&walk, req, false);
  210. if (unlikely(err)) {
  211. pr_err("%s: skcipher_walk_virt failed: %d\n",
  212. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  213. return err;
  214. }
  215. while ((nbytes = walk.nbytes) != 0) {
  216. err = wc_AesSetIV(ctx->aes_encrypt, walk.iv);
  217. if (unlikely(err)) {
  218. pr_err("%s: wc_AesSetIV failed: %d\n",
  219. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  220. return -EINVAL;
  221. }
  222. err = wc_AesCbcEncrypt(ctx->aes_encrypt, walk.dst.virt.addr,
  223. walk.src.virt.addr, nbytes);
  224. if (unlikely(err)) {
  225. pr_err("%s: wc_AesCbcEncrypt failed: %d\n",
  226. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  227. return -EINVAL;
  228. }
  229. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  230. if (unlikely(err)) {
  231. pr_err("%s: skcipher_walk_done failed: %d\n",
  232. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  233. return err;
  234. }
  235. }
  236. return err;
  237. }
  238. static int km_AesCbcDecrypt(struct skcipher_request *req)
  239. {
  240. struct crypto_skcipher * tfm = NULL;
  241. struct km_AesCtx * ctx = NULL;
  242. struct skcipher_walk walk;
  243. unsigned int nbytes = 0;
  244. int err = 0;
  245. tfm = crypto_skcipher_reqtfm(req);
  246. ctx = crypto_skcipher_ctx(tfm);
  247. err = skcipher_walk_virt(&walk, req, false);
  248. if (unlikely(err)) {
  249. pr_err("%s: skcipher_walk_virt failed: %d\n",
  250. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  251. return err;
  252. }
  253. while ((nbytes = walk.nbytes) != 0) {
  254. err = wc_AesSetIV(ctx->aes_decrypt, walk.iv);
  255. if (unlikely(err)) {
  256. pr_err("%s: wc_AesSetKey failed: %d\n",
  257. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  258. return -EINVAL;
  259. }
  260. err = wc_AesCbcDecrypt(ctx->aes_decrypt, walk.dst.virt.addr,
  261. walk.src.virt.addr, nbytes);
  262. if (unlikely(err)) {
  263. pr_err("%s: wc_AesCbcDecrypt failed: %d\n",
  264. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  265. return -EINVAL;
  266. }
  267. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  268. if (unlikely(err)) {
  269. pr_err("%s: skcipher_walk_done failed: %d\n",
  270. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  271. return err;
  272. }
  273. }
  274. return err;
  275. }
  276. static struct skcipher_alg cbcAesAlg = {
  277. .base.cra_name = WOLFKM_AESCBC_NAME,
  278. .base.cra_driver_name = WOLFKM_AESCBC_DRIVER,
  279. .base.cra_priority = WOLFSSL_LINUXKM_LKCAPI_PRIORITY,
  280. .base.cra_blocksize = AES_BLOCK_SIZE,
  281. .base.cra_ctxsize = sizeof(struct km_AesCtx),
  282. .base.cra_module = THIS_MODULE,
  283. .init = km_AesCbcInit,
  284. .exit = km_AesExit,
  285. .min_keysize = AES_128_KEY_SIZE,
  286. .max_keysize = AES_256_KEY_SIZE,
  287. .ivsize = AES_BLOCK_SIZE,
  288. .setkey = km_AesCbcSetKey,
  289. .encrypt = km_AesCbcEncrypt,
  290. .decrypt = km_AesCbcDecrypt,
  291. };
  292. static int cbcAesAlg_loaded = 0;
  293. #endif /* HAVE_AES_CBC &&
  294. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESCBC)
  295. */
  296. #if defined(WOLFSSL_AES_CFB) && \
  297. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  298. defined(LINUXKM_LKCAPI_REGISTER_AESCFB))
  299. static int km_AesCfbInit(struct crypto_skcipher *tfm)
  300. {
  301. struct km_AesCtx * ctx = crypto_skcipher_ctx(tfm);
  302. return km_AesInitCommon(ctx, WOLFKM_AESCFB_DRIVER, 0);
  303. }
  304. static int km_AesCfbSetKey(struct crypto_skcipher *tfm, const u8 *in_key,
  305. unsigned int key_len)
  306. {
  307. struct km_AesCtx * ctx = crypto_skcipher_ctx(tfm);
  308. return km_AesSetKeyCommon(ctx, in_key, key_len, WOLFKM_AESCFB_DRIVER);
  309. }
  310. static int km_AesCfbEncrypt(struct skcipher_request *req)
  311. {
  312. struct crypto_skcipher * tfm = NULL;
  313. struct km_AesCtx * ctx = NULL;
  314. struct skcipher_walk walk;
  315. unsigned int nbytes = 0;
  316. int err = 0;
  317. tfm = crypto_skcipher_reqtfm(req);
  318. ctx = crypto_skcipher_ctx(tfm);
  319. err = skcipher_walk_virt(&walk, req, false);
  320. if (unlikely(err)) {
  321. pr_err("%s: skcipher_walk_virt failed: %d\n",
  322. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  323. return err;
  324. }
  325. while ((nbytes = walk.nbytes) != 0) {
  326. err = wc_AesSetIV(ctx->aes_encrypt, walk.iv);
  327. if (unlikely(err)) {
  328. pr_err("%s: wc_AesSetKey failed: %d\n",
  329. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  330. return -EINVAL;
  331. }
  332. err = wc_AesCfbEncrypt(ctx->aes_encrypt, walk.dst.virt.addr,
  333. walk.src.virt.addr, nbytes);
  334. if (unlikely(err)) {
  335. pr_err("%s: wc_AesCfbEncrypt failed %d\n",
  336. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  337. return -EINVAL;
  338. }
  339. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  340. if (unlikely(err)) {
  341. pr_err("%s: skcipher_walk_done failed: %d\n",
  342. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  343. return err;
  344. }
  345. }
  346. return err;
  347. }
  348. static int km_AesCfbDecrypt(struct skcipher_request *req)
  349. {
  350. struct crypto_skcipher * tfm = NULL;
  351. struct km_AesCtx * ctx = NULL;
  352. struct skcipher_walk walk;
  353. unsigned int nbytes = 0;
  354. int err = 0;
  355. tfm = crypto_skcipher_reqtfm(req);
  356. ctx = crypto_skcipher_ctx(tfm);
  357. err = skcipher_walk_virt(&walk, req, false);
  358. if (unlikely(err)) {
  359. pr_err("%s: skcipher_walk_virt failed: %d\n",
  360. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  361. return err;
  362. }
  363. while ((nbytes = walk.nbytes) != 0) {
  364. err = wc_AesSetIV(ctx->aes_encrypt, walk.iv);
  365. if (unlikely(err)) {
  366. pr_err("%s: wc_AesSetKey failed: %d\n",
  367. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  368. return -EINVAL;
  369. }
  370. err = wc_AesCfbDecrypt(ctx->aes_encrypt, walk.dst.virt.addr,
  371. walk.src.virt.addr, nbytes);
  372. if (unlikely(err)) {
  373. pr_err("%s: wc_AesCfbDecrypt failed: %d\n",
  374. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  375. return -EINVAL;
  376. }
  377. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  378. if (unlikely(err)) {
  379. pr_err("%s: skcipher_walk_done failed: %d\n",
  380. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  381. return err;
  382. }
  383. }
  384. return err;
  385. }
  386. static struct skcipher_alg cfbAesAlg = {
  387. .base.cra_name = WOLFKM_AESCFB_NAME,
  388. .base.cra_driver_name = WOLFKM_AESCFB_DRIVER,
  389. .base.cra_priority = WOLFSSL_LINUXKM_LKCAPI_PRIORITY,
  390. .base.cra_blocksize = AES_BLOCK_SIZE,
  391. .base.cra_ctxsize = sizeof(struct km_AesCtx),
  392. .base.cra_module = THIS_MODULE,
  393. .init = km_AesCfbInit,
  394. .exit = km_AesExit,
  395. .min_keysize = AES_128_KEY_SIZE,
  396. .max_keysize = AES_256_KEY_SIZE,
  397. .ivsize = AES_BLOCK_SIZE,
  398. .setkey = km_AesCfbSetKey,
  399. .encrypt = km_AesCfbEncrypt,
  400. .decrypt = km_AesCfbDecrypt,
  401. };
  402. static int cfbAesAlg_loaded = 0;
  403. #endif /* WOLFSSL_AES_CFB &&
  404. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESCBC)
  405. */
  406. #if defined(HAVE_AESGCM) && \
  407. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  408. defined(LINUXKM_LKCAPI_REGISTER_AESGCM))
  409. #ifndef WOLFSSL_AESGCM_STREAM
  410. #error LKCAPI registration of AES-GCM requires WOLFSSL_AESGCM_STREAM (--enable-aesgcm-stream).
  411. #endif
  412. static int km_AesGcmInit(struct crypto_aead * tfm)
  413. {
  414. struct km_AesCtx * ctx = crypto_aead_ctx(tfm);
  415. return km_AesInitCommon(ctx, WOLFKM_AESGCM_DRIVER, 0);
  416. }
  417. static void km_AesGcmExit(struct crypto_aead * tfm)
  418. {
  419. struct km_AesCtx * ctx = crypto_aead_ctx(tfm);
  420. km_AesExitCommon(ctx);
  421. }
  422. static int km_AesGcmSetKey(struct crypto_aead *tfm, const u8 *in_key,
  423. unsigned int key_len)
  424. {
  425. int err;
  426. struct km_AesCtx * ctx = crypto_aead_ctx(tfm);
  427. err = wc_AesGcmSetKey(ctx->aes_encrypt, in_key, key_len);
  428. if (unlikely(err)) {
  429. pr_err("%s: wc_AesGcmSetKey failed: %d\n",
  430. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  431. return -ENOKEY;
  432. }
  433. return 0;
  434. }
  435. static int km_AesGcmSetAuthsize(struct crypto_aead *tfm, unsigned int authsize)
  436. {
  437. (void)tfm;
  438. if (authsize > AES_BLOCK_SIZE ||
  439. authsize < WOLFSSL_MIN_AUTH_TAG_SZ) {
  440. pr_err("%s: invalid authsize: %d\n",
  441. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), authsize);
  442. return -EINVAL;
  443. }
  444. return 0;
  445. }
  446. /*
  447. * aead ciphers recieve data in scatterlists in following order:
  448. * encrypt
  449. * req->src: aad||plaintext
  450. * req->dst: aad||ciphertext||tag
  451. * decrypt
  452. * req->src: aad||ciphertext||tag
  453. * req->dst: aad||plaintext, return 0 or -EBADMSG
  454. */
  455. static int km_AesGcmEncrypt(struct aead_request *req)
  456. {
  457. struct crypto_aead * tfm = NULL;
  458. struct km_AesCtx * ctx = NULL;
  459. struct skcipher_walk walk;
  460. struct scatter_walk assocSgWalk;
  461. unsigned int nbytes = 0;
  462. u8 authTag[AES_BLOCK_SIZE];
  463. int err = 0;
  464. unsigned int assocLeft = 0;
  465. unsigned int cryptLeft = 0;
  466. u8 * assoc = NULL;
  467. tfm = crypto_aead_reqtfm(req);
  468. ctx = crypto_aead_ctx(tfm);
  469. assocLeft = req->assoclen;
  470. cryptLeft = req->cryptlen;
  471. scatterwalk_start(&assocSgWalk, req->src);
  472. err = skcipher_walk_aead_encrypt(&walk, req, false);
  473. if (unlikely(err)) {
  474. pr_err("%s: skcipher_walk_aead_encrypt failed: %d\n",
  475. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  476. return -1;
  477. }
  478. err = wc_AesGcmInit(ctx->aes_encrypt, NULL /*key*/, 0 /*keylen*/, walk.iv,
  479. AES_BLOCK_SIZE);
  480. if (unlikely(err)) {
  481. pr_err("%s: wc_AesGcmInit failed: %d\n",
  482. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  483. return -EINVAL;
  484. }
  485. assoc = scatterwalk_map(&assocSgWalk);
  486. if (unlikely(IS_ERR(assoc))) {
  487. pr_err("%s: scatterwalk_map failed: %ld\n",
  488. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)),
  489. PTR_ERR(assoc));
  490. return err;
  491. }
  492. err = wc_AesGcmEncryptUpdate(ctx->aes_encrypt, NULL, NULL, 0,
  493. assoc, assocLeft);
  494. assocLeft -= assocLeft;
  495. scatterwalk_unmap(assoc);
  496. assoc = NULL;
  497. if (unlikely(err)) {
  498. pr_err("%s: wc_AesGcmEncryptUpdate failed: %d\n",
  499. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  500. return -EINVAL;
  501. }
  502. while ((nbytes = walk.nbytes) != 0) {
  503. int n = nbytes;
  504. if (likely(cryptLeft && nbytes)) {
  505. n = cryptLeft < nbytes ? cryptLeft : nbytes;
  506. err = wc_AesGcmEncryptUpdate(
  507. ctx->aes_encrypt,
  508. walk.dst.virt.addr,
  509. walk.src.virt.addr,
  510. cryptLeft,
  511. NULL, 0);
  512. nbytes -= n;
  513. cryptLeft -= n;
  514. }
  515. if (unlikely(err)) {
  516. pr_err("%s: wc_AesGcmEncryptUpdate failed: %d\n",
  517. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  518. return -EINVAL;
  519. }
  520. err = skcipher_walk_done(&walk, nbytes);
  521. if (unlikely(err)) {
  522. pr_err("%s: skcipher_walk_done failed: %d\n",
  523. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  524. return err;
  525. }
  526. }
  527. err = wc_AesGcmEncryptFinal(ctx->aes_encrypt, authTag, tfm->authsize);
  528. if (unlikely(err)) {
  529. pr_err("%s: wc_AesGcmEncryptFinal failed with return code %d\n",
  530. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  531. return -EINVAL;
  532. }
  533. /* Now copy the auth tag into request scatterlist. */
  534. scatterwalk_map_and_copy(authTag, req->dst,
  535. req->assoclen + req->cryptlen,
  536. tfm->authsize, 1);
  537. return err;
  538. }
  539. static int km_AesGcmDecrypt(struct aead_request *req)
  540. {
  541. struct crypto_aead * tfm = NULL;
  542. struct km_AesCtx * ctx = NULL;
  543. struct skcipher_walk walk;
  544. struct scatter_walk assocSgWalk;
  545. unsigned int nbytes = 0;
  546. u8 origAuthTag[AES_BLOCK_SIZE];
  547. int err = 0;
  548. unsigned int assocLeft = 0;
  549. unsigned int cryptLeft = 0;
  550. u8 * assoc = NULL;
  551. tfm = crypto_aead_reqtfm(req);
  552. ctx = crypto_aead_ctx(tfm);
  553. assocLeft = req->assoclen;
  554. cryptLeft = req->cryptlen - tfm->authsize;
  555. /* Copy out original auth tag from req->src. */
  556. scatterwalk_map_and_copy(origAuthTag, req->src,
  557. req->assoclen + req->cryptlen - tfm->authsize,
  558. tfm->authsize, 0);
  559. scatterwalk_start(&assocSgWalk, req->src);
  560. err = skcipher_walk_aead_decrypt(&walk, req, false);
  561. if (unlikely(err)) {
  562. pr_err("%s: skcipher_walk_aead_decrypt failed: %d\n",
  563. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  564. return err;
  565. }
  566. err = wc_AesGcmInit(ctx->aes_encrypt, NULL /*key*/, 0 /*keylen*/, walk.iv,
  567. AES_BLOCK_SIZE);
  568. if (unlikely(err)) {
  569. pr_err("%s: wc_AesGcmInit failed: %d\n",
  570. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  571. return -EINVAL;
  572. }
  573. assoc = scatterwalk_map(&assocSgWalk);
  574. if (unlikely(IS_ERR(assoc))) {
  575. pr_err("%s: scatterwalk_map failed: %ld\n",
  576. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)),
  577. PTR_ERR(assoc));
  578. return err;
  579. }
  580. err = wc_AesGcmDecryptUpdate(ctx->aes_encrypt, NULL, NULL, 0,
  581. assoc, assocLeft);
  582. assocLeft -= assocLeft;
  583. scatterwalk_unmap(assoc);
  584. assoc = NULL;
  585. if (unlikely(err)) {
  586. pr_err("%s: wc_AesGcmDecryptUpdate failed: %d\n",
  587. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  588. return -EINVAL;
  589. }
  590. while ((nbytes = walk.nbytes) != 0) {
  591. int n = nbytes;
  592. if (likely(cryptLeft && nbytes)) {
  593. n = cryptLeft < nbytes ? cryptLeft : nbytes;
  594. err = wc_AesGcmDecryptUpdate(
  595. ctx->aes_encrypt,
  596. walk.dst.virt.addr,
  597. walk.src.virt.addr,
  598. cryptLeft,
  599. NULL, 0);
  600. nbytes -= n;
  601. cryptLeft -= n;
  602. }
  603. if (unlikely(err)) {
  604. pr_err("%s: wc_AesGcmDecryptUpdate failed: %d\n",
  605. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  606. return -EINVAL;
  607. }
  608. err = skcipher_walk_done(&walk, nbytes);
  609. if (unlikely(err)) {
  610. pr_err("%s: skcipher_walk_done failed: %d\n",
  611. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  612. return err;
  613. }
  614. }
  615. err = wc_AesGcmDecryptFinal(ctx->aes_encrypt, origAuthTag, tfm->authsize);
  616. if (unlikely(err)) {
  617. pr_err("%s: wc_AesGcmDecryptFinal failed with return code %d\n",
  618. crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm)), err);
  619. if (err == AES_GCM_AUTH_E) {
  620. return -EBADMSG;
  621. }
  622. else {
  623. return -EINVAL;
  624. }
  625. }
  626. return err;
  627. }
  628. static struct aead_alg gcmAesAead = {
  629. .base.cra_name = WOLFKM_AESGCM_NAME,
  630. .base.cra_driver_name = WOLFKM_AESGCM_DRIVER,
  631. .base.cra_priority = WOLFSSL_LINUXKM_LKCAPI_PRIORITY,
  632. .base.cra_blocksize = 1,
  633. .base.cra_ctxsize = sizeof(struct km_AesCtx),
  634. .base.cra_module = THIS_MODULE,
  635. .init = km_AesGcmInit,
  636. .exit = km_AesGcmExit,
  637. .setkey = km_AesGcmSetKey,
  638. .setauthsize = km_AesGcmSetAuthsize,
  639. .encrypt = km_AesGcmEncrypt,
  640. .decrypt = km_AesGcmDecrypt,
  641. .ivsize = AES_BLOCK_SIZE,
  642. .maxauthsize = AES_BLOCK_SIZE,
  643. .chunksize = AES_BLOCK_SIZE,
  644. };
  645. static int gcmAesAead_loaded = 0;
  646. #endif /* HAVE_AESGCM &&
  647. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESGCM) &&
  648. */
  649. #if defined(WOLFSSL_AES_XTS) && \
  650. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  651. defined(LINUXKM_LKCAPI_REGISTER_AESXTS))
  652. struct km_AesXtsCtx {
  653. XtsAes *aesXts; /* allocated in km_AesXtsInitCommon() to assure alignment
  654. * for AESNI.
  655. */
  656. };
  657. static int km_AesXtsInitCommon(struct km_AesXtsCtx * ctx, const char * name)
  658. {
  659. int err;
  660. ctx->aesXts = (XtsAes *)malloc(sizeof(*ctx->aesXts));
  661. if (! ctx->aesXts)
  662. return -MEMORY_E;
  663. err = wc_AesXtsInit(ctx->aesXts, NULL, INVALID_DEVID);
  664. if (unlikely(err)) {
  665. pr_err("%s: km_AesXtsInitCommon failed: %d\n", name, err);
  666. return -EINVAL;
  667. }
  668. return 0;
  669. }
  670. static int km_AesXtsInit(struct crypto_skcipher *tfm)
  671. {
  672. struct km_AesXtsCtx * ctx = crypto_skcipher_ctx(tfm);
  673. return km_AesXtsInitCommon(ctx, WOLFKM_AESXTS_DRIVER);
  674. }
  675. static void km_AesXtsExit(struct crypto_skcipher *tfm)
  676. {
  677. struct km_AesXtsCtx * ctx = crypto_skcipher_ctx(tfm);
  678. wc_AesXtsFree(ctx->aesXts);
  679. free(ctx->aesXts);
  680. ctx->aesXts = NULL;
  681. }
  682. static int km_AesXtsSetKey(struct crypto_skcipher *tfm, const u8 *in_key,
  683. unsigned int key_len)
  684. {
  685. int err;
  686. struct km_AesXtsCtx * ctx = crypto_skcipher_ctx(tfm);
  687. err = wc_AesXtsSetKeyNoInit(ctx->aesXts, in_key, key_len,
  688. AES_ENCRYPTION_AND_DECRYPTION);
  689. if (unlikely(err)) {
  690. pr_err("%s: wc_AesXtsSetKeyNoInit failed: %d\n",
  691. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  692. return -EINVAL;
  693. }
  694. return 0;
  695. }
  696. /* see /usr/src/linux/drivers/md/dm-crypt.c */
  697. static int km_AesXtsEncrypt(struct skcipher_request *req)
  698. {
  699. int err = 0;
  700. struct crypto_skcipher * tfm = NULL;
  701. struct km_AesXtsCtx * ctx = NULL;
  702. struct skcipher_walk walk;
  703. unsigned int nbytes = 0;
  704. tfm = crypto_skcipher_reqtfm(req);
  705. ctx = crypto_skcipher_ctx(tfm);
  706. err = skcipher_walk_virt(&walk, req, false);
  707. if (unlikely(err)) {
  708. pr_err("%s: skcipher_walk_virt failed: %d\n",
  709. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  710. return err;
  711. }
  712. while ((nbytes = walk.nbytes) != 0) {
  713. err = wc_AesXtsEncrypt(ctx->aesXts, walk.dst.virt.addr,
  714. walk.src.virt.addr, nbytes,
  715. walk.iv, walk.ivsize);
  716. if (unlikely(err)) {
  717. pr_err("%s: wc_AesXtsEncrypt failed: %d\n",
  718. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  719. return -EINVAL;
  720. }
  721. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  722. if (unlikely(err)) {
  723. pr_err("%s: skcipher_walk_done failed: %d\n",
  724. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  725. return err;
  726. }
  727. }
  728. return err;
  729. }
  730. static int km_AesXtsDecrypt(struct skcipher_request *req)
  731. {
  732. int err = 0;
  733. struct crypto_skcipher * tfm = NULL;
  734. struct km_AesXtsCtx * ctx = NULL;
  735. struct skcipher_walk walk;
  736. unsigned int nbytes = 0;
  737. tfm = crypto_skcipher_reqtfm(req);
  738. ctx = crypto_skcipher_ctx(tfm);
  739. err = skcipher_walk_virt(&walk, req, false);
  740. if (unlikely(err)) {
  741. pr_err("%s: skcipher_walk_virt failed: %d\n",
  742. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  743. return err;
  744. }
  745. while ((nbytes = walk.nbytes) != 0) {
  746. err = wc_AesXtsDecrypt(ctx->aesXts, walk.dst.virt.addr,
  747. walk.src.virt.addr, nbytes,
  748. walk.iv, walk.ivsize);
  749. if (unlikely(err)) {
  750. pr_err("%s: wc_AesCbcDecrypt failed: %d\n",
  751. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  752. return -EINVAL;
  753. }
  754. err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
  755. if (unlikely(err)) {
  756. pr_err("%s: skcipher_walk_done failed: %d\n",
  757. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm)), err);
  758. return err;
  759. }
  760. }
  761. return err;
  762. }
  763. static struct skcipher_alg xtsAesAlg = {
  764. .base.cra_name = WOLFKM_AESXTS_NAME,
  765. .base.cra_driver_name = WOLFKM_AESXTS_DRIVER,
  766. .base.cra_priority = WOLFSSL_LINUXKM_LKCAPI_PRIORITY,
  767. .base.cra_blocksize = AES_BLOCK_SIZE,
  768. .base.cra_ctxsize = sizeof(struct km_AesXtsCtx),
  769. .base.cra_module = THIS_MODULE,
  770. .min_keysize = 2 * AES_128_KEY_SIZE,
  771. .max_keysize = 2 * AES_256_KEY_SIZE,
  772. .ivsize = AES_BLOCK_SIZE,
  773. .walksize = 2 * AES_BLOCK_SIZE,
  774. .init = km_AesXtsInit,
  775. .exit = km_AesXtsExit,
  776. .setkey = km_AesXtsSetKey,
  777. .encrypt = km_AesXtsEncrypt,
  778. .decrypt = km_AesXtsDecrypt
  779. };
  780. static int xtsAesAlg_loaded = 0;
  781. #endif /* WOLFSSL_AES_XTS &&
  782. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESXTS)
  783. */
  784. /* cipher tests, cribbed from test.c, with supplementary LKCAPI tests: */
  785. #if defined(HAVE_AES_CBC) && \
  786. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  787. defined(LINUXKM_LKCAPI_REGISTER_AESCBC))
  788. static int linuxkm_test_aescbc(void)
  789. {
  790. int ret = 0;
  791. struct crypto_skcipher * tfm = NULL;
  792. struct skcipher_request * req = NULL;
  793. struct scatterlist src, dst;
  794. Aes *aes;
  795. int aes_inited = 0;
  796. static const byte key32[] =
  797. {
  798. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  799. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  800. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  801. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  802. };
  803. static const byte p_vector[] =
  804. /* Now is the time for all good men w/o trailing 0 */
  805. {
  806. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  807. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  808. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  809. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  810. };
  811. static const byte iv[] = "1234567890abcdef";
  812. static const byte c_vector[] =
  813. {
  814. 0xd7,0xd6,0x04,0x5b,0x4d,0xc4,0x90,0xdf,
  815. 0x4a,0x82,0xed,0x61,0x26,0x4e,0x23,0xb3,
  816. 0xe4,0xb5,0x85,0x30,0x29,0x4c,0x9d,0xcf,
  817. 0x73,0xc9,0x46,0xd1,0xaa,0xc8,0xcb,0x62
  818. };
  819. byte iv_copy[sizeof(iv)];
  820. byte enc[sizeof(p_vector)];
  821. byte dec[sizeof(p_vector)];
  822. u8 * enc2 = NULL;
  823. u8 * dec2 = NULL;
  824. aes = (Aes *)malloc(sizeof(*aes));
  825. if (aes == NULL)
  826. return -ENOMEM;
  827. XMEMSET(enc, 0, sizeof(enc));
  828. XMEMSET(dec, 0, sizeof(enc));
  829. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  830. if (ret) {
  831. pr_err("wolfcrypt wc_AesInit failed with return code %d.\n", ret);
  832. goto test_cbc_end;
  833. }
  834. aes_inited = 1;
  835. ret = wc_AesSetKey(aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  836. if (ret) {
  837. pr_err("wolfcrypt wc_AesSetKey failed with return code %d\n", ret);
  838. goto test_cbc_end;
  839. }
  840. ret = wc_AesCbcEncrypt(aes, enc, p_vector, sizeof(p_vector));
  841. if (ret) {
  842. pr_err("wolfcrypt wc_AesCbcEncrypt failed with return code %d\n", ret);
  843. goto test_cbc_end;
  844. }
  845. if (XMEMCMP(enc, c_vector, sizeof(c_vector)) != 0) {
  846. pr_err("wolfcrypt wc_AesCbcEncrypt KAT mismatch\n");
  847. return LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  848. }
  849. /* Re init for decrypt and set flag. */
  850. wc_AesFree(aes);
  851. aes_inited = 0;
  852. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  853. if (ret) {
  854. pr_err("wolfcrypt wc_AesInit failed with return code %d.\n", ret);
  855. goto test_cbc_end;
  856. }
  857. aes_inited = 1;
  858. ret = wc_AesSetKey(aes, key32, AES_BLOCK_SIZE * 2, iv, AES_DECRYPTION);
  859. if (ret) {
  860. pr_err("wolfcrypt wc_AesSetKey failed with return code %d.\n", ret);
  861. goto test_cbc_end;
  862. }
  863. ret = wc_AesCbcDecrypt(aes, dec, enc, sizeof(p_vector));
  864. if (ret) {
  865. pr_err("wolfcrypt wc_AesCbcDecrypt failed with return code %d\n", ret);
  866. goto test_cbc_end;
  867. }
  868. ret = XMEMCMP(p_vector, dec, sizeof(p_vector));
  869. if (ret) {
  870. pr_err("error: p_vector and dec do not match: %d\n", ret);
  871. goto test_cbc_end;
  872. }
  873. /* now the kernel crypto part */
  874. enc2 = malloc(sizeof(p_vector));
  875. if (!enc2) {
  876. pr_err("error: malloc failed\n");
  877. goto test_cbc_end;
  878. }
  879. dec2 = malloc(sizeof(p_vector));
  880. if (!dec2) {
  881. pr_err("error: malloc failed\n");
  882. goto test_cbc_end;
  883. }
  884. memcpy(dec2, p_vector, sizeof(p_vector));
  885. tfm = crypto_alloc_skcipher(WOLFKM_AESCBC_NAME, 0, 0);
  886. if (IS_ERR(tfm)) {
  887. pr_err("error: allocating AES skcipher algorithm %s failed: %ld\n",
  888. WOLFKM_AESCBC_DRIVER, PTR_ERR(tfm));
  889. goto test_cbc_end;
  890. }
  891. #ifndef LINUXKM_LKCAPI_PRIORITY_ALLOW_MASKING
  892. {
  893. const char *driver_name =
  894. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  895. if (strcmp(driver_name, WOLFKM_AESCBC_DRIVER)) {
  896. pr_err("error: unexpected implementation for %s: %s (expected %s)\n",
  897. WOLFKM_AESCBC_NAME, driver_name, WOLFKM_AESCBC_DRIVER);
  898. ret = -ENOENT;
  899. goto test_cbc_end;
  900. }
  901. }
  902. #endif
  903. ret = crypto_skcipher_setkey(tfm, key32, AES_BLOCK_SIZE * 2);
  904. if (ret) {
  905. pr_err("error: crypto_skcipher_setkey returned: %d\n", ret);
  906. goto test_cbc_end;
  907. }
  908. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  909. if (IS_ERR(req)) {
  910. pr_err("error: allocating AES skcipher request %s failed\n",
  911. WOLFKM_AESCBC_DRIVER);
  912. goto test_cbc_end;
  913. }
  914. sg_init_one(&src, dec2, sizeof(p_vector));
  915. sg_init_one(&dst, enc2, sizeof(p_vector));
  916. XMEMCPY(iv_copy, iv, sizeof(iv));
  917. skcipher_request_set_crypt(req, &src, &dst, sizeof(p_vector), iv_copy);
  918. ret = crypto_skcipher_encrypt(req);
  919. if (ret) {
  920. pr_err("error: crypto_skcipher_encrypt returned: %d\n", ret);
  921. goto test_cbc_end;
  922. }
  923. ret = XMEMCMP(enc, enc2, sizeof(p_vector));
  924. if (ret) {
  925. pr_err("error: enc and enc2 do not match: %d\n", ret);
  926. goto test_cbc_end;
  927. }
  928. memset(dec2, 0, sizeof(p_vector));
  929. sg_init_one(&src, enc2, sizeof(p_vector));
  930. sg_init_one(&dst, dec2, sizeof(p_vector));
  931. XMEMCPY(iv_copy, iv, sizeof(iv));
  932. skcipher_request_set_crypt(req, &src, &dst, sizeof(p_vector), iv_copy);
  933. ret = crypto_skcipher_decrypt(req);
  934. if (ret) {
  935. pr_err("ERROR: crypto_skcipher_decrypt returned %d\n", ret);
  936. goto test_cbc_end;
  937. }
  938. ret = XMEMCMP(dec, dec2, sizeof(p_vector));
  939. if (ret) {
  940. pr_err("error: dec and dec2 do not match: %d\n", ret);
  941. goto test_cbc_end;
  942. }
  943. test_cbc_end:
  944. if (enc2) { free(enc2); }
  945. if (dec2) { free(dec2); }
  946. if (req) { skcipher_request_free(req); }
  947. if (tfm) { crypto_free_skcipher(tfm); }
  948. if (aes_inited)
  949. wc_AesFree(aes);
  950. free(aes);
  951. return ret;
  952. }
  953. #endif /* HAVE_AES_CBC &&
  954. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESCBC)
  955. */
  956. #if defined(WOLFSSL_AES_CFB) && \
  957. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  958. defined(LINUXKM_LKCAPI_REGISTER_AESCFB))
  959. static int linuxkm_test_aescfb(void)
  960. {
  961. int ret = 0;
  962. struct crypto_skcipher * tfm = NULL;
  963. struct skcipher_request * req = NULL;
  964. struct scatterlist src, dst;
  965. Aes *aes;
  966. int aes_inited = 0;
  967. static const byte key32[] =
  968. {
  969. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  970. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  971. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  972. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  973. };
  974. static const byte p_vector[] =
  975. /* Now is the time for all good men w/o trailing 0 */
  976. {
  977. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  978. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  979. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  980. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  981. };
  982. static const byte iv[] = "1234567890abcdef";
  983. static const byte c_vector[] =
  984. {
  985. 0x56,0x35,0x3f,0xdd,0xde,0xa6,0x15,0x87,
  986. 0x57,0xdc,0x34,0x62,0x9a,0x68,0x96,0x51,
  987. 0xc7,0x09,0xb9,0x4e,0x47,0x6b,0x24,0x72,
  988. 0x19,0x5a,0xdf,0x7e,0xba,0xa8,0x01,0xb6
  989. };
  990. byte iv_copy[sizeof(iv)];
  991. byte enc[sizeof(p_vector)];
  992. byte dec[sizeof(p_vector)];
  993. u8 * enc2 = NULL;
  994. u8 * dec2 = NULL;
  995. aes = (Aes *)malloc(sizeof(*aes));
  996. if (aes == NULL)
  997. return -ENOMEM;
  998. XMEMSET(enc, 0, sizeof(enc));
  999. XMEMSET(dec, 0, sizeof(enc));
  1000. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  1001. if (ret) {
  1002. pr_err("wolfcrypt wc_AesInit failed with return code %d.\n", ret);
  1003. goto test_cfb_end;
  1004. }
  1005. aes_inited = 1;
  1006. ret = wc_AesSetKey(aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  1007. if (ret) {
  1008. pr_err("wolfcrypt wc_AesSetKey failed with return code %d\n", ret);
  1009. goto test_cfb_end;
  1010. }
  1011. ret = wc_AesCfbEncrypt(aes, enc, p_vector, sizeof(p_vector));
  1012. if (ret) {
  1013. pr_err("wolfcrypt wc_AesCfbEncrypt failed with return code %d\n", ret);
  1014. goto test_cfb_end;
  1015. }
  1016. if (XMEMCMP(enc, c_vector, sizeof(c_vector)) != 0) {
  1017. pr_err("wolfcrypt wc_AesCfbEncrypt KAT mismatch\n");
  1018. return LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1019. }
  1020. /* Re init for decrypt and set flag. */
  1021. wc_AesFree(aes);
  1022. aes_inited = 0;
  1023. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  1024. if (ret) {
  1025. pr_err("wolfcrypt wc_AesInit failed with return code %d.\n", ret);
  1026. goto test_cfb_end;
  1027. }
  1028. aes_inited = 1;
  1029. ret = wc_AesSetKey(aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  1030. if (ret) {
  1031. pr_err("wolfcrypt wc_AesSetKey failed with return code %d.\n", ret);
  1032. goto test_cfb_end;
  1033. }
  1034. ret = wc_AesCfbDecrypt(aes, dec, enc, sizeof(p_vector));
  1035. if (ret) {
  1036. pr_err("wolfcrypt wc_AesCfbDecrypt failed with return code %d\n", ret);
  1037. goto test_cfb_end;
  1038. }
  1039. ret = XMEMCMP(p_vector, dec, sizeof(p_vector));
  1040. if (ret) {
  1041. pr_err("error: p_vector and dec do not match: %d\n", ret);
  1042. goto test_cfb_end;
  1043. }
  1044. /* now the kernel crypto part */
  1045. enc2 = malloc(sizeof(p_vector));
  1046. if (!enc2) {
  1047. pr_err("error: malloc failed\n");
  1048. goto test_cfb_end;
  1049. }
  1050. dec2 = malloc(sizeof(p_vector));
  1051. if (!dec2) {
  1052. pr_err("error: malloc failed\n");
  1053. goto test_cfb_end;
  1054. }
  1055. memcpy(dec2, p_vector, sizeof(p_vector));
  1056. tfm = crypto_alloc_skcipher(WOLFKM_AESCFB_NAME, 0, 0);
  1057. if (IS_ERR(tfm)) {
  1058. pr_err("error: allocating AES skcipher algorithm %s failed: %ld\n",
  1059. WOLFKM_AESCFB_DRIVER, PTR_ERR(tfm));
  1060. goto test_cfb_end;
  1061. }
  1062. #ifndef LINUXKM_LKCAPI_PRIORITY_ALLOW_MASKING
  1063. {
  1064. const char *driver_name =
  1065. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  1066. if (strcmp(driver_name, WOLFKM_AESCFB_DRIVER)) {
  1067. pr_err("error: unexpected implementation for %s: %s (expected %s)\n",
  1068. WOLFKM_AESCFB_NAME, driver_name, WOLFKM_AESCFB_DRIVER);
  1069. ret = -ENOENT;
  1070. goto test_cfb_end;
  1071. }
  1072. }
  1073. #endif
  1074. ret = crypto_skcipher_setkey(tfm, key32, AES_BLOCK_SIZE * 2);
  1075. if (ret) {
  1076. pr_err("error: crypto_skcipher_setkey returned: %d\n", ret);
  1077. goto test_cfb_end;
  1078. }
  1079. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  1080. if (IS_ERR(req)) {
  1081. pr_err("error: allocating AES skcipher request %s failed\n",
  1082. WOLFKM_AESCFB_DRIVER);
  1083. goto test_cfb_end;
  1084. }
  1085. sg_init_one(&src, dec2, sizeof(p_vector));
  1086. sg_init_one(&dst, enc2, sizeof(p_vector));
  1087. XMEMCPY(iv_copy, iv, sizeof(iv));
  1088. skcipher_request_set_crypt(req, &src, &dst, sizeof(p_vector), iv_copy);
  1089. ret = crypto_skcipher_encrypt(req);
  1090. if (ret) {
  1091. pr_err("error: crypto_skcipher_encrypt returned: %d\n", ret);
  1092. goto test_cfb_end;
  1093. }
  1094. ret = XMEMCMP(enc, enc2, sizeof(p_vector));
  1095. if (ret) {
  1096. pr_err("error: enc and enc2 do not match: %d\n", ret);
  1097. goto test_cfb_end;
  1098. }
  1099. memset(dec2, 0, sizeof(p_vector));
  1100. sg_init_one(&src, enc2, sizeof(p_vector));
  1101. sg_init_one(&dst, dec2, sizeof(p_vector));
  1102. XMEMCPY(iv_copy, iv, sizeof(iv));
  1103. skcipher_request_set_crypt(req, &src, &dst, sizeof(p_vector), iv_copy);
  1104. ret = crypto_skcipher_decrypt(req);
  1105. if (ret) {
  1106. pr_err("error: crypto_skcipher_decrypt returned: %d\n", ret);
  1107. goto test_cfb_end;
  1108. }
  1109. ret = XMEMCMP(dec, dec2, sizeof(p_vector));
  1110. if (ret) {
  1111. pr_err("error: dec and dec2 do not match: %d\n", ret);
  1112. goto test_cfb_end;
  1113. }
  1114. test_cfb_end:
  1115. if (enc2) { free(enc2); }
  1116. if (dec2) { free(dec2); }
  1117. if (req) { skcipher_request_free(req); }
  1118. if (tfm) { crypto_free_skcipher(tfm); }
  1119. if (aes_inited)
  1120. wc_AesFree(aes);
  1121. free(aes);
  1122. return ret;
  1123. }
  1124. #endif /* WOLFSSL_AES_CFB &&
  1125. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESCFB)
  1126. */
  1127. #if defined(HAVE_AESGCM) && \
  1128. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  1129. defined(LINUXKM_LKCAPI_REGISTER_AESGCM))
  1130. static int linuxkm_test_aesgcm(void)
  1131. {
  1132. int ret = 0;
  1133. struct crypto_aead * tfm = NULL;
  1134. struct aead_request * req = NULL;
  1135. struct scatterlist * src = NULL;
  1136. struct scatterlist * dst = NULL;
  1137. Aes *aes;
  1138. int aes_inited = 0;
  1139. static const byte key32[] =
  1140. {
  1141. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  1142. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  1143. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  1144. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  1145. };
  1146. static const byte p_vector[] =
  1147. /* Now is the time for all w/o trailing 0 */
  1148. {
  1149. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  1150. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  1151. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  1152. };
  1153. static const byte assoc[] =
  1154. {
  1155. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1156. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1157. 0xab, 0xad, 0xda, 0xd2
  1158. };
  1159. static const byte ivstr[] = "1234567890abcdef";
  1160. static const byte c_vector[] =
  1161. {
  1162. 0x0c,0x97,0x05,0x3c,0xef,0x5c,0x63,0x6b,
  1163. 0x15,0xe4,0x00,0x63,0xf8,0x8c,0xd0,0x95,
  1164. 0x27,0x81,0x90,0x9c,0x9f,0xe6,0x98,0xe9
  1165. };
  1166. static const byte KAT_authTag[] =
  1167. {
  1168. 0xc9,0xd5,0x7a,0x77,0xac,0x28,0xc2,0xe7,
  1169. 0xe4,0x28,0x90,0xaa,0x09,0xab,0xf9,0x7c
  1170. };
  1171. byte enc[sizeof(p_vector)];
  1172. byte authTag[AES_BLOCK_SIZE];
  1173. byte dec[sizeof(p_vector)];
  1174. u8 * assoc2 = NULL;
  1175. u8 * enc2 = NULL;
  1176. u8 * dec2 = NULL;
  1177. u8 * iv = NULL;
  1178. size_t encryptLen = sizeof(p_vector);
  1179. size_t decryptLen = sizeof(p_vector) + sizeof(authTag);
  1180. /* Init stack variables. */
  1181. XMEMSET(enc, 0, sizeof(p_vector));
  1182. XMEMSET(dec, 0, sizeof(p_vector));
  1183. XMEMSET(authTag, 0, AES_BLOCK_SIZE);
  1184. aes = (Aes *)malloc(sizeof(*aes));
  1185. if (aes == NULL)
  1186. return -ENOMEM;
  1187. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  1188. if (ret) {
  1189. pr_err("error: wc_AesInit failed with return code %d.\n", ret);
  1190. goto test_gcm_end;
  1191. }
  1192. aes_inited = 1;
  1193. ret = wc_AesGcmInit(aes, key32, sizeof(key32)/sizeof(byte), ivstr,
  1194. AES_BLOCK_SIZE);
  1195. if (ret) {
  1196. pr_err("error: wc_AesGcmInit failed with return code %d.\n", ret);
  1197. goto test_gcm_end;
  1198. }
  1199. ret = wc_AesGcmEncryptUpdate(aes, NULL, NULL, 0, assoc, sizeof(assoc));
  1200. if (ret) {
  1201. pr_err("error: wc_AesGcmEncryptUpdate failed with return code %d\n",
  1202. ret);
  1203. goto test_gcm_end;
  1204. }
  1205. ret = wc_AesGcmEncryptUpdate(aes, enc, p_vector, sizeof(p_vector), NULL, 0);
  1206. if (ret) {
  1207. pr_err("error: wc_AesGcmEncryptUpdate failed with return code %d\n",
  1208. ret);
  1209. goto test_gcm_end;
  1210. }
  1211. if (XMEMCMP(enc, c_vector, sizeof(c_vector)) != 0) {
  1212. pr_err("wolfcrypt AES-GCM KAT mismatch on ciphertext\n");
  1213. ret = LINUXKM_LKCAPI_AESGCM_KAT_MISMATCH_E;
  1214. goto test_gcm_end;
  1215. }
  1216. ret = wc_AesGcmEncryptFinal(aes, authTag, AES_BLOCK_SIZE);
  1217. if (ret) {
  1218. pr_err("error: wc_AesGcmEncryptFinal failed with return code %d\n",
  1219. ret);
  1220. goto test_gcm_end;
  1221. }
  1222. if (XMEMCMP(authTag, KAT_authTag, sizeof(KAT_authTag)) != 0) {
  1223. pr_err("wolfcrypt AES-GCM KAT mismatch on authTag\n");
  1224. ret = LINUXKM_LKCAPI_AESGCM_KAT_MISMATCH_E;
  1225. goto test_gcm_end;
  1226. }
  1227. ret = wc_AesGcmInit(aes, key32, sizeof(key32)/sizeof(byte), ivstr,
  1228. AES_BLOCK_SIZE);
  1229. if (ret) {
  1230. pr_err("error: wc_AesGcmInit failed with return code %d.\n", ret);
  1231. goto test_gcm_end;
  1232. }
  1233. ret = wc_AesGcmDecryptUpdate(aes, dec, enc, sizeof(p_vector),
  1234. assoc, sizeof(assoc));
  1235. if (ret) {
  1236. pr_err("error: wc_AesGcmDecryptUpdate failed with return code %d\n",
  1237. ret);
  1238. goto test_gcm_end;
  1239. }
  1240. ret = wc_AesGcmDecryptFinal(aes, authTag, AES_BLOCK_SIZE);
  1241. if (ret) {
  1242. pr_err("error: wc_AesGcmEncryptFinal failed with return code %d\n",
  1243. ret);
  1244. goto test_gcm_end;
  1245. }
  1246. ret = XMEMCMP(p_vector, dec, sizeof(p_vector));
  1247. if (ret) {
  1248. pr_err("error: gcm: p_vector and dec do not match: %d\n", ret);
  1249. goto test_gcm_end;
  1250. }
  1251. /* now the kernel crypto part */
  1252. assoc2 = malloc(sizeof(assoc));
  1253. if (IS_ERR(assoc2)) {
  1254. pr_err("error: malloc failed\n");
  1255. goto test_gcm_end;
  1256. }
  1257. memset(assoc2, 0, sizeof(assoc));
  1258. memcpy(assoc2, assoc, sizeof(assoc));
  1259. iv = malloc(AES_BLOCK_SIZE);
  1260. if (IS_ERR(iv)) {
  1261. pr_err("error: malloc failed\n");
  1262. goto test_gcm_end;
  1263. }
  1264. memset(iv, 0, AES_BLOCK_SIZE);
  1265. memcpy(iv, ivstr, AES_BLOCK_SIZE);
  1266. enc2 = malloc(decryptLen);
  1267. if (IS_ERR(enc2)) {
  1268. pr_err("error: malloc failed\n");
  1269. goto test_gcm_end;
  1270. }
  1271. dec2 = malloc(decryptLen);
  1272. if (IS_ERR(dec2)) {
  1273. pr_err("error: malloc failed\n");
  1274. goto test_gcm_end;
  1275. }
  1276. memset(enc2, 0, decryptLen);
  1277. memset(dec2, 0, decryptLen);
  1278. memcpy(dec2, p_vector, sizeof(p_vector));
  1279. tfm = crypto_alloc_aead(WOLFKM_AESGCM_NAME, 0, 0);
  1280. if (IS_ERR(tfm)) {
  1281. pr_err("error: allocating AES skcipher algorithm %s failed: %ld\n",
  1282. WOLFKM_AESGCM_DRIVER, PTR_ERR(tfm));
  1283. goto test_gcm_end;
  1284. }
  1285. #ifndef LINUXKM_LKCAPI_PRIORITY_ALLOW_MASKING
  1286. {
  1287. const char *driver_name = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
  1288. if (strcmp(driver_name, WOLFKM_AESGCM_DRIVER)) {
  1289. pr_err("error: unexpected implementation for %s: %s (expected %s)\n",
  1290. WOLFKM_AESGCM_NAME, driver_name, WOLFKM_AESGCM_DRIVER);
  1291. ret = -ENOENT;
  1292. goto test_gcm_end;
  1293. }
  1294. }
  1295. #endif
  1296. ret = crypto_aead_setkey(tfm, key32, AES_BLOCK_SIZE * 2);
  1297. if (ret) {
  1298. pr_err("error: crypto_aead_setkey returned: %d\n", ret);
  1299. goto test_gcm_end;
  1300. }
  1301. ret = crypto_aead_setauthsize(tfm, sizeof(authTag));
  1302. if (ret) {
  1303. pr_err("error: crypto_aead_setauthsize returned: %d\n", ret);
  1304. goto test_gcm_end;
  1305. }
  1306. req = aead_request_alloc(tfm, GFP_KERNEL);
  1307. if (IS_ERR(req)) {
  1308. pr_err("error: allocating AES aead request %s failed: %ld\n",
  1309. WOLFKM_AESCBC_DRIVER, PTR_ERR(req));
  1310. goto test_gcm_end;
  1311. }
  1312. src = malloc(sizeof(struct scatterlist) * 2);
  1313. dst = malloc(sizeof(struct scatterlist) * 2);
  1314. if (IS_ERR(src) || IS_ERR(dst)) {
  1315. pr_err("error: malloc src or dst failed: %ld, %ld\n",
  1316. PTR_ERR(src), PTR_ERR(dst));
  1317. goto test_gcm_end;
  1318. }
  1319. sg_init_table(src, 2);
  1320. sg_set_buf(src, assoc2, sizeof(assoc));
  1321. sg_set_buf(&src[1], dec2, sizeof(p_vector));
  1322. sg_init_table(dst, 2);
  1323. sg_set_buf(dst, assoc2, sizeof(assoc));
  1324. sg_set_buf(&dst[1], enc2, decryptLen);
  1325. aead_request_set_callback(req, 0, NULL, NULL);
  1326. aead_request_set_ad(req, sizeof(assoc));
  1327. aead_request_set_crypt(req, src, dst, sizeof(p_vector), iv);
  1328. ret = crypto_aead_encrypt(req);
  1329. if (ret) {
  1330. pr_err("error: crypto_aead_encrypt returned: %d\n", ret);
  1331. goto test_gcm_end;
  1332. }
  1333. ret = XMEMCMP(enc, enc2, sizeof(p_vector));
  1334. if (ret) {
  1335. pr_err("error: enc and enc2 do not match: %d\n", ret);
  1336. goto test_gcm_end;
  1337. }
  1338. ret = XMEMCMP(authTag, enc2 + encryptLen, sizeof(authTag));
  1339. if (ret) {
  1340. pr_err("error: authTags do not match: %d\n", ret);
  1341. goto test_gcm_end;
  1342. }
  1343. /* Now decrypt crypto request. Reverse src and dst. */
  1344. memset(dec2, 0, decryptLen);
  1345. aead_request_set_ad(req, sizeof(assoc));
  1346. aead_request_set_crypt(req, dst, src, decryptLen, iv);
  1347. ret = crypto_aead_decrypt(req);
  1348. if (ret) {
  1349. pr_err("error: crypto_aead_decrypt returned: %d\n", ret);
  1350. goto test_gcm_end;
  1351. }
  1352. ret = XMEMCMP(dec, dec2, sizeof(p_vector));
  1353. if (ret) {
  1354. pr_err("error: dec and dec2 do not match: %d\n", ret);
  1355. goto test_gcm_end;
  1356. }
  1357. test_gcm_end:
  1358. if (req) { aead_request_free(req); req = NULL; }
  1359. if (tfm) { crypto_free_aead(tfm); tfm = NULL; }
  1360. if (src) { free(src); src = NULL; }
  1361. if (dst) { free(dst); dst = NULL; }
  1362. if (dec2) { free(dec2); dec2 = NULL; }
  1363. if (enc2) { free(enc2); enc2 = NULL; }
  1364. if (assoc2) { free(assoc2); assoc2 = NULL; }
  1365. if (iv) { free(iv); iv = NULL; }
  1366. if (aes_inited)
  1367. wc_AesFree(aes);
  1368. free(aes);
  1369. return ret;
  1370. }
  1371. #endif /* HAVE_AESGCM &&
  1372. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESGCM) &&
  1373. */
  1374. #if defined(WOLFSSL_AES_XTS) && \
  1375. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  1376. defined(LINUXKM_LKCAPI_REGISTER_AESXTS))
  1377. /* test vectors from
  1378. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html
  1379. */
  1380. #ifdef WOLFSSL_AES_128
  1381. static int aes_xts_128_test(void)
  1382. {
  1383. XtsAes *aes = NULL;
  1384. int aes_inited = 0;
  1385. int ret = 0;
  1386. #define AES_XTS_128_TEST_BUF_SIZ (AES_BLOCK_SIZE * 2 + 8)
  1387. unsigned char *buf = NULL;
  1388. unsigned char *cipher = NULL;
  1389. u8 * enc2 = NULL;
  1390. u8 * dec2 = NULL;
  1391. struct scatterlist * src = NULL;
  1392. struct scatterlist * dst = NULL;
  1393. struct crypto_skcipher *tfm = NULL;
  1394. struct skcipher_request *req = NULL;
  1395. u8 iv[AES_BLOCK_SIZE];
  1396. byte* large_input = NULL;
  1397. /* 128 key tests */
  1398. static const unsigned char k1[] = {
  1399. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  1400. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  1401. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  1402. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  1403. };
  1404. static const unsigned char i1[] = {
  1405. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  1406. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  1407. };
  1408. static const unsigned char p1[] = {
  1409. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  1410. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  1411. };
  1412. /* plain text test of partial block is not from NIST test vector list */
  1413. static const unsigned char pp[] = {
  1414. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  1415. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  1416. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  1417. };
  1418. static const unsigned char c1[] = {
  1419. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  1420. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  1421. };
  1422. /* plain text test of partial block is not from NIST test vector list */
  1423. static const unsigned char cp[] = {
  1424. 0x2b, 0xf7, 0x2c, 0xf3, 0xeb, 0x85, 0xef, 0x7b,
  1425. 0x0b, 0x76, 0xa0, 0xaa, 0xf3, 0x3f, 0x25, 0x8b,
  1426. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a
  1427. };
  1428. static const unsigned char k2[] = {
  1429. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  1430. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  1431. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  1432. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  1433. };
  1434. static const unsigned char i2[] = {
  1435. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  1436. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  1437. };
  1438. static const unsigned char p2[] = {
  1439. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  1440. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  1441. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  1442. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  1443. };
  1444. static const unsigned char c2[] = {
  1445. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  1446. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  1447. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  1448. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  1449. };
  1450. #ifndef HAVE_FIPS /* FIPS requires different keys for main and tweak. */
  1451. static const unsigned char k3[] = {
  1452. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1453. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1454. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1455. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1456. };
  1457. static const unsigned char i3[] = {
  1458. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1459. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1460. };
  1461. static const unsigned char p3[] = {
  1462. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1463. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1464. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1465. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  1466. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  1467. };
  1468. static const unsigned char c3[] = {
  1469. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  1470. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  1471. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  1472. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  1473. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  1474. };
  1475. #endif /* HAVE_FIPS */
  1476. if ((aes = (XtsAes *)XMALLOC(sizeof(*aes), NULL, DYNAMIC_TYPE_AES))
  1477. == NULL)
  1478. {
  1479. ret = MEMORY_E;
  1480. goto out;
  1481. }
  1482. if ((buf = (unsigned char *)XMALLOC(AES_XTS_128_TEST_BUF_SIZ, NULL,
  1483. DYNAMIC_TYPE_AES)) == NULL)
  1484. {
  1485. ret = MEMORY_E;
  1486. goto out;
  1487. }
  1488. if ((cipher = (unsigned char *)XMALLOC(AES_XTS_128_TEST_BUF_SIZ, NULL,
  1489. DYNAMIC_TYPE_AES)) == NULL)
  1490. {
  1491. ret = MEMORY_E;
  1492. goto out;
  1493. }
  1494. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1495. ret = wc_AesXtsInit(aes, NULL, INVALID_DEVID);
  1496. if (ret != 0)
  1497. goto out;
  1498. else
  1499. aes_inited = 1;
  1500. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  1501. if (ret != 0)
  1502. goto out;
  1503. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  1504. if (ret != 0)
  1505. goto out;
  1506. if (XMEMCMP(c2, buf, sizeof(c2))) {
  1507. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1508. goto out;
  1509. }
  1510. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  1511. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  1512. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  1513. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  1514. if (ret != 0)
  1515. goto out;
  1516. if (XMEMCMP(c2, buf, sizeof(c2))) {
  1517. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1518. goto out;
  1519. }
  1520. #endif
  1521. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1522. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  1523. if (ret != 0)
  1524. goto out;
  1525. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  1526. if (ret != 0)
  1527. goto out;
  1528. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE)) {
  1529. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1530. goto out;
  1531. }
  1532. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  1533. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  1534. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  1535. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  1536. if (ret != 0)
  1537. goto out;
  1538. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE)) {
  1539. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1540. goto out;
  1541. }
  1542. #endif
  1543. /* partial block encryption test */
  1544. XMEMSET(cipher, 0, AES_XTS_128_TEST_BUF_SIZ);
  1545. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  1546. if (ret != 0)
  1547. goto out;
  1548. if (XMEMCMP(cp, cipher, sizeof(cp))) {
  1549. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1550. goto out;
  1551. }
  1552. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  1553. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  1554. XMEMSET(cipher, 0, AES_XTS_128_TEST_BUF_SIZ);
  1555. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  1556. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  1557. if (ret != 0)
  1558. goto out;
  1559. if (XMEMCMP(cp, cipher, sizeof(cp))) {
  1560. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1561. goto out;
  1562. }
  1563. #endif
  1564. /* partial block decrypt test */
  1565. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1566. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  1567. if (ret != 0)
  1568. goto out;
  1569. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  1570. if (ret != 0)
  1571. goto out;
  1572. if (XMEMCMP(pp, buf, sizeof(pp))) {
  1573. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1574. goto out;
  1575. }
  1576. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  1577. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  1578. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1579. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  1580. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  1581. if (ret != 0)
  1582. goto out;
  1583. if (XMEMCMP(pp, buf, sizeof(pp))) {
  1584. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1585. goto out;
  1586. }
  1587. #endif
  1588. /* NIST decrypt test vector */
  1589. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1590. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  1591. if (ret != 0)
  1592. goto out;
  1593. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE)) {
  1594. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1595. goto out;
  1596. }
  1597. #if defined(DEBUG_VECTOR_REGISTER_ACCESS) && defined(WC_C_DYNAMIC_FALLBACK)
  1598. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(SYSLIB_FAILED_E);
  1599. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1600. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  1601. WC_DEBUG_SET_VECTOR_REGISTERS_RETVAL(0);
  1602. if (ret != 0)
  1603. goto out;
  1604. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE)) {
  1605. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1606. goto out;
  1607. }
  1608. #endif
  1609. /* fail case with decrypting using wrong key */
  1610. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1611. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  1612. if (ret != 0)
  1613. goto out;
  1614. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) { /* fail case with wrong key */
  1615. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1616. goto out;
  1617. }
  1618. /* set correct key and retest */
  1619. XMEMSET(buf, 0, AES_XTS_128_TEST_BUF_SIZ);
  1620. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  1621. if (ret != 0)
  1622. goto out;
  1623. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  1624. if (ret != 0)
  1625. goto out;
  1626. if (XMEMCMP(p2, buf, sizeof(p2))) {
  1627. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1628. goto out;
  1629. }
  1630. #ifndef HAVE_FIPS
  1631. /* Test ciphertext stealing in-place. */
  1632. XMEMCPY(buf, p3, sizeof(p3));
  1633. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_ENCRYPTION);
  1634. if (ret != 0)
  1635. goto out;
  1636. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  1637. if (ret != 0)
  1638. goto out;
  1639. if (XMEMCMP(c3, buf, sizeof(c3))) {
  1640. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1641. goto out;
  1642. }
  1643. ret = wc_AesXtsSetKeyNoInit(aes, k3, sizeof(k3), AES_DECRYPTION);
  1644. if (ret != 0)
  1645. goto out;
  1646. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  1647. if (ret != 0)
  1648. goto out;
  1649. if (XMEMCMP(p3, buf, sizeof(p3))) {
  1650. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1651. goto out;
  1652. }
  1653. #endif /* HAVE_FIPS */
  1654. {
  1655. #define LARGE_XTS_SZ 1024
  1656. int i;
  1657. int j;
  1658. large_input = (byte *)XMALLOC(LARGE_XTS_SZ, NULL,
  1659. DYNAMIC_TYPE_TMP_BUFFER);
  1660. if (large_input == NULL) {
  1661. ret = MEMORY_E;
  1662. goto out;
  1663. }
  1664. for (i = 0; i < (int)LARGE_XTS_SZ; i++)
  1665. large_input[i] = (byte)i;
  1666. for (j = 16; j < (int)LARGE_XTS_SZ; j++) {
  1667. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  1668. if (ret != 0)
  1669. goto out;
  1670. ret = wc_AesXtsEncrypt(aes, large_input, large_input, j, i1,
  1671. sizeof(i1));
  1672. if (ret != 0)
  1673. goto out;
  1674. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  1675. if (ret != 0)
  1676. goto out;
  1677. ret = wc_AesXtsDecrypt(aes, large_input, large_input, j, i1,
  1678. sizeof(i1));
  1679. if (ret != 0)
  1680. goto out;
  1681. for (i = 0; i < j; i++) {
  1682. if (large_input[i] != (byte)i) {
  1683. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1684. goto out;
  1685. }
  1686. }
  1687. }
  1688. }
  1689. /* now the kernel crypto part */
  1690. enc2 = XMALLOC(sizeof(pp), NULL, DYNAMIC_TYPE_AES);
  1691. if (!enc2) {
  1692. pr_err("error: malloc failed\n");
  1693. ret = -ENOMEM;
  1694. goto test_xts_end;
  1695. }
  1696. dec2 = XMALLOC(sizeof(pp), NULL, DYNAMIC_TYPE_AES);
  1697. if (!dec2) {
  1698. pr_err("error: malloc failed\n");
  1699. ret = -ENOMEM;
  1700. goto test_xts_end;
  1701. }
  1702. src = XMALLOC(sizeof(*src) * 2, NULL, DYNAMIC_TYPE_AES);
  1703. if (! src) {
  1704. pr_err("error: malloc failed\n");
  1705. ret = -ENOMEM;
  1706. goto test_xts_end;
  1707. }
  1708. dst = XMALLOC(sizeof(*dst) * 2, NULL, DYNAMIC_TYPE_AES);
  1709. if (! dst) {
  1710. pr_err("error: malloc failed\n");
  1711. ret = -ENOMEM;
  1712. goto test_xts_end;
  1713. }
  1714. tfm = crypto_alloc_skcipher(WOLFKM_AESXTS_NAME, 0, 0);
  1715. if (IS_ERR(tfm)) {
  1716. ret = PTR_ERR(tfm);
  1717. pr_err("error: allocating AES skcipher algorithm %s failed: %d\n",
  1718. WOLFKM_AESXTS_DRIVER, ret);
  1719. goto test_xts_end;
  1720. }
  1721. #ifndef LINUXKM_LKCAPI_PRIORITY_ALLOW_MASKING
  1722. {
  1723. const char *driver_name =
  1724. crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  1725. if (strcmp(driver_name, WOLFKM_AESXTS_DRIVER)) {
  1726. pr_err("error: unexpected implementation for %s: %s (expected %s)\n",
  1727. WOLFKM_AESXTS_NAME, driver_name, WOLFKM_AESXTS_DRIVER);
  1728. ret = -ENOENT;
  1729. goto test_xts_end;
  1730. }
  1731. }
  1732. #endif
  1733. ret = crypto_skcipher_ivsize(tfm);
  1734. if (ret != sizeof(iv)) {
  1735. pr_err("error: AES skcipher algorithm %s crypto_skcipher_ivsize()"
  1736. " returned %d but expected %d\n",
  1737. WOLFKM_AESXTS_DRIVER, ret, (int)sizeof(iv));
  1738. ret = -EINVAL;
  1739. goto test_xts_end;
  1740. }
  1741. ret = crypto_skcipher_setkey(tfm, k1, sizeof(k1));
  1742. if (ret) {
  1743. pr_err("error: crypto_skcipher_setkey for %s returned: %d\n",
  1744. WOLFKM_AESXTS_NAME, ret);
  1745. goto test_xts_end;
  1746. }
  1747. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  1748. if (IS_ERR(req)) {
  1749. ret = PTR_ERR(req);
  1750. pr_err("error: allocating AES skcipher request %s failed: %d\n",
  1751. WOLFKM_AESXTS_DRIVER, ret);
  1752. goto test_xts_end;
  1753. }
  1754. memcpy(dec2, p1, sizeof(p1));
  1755. memset(enc2, 0, sizeof(p1));
  1756. sg_init_one(src, dec2, sizeof(p1));
  1757. sg_init_one(dst, enc2, sizeof(p1));
  1758. memcpy(iv, i1, sizeof(iv));
  1759. skcipher_request_set_crypt(req, src, dst, sizeof(p1), iv);
  1760. ret = crypto_skcipher_encrypt(req);
  1761. if (ret) {
  1762. pr_err("error: crypto_skcipher_encrypt returned: %d\n", ret);
  1763. goto test_xts_end;
  1764. }
  1765. ret = XMEMCMP(c1, enc2, sizeof(c1));
  1766. if (ret) {
  1767. pr_err("error: c1 and enc2 do not match: %d\n", ret);
  1768. ret = -EINVAL;
  1769. goto test_xts_end;
  1770. }
  1771. memset(dec2, 0, sizeof(p1));
  1772. sg_init_one(src, enc2, sizeof(p1));
  1773. sg_init_one(dst, dec2, sizeof(p1));
  1774. memcpy(iv, i1, sizeof(iv));
  1775. skcipher_request_set_crypt(req, src, dst, sizeof(p1), iv);
  1776. ret = crypto_skcipher_decrypt(req);
  1777. if (ret) {
  1778. pr_err("ERROR: crypto_skcipher_decrypt returned %d\n", ret);
  1779. goto test_xts_end;
  1780. }
  1781. ret = XMEMCMP(p1, dec2, sizeof(p1));
  1782. if (ret) {
  1783. pr_err("error: p1 and dec2 do not match: %d\n", ret);
  1784. ret = -EINVAL;
  1785. goto test_xts_end;
  1786. }
  1787. memcpy(dec2, pp, sizeof(pp));
  1788. memset(enc2, 0, sizeof(pp));
  1789. sg_init_one(src, dec2, sizeof(pp));
  1790. sg_init_one(dst, enc2, sizeof(pp));
  1791. memcpy(iv, i1, sizeof(iv));
  1792. skcipher_request_set_crypt(req, src, dst, sizeof(pp), iv);
  1793. ret = crypto_skcipher_encrypt(req);
  1794. if (ret) {
  1795. pr_err("error: crypto_skcipher_encrypt returned: %d\n", ret);
  1796. goto test_xts_end;
  1797. }
  1798. ret = XMEMCMP(cp, enc2, sizeof(cp));
  1799. if (ret) {
  1800. pr_err("error: cp and enc2 do not match: %d\n", ret);
  1801. ret = -EINVAL;
  1802. goto test_xts_end;
  1803. }
  1804. memset(dec2, 0, sizeof(pp));
  1805. sg_init_one(src, enc2, sizeof(pp));
  1806. sg_init_one(dst, dec2, sizeof(pp));
  1807. memcpy(iv, i1, sizeof(iv));
  1808. skcipher_request_set_crypt(req, src, dst, sizeof(pp), iv);
  1809. ret = crypto_skcipher_decrypt(req);
  1810. if (ret) {
  1811. pr_err("ERROR: crypto_skcipher_decrypt returned %d\n", ret);
  1812. goto test_xts_end;
  1813. }
  1814. ret = XMEMCMP(pp, dec2, sizeof(pp));
  1815. if (ret) {
  1816. pr_err("error: pp and dec2 do not match: %d\n", ret);
  1817. ret = -EINVAL;
  1818. goto test_xts_end;
  1819. }
  1820. test_xts_end:
  1821. if (enc2)
  1822. XFREE(enc2, NULL, DYNAMIC_TYPE_AES);
  1823. if (dec2)
  1824. XFREE(dec2, NULL, DYNAMIC_TYPE_AES);
  1825. if (src)
  1826. XFREE(src, NULL, DYNAMIC_TYPE_AES);
  1827. if (dst)
  1828. XFREE(dst, NULL, DYNAMIC_TYPE_AES);
  1829. if (req)
  1830. skcipher_request_free(req);
  1831. if (tfm)
  1832. crypto_free_skcipher(tfm);
  1833. out:
  1834. if (large_input)
  1835. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1836. if (aes_inited)
  1837. wc_AesXtsFree(aes);
  1838. if (buf)
  1839. XFREE(buf, NULL, DYNAMIC_TYPE_AES);
  1840. if (cipher)
  1841. XFREE(cipher, NULL, DYNAMIC_TYPE_AES);
  1842. if (aes)
  1843. XFREE(aes, NULL, DYNAMIC_TYPE_AES);
  1844. #undef AES_XTS_128_TEST_BUF_SIZ
  1845. return ret;
  1846. }
  1847. #endif /* WOLFSSL_AES_128 */
  1848. #ifdef WOLFSSL_AES_256
  1849. static int aes_xts_256_test(void)
  1850. {
  1851. XtsAes *aes = NULL;
  1852. int aes_inited = 0;
  1853. int ret = 0;
  1854. #define AES_XTS_256_TEST_BUF_SIZ (AES_BLOCK_SIZE * 3)
  1855. unsigned char *buf = NULL;
  1856. unsigned char *cipher = NULL;
  1857. u8 * enc2 = NULL;
  1858. u8 * dec2 = NULL;
  1859. struct scatterlist * src = NULL;
  1860. struct scatterlist * dst = NULL;
  1861. struct crypto_skcipher *tfm = NULL;
  1862. struct skcipher_request *req = NULL;
  1863. u8 iv[AES_BLOCK_SIZE];
  1864. /* 256 key tests */
  1865. static const unsigned char k1[] = {
  1866. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  1867. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  1868. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  1869. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  1870. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  1871. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  1872. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  1873. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  1874. };
  1875. static const unsigned char i1[] = {
  1876. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  1877. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  1878. };
  1879. static const unsigned char p1[] = {
  1880. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  1881. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  1882. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  1883. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  1884. };
  1885. static const unsigned char c1[] = {
  1886. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  1887. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  1888. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  1889. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  1890. };
  1891. /* plain text test of partial block is not from NIST test vector list */
  1892. static const unsigned char pp[] = {
  1893. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  1894. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  1895. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  1896. };
  1897. static const unsigned char cp[] = {
  1898. 0x65, 0x5e, 0x1d, 0x37, 0x4a, 0x91, 0xe7, 0x6c,
  1899. 0x4f, 0x83, 0x92, 0xbc, 0x5a, 0x10, 0x55, 0x27,
  1900. 0x61, 0x0e, 0x5a, 0xde, 0xca, 0xc5, 0x12, 0xd8
  1901. };
  1902. static const unsigned char k2[] = {
  1903. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  1904. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  1905. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  1906. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  1907. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  1908. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  1909. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  1910. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  1911. };
  1912. static const unsigned char i2[] = {
  1913. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  1914. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  1915. };
  1916. static const unsigned char p2[] = {
  1917. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  1918. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  1919. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  1920. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  1921. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  1922. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  1923. };
  1924. static const unsigned char c2[] = {
  1925. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  1926. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  1927. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  1928. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  1929. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  1930. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  1931. };
  1932. if ((aes = (XtsAes *)XMALLOC(sizeof(*aes), NULL, DYNAMIC_TYPE_AES))
  1933. == NULL)
  1934. {
  1935. ret = MEMORY_E;
  1936. goto out;
  1937. }
  1938. if ((buf = (unsigned char *)XMALLOC(AES_XTS_256_TEST_BUF_SIZ, NULL,
  1939. DYNAMIC_TYPE_AES)) == NULL)
  1940. {
  1941. ret = MEMORY_E;
  1942. goto out;
  1943. }
  1944. if ((cipher = (unsigned char *)XMALLOC(AES_XTS_256_TEST_BUF_SIZ, NULL,
  1945. DYNAMIC_TYPE_AES)) == NULL)
  1946. {
  1947. ret = MEMORY_E;
  1948. goto out;
  1949. }
  1950. ret = wc_AesXtsInit(aes, NULL, INVALID_DEVID);
  1951. if (ret != 0)
  1952. goto out;
  1953. else
  1954. aes_inited = 1;
  1955. XMEMSET(buf, 0, AES_XTS_256_TEST_BUF_SIZ);
  1956. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_ENCRYPTION);
  1957. if (ret != 0)
  1958. goto out;
  1959. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  1960. if (ret != 0)
  1961. goto out;
  1962. if (XMEMCMP(c2, buf, sizeof(c2))) {
  1963. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1964. goto out;
  1965. }
  1966. XMEMSET(buf, 0, AES_XTS_256_TEST_BUF_SIZ);
  1967. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_ENCRYPTION);
  1968. if (ret != 0)
  1969. goto out;
  1970. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  1971. if (ret != 0)
  1972. goto out;
  1973. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE)) {
  1974. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1975. goto out;
  1976. }
  1977. /* partial block encryption test */
  1978. XMEMSET(cipher, 0, AES_XTS_256_TEST_BUF_SIZ);
  1979. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  1980. if (ret != 0)
  1981. goto out;
  1982. /* partial block decrypt test */
  1983. XMEMSET(buf, 0, AES_XTS_256_TEST_BUF_SIZ);
  1984. ret = wc_AesXtsSetKeyNoInit(aes, k1, sizeof(k1), AES_DECRYPTION);
  1985. if (ret != 0)
  1986. goto out;
  1987. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  1988. if (ret != 0)
  1989. goto out;
  1990. if (XMEMCMP(pp, buf, sizeof(pp))) {
  1991. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  1992. goto out;
  1993. }
  1994. /* NIST decrypt test vector */
  1995. XMEMSET(buf, 0, AES_XTS_256_TEST_BUF_SIZ);
  1996. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  1997. if (ret != 0)
  1998. goto out;
  1999. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE)) {
  2000. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  2001. goto out;
  2002. }
  2003. XMEMSET(buf, 0, AES_XTS_256_TEST_BUF_SIZ);
  2004. ret = wc_AesXtsSetKeyNoInit(aes, k2, sizeof(k2), AES_DECRYPTION);
  2005. if (ret != 0)
  2006. goto out;
  2007. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  2008. if (ret != 0)
  2009. goto out;
  2010. if (XMEMCMP(p2, buf, sizeof(p2))) {
  2011. ret = LINUXKM_LKCAPI_AES_KAT_MISMATCH_E;
  2012. goto out;
  2013. }
  2014. /* now the kernel crypto part */
  2015. enc2 = XMALLOC(sizeof(p1), NULL, DYNAMIC_TYPE_AES);
  2016. if (!enc2) {
  2017. pr_err("error: malloc failed\n");
  2018. ret = -ENOMEM;
  2019. goto test_xts_end;
  2020. }
  2021. dec2 = XMALLOC(sizeof(p1), NULL, DYNAMIC_TYPE_AES);
  2022. if (!dec2) {
  2023. pr_err("error: malloc failed\n");
  2024. ret = -ENOMEM;
  2025. goto test_xts_end;
  2026. }
  2027. src = XMALLOC(sizeof(*src) * 2, NULL, DYNAMIC_TYPE_AES);
  2028. if (! src) {
  2029. pr_err("error: malloc failed\n");
  2030. ret = -ENOMEM;
  2031. goto test_xts_end;
  2032. }
  2033. dst = XMALLOC(sizeof(*dst) * 2, NULL, DYNAMIC_TYPE_AES);
  2034. if (! dst) {
  2035. pr_err("error: malloc failed\n");
  2036. ret = -ENOMEM;
  2037. goto test_xts_end;
  2038. }
  2039. tfm = crypto_alloc_skcipher(WOLFKM_AESXTS_NAME, 0, 0);
  2040. if (IS_ERR(tfm)) {
  2041. ret = PTR_ERR(tfm);
  2042. pr_err("error: allocating AES skcipher algorithm %s failed: %d\n",
  2043. WOLFKM_AESXTS_DRIVER, ret);
  2044. goto test_xts_end;
  2045. }
  2046. #ifndef LINUXKM_LKCAPI_PRIORITY_ALLOW_MASKING
  2047. {
  2048. const char *driver_name = crypto_tfm_alg_driver_name(crypto_skcipher_tfm(tfm));
  2049. if (strcmp(driver_name, WOLFKM_AESXTS_DRIVER)) {
  2050. pr_err("error: unexpected implementation for %s: %s (expected %s)\n",
  2051. WOLFKM_AESXTS_NAME, driver_name, WOLFKM_AESXTS_DRIVER);
  2052. ret = -ENOENT;
  2053. goto test_xts_end;
  2054. }
  2055. }
  2056. #endif
  2057. ret = crypto_skcipher_ivsize(tfm);
  2058. if (ret != sizeof(iv)) {
  2059. pr_err("error: AES skcipher algorithm %s crypto_skcipher_ivsize()"
  2060. " returned %d but expected %d\n",
  2061. WOLFKM_AESXTS_DRIVER, ret, (int)sizeof(iv));
  2062. ret = -EINVAL;
  2063. goto test_xts_end;
  2064. }
  2065. ret = crypto_skcipher_setkey(tfm, k1, sizeof(k1));
  2066. if (ret) {
  2067. pr_err("error: crypto_skcipher_setkey for %s returned: %d\n",
  2068. WOLFKM_AESXTS_NAME, ret);
  2069. goto test_xts_end;
  2070. }
  2071. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  2072. if (IS_ERR(req)) {
  2073. ret = PTR_ERR(req);
  2074. pr_err("error: allocating AES skcipher request %s failed: %d\n",
  2075. WOLFKM_AESXTS_DRIVER, ret);
  2076. goto test_xts_end;
  2077. }
  2078. memcpy(dec2, p1, sizeof(p1));
  2079. memset(enc2, 0, sizeof(p1));
  2080. sg_init_one(src, dec2, sizeof(p1));
  2081. sg_init_one(dst, enc2, sizeof(p1));
  2082. memcpy(iv, i1, sizeof(iv));
  2083. skcipher_request_set_crypt(req, src, dst, sizeof(p1), iv);
  2084. ret = crypto_skcipher_encrypt(req);
  2085. if (ret) {
  2086. pr_err("error: crypto_skcipher_encrypt returned: %d\n", ret);
  2087. goto test_xts_end;
  2088. }
  2089. ret = XMEMCMP(c1, enc2, sizeof(c1));
  2090. if (ret) {
  2091. pr_err("error: c1 and enc2 do not match: %d\n", ret);
  2092. ret = -EINVAL;
  2093. goto test_xts_end;
  2094. }
  2095. memset(dec2, 0, sizeof(p1));
  2096. sg_init_one(src, enc2, sizeof(p1));
  2097. sg_init_one(dst, dec2, sizeof(p1));
  2098. memcpy(iv, i1, sizeof(iv));
  2099. skcipher_request_set_crypt(req, src, dst, sizeof(p1), iv);
  2100. ret = crypto_skcipher_decrypt(req);
  2101. if (ret) {
  2102. pr_err("ERROR: crypto_skcipher_decrypt returned %d\n", ret);
  2103. goto test_xts_end;
  2104. }
  2105. ret = XMEMCMP(p1, dec2, sizeof(p1));
  2106. if (ret) {
  2107. pr_err("error: p1 and dec2 do not match: %d\n", ret);
  2108. ret = -EINVAL;
  2109. goto test_xts_end;
  2110. }
  2111. memcpy(dec2, pp, sizeof(pp));
  2112. memset(enc2, 0, sizeof(pp));
  2113. sg_init_one(src, dec2, sizeof(pp));
  2114. sg_init_one(dst, enc2, sizeof(pp));
  2115. memcpy(iv, i1, sizeof(iv));
  2116. skcipher_request_set_crypt(req, src, dst, sizeof(pp), iv);
  2117. ret = crypto_skcipher_encrypt(req);
  2118. if (ret) {
  2119. pr_err("error: crypto_skcipher_encrypt returned: %d\n", ret);
  2120. goto test_xts_end;
  2121. }
  2122. ret = XMEMCMP(cp, enc2, sizeof(cp));
  2123. if (ret) {
  2124. pr_err("error: cp and enc2 do not match: %d\n", ret);
  2125. ret = -EINVAL;
  2126. goto test_xts_end;
  2127. }
  2128. memset(dec2, 0, sizeof(pp));
  2129. sg_init_one(src, enc2, sizeof(pp));
  2130. sg_init_one(dst, dec2, sizeof(pp));
  2131. memcpy(iv, i1, sizeof(iv));
  2132. skcipher_request_set_crypt(req, src, dst, sizeof(pp), iv);
  2133. ret = crypto_skcipher_decrypt(req);
  2134. if (ret) {
  2135. pr_err("ERROR: crypto_skcipher_decrypt returned %d\n", ret);
  2136. goto test_xts_end;
  2137. }
  2138. ret = XMEMCMP(pp, dec2, sizeof(pp));
  2139. if (ret) {
  2140. pr_err("error: pp and dec2 do not match: %d\n", ret);
  2141. ret = -EINVAL;
  2142. goto test_xts_end;
  2143. }
  2144. test_xts_end:
  2145. if (enc2)
  2146. XFREE(enc2, NULL, DYNAMIC_TYPE_AES);
  2147. if (dec2)
  2148. XFREE(dec2, NULL, DYNAMIC_TYPE_AES);
  2149. if (src)
  2150. XFREE(src, NULL, DYNAMIC_TYPE_AES);
  2151. if (dst)
  2152. XFREE(dst, NULL, DYNAMIC_TYPE_AES);
  2153. if (req)
  2154. skcipher_request_free(req);
  2155. if (tfm)
  2156. crypto_free_skcipher(tfm);
  2157. out:
  2158. if (aes_inited)
  2159. wc_AesXtsFree(aes);
  2160. if (buf)
  2161. XFREE(buf, NULL, DYNAMIC_TYPE_AES);
  2162. if (cipher)
  2163. XFREE(cipher, NULL, DYNAMIC_TYPE_AES);
  2164. if (aes)
  2165. XFREE(aes, NULL, DYNAMIC_TYPE_AES);
  2166. #undef AES_XTS_256_TEST_BUF_SIZ
  2167. return ret;
  2168. }
  2169. #endif /* WOLFSSL_AES_256 */
  2170. static int linuxkm_test_aesxts(void) {
  2171. int ret;
  2172. #ifdef WOLFSSL_AES_128
  2173. ret = aes_xts_128_test();
  2174. if (ret != 0) {
  2175. pr_err("aes_xts_128_test() failed with retval %d.\n", ret);
  2176. goto out;
  2177. }
  2178. #endif
  2179. #ifdef WOLFSSL_AES_256
  2180. ret = aes_xts_256_test();
  2181. if (ret != 0) {
  2182. pr_err("aes_xts_256_test() failed with retval %d.\n", ret);
  2183. goto out;
  2184. }
  2185. #endif
  2186. out:
  2187. return ret;
  2188. }
  2189. #endif /* WOLFSSL_AES_XTS &&
  2190. * (LINUXKM_LKCAPI_REGISTER_ALL || LINUXKM_LKCAPI_REGISTER_AESXTS)
  2191. */
  2192. #endif /* !NO_AES */
  2193. static int linuxkm_lkcapi_register(void)
  2194. {
  2195. int ret = 0;
  2196. #define REGISTER_ALG(alg, installer, tester) do { \
  2197. if (alg ## _loaded) { \
  2198. pr_err("ERROR: %s is already registered.\n", \
  2199. (alg).base.cra_driver_name); \
  2200. return -EEXIST; \
  2201. } \
  2202. \
  2203. ret = (installer)(&(alg)); \
  2204. \
  2205. if (ret) { \
  2206. pr_err("ERROR: " #installer " for %s failed " \
  2207. "with return code %d.\n", \
  2208. (alg).base.cra_driver_name, ret); \
  2209. return ret; \
  2210. } \
  2211. \
  2212. alg ## _loaded = 1; \
  2213. \
  2214. ret = (tester()); \
  2215. \
  2216. if (ret) { \
  2217. pr_err("ERROR: self-test for %s failed " \
  2218. "with return code %d.\n", \
  2219. (alg).base.cra_driver_name, ret); \
  2220. return ret; \
  2221. } \
  2222. pr_info("%s self-test OK -- " \
  2223. "registered for %s with priority %d.\n", \
  2224. (alg).base.cra_driver_name, \
  2225. (alg).base.cra_name, \
  2226. (alg).base.cra_priority); \
  2227. } while (0)
  2228. #if defined(HAVE_AES_CBC) && \
  2229. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2230. defined(LINUXKM_LKCAPI_REGISTER_AESCBC))
  2231. REGISTER_ALG(cbcAesAlg, crypto_register_skcipher, linuxkm_test_aescbc);
  2232. #endif
  2233. #if defined(WOLFSSL_AES_CFB) && \
  2234. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2235. defined(LINUXKM_LKCAPI_REGISTER_AESCFB))
  2236. REGISTER_ALG(cfbAesAlg, crypto_register_skcipher, linuxkm_test_aescfb);
  2237. #endif
  2238. #if defined(HAVE_AESGCM) && \
  2239. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2240. defined(LINUXKM_LKCAPI_REGISTER_AESGCM))
  2241. REGISTER_ALG(gcmAesAead, crypto_register_aead, linuxkm_test_aesgcm);
  2242. #endif
  2243. #if defined(WOLFSSL_AES_XTS) && \
  2244. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2245. defined(LINUXKM_LKCAPI_REGISTER_AESXTS))
  2246. REGISTER_ALG(xtsAesAlg, crypto_register_skcipher, linuxkm_test_aesxts);
  2247. #endif
  2248. #undef REGISTER_ALG
  2249. return 0;
  2250. }
  2251. static void linuxkm_lkcapi_unregister(void)
  2252. {
  2253. #define UNREGISTER_ALG(alg, uninstaller) do { \
  2254. if (alg ## _loaded) { \
  2255. (uninstaller)(&(alg)); \
  2256. alg ## _loaded = 0; \
  2257. } \
  2258. } while (0)
  2259. #if defined(HAVE_AES_CBC) && \
  2260. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2261. defined(LINUXKM_LKCAPI_REGISTER_AESCBC))
  2262. UNREGISTER_ALG(cbcAesAlg, crypto_unregister_skcipher);
  2263. #endif
  2264. #if defined(WOLFSSL_AES_CFB) && \
  2265. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2266. defined(LINUXKM_LKCAPI_REGISTER_AESCFB))
  2267. UNREGISTER_ALG(cfbAesAlg, crypto_unregister_skcipher);
  2268. #endif
  2269. #if defined(HAVE_AESGCM) && \
  2270. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2271. defined(LINUXKM_LKCAPI_REGISTER_AESGCM))
  2272. UNREGISTER_ALG(gcmAesAead, crypto_unregister_aead);
  2273. #endif
  2274. #if defined(WOLFSSL_AES_XTS) && \
  2275. (defined(LINUXKM_LKCAPI_REGISTER_ALL) || \
  2276. defined(LINUXKM_LKCAPI_REGISTER_AESXTS))
  2277. UNREGISTER_ALG(xtsAesAlg, crypto_unregister_skcipher);
  2278. #endif
  2279. #undef UNREGISTER_ALG
  2280. }