atmel-aes.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for ATMEL AES HW acceleration.
  5. *
  6. * Copyright (c) 2012 Eukréa Electromatique - ATMEL
  7. * Author: Nicolas Royer <nicolas@eukrea.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * Some ideas are from omap-aes.c driver.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/hw_random.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/device.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/of_device.h>
  31. #include <linux/delay.h>
  32. #include <linux/crypto.h>
  33. #include <crypto/scatterwalk.h>
  34. #include <crypto/algapi.h>
  35. #include <crypto/aes.h>
  36. #include <crypto/gcm.h>
  37. #include <crypto/xts.h>
  38. #include <crypto/internal/aead.h>
  39. #include <linux/platform_data/crypto-atmel.h>
  40. #include <dt-bindings/dma/at91.h>
  41. #include "atmel-aes-regs.h"
  42. #include "atmel-authenc.h"
  43. #define ATMEL_AES_PRIORITY 300
  44. #define ATMEL_AES_BUFFER_ORDER 2
  45. #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER)
  46. #define CFB8_BLOCK_SIZE 1
  47. #define CFB16_BLOCK_SIZE 2
  48. #define CFB32_BLOCK_SIZE 4
  49. #define CFB64_BLOCK_SIZE 8
  50. #define SIZE_IN_WORDS(x) ((x) >> 2)
  51. /* AES flags */
  52. /* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */
  53. #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC
  54. #define AES_FLAGS_GTAGEN AES_MR_GTAGEN
  55. #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK)
  56. #define AES_FLAGS_ECB AES_MR_OPMOD_ECB
  57. #define AES_FLAGS_CBC AES_MR_OPMOD_CBC
  58. #define AES_FLAGS_OFB AES_MR_OPMOD_OFB
  59. #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b)
  60. #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b)
  61. #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b)
  62. #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b)
  63. #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b)
  64. #define AES_FLAGS_CTR AES_MR_OPMOD_CTR
  65. #define AES_FLAGS_GCM AES_MR_OPMOD_GCM
  66. #define AES_FLAGS_XTS AES_MR_OPMOD_XTS
  67. #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \
  68. AES_FLAGS_ENCRYPT | \
  69. AES_FLAGS_GTAGEN)
  70. #define AES_FLAGS_BUSY BIT(3)
  71. #define AES_FLAGS_DUMP_REG BIT(4)
  72. #define AES_FLAGS_OWN_SHA BIT(5)
  73. #define AES_FLAGS_PERSISTENT AES_FLAGS_BUSY
  74. #define ATMEL_AES_QUEUE_LENGTH 50
  75. #define ATMEL_AES_DMA_THRESHOLD 256
  76. struct atmel_aes_caps {
  77. bool has_dualbuff;
  78. bool has_cfb64;
  79. bool has_gcm;
  80. bool has_xts;
  81. bool has_authenc;
  82. u32 max_burst_size;
  83. };
  84. struct atmel_aes_dev;
  85. typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *);
  86. struct atmel_aes_base_ctx {
  87. struct atmel_aes_dev *dd;
  88. atmel_aes_fn_t start;
  89. int keylen;
  90. u32 key[AES_KEYSIZE_256 / sizeof(u32)];
  91. u16 block_size;
  92. bool is_aead;
  93. };
  94. struct atmel_aes_ctx {
  95. struct atmel_aes_base_ctx base;
  96. };
  97. struct atmel_aes_ctr_ctx {
  98. struct atmel_aes_base_ctx base;
  99. u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
  100. size_t offset;
  101. struct scatterlist src[2];
  102. struct scatterlist dst[2];
  103. };
  104. struct atmel_aes_gcm_ctx {
  105. struct atmel_aes_base_ctx base;
  106. struct scatterlist src[2];
  107. struct scatterlist dst[2];
  108. u32 j0[AES_BLOCK_SIZE / sizeof(u32)];
  109. u32 tag[AES_BLOCK_SIZE / sizeof(u32)];
  110. u32 ghash[AES_BLOCK_SIZE / sizeof(u32)];
  111. size_t textlen;
  112. const u32 *ghash_in;
  113. u32 *ghash_out;
  114. atmel_aes_fn_t ghash_resume;
  115. };
  116. struct atmel_aes_xts_ctx {
  117. struct atmel_aes_base_ctx base;
  118. u32 key2[AES_KEYSIZE_256 / sizeof(u32)];
  119. };
  120. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  121. struct atmel_aes_authenc_ctx {
  122. struct atmel_aes_base_ctx base;
  123. struct atmel_sha_authenc_ctx *auth;
  124. };
  125. #endif
  126. struct atmel_aes_reqctx {
  127. unsigned long mode;
  128. u32 lastc[AES_BLOCK_SIZE / sizeof(u32)];
  129. };
  130. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  131. struct atmel_aes_authenc_reqctx {
  132. struct atmel_aes_reqctx base;
  133. struct scatterlist src[2];
  134. struct scatterlist dst[2];
  135. size_t textlen;
  136. u32 digest[SHA512_DIGEST_SIZE / sizeof(u32)];
  137. /* auth_req MUST be place last. */
  138. struct ahash_request auth_req;
  139. };
  140. #endif
  141. struct atmel_aes_dma {
  142. struct dma_chan *chan;
  143. struct scatterlist *sg;
  144. int nents;
  145. unsigned int remainder;
  146. unsigned int sg_len;
  147. };
  148. struct atmel_aes_dev {
  149. struct list_head list;
  150. unsigned long phys_base;
  151. void __iomem *io_base;
  152. struct crypto_async_request *areq;
  153. struct atmel_aes_base_ctx *ctx;
  154. bool is_async;
  155. atmel_aes_fn_t resume;
  156. atmel_aes_fn_t cpu_transfer_complete;
  157. struct device *dev;
  158. struct clk *iclk;
  159. int irq;
  160. unsigned long flags;
  161. spinlock_t lock;
  162. struct crypto_queue queue;
  163. struct tasklet_struct done_task;
  164. struct tasklet_struct queue_task;
  165. size_t total;
  166. size_t datalen;
  167. u32 *data;
  168. struct atmel_aes_dma src;
  169. struct atmel_aes_dma dst;
  170. size_t buflen;
  171. void *buf;
  172. struct scatterlist aligned_sg;
  173. struct scatterlist *real_dst;
  174. struct atmel_aes_caps caps;
  175. u32 hw_version;
  176. };
  177. struct atmel_aes_drv {
  178. struct list_head dev_list;
  179. spinlock_t lock;
  180. };
  181. static struct atmel_aes_drv atmel_aes = {
  182. .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list),
  183. .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock),
  184. };
  185. #ifdef VERBOSE_DEBUG
  186. static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz)
  187. {
  188. switch (offset) {
  189. case AES_CR:
  190. return "CR";
  191. case AES_MR:
  192. return "MR";
  193. case AES_ISR:
  194. return "ISR";
  195. case AES_IMR:
  196. return "IMR";
  197. case AES_IER:
  198. return "IER";
  199. case AES_IDR:
  200. return "IDR";
  201. case AES_KEYWR(0):
  202. case AES_KEYWR(1):
  203. case AES_KEYWR(2):
  204. case AES_KEYWR(3):
  205. case AES_KEYWR(4):
  206. case AES_KEYWR(5):
  207. case AES_KEYWR(6):
  208. case AES_KEYWR(7):
  209. snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2);
  210. break;
  211. case AES_IDATAR(0):
  212. case AES_IDATAR(1):
  213. case AES_IDATAR(2):
  214. case AES_IDATAR(3):
  215. snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2);
  216. break;
  217. case AES_ODATAR(0):
  218. case AES_ODATAR(1):
  219. case AES_ODATAR(2):
  220. case AES_ODATAR(3):
  221. snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2);
  222. break;
  223. case AES_IVR(0):
  224. case AES_IVR(1):
  225. case AES_IVR(2):
  226. case AES_IVR(3):
  227. snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2);
  228. break;
  229. case AES_AADLENR:
  230. return "AADLENR";
  231. case AES_CLENR:
  232. return "CLENR";
  233. case AES_GHASHR(0):
  234. case AES_GHASHR(1):
  235. case AES_GHASHR(2):
  236. case AES_GHASHR(3):
  237. snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2);
  238. break;
  239. case AES_TAGR(0):
  240. case AES_TAGR(1):
  241. case AES_TAGR(2):
  242. case AES_TAGR(3):
  243. snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2);
  244. break;
  245. case AES_CTRR:
  246. return "CTRR";
  247. case AES_GCMHR(0):
  248. case AES_GCMHR(1):
  249. case AES_GCMHR(2):
  250. case AES_GCMHR(3):
  251. snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2);
  252. break;
  253. case AES_EMR:
  254. return "EMR";
  255. case AES_TWR(0):
  256. case AES_TWR(1):
  257. case AES_TWR(2):
  258. case AES_TWR(3):
  259. snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2);
  260. break;
  261. case AES_ALPHAR(0):
  262. case AES_ALPHAR(1):
  263. case AES_ALPHAR(2):
  264. case AES_ALPHAR(3):
  265. snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2);
  266. break;
  267. default:
  268. snprintf(tmp, sz, "0x%02x", offset);
  269. break;
  270. }
  271. return tmp;
  272. }
  273. #endif /* VERBOSE_DEBUG */
  274. /* Shared functions */
  275. static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset)
  276. {
  277. u32 value = readl_relaxed(dd->io_base + offset);
  278. #ifdef VERBOSE_DEBUG
  279. if (dd->flags & AES_FLAGS_DUMP_REG) {
  280. char tmp[16];
  281. dev_vdbg(dd->dev, "read 0x%08x from %s\n", value,
  282. atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
  283. }
  284. #endif /* VERBOSE_DEBUG */
  285. return value;
  286. }
  287. static inline void atmel_aes_write(struct atmel_aes_dev *dd,
  288. u32 offset, u32 value)
  289. {
  290. #ifdef VERBOSE_DEBUG
  291. if (dd->flags & AES_FLAGS_DUMP_REG) {
  292. char tmp[16];
  293. dev_vdbg(dd->dev, "write 0x%08x into %s\n", value,
  294. atmel_aes_reg_name(offset, tmp, sizeof(tmp)));
  295. }
  296. #endif /* VERBOSE_DEBUG */
  297. writel_relaxed(value, dd->io_base + offset);
  298. }
  299. static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset,
  300. u32 *value, int count)
  301. {
  302. for (; count--; value++, offset += 4)
  303. *value = atmel_aes_read(dd, offset);
  304. }
  305. static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset,
  306. const u32 *value, int count)
  307. {
  308. for (; count--; value++, offset += 4)
  309. atmel_aes_write(dd, offset, *value);
  310. }
  311. static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset,
  312. u32 *value)
  313. {
  314. atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
  315. }
  316. static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset,
  317. const u32 *value)
  318. {
  319. atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE));
  320. }
  321. static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd,
  322. atmel_aes_fn_t resume)
  323. {
  324. u32 isr = atmel_aes_read(dd, AES_ISR);
  325. if (unlikely(isr & AES_INT_DATARDY))
  326. return resume(dd);
  327. dd->resume = resume;
  328. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  329. return -EINPROGRESS;
  330. }
  331. static inline size_t atmel_aes_padlen(size_t len, size_t block_size)
  332. {
  333. len &= block_size - 1;
  334. return len ? block_size - len : 0;
  335. }
  336. static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx)
  337. {
  338. struct atmel_aes_dev *aes_dd = NULL;
  339. struct atmel_aes_dev *tmp;
  340. spin_lock_bh(&atmel_aes.lock);
  341. if (!ctx->dd) {
  342. list_for_each_entry(tmp, &atmel_aes.dev_list, list) {
  343. aes_dd = tmp;
  344. break;
  345. }
  346. ctx->dd = aes_dd;
  347. } else {
  348. aes_dd = ctx->dd;
  349. }
  350. spin_unlock_bh(&atmel_aes.lock);
  351. return aes_dd;
  352. }
  353. static int atmel_aes_hw_init(struct atmel_aes_dev *dd)
  354. {
  355. int err;
  356. err = clk_enable(dd->iclk);
  357. if (err)
  358. return err;
  359. atmel_aes_write(dd, AES_CR, AES_CR_SWRST);
  360. atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET);
  361. return 0;
  362. }
  363. static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd)
  364. {
  365. return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff;
  366. }
  367. static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd)
  368. {
  369. int err;
  370. err = atmel_aes_hw_init(dd);
  371. if (err)
  372. return err;
  373. dd->hw_version = atmel_aes_get_version(dd);
  374. dev_info(dd->dev, "version: 0x%x\n", dd->hw_version);
  375. clk_disable(dd->iclk);
  376. return 0;
  377. }
  378. static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd,
  379. const struct atmel_aes_reqctx *rctx)
  380. {
  381. /* Clear all but persistent flags and set request flags. */
  382. dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode;
  383. }
  384. static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd)
  385. {
  386. return (dd->flags & AES_FLAGS_ENCRYPT);
  387. }
  388. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  389. static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err);
  390. #endif
  391. static void atmel_aes_set_iv_as_last_ciphertext_block(struct atmel_aes_dev *dd)
  392. {
  393. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  394. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  395. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  396. unsigned int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  397. if (req->nbytes < ivsize)
  398. return;
  399. if (rctx->mode & AES_FLAGS_ENCRYPT) {
  400. scatterwalk_map_and_copy(req->info, req->dst,
  401. req->nbytes - ivsize, ivsize, 0);
  402. } else {
  403. if (req->src == req->dst)
  404. memcpy(req->info, rctx->lastc, ivsize);
  405. else
  406. scatterwalk_map_and_copy(req->info, req->src,
  407. req->nbytes - ivsize,
  408. ivsize, 0);
  409. }
  410. }
  411. static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err)
  412. {
  413. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  414. if (dd->ctx->is_aead)
  415. atmel_aes_authenc_complete(dd, err);
  416. #endif
  417. clk_disable(dd->iclk);
  418. dd->flags &= ~AES_FLAGS_BUSY;
  419. if (!dd->ctx->is_aead)
  420. atmel_aes_set_iv_as_last_ciphertext_block(dd);
  421. if (dd->is_async)
  422. dd->areq->complete(dd->areq, err);
  423. tasklet_schedule(&dd->queue_task);
  424. return err;
  425. }
  426. static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma,
  427. const u32 *iv, const u32 *key, int keylen)
  428. {
  429. u32 valmr = 0;
  430. /* MR register must be set before IV registers */
  431. if (keylen == AES_KEYSIZE_128)
  432. valmr |= AES_MR_KEYSIZE_128;
  433. else if (keylen == AES_KEYSIZE_192)
  434. valmr |= AES_MR_KEYSIZE_192;
  435. else
  436. valmr |= AES_MR_KEYSIZE_256;
  437. valmr |= dd->flags & AES_FLAGS_MODE_MASK;
  438. if (use_dma) {
  439. valmr |= AES_MR_SMOD_IDATAR0;
  440. if (dd->caps.has_dualbuff)
  441. valmr |= AES_MR_DUALBUFF;
  442. } else {
  443. valmr |= AES_MR_SMOD_AUTO;
  444. }
  445. atmel_aes_write(dd, AES_MR, valmr);
  446. atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen));
  447. if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB)
  448. atmel_aes_write_block(dd, AES_IVR(0), iv);
  449. }
  450. static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma,
  451. const u32 *iv)
  452. {
  453. atmel_aes_write_ctrl_key(dd, use_dma, iv,
  454. dd->ctx->key, dd->ctx->keylen);
  455. }
  456. /* CPU transfer */
  457. static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd)
  458. {
  459. int err = 0;
  460. u32 isr;
  461. for (;;) {
  462. atmel_aes_read_block(dd, AES_ODATAR(0), dd->data);
  463. dd->data += 4;
  464. dd->datalen -= AES_BLOCK_SIZE;
  465. if (dd->datalen < AES_BLOCK_SIZE)
  466. break;
  467. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  468. isr = atmel_aes_read(dd, AES_ISR);
  469. if (!(isr & AES_INT_DATARDY)) {
  470. dd->resume = atmel_aes_cpu_transfer;
  471. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  472. return -EINPROGRESS;
  473. }
  474. }
  475. if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
  476. dd->buf, dd->total))
  477. err = -EINVAL;
  478. if (err)
  479. return atmel_aes_complete(dd, err);
  480. return dd->cpu_transfer_complete(dd);
  481. }
  482. static int atmel_aes_cpu_start(struct atmel_aes_dev *dd,
  483. struct scatterlist *src,
  484. struct scatterlist *dst,
  485. size_t len,
  486. atmel_aes_fn_t resume)
  487. {
  488. size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE);
  489. if (unlikely(len == 0))
  490. return -EINVAL;
  491. sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
  492. dd->total = len;
  493. dd->real_dst = dst;
  494. dd->cpu_transfer_complete = resume;
  495. dd->datalen = len + padlen;
  496. dd->data = (u32 *)dd->buf;
  497. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  498. return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer);
  499. }
  500. /* DMA transfer */
  501. static void atmel_aes_dma_callback(void *data);
  502. static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd,
  503. struct scatterlist *sg,
  504. size_t len,
  505. struct atmel_aes_dma *dma)
  506. {
  507. int nents;
  508. if (!IS_ALIGNED(len, dd->ctx->block_size))
  509. return false;
  510. for (nents = 0; sg; sg = sg_next(sg), ++nents) {
  511. if (!IS_ALIGNED(sg->offset, sizeof(u32)))
  512. return false;
  513. if (len <= sg->length) {
  514. if (!IS_ALIGNED(len, dd->ctx->block_size))
  515. return false;
  516. dma->nents = nents+1;
  517. dma->remainder = sg->length - len;
  518. sg->length = len;
  519. return true;
  520. }
  521. if (!IS_ALIGNED(sg->length, dd->ctx->block_size))
  522. return false;
  523. len -= sg->length;
  524. }
  525. return false;
  526. }
  527. static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma)
  528. {
  529. struct scatterlist *sg = dma->sg;
  530. int nents = dma->nents;
  531. if (!dma->remainder)
  532. return;
  533. while (--nents > 0 && sg)
  534. sg = sg_next(sg);
  535. if (!sg)
  536. return;
  537. sg->length += dma->remainder;
  538. }
  539. static int atmel_aes_map(struct atmel_aes_dev *dd,
  540. struct scatterlist *src,
  541. struct scatterlist *dst,
  542. size_t len)
  543. {
  544. bool src_aligned, dst_aligned;
  545. size_t padlen;
  546. dd->total = len;
  547. dd->src.sg = src;
  548. dd->dst.sg = dst;
  549. dd->real_dst = dst;
  550. src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src);
  551. if (src == dst)
  552. dst_aligned = src_aligned;
  553. else
  554. dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst);
  555. if (!src_aligned || !dst_aligned) {
  556. padlen = atmel_aes_padlen(len, dd->ctx->block_size);
  557. if (dd->buflen < len + padlen)
  558. return -ENOMEM;
  559. if (!src_aligned) {
  560. sg_copy_to_buffer(src, sg_nents(src), dd->buf, len);
  561. dd->src.sg = &dd->aligned_sg;
  562. dd->src.nents = 1;
  563. dd->src.remainder = 0;
  564. }
  565. if (!dst_aligned) {
  566. dd->dst.sg = &dd->aligned_sg;
  567. dd->dst.nents = 1;
  568. dd->dst.remainder = 0;
  569. }
  570. sg_init_table(&dd->aligned_sg, 1);
  571. sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen);
  572. }
  573. if (dd->src.sg == dd->dst.sg) {
  574. dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
  575. DMA_BIDIRECTIONAL);
  576. dd->dst.sg_len = dd->src.sg_len;
  577. if (!dd->src.sg_len)
  578. return -EFAULT;
  579. } else {
  580. dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents,
  581. DMA_TO_DEVICE);
  582. if (!dd->src.sg_len)
  583. return -EFAULT;
  584. dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents,
  585. DMA_FROM_DEVICE);
  586. if (!dd->dst.sg_len) {
  587. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  588. DMA_TO_DEVICE);
  589. return -EFAULT;
  590. }
  591. }
  592. return 0;
  593. }
  594. static void atmel_aes_unmap(struct atmel_aes_dev *dd)
  595. {
  596. if (dd->src.sg == dd->dst.sg) {
  597. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  598. DMA_BIDIRECTIONAL);
  599. if (dd->src.sg != &dd->aligned_sg)
  600. atmel_aes_restore_sg(&dd->src);
  601. } else {
  602. dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents,
  603. DMA_FROM_DEVICE);
  604. if (dd->dst.sg != &dd->aligned_sg)
  605. atmel_aes_restore_sg(&dd->dst);
  606. dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents,
  607. DMA_TO_DEVICE);
  608. if (dd->src.sg != &dd->aligned_sg)
  609. atmel_aes_restore_sg(&dd->src);
  610. }
  611. if (dd->dst.sg == &dd->aligned_sg)
  612. sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst),
  613. dd->buf, dd->total);
  614. }
  615. static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd,
  616. enum dma_slave_buswidth addr_width,
  617. enum dma_transfer_direction dir,
  618. u32 maxburst)
  619. {
  620. struct dma_async_tx_descriptor *desc;
  621. struct dma_slave_config config;
  622. dma_async_tx_callback callback;
  623. struct atmel_aes_dma *dma;
  624. int err;
  625. memset(&config, 0, sizeof(config));
  626. config.direction = dir;
  627. config.src_addr_width = addr_width;
  628. config.dst_addr_width = addr_width;
  629. config.src_maxburst = maxburst;
  630. config.dst_maxburst = maxburst;
  631. switch (dir) {
  632. case DMA_MEM_TO_DEV:
  633. dma = &dd->src;
  634. callback = NULL;
  635. config.dst_addr = dd->phys_base + AES_IDATAR(0);
  636. break;
  637. case DMA_DEV_TO_MEM:
  638. dma = &dd->dst;
  639. callback = atmel_aes_dma_callback;
  640. config.src_addr = dd->phys_base + AES_ODATAR(0);
  641. break;
  642. default:
  643. return -EINVAL;
  644. }
  645. err = dmaengine_slave_config(dma->chan, &config);
  646. if (err)
  647. return err;
  648. desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir,
  649. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  650. if (!desc)
  651. return -ENOMEM;
  652. desc->callback = callback;
  653. desc->callback_param = dd;
  654. dmaengine_submit(desc);
  655. dma_async_issue_pending(dma->chan);
  656. return 0;
  657. }
  658. static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd,
  659. enum dma_transfer_direction dir)
  660. {
  661. struct atmel_aes_dma *dma;
  662. switch (dir) {
  663. case DMA_MEM_TO_DEV:
  664. dma = &dd->src;
  665. break;
  666. case DMA_DEV_TO_MEM:
  667. dma = &dd->dst;
  668. break;
  669. default:
  670. return;
  671. }
  672. dmaengine_terminate_all(dma->chan);
  673. }
  674. static int atmel_aes_dma_start(struct atmel_aes_dev *dd,
  675. struct scatterlist *src,
  676. struct scatterlist *dst,
  677. size_t len,
  678. atmel_aes_fn_t resume)
  679. {
  680. enum dma_slave_buswidth addr_width;
  681. u32 maxburst;
  682. int err;
  683. switch (dd->ctx->block_size) {
  684. case CFB8_BLOCK_SIZE:
  685. addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  686. maxburst = 1;
  687. break;
  688. case CFB16_BLOCK_SIZE:
  689. addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  690. maxburst = 1;
  691. break;
  692. case CFB32_BLOCK_SIZE:
  693. case CFB64_BLOCK_SIZE:
  694. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  695. maxburst = 1;
  696. break;
  697. case AES_BLOCK_SIZE:
  698. addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  699. maxburst = dd->caps.max_burst_size;
  700. break;
  701. default:
  702. err = -EINVAL;
  703. goto exit;
  704. }
  705. err = atmel_aes_map(dd, src, dst, len);
  706. if (err)
  707. goto exit;
  708. dd->resume = resume;
  709. /* Set output DMA transfer first */
  710. err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM,
  711. maxburst);
  712. if (err)
  713. goto unmap;
  714. /* Then set input DMA transfer */
  715. err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV,
  716. maxburst);
  717. if (err)
  718. goto output_transfer_stop;
  719. return -EINPROGRESS;
  720. output_transfer_stop:
  721. atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
  722. unmap:
  723. atmel_aes_unmap(dd);
  724. exit:
  725. return atmel_aes_complete(dd, err);
  726. }
  727. static void atmel_aes_dma_stop(struct atmel_aes_dev *dd)
  728. {
  729. atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV);
  730. atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM);
  731. atmel_aes_unmap(dd);
  732. }
  733. static void atmel_aes_dma_callback(void *data)
  734. {
  735. struct atmel_aes_dev *dd = data;
  736. atmel_aes_dma_stop(dd);
  737. dd->is_async = true;
  738. (void)dd->resume(dd);
  739. }
  740. static int atmel_aes_handle_queue(struct atmel_aes_dev *dd,
  741. struct crypto_async_request *new_areq)
  742. {
  743. struct crypto_async_request *areq, *backlog;
  744. struct atmel_aes_base_ctx *ctx;
  745. unsigned long flags;
  746. bool start_async;
  747. int err, ret = 0;
  748. spin_lock_irqsave(&dd->lock, flags);
  749. if (new_areq)
  750. ret = crypto_enqueue_request(&dd->queue, new_areq);
  751. if (dd->flags & AES_FLAGS_BUSY) {
  752. spin_unlock_irqrestore(&dd->lock, flags);
  753. return ret;
  754. }
  755. backlog = crypto_get_backlog(&dd->queue);
  756. areq = crypto_dequeue_request(&dd->queue);
  757. if (areq)
  758. dd->flags |= AES_FLAGS_BUSY;
  759. spin_unlock_irqrestore(&dd->lock, flags);
  760. if (!areq)
  761. return ret;
  762. if (backlog)
  763. backlog->complete(backlog, -EINPROGRESS);
  764. ctx = crypto_tfm_ctx(areq->tfm);
  765. dd->areq = areq;
  766. dd->ctx = ctx;
  767. start_async = (areq != new_areq);
  768. dd->is_async = start_async;
  769. /* WARNING: ctx->start() MAY change dd->is_async. */
  770. err = ctx->start(dd);
  771. return (start_async) ? ret : err;
  772. }
  773. /* AES async block ciphers */
  774. static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd)
  775. {
  776. return atmel_aes_complete(dd, 0);
  777. }
  778. static int atmel_aes_start(struct atmel_aes_dev *dd)
  779. {
  780. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  781. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  782. bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD ||
  783. dd->ctx->block_size != AES_BLOCK_SIZE);
  784. int err;
  785. atmel_aes_set_mode(dd, rctx);
  786. err = atmel_aes_hw_init(dd);
  787. if (err)
  788. return atmel_aes_complete(dd, err);
  789. atmel_aes_write_ctrl(dd, use_dma, req->info);
  790. if (use_dma)
  791. return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
  792. atmel_aes_transfer_complete);
  793. return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
  794. atmel_aes_transfer_complete);
  795. }
  796. static inline struct atmel_aes_ctr_ctx *
  797. atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx)
  798. {
  799. return container_of(ctx, struct atmel_aes_ctr_ctx, base);
  800. }
  801. static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
  802. {
  803. struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
  804. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  805. struct scatterlist *src, *dst;
  806. size_t datalen;
  807. u32 ctr;
  808. u16 blocks, start, end;
  809. bool use_dma, fragmented = false;
  810. /* Check for transfer completion. */
  811. ctx->offset += dd->total;
  812. if (ctx->offset >= req->nbytes)
  813. return atmel_aes_transfer_complete(dd);
  814. /* Compute data length. */
  815. datalen = req->nbytes - ctx->offset;
  816. blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
  817. ctr = be32_to_cpu(ctx->iv[3]);
  818. /* Check 16bit counter overflow. */
  819. start = ctr & 0xffff;
  820. end = start + blocks - 1;
  821. if (blocks >> 16 || end < start) {
  822. ctr |= 0xffff;
  823. datalen = AES_BLOCK_SIZE * (0x10000 - start);
  824. fragmented = true;
  825. }
  826. use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
  827. /* Jump to offset. */
  828. src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset);
  829. dst = ((req->src == req->dst) ? src :
  830. scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset));
  831. /* Configure hardware. */
  832. atmel_aes_write_ctrl(dd, use_dma, ctx->iv);
  833. if (unlikely(fragmented)) {
  834. /*
  835. * Increment the counter manually to cope with the hardware
  836. * counter overflow.
  837. */
  838. ctx->iv[3] = cpu_to_be32(ctr);
  839. crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE);
  840. }
  841. if (use_dma)
  842. return atmel_aes_dma_start(dd, src, dst, datalen,
  843. atmel_aes_ctr_transfer);
  844. return atmel_aes_cpu_start(dd, src, dst, datalen,
  845. atmel_aes_ctr_transfer);
  846. }
  847. static int atmel_aes_ctr_start(struct atmel_aes_dev *dd)
  848. {
  849. struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
  850. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  851. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  852. int err;
  853. atmel_aes_set_mode(dd, rctx);
  854. err = atmel_aes_hw_init(dd);
  855. if (err)
  856. return atmel_aes_complete(dd, err);
  857. memcpy(ctx->iv, req->info, AES_BLOCK_SIZE);
  858. ctx->offset = 0;
  859. dd->total = 0;
  860. return atmel_aes_ctr_transfer(dd);
  861. }
  862. static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
  863. {
  864. struct crypto_ablkcipher *ablkcipher = crypto_ablkcipher_reqtfm(req);
  865. struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher);
  866. struct atmel_aes_reqctx *rctx;
  867. struct atmel_aes_dev *dd;
  868. switch (mode & AES_FLAGS_OPMODE_MASK) {
  869. case AES_FLAGS_CFB8:
  870. ctx->block_size = CFB8_BLOCK_SIZE;
  871. break;
  872. case AES_FLAGS_CFB16:
  873. ctx->block_size = CFB16_BLOCK_SIZE;
  874. break;
  875. case AES_FLAGS_CFB32:
  876. ctx->block_size = CFB32_BLOCK_SIZE;
  877. break;
  878. case AES_FLAGS_CFB64:
  879. ctx->block_size = CFB64_BLOCK_SIZE;
  880. break;
  881. default:
  882. ctx->block_size = AES_BLOCK_SIZE;
  883. break;
  884. }
  885. ctx->is_aead = false;
  886. dd = atmel_aes_find_dev(ctx);
  887. if (!dd)
  888. return -ENODEV;
  889. rctx = ablkcipher_request_ctx(req);
  890. rctx->mode = mode;
  891. if (!(mode & AES_FLAGS_ENCRYPT) && (req->src == req->dst)) {
  892. unsigned int ivsize = crypto_ablkcipher_ivsize(ablkcipher);
  893. if (req->nbytes >= ivsize)
  894. scatterwalk_map_and_copy(rctx->lastc, req->src,
  895. req->nbytes - ivsize,
  896. ivsize, 0);
  897. }
  898. return atmel_aes_handle_queue(dd, &req->base);
  899. }
  900. static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  901. unsigned int keylen)
  902. {
  903. struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  904. if (keylen != AES_KEYSIZE_128 &&
  905. keylen != AES_KEYSIZE_192 &&
  906. keylen != AES_KEYSIZE_256) {
  907. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  908. return -EINVAL;
  909. }
  910. memcpy(ctx->key, key, keylen);
  911. ctx->keylen = keylen;
  912. return 0;
  913. }
  914. static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req)
  915. {
  916. return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
  917. }
  918. static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req)
  919. {
  920. return atmel_aes_crypt(req, AES_FLAGS_ECB);
  921. }
  922. static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req)
  923. {
  924. return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
  925. }
  926. static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req)
  927. {
  928. return atmel_aes_crypt(req, AES_FLAGS_CBC);
  929. }
  930. static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req)
  931. {
  932. return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT);
  933. }
  934. static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req)
  935. {
  936. return atmel_aes_crypt(req, AES_FLAGS_OFB);
  937. }
  938. static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req)
  939. {
  940. return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT);
  941. }
  942. static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req)
  943. {
  944. return atmel_aes_crypt(req, AES_FLAGS_CFB128);
  945. }
  946. static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req)
  947. {
  948. return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT);
  949. }
  950. static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req)
  951. {
  952. return atmel_aes_crypt(req, AES_FLAGS_CFB64);
  953. }
  954. static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req)
  955. {
  956. return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT);
  957. }
  958. static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req)
  959. {
  960. return atmel_aes_crypt(req, AES_FLAGS_CFB32);
  961. }
  962. static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req)
  963. {
  964. return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT);
  965. }
  966. static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req)
  967. {
  968. return atmel_aes_crypt(req, AES_FLAGS_CFB16);
  969. }
  970. static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req)
  971. {
  972. return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT);
  973. }
  974. static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req)
  975. {
  976. return atmel_aes_crypt(req, AES_FLAGS_CFB8);
  977. }
  978. static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req)
  979. {
  980. return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT);
  981. }
  982. static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req)
  983. {
  984. return atmel_aes_crypt(req, AES_FLAGS_CTR);
  985. }
  986. static int atmel_aes_cra_init(struct crypto_tfm *tfm)
  987. {
  988. struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  989. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  990. ctx->base.start = atmel_aes_start;
  991. return 0;
  992. }
  993. static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm)
  994. {
  995. struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm);
  996. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  997. ctx->base.start = atmel_aes_ctr_start;
  998. return 0;
  999. }
  1000. static struct crypto_alg aes_algs[] = {
  1001. {
  1002. .cra_name = "ecb(aes)",
  1003. .cra_driver_name = "atmel-ecb-aes",
  1004. .cra_priority = ATMEL_AES_PRIORITY,
  1005. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1006. .cra_blocksize = AES_BLOCK_SIZE,
  1007. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1008. .cra_alignmask = 0xf,
  1009. .cra_type = &crypto_ablkcipher_type,
  1010. .cra_module = THIS_MODULE,
  1011. .cra_init = atmel_aes_cra_init,
  1012. .cra_u.ablkcipher = {
  1013. .min_keysize = AES_MIN_KEY_SIZE,
  1014. .max_keysize = AES_MAX_KEY_SIZE,
  1015. .setkey = atmel_aes_setkey,
  1016. .encrypt = atmel_aes_ecb_encrypt,
  1017. .decrypt = atmel_aes_ecb_decrypt,
  1018. }
  1019. },
  1020. {
  1021. .cra_name = "cbc(aes)",
  1022. .cra_driver_name = "atmel-cbc-aes",
  1023. .cra_priority = ATMEL_AES_PRIORITY,
  1024. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1025. .cra_blocksize = AES_BLOCK_SIZE,
  1026. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1027. .cra_alignmask = 0xf,
  1028. .cra_type = &crypto_ablkcipher_type,
  1029. .cra_module = THIS_MODULE,
  1030. .cra_init = atmel_aes_cra_init,
  1031. .cra_u.ablkcipher = {
  1032. .min_keysize = AES_MIN_KEY_SIZE,
  1033. .max_keysize = AES_MAX_KEY_SIZE,
  1034. .ivsize = AES_BLOCK_SIZE,
  1035. .setkey = atmel_aes_setkey,
  1036. .encrypt = atmel_aes_cbc_encrypt,
  1037. .decrypt = atmel_aes_cbc_decrypt,
  1038. }
  1039. },
  1040. {
  1041. .cra_name = "ofb(aes)",
  1042. .cra_driver_name = "atmel-ofb-aes",
  1043. .cra_priority = ATMEL_AES_PRIORITY,
  1044. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1045. .cra_blocksize = AES_BLOCK_SIZE,
  1046. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1047. .cra_alignmask = 0xf,
  1048. .cra_type = &crypto_ablkcipher_type,
  1049. .cra_module = THIS_MODULE,
  1050. .cra_init = atmel_aes_cra_init,
  1051. .cra_u.ablkcipher = {
  1052. .min_keysize = AES_MIN_KEY_SIZE,
  1053. .max_keysize = AES_MAX_KEY_SIZE,
  1054. .ivsize = AES_BLOCK_SIZE,
  1055. .setkey = atmel_aes_setkey,
  1056. .encrypt = atmel_aes_ofb_encrypt,
  1057. .decrypt = atmel_aes_ofb_decrypt,
  1058. }
  1059. },
  1060. {
  1061. .cra_name = "cfb(aes)",
  1062. .cra_driver_name = "atmel-cfb-aes",
  1063. .cra_priority = ATMEL_AES_PRIORITY,
  1064. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1065. .cra_blocksize = AES_BLOCK_SIZE,
  1066. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1067. .cra_alignmask = 0xf,
  1068. .cra_type = &crypto_ablkcipher_type,
  1069. .cra_module = THIS_MODULE,
  1070. .cra_init = atmel_aes_cra_init,
  1071. .cra_u.ablkcipher = {
  1072. .min_keysize = AES_MIN_KEY_SIZE,
  1073. .max_keysize = AES_MAX_KEY_SIZE,
  1074. .ivsize = AES_BLOCK_SIZE,
  1075. .setkey = atmel_aes_setkey,
  1076. .encrypt = atmel_aes_cfb_encrypt,
  1077. .decrypt = atmel_aes_cfb_decrypt,
  1078. }
  1079. },
  1080. {
  1081. .cra_name = "cfb32(aes)",
  1082. .cra_driver_name = "atmel-cfb32-aes",
  1083. .cra_priority = ATMEL_AES_PRIORITY,
  1084. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1085. .cra_blocksize = CFB32_BLOCK_SIZE,
  1086. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1087. .cra_alignmask = 0x3,
  1088. .cra_type = &crypto_ablkcipher_type,
  1089. .cra_module = THIS_MODULE,
  1090. .cra_init = atmel_aes_cra_init,
  1091. .cra_u.ablkcipher = {
  1092. .min_keysize = AES_MIN_KEY_SIZE,
  1093. .max_keysize = AES_MAX_KEY_SIZE,
  1094. .ivsize = AES_BLOCK_SIZE,
  1095. .setkey = atmel_aes_setkey,
  1096. .encrypt = atmel_aes_cfb32_encrypt,
  1097. .decrypt = atmel_aes_cfb32_decrypt,
  1098. }
  1099. },
  1100. {
  1101. .cra_name = "cfb16(aes)",
  1102. .cra_driver_name = "atmel-cfb16-aes",
  1103. .cra_priority = ATMEL_AES_PRIORITY,
  1104. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1105. .cra_blocksize = CFB16_BLOCK_SIZE,
  1106. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1107. .cra_alignmask = 0x1,
  1108. .cra_type = &crypto_ablkcipher_type,
  1109. .cra_module = THIS_MODULE,
  1110. .cra_init = atmel_aes_cra_init,
  1111. .cra_u.ablkcipher = {
  1112. .min_keysize = AES_MIN_KEY_SIZE,
  1113. .max_keysize = AES_MAX_KEY_SIZE,
  1114. .ivsize = AES_BLOCK_SIZE,
  1115. .setkey = atmel_aes_setkey,
  1116. .encrypt = atmel_aes_cfb16_encrypt,
  1117. .decrypt = atmel_aes_cfb16_decrypt,
  1118. }
  1119. },
  1120. {
  1121. .cra_name = "cfb8(aes)",
  1122. .cra_driver_name = "atmel-cfb8-aes",
  1123. .cra_priority = ATMEL_AES_PRIORITY,
  1124. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1125. .cra_blocksize = CFB8_BLOCK_SIZE,
  1126. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1127. .cra_alignmask = 0x0,
  1128. .cra_type = &crypto_ablkcipher_type,
  1129. .cra_module = THIS_MODULE,
  1130. .cra_init = atmel_aes_cra_init,
  1131. .cra_u.ablkcipher = {
  1132. .min_keysize = AES_MIN_KEY_SIZE,
  1133. .max_keysize = AES_MAX_KEY_SIZE,
  1134. .ivsize = AES_BLOCK_SIZE,
  1135. .setkey = atmel_aes_setkey,
  1136. .encrypt = atmel_aes_cfb8_encrypt,
  1137. .decrypt = atmel_aes_cfb8_decrypt,
  1138. }
  1139. },
  1140. {
  1141. .cra_name = "ctr(aes)",
  1142. .cra_driver_name = "atmel-ctr-aes",
  1143. .cra_priority = ATMEL_AES_PRIORITY,
  1144. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1145. .cra_blocksize = 1,
  1146. .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx),
  1147. .cra_alignmask = 0xf,
  1148. .cra_type = &crypto_ablkcipher_type,
  1149. .cra_module = THIS_MODULE,
  1150. .cra_init = atmel_aes_ctr_cra_init,
  1151. .cra_u.ablkcipher = {
  1152. .min_keysize = AES_MIN_KEY_SIZE,
  1153. .max_keysize = AES_MAX_KEY_SIZE,
  1154. .ivsize = AES_BLOCK_SIZE,
  1155. .setkey = atmel_aes_setkey,
  1156. .encrypt = atmel_aes_ctr_encrypt,
  1157. .decrypt = atmel_aes_ctr_decrypt,
  1158. }
  1159. },
  1160. };
  1161. static struct crypto_alg aes_cfb64_alg = {
  1162. .cra_name = "cfb64(aes)",
  1163. .cra_driver_name = "atmel-cfb64-aes",
  1164. .cra_priority = ATMEL_AES_PRIORITY,
  1165. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1166. .cra_blocksize = CFB64_BLOCK_SIZE,
  1167. .cra_ctxsize = sizeof(struct atmel_aes_ctx),
  1168. .cra_alignmask = 0x7,
  1169. .cra_type = &crypto_ablkcipher_type,
  1170. .cra_module = THIS_MODULE,
  1171. .cra_init = atmel_aes_cra_init,
  1172. .cra_u.ablkcipher = {
  1173. .min_keysize = AES_MIN_KEY_SIZE,
  1174. .max_keysize = AES_MAX_KEY_SIZE,
  1175. .ivsize = AES_BLOCK_SIZE,
  1176. .setkey = atmel_aes_setkey,
  1177. .encrypt = atmel_aes_cfb64_encrypt,
  1178. .decrypt = atmel_aes_cfb64_decrypt,
  1179. }
  1180. };
  1181. /* gcm aead functions */
  1182. static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
  1183. const u32 *data, size_t datalen,
  1184. const u32 *ghash_in, u32 *ghash_out,
  1185. atmel_aes_fn_t resume);
  1186. static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd);
  1187. static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd);
  1188. static int atmel_aes_gcm_start(struct atmel_aes_dev *dd);
  1189. static int atmel_aes_gcm_process(struct atmel_aes_dev *dd);
  1190. static int atmel_aes_gcm_length(struct atmel_aes_dev *dd);
  1191. static int atmel_aes_gcm_data(struct atmel_aes_dev *dd);
  1192. static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd);
  1193. static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd);
  1194. static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd);
  1195. static inline struct atmel_aes_gcm_ctx *
  1196. atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx)
  1197. {
  1198. return container_of(ctx, struct atmel_aes_gcm_ctx, base);
  1199. }
  1200. static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd,
  1201. const u32 *data, size_t datalen,
  1202. const u32 *ghash_in, u32 *ghash_out,
  1203. atmel_aes_fn_t resume)
  1204. {
  1205. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1206. dd->data = (u32 *)data;
  1207. dd->datalen = datalen;
  1208. ctx->ghash_in = ghash_in;
  1209. ctx->ghash_out = ghash_out;
  1210. ctx->ghash_resume = resume;
  1211. atmel_aes_write_ctrl(dd, false, NULL);
  1212. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init);
  1213. }
  1214. static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd)
  1215. {
  1216. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1217. /* Set the data length. */
  1218. atmel_aes_write(dd, AES_AADLENR, dd->total);
  1219. atmel_aes_write(dd, AES_CLENR, 0);
  1220. /* If needed, overwrite the GCM Intermediate Hash Word Registers */
  1221. if (ctx->ghash_in)
  1222. atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in);
  1223. return atmel_aes_gcm_ghash_finalize(dd);
  1224. }
  1225. static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd)
  1226. {
  1227. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1228. u32 isr;
  1229. /* Write data into the Input Data Registers. */
  1230. while (dd->datalen > 0) {
  1231. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  1232. dd->data += 4;
  1233. dd->datalen -= AES_BLOCK_SIZE;
  1234. isr = atmel_aes_read(dd, AES_ISR);
  1235. if (!(isr & AES_INT_DATARDY)) {
  1236. dd->resume = atmel_aes_gcm_ghash_finalize;
  1237. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  1238. return -EINPROGRESS;
  1239. }
  1240. }
  1241. /* Read the computed hash from GHASHRx. */
  1242. atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out);
  1243. return ctx->ghash_resume(dd);
  1244. }
  1245. static int atmel_aes_gcm_start(struct atmel_aes_dev *dd)
  1246. {
  1247. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1248. struct aead_request *req = aead_request_cast(dd->areq);
  1249. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1250. struct atmel_aes_reqctx *rctx = aead_request_ctx(req);
  1251. size_t ivsize = crypto_aead_ivsize(tfm);
  1252. size_t datalen, padlen;
  1253. const void *iv = req->iv;
  1254. u8 *data = dd->buf;
  1255. int err;
  1256. atmel_aes_set_mode(dd, rctx);
  1257. err = atmel_aes_hw_init(dd);
  1258. if (err)
  1259. return atmel_aes_complete(dd, err);
  1260. if (likely(ivsize == GCM_AES_IV_SIZE)) {
  1261. memcpy(ctx->j0, iv, ivsize);
  1262. ctx->j0[3] = cpu_to_be32(1);
  1263. return atmel_aes_gcm_process(dd);
  1264. }
  1265. padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE);
  1266. datalen = ivsize + padlen + AES_BLOCK_SIZE;
  1267. if (datalen > dd->buflen)
  1268. return atmel_aes_complete(dd, -EINVAL);
  1269. memcpy(data, iv, ivsize);
  1270. memset(data + ivsize, 0, padlen + sizeof(u64));
  1271. ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8);
  1272. return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen,
  1273. NULL, ctx->j0, atmel_aes_gcm_process);
  1274. }
  1275. static int atmel_aes_gcm_process(struct atmel_aes_dev *dd)
  1276. {
  1277. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1278. struct aead_request *req = aead_request_cast(dd->areq);
  1279. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1280. bool enc = atmel_aes_is_encrypt(dd);
  1281. u32 authsize;
  1282. /* Compute text length. */
  1283. authsize = crypto_aead_authsize(tfm);
  1284. ctx->textlen = req->cryptlen - (enc ? 0 : authsize);
  1285. /*
  1286. * According to tcrypt test suite, the GCM Automatic Tag Generation
  1287. * fails when both the message and its associated data are empty.
  1288. */
  1289. if (likely(req->assoclen != 0 || ctx->textlen != 0))
  1290. dd->flags |= AES_FLAGS_GTAGEN;
  1291. atmel_aes_write_ctrl(dd, false, NULL);
  1292. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length);
  1293. }
  1294. static int atmel_aes_gcm_length(struct atmel_aes_dev *dd)
  1295. {
  1296. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1297. struct aead_request *req = aead_request_cast(dd->areq);
  1298. u32 j0_lsw, *j0 = ctx->j0;
  1299. size_t padlen;
  1300. /* Write incr32(J0) into IV. */
  1301. j0_lsw = j0[3];
  1302. j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1);
  1303. atmel_aes_write_block(dd, AES_IVR(0), j0);
  1304. j0[3] = j0_lsw;
  1305. /* Set aad and text lengths. */
  1306. atmel_aes_write(dd, AES_AADLENR, req->assoclen);
  1307. atmel_aes_write(dd, AES_CLENR, ctx->textlen);
  1308. /* Check whether AAD are present. */
  1309. if (unlikely(req->assoclen == 0)) {
  1310. dd->datalen = 0;
  1311. return atmel_aes_gcm_data(dd);
  1312. }
  1313. /* Copy assoc data and add padding. */
  1314. padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE);
  1315. if (unlikely(req->assoclen + padlen > dd->buflen))
  1316. return atmel_aes_complete(dd, -EINVAL);
  1317. sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen);
  1318. /* Write assoc data into the Input Data register. */
  1319. dd->data = (u32 *)dd->buf;
  1320. dd->datalen = req->assoclen + padlen;
  1321. return atmel_aes_gcm_data(dd);
  1322. }
  1323. static int atmel_aes_gcm_data(struct atmel_aes_dev *dd)
  1324. {
  1325. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1326. struct aead_request *req = aead_request_cast(dd->areq);
  1327. bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD);
  1328. struct scatterlist *src, *dst;
  1329. u32 isr, mr;
  1330. /* Write AAD first. */
  1331. while (dd->datalen > 0) {
  1332. atmel_aes_write_block(dd, AES_IDATAR(0), dd->data);
  1333. dd->data += 4;
  1334. dd->datalen -= AES_BLOCK_SIZE;
  1335. isr = atmel_aes_read(dd, AES_ISR);
  1336. if (!(isr & AES_INT_DATARDY)) {
  1337. dd->resume = atmel_aes_gcm_data;
  1338. atmel_aes_write(dd, AES_IER, AES_INT_DATARDY);
  1339. return -EINPROGRESS;
  1340. }
  1341. }
  1342. /* GMAC only. */
  1343. if (unlikely(ctx->textlen == 0))
  1344. return atmel_aes_gcm_tag_init(dd);
  1345. /* Prepare src and dst scatter lists to transfer cipher/plain texts */
  1346. src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen);
  1347. dst = ((req->src == req->dst) ? src :
  1348. scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen));
  1349. if (use_dma) {
  1350. /* Update the Mode Register for DMA transfers. */
  1351. mr = atmel_aes_read(dd, AES_MR);
  1352. mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF);
  1353. mr |= AES_MR_SMOD_IDATAR0;
  1354. if (dd->caps.has_dualbuff)
  1355. mr |= AES_MR_DUALBUFF;
  1356. atmel_aes_write(dd, AES_MR, mr);
  1357. return atmel_aes_dma_start(dd, src, dst, ctx->textlen,
  1358. atmel_aes_gcm_tag_init);
  1359. }
  1360. return atmel_aes_cpu_start(dd, src, dst, ctx->textlen,
  1361. atmel_aes_gcm_tag_init);
  1362. }
  1363. static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd)
  1364. {
  1365. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1366. struct aead_request *req = aead_request_cast(dd->areq);
  1367. u64 *data = dd->buf;
  1368. if (likely(dd->flags & AES_FLAGS_GTAGEN)) {
  1369. if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) {
  1370. dd->resume = atmel_aes_gcm_tag_init;
  1371. atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY);
  1372. return -EINPROGRESS;
  1373. }
  1374. return atmel_aes_gcm_finalize(dd);
  1375. }
  1376. /* Read the GCM Intermediate Hash Word Registers. */
  1377. atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash);
  1378. data[0] = cpu_to_be64(req->assoclen * 8);
  1379. data[1] = cpu_to_be64(ctx->textlen * 8);
  1380. return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE,
  1381. ctx->ghash, ctx->ghash, atmel_aes_gcm_tag);
  1382. }
  1383. static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd)
  1384. {
  1385. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1386. unsigned long flags;
  1387. /*
  1388. * Change mode to CTR to complete the tag generation.
  1389. * Use J0 as Initialization Vector.
  1390. */
  1391. flags = dd->flags;
  1392. dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN);
  1393. dd->flags |= AES_FLAGS_CTR;
  1394. atmel_aes_write_ctrl(dd, false, ctx->j0);
  1395. dd->flags = flags;
  1396. atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash);
  1397. return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize);
  1398. }
  1399. static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd)
  1400. {
  1401. struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx);
  1402. struct aead_request *req = aead_request_cast(dd->areq);
  1403. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1404. bool enc = atmel_aes_is_encrypt(dd);
  1405. u32 offset, authsize, itag[4], *otag = ctx->tag;
  1406. int err;
  1407. /* Read the computed tag. */
  1408. if (likely(dd->flags & AES_FLAGS_GTAGEN))
  1409. atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag);
  1410. else
  1411. atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag);
  1412. offset = req->assoclen + ctx->textlen;
  1413. authsize = crypto_aead_authsize(tfm);
  1414. if (enc) {
  1415. scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1);
  1416. err = 0;
  1417. } else {
  1418. scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0);
  1419. err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0;
  1420. }
  1421. return atmel_aes_complete(dd, err);
  1422. }
  1423. static int atmel_aes_gcm_crypt(struct aead_request *req,
  1424. unsigned long mode)
  1425. {
  1426. struct atmel_aes_base_ctx *ctx;
  1427. struct atmel_aes_reqctx *rctx;
  1428. struct atmel_aes_dev *dd;
  1429. ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  1430. ctx->block_size = AES_BLOCK_SIZE;
  1431. ctx->is_aead = true;
  1432. dd = atmel_aes_find_dev(ctx);
  1433. if (!dd)
  1434. return -ENODEV;
  1435. rctx = aead_request_ctx(req);
  1436. rctx->mode = AES_FLAGS_GCM | mode;
  1437. return atmel_aes_handle_queue(dd, &req->base);
  1438. }
  1439. static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  1440. unsigned int keylen)
  1441. {
  1442. struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
  1443. if (keylen != AES_KEYSIZE_256 &&
  1444. keylen != AES_KEYSIZE_192 &&
  1445. keylen != AES_KEYSIZE_128) {
  1446. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1447. return -EINVAL;
  1448. }
  1449. memcpy(ctx->key, key, keylen);
  1450. ctx->keylen = keylen;
  1451. return 0;
  1452. }
  1453. static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm,
  1454. unsigned int authsize)
  1455. {
  1456. /* Same as crypto_gcm_authsize() from crypto/gcm.c */
  1457. switch (authsize) {
  1458. case 4:
  1459. case 8:
  1460. case 12:
  1461. case 13:
  1462. case 14:
  1463. case 15:
  1464. case 16:
  1465. break;
  1466. default:
  1467. return -EINVAL;
  1468. }
  1469. return 0;
  1470. }
  1471. static int atmel_aes_gcm_encrypt(struct aead_request *req)
  1472. {
  1473. return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT);
  1474. }
  1475. static int atmel_aes_gcm_decrypt(struct aead_request *req)
  1476. {
  1477. return atmel_aes_gcm_crypt(req, 0);
  1478. }
  1479. static int atmel_aes_gcm_init(struct crypto_aead *tfm)
  1480. {
  1481. struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
  1482. crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx));
  1483. ctx->base.start = atmel_aes_gcm_start;
  1484. return 0;
  1485. }
  1486. static struct aead_alg aes_gcm_alg = {
  1487. .setkey = atmel_aes_gcm_setkey,
  1488. .setauthsize = atmel_aes_gcm_setauthsize,
  1489. .encrypt = atmel_aes_gcm_encrypt,
  1490. .decrypt = atmel_aes_gcm_decrypt,
  1491. .init = atmel_aes_gcm_init,
  1492. .ivsize = GCM_AES_IV_SIZE,
  1493. .maxauthsize = AES_BLOCK_SIZE,
  1494. .base = {
  1495. .cra_name = "gcm(aes)",
  1496. .cra_driver_name = "atmel-gcm-aes",
  1497. .cra_priority = ATMEL_AES_PRIORITY,
  1498. .cra_flags = CRYPTO_ALG_ASYNC,
  1499. .cra_blocksize = 1,
  1500. .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx),
  1501. .cra_alignmask = 0xf,
  1502. .cra_module = THIS_MODULE,
  1503. },
  1504. };
  1505. /* xts functions */
  1506. static inline struct atmel_aes_xts_ctx *
  1507. atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx)
  1508. {
  1509. return container_of(ctx, struct atmel_aes_xts_ctx, base);
  1510. }
  1511. static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd);
  1512. static int atmel_aes_xts_start(struct atmel_aes_dev *dd)
  1513. {
  1514. struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx);
  1515. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  1516. struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req);
  1517. unsigned long flags;
  1518. int err;
  1519. atmel_aes_set_mode(dd, rctx);
  1520. err = atmel_aes_hw_init(dd);
  1521. if (err)
  1522. return atmel_aes_complete(dd, err);
  1523. /* Compute the tweak value from req->info with ecb(aes). */
  1524. flags = dd->flags;
  1525. dd->flags &= ~AES_FLAGS_MODE_MASK;
  1526. dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT);
  1527. atmel_aes_write_ctrl_key(dd, false, NULL,
  1528. ctx->key2, ctx->base.keylen);
  1529. dd->flags = flags;
  1530. atmel_aes_write_block(dd, AES_IDATAR(0), req->info);
  1531. return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data);
  1532. }
  1533. static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd)
  1534. {
  1535. struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
  1536. bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD);
  1537. u32 tweak[AES_BLOCK_SIZE / sizeof(u32)];
  1538. static const u32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), };
  1539. u8 *tweak_bytes = (u8 *)tweak;
  1540. int i;
  1541. /* Read the computed ciphered tweak value. */
  1542. atmel_aes_read_block(dd, AES_ODATAR(0), tweak);
  1543. /*
  1544. * Hardware quirk:
  1545. * the order of the ciphered tweak bytes need to be reversed before
  1546. * writing them into the ODATARx registers.
  1547. */
  1548. for (i = 0; i < AES_BLOCK_SIZE/2; ++i) {
  1549. u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i];
  1550. tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i];
  1551. tweak_bytes[i] = tmp;
  1552. }
  1553. /* Process the data. */
  1554. atmel_aes_write_ctrl(dd, use_dma, NULL);
  1555. atmel_aes_write_block(dd, AES_TWR(0), tweak);
  1556. atmel_aes_write_block(dd, AES_ALPHAR(0), one);
  1557. if (use_dma)
  1558. return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes,
  1559. atmel_aes_transfer_complete);
  1560. return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes,
  1561. atmel_aes_transfer_complete);
  1562. }
  1563. static int atmel_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  1564. unsigned int keylen)
  1565. {
  1566. struct atmel_aes_xts_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  1567. int err;
  1568. err = xts_check_key(crypto_ablkcipher_tfm(tfm), key, keylen);
  1569. if (err)
  1570. return err;
  1571. memcpy(ctx->base.key, key, keylen/2);
  1572. memcpy(ctx->key2, key + keylen/2, keylen/2);
  1573. ctx->base.keylen = keylen/2;
  1574. return 0;
  1575. }
  1576. static int atmel_aes_xts_encrypt(struct ablkcipher_request *req)
  1577. {
  1578. return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT);
  1579. }
  1580. static int atmel_aes_xts_decrypt(struct ablkcipher_request *req)
  1581. {
  1582. return atmel_aes_crypt(req, AES_FLAGS_XTS);
  1583. }
  1584. static int atmel_aes_xts_cra_init(struct crypto_tfm *tfm)
  1585. {
  1586. struct atmel_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  1587. tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx);
  1588. ctx->base.start = atmel_aes_xts_start;
  1589. return 0;
  1590. }
  1591. static struct crypto_alg aes_xts_alg = {
  1592. .cra_name = "xts(aes)",
  1593. .cra_driver_name = "atmel-xts-aes",
  1594. .cra_priority = ATMEL_AES_PRIORITY,
  1595. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1596. .cra_blocksize = AES_BLOCK_SIZE,
  1597. .cra_ctxsize = sizeof(struct atmel_aes_xts_ctx),
  1598. .cra_alignmask = 0xf,
  1599. .cra_type = &crypto_ablkcipher_type,
  1600. .cra_module = THIS_MODULE,
  1601. .cra_init = atmel_aes_xts_cra_init,
  1602. .cra_u.ablkcipher = {
  1603. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1604. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1605. .ivsize = AES_BLOCK_SIZE,
  1606. .setkey = atmel_aes_xts_setkey,
  1607. .encrypt = atmel_aes_xts_encrypt,
  1608. .decrypt = atmel_aes_xts_decrypt,
  1609. }
  1610. };
  1611. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  1612. /* authenc aead functions */
  1613. static int atmel_aes_authenc_start(struct atmel_aes_dev *dd);
  1614. static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
  1615. bool is_async);
  1616. static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
  1617. bool is_async);
  1618. static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd);
  1619. static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
  1620. bool is_async);
  1621. static void atmel_aes_authenc_complete(struct atmel_aes_dev *dd, int err)
  1622. {
  1623. struct aead_request *req = aead_request_cast(dd->areq);
  1624. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1625. if (err && (dd->flags & AES_FLAGS_OWN_SHA))
  1626. atmel_sha_authenc_abort(&rctx->auth_req);
  1627. dd->flags &= ~AES_FLAGS_OWN_SHA;
  1628. }
  1629. static int atmel_aes_authenc_start(struct atmel_aes_dev *dd)
  1630. {
  1631. struct aead_request *req = aead_request_cast(dd->areq);
  1632. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1633. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1634. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1635. int err;
  1636. atmel_aes_set_mode(dd, &rctx->base);
  1637. err = atmel_aes_hw_init(dd);
  1638. if (err)
  1639. return atmel_aes_complete(dd, err);
  1640. return atmel_sha_authenc_schedule(&rctx->auth_req, ctx->auth,
  1641. atmel_aes_authenc_init, dd);
  1642. }
  1643. static int atmel_aes_authenc_init(struct atmel_aes_dev *dd, int err,
  1644. bool is_async)
  1645. {
  1646. struct aead_request *req = aead_request_cast(dd->areq);
  1647. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1648. if (is_async)
  1649. dd->is_async = true;
  1650. if (err)
  1651. return atmel_aes_complete(dd, err);
  1652. /* If here, we've got the ownership of the SHA device. */
  1653. dd->flags |= AES_FLAGS_OWN_SHA;
  1654. /* Configure the SHA device. */
  1655. return atmel_sha_authenc_init(&rctx->auth_req,
  1656. req->src, req->assoclen,
  1657. rctx->textlen,
  1658. atmel_aes_authenc_transfer, dd);
  1659. }
  1660. static int atmel_aes_authenc_transfer(struct atmel_aes_dev *dd, int err,
  1661. bool is_async)
  1662. {
  1663. struct aead_request *req = aead_request_cast(dd->areq);
  1664. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1665. bool enc = atmel_aes_is_encrypt(dd);
  1666. struct scatterlist *src, *dst;
  1667. u32 iv[AES_BLOCK_SIZE / sizeof(u32)];
  1668. u32 emr;
  1669. if (is_async)
  1670. dd->is_async = true;
  1671. if (err)
  1672. return atmel_aes_complete(dd, err);
  1673. /* Prepare src and dst scatter-lists to transfer cipher/plain texts. */
  1674. src = scatterwalk_ffwd(rctx->src, req->src, req->assoclen);
  1675. dst = src;
  1676. if (req->src != req->dst)
  1677. dst = scatterwalk_ffwd(rctx->dst, req->dst, req->assoclen);
  1678. /* Configure the AES device. */
  1679. memcpy(iv, req->iv, sizeof(iv));
  1680. /*
  1681. * Here we always set the 2nd parameter of atmel_aes_write_ctrl() to
  1682. * 'true' even if the data transfer is actually performed by the CPU (so
  1683. * not by the DMA) because we must force the AES_MR_SMOD bitfield to the
  1684. * value AES_MR_SMOD_IDATAR0. Indeed, both AES_MR_SMOD and SHA_MR_SMOD
  1685. * must be set to *_MR_SMOD_IDATAR0.
  1686. */
  1687. atmel_aes_write_ctrl(dd, true, iv);
  1688. emr = AES_EMR_PLIPEN;
  1689. if (!enc)
  1690. emr |= AES_EMR_PLIPD;
  1691. atmel_aes_write(dd, AES_EMR, emr);
  1692. /* Transfer data. */
  1693. return atmel_aes_dma_start(dd, src, dst, rctx->textlen,
  1694. atmel_aes_authenc_digest);
  1695. }
  1696. static int atmel_aes_authenc_digest(struct atmel_aes_dev *dd)
  1697. {
  1698. struct aead_request *req = aead_request_cast(dd->areq);
  1699. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1700. /* atmel_sha_authenc_final() releases the SHA device. */
  1701. dd->flags &= ~AES_FLAGS_OWN_SHA;
  1702. return atmel_sha_authenc_final(&rctx->auth_req,
  1703. rctx->digest, sizeof(rctx->digest),
  1704. atmel_aes_authenc_final, dd);
  1705. }
  1706. static int atmel_aes_authenc_final(struct atmel_aes_dev *dd, int err,
  1707. bool is_async)
  1708. {
  1709. struct aead_request *req = aead_request_cast(dd->areq);
  1710. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1711. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1712. bool enc = atmel_aes_is_encrypt(dd);
  1713. u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)], *odigest = rctx->digest;
  1714. u32 offs, authsize;
  1715. if (is_async)
  1716. dd->is_async = true;
  1717. if (err)
  1718. goto complete;
  1719. offs = req->assoclen + rctx->textlen;
  1720. authsize = crypto_aead_authsize(tfm);
  1721. if (enc) {
  1722. scatterwalk_map_and_copy(odigest, req->dst, offs, authsize, 1);
  1723. } else {
  1724. scatterwalk_map_and_copy(idigest, req->src, offs, authsize, 0);
  1725. if (crypto_memneq(idigest, odigest, authsize))
  1726. err = -EBADMSG;
  1727. }
  1728. complete:
  1729. return atmel_aes_complete(dd, err);
  1730. }
  1731. static int atmel_aes_authenc_setkey(struct crypto_aead *tfm, const u8 *key,
  1732. unsigned int keylen)
  1733. {
  1734. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1735. struct crypto_authenc_keys keys;
  1736. u32 flags;
  1737. int err;
  1738. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  1739. goto badkey;
  1740. if (keys.enckeylen > sizeof(ctx->base.key))
  1741. goto badkey;
  1742. /* Save auth key. */
  1743. flags = crypto_aead_get_flags(tfm);
  1744. err = atmel_sha_authenc_setkey(ctx->auth,
  1745. keys.authkey, keys.authkeylen,
  1746. &flags);
  1747. crypto_aead_set_flags(tfm, flags & CRYPTO_TFM_RES_MASK);
  1748. if (err) {
  1749. memzero_explicit(&keys, sizeof(keys));
  1750. return err;
  1751. }
  1752. /* Save enc key. */
  1753. ctx->base.keylen = keys.enckeylen;
  1754. memcpy(ctx->base.key, keys.enckey, keys.enckeylen);
  1755. memzero_explicit(&keys, sizeof(keys));
  1756. return 0;
  1757. badkey:
  1758. crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  1759. memzero_explicit(&keys, sizeof(keys));
  1760. return -EINVAL;
  1761. }
  1762. static int atmel_aes_authenc_init_tfm(struct crypto_aead *tfm,
  1763. unsigned long auth_mode)
  1764. {
  1765. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1766. unsigned int auth_reqsize = atmel_sha_authenc_get_reqsize();
  1767. ctx->auth = atmel_sha_authenc_spawn(auth_mode);
  1768. if (IS_ERR(ctx->auth))
  1769. return PTR_ERR(ctx->auth);
  1770. crypto_aead_set_reqsize(tfm, (sizeof(struct atmel_aes_authenc_reqctx) +
  1771. auth_reqsize));
  1772. ctx->base.start = atmel_aes_authenc_start;
  1773. return 0;
  1774. }
  1775. static int atmel_aes_authenc_hmac_sha1_init_tfm(struct crypto_aead *tfm)
  1776. {
  1777. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA1);
  1778. }
  1779. static int atmel_aes_authenc_hmac_sha224_init_tfm(struct crypto_aead *tfm)
  1780. {
  1781. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA224);
  1782. }
  1783. static int atmel_aes_authenc_hmac_sha256_init_tfm(struct crypto_aead *tfm)
  1784. {
  1785. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA256);
  1786. }
  1787. static int atmel_aes_authenc_hmac_sha384_init_tfm(struct crypto_aead *tfm)
  1788. {
  1789. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA384);
  1790. }
  1791. static int atmel_aes_authenc_hmac_sha512_init_tfm(struct crypto_aead *tfm)
  1792. {
  1793. return atmel_aes_authenc_init_tfm(tfm, SHA_FLAGS_HMAC_SHA512);
  1794. }
  1795. static void atmel_aes_authenc_exit_tfm(struct crypto_aead *tfm)
  1796. {
  1797. struct atmel_aes_authenc_ctx *ctx = crypto_aead_ctx(tfm);
  1798. atmel_sha_authenc_free(ctx->auth);
  1799. }
  1800. static int atmel_aes_authenc_crypt(struct aead_request *req,
  1801. unsigned long mode)
  1802. {
  1803. struct atmel_aes_authenc_reqctx *rctx = aead_request_ctx(req);
  1804. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  1805. struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm);
  1806. u32 authsize = crypto_aead_authsize(tfm);
  1807. bool enc = (mode & AES_FLAGS_ENCRYPT);
  1808. struct atmel_aes_dev *dd;
  1809. /* Compute text length. */
  1810. if (!enc && req->cryptlen < authsize)
  1811. return -EINVAL;
  1812. rctx->textlen = req->cryptlen - (enc ? 0 : authsize);
  1813. /*
  1814. * Currently, empty messages are not supported yet:
  1815. * the SHA auto-padding can be used only on non-empty messages.
  1816. * Hence a special case needs to be implemented for empty message.
  1817. */
  1818. if (!rctx->textlen && !req->assoclen)
  1819. return -EINVAL;
  1820. rctx->base.mode = mode;
  1821. ctx->block_size = AES_BLOCK_SIZE;
  1822. ctx->is_aead = true;
  1823. dd = atmel_aes_find_dev(ctx);
  1824. if (!dd)
  1825. return -ENODEV;
  1826. return atmel_aes_handle_queue(dd, &req->base);
  1827. }
  1828. static int atmel_aes_authenc_cbc_aes_encrypt(struct aead_request *req)
  1829. {
  1830. return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT);
  1831. }
  1832. static int atmel_aes_authenc_cbc_aes_decrypt(struct aead_request *req)
  1833. {
  1834. return atmel_aes_authenc_crypt(req, AES_FLAGS_CBC);
  1835. }
  1836. static struct aead_alg aes_authenc_algs[] = {
  1837. {
  1838. .setkey = atmel_aes_authenc_setkey,
  1839. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1840. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1841. .init = atmel_aes_authenc_hmac_sha1_init_tfm,
  1842. .exit = atmel_aes_authenc_exit_tfm,
  1843. .ivsize = AES_BLOCK_SIZE,
  1844. .maxauthsize = SHA1_DIGEST_SIZE,
  1845. .base = {
  1846. .cra_name = "authenc(hmac(sha1),cbc(aes))",
  1847. .cra_driver_name = "atmel-authenc-hmac-sha1-cbc-aes",
  1848. .cra_priority = ATMEL_AES_PRIORITY,
  1849. .cra_flags = CRYPTO_ALG_ASYNC,
  1850. .cra_blocksize = AES_BLOCK_SIZE,
  1851. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1852. .cra_alignmask = 0xf,
  1853. .cra_module = THIS_MODULE,
  1854. },
  1855. },
  1856. {
  1857. .setkey = atmel_aes_authenc_setkey,
  1858. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1859. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1860. .init = atmel_aes_authenc_hmac_sha224_init_tfm,
  1861. .exit = atmel_aes_authenc_exit_tfm,
  1862. .ivsize = AES_BLOCK_SIZE,
  1863. .maxauthsize = SHA224_DIGEST_SIZE,
  1864. .base = {
  1865. .cra_name = "authenc(hmac(sha224),cbc(aes))",
  1866. .cra_driver_name = "atmel-authenc-hmac-sha224-cbc-aes",
  1867. .cra_priority = ATMEL_AES_PRIORITY,
  1868. .cra_flags = CRYPTO_ALG_ASYNC,
  1869. .cra_blocksize = AES_BLOCK_SIZE,
  1870. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1871. .cra_alignmask = 0xf,
  1872. .cra_module = THIS_MODULE,
  1873. },
  1874. },
  1875. {
  1876. .setkey = atmel_aes_authenc_setkey,
  1877. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1878. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1879. .init = atmel_aes_authenc_hmac_sha256_init_tfm,
  1880. .exit = atmel_aes_authenc_exit_tfm,
  1881. .ivsize = AES_BLOCK_SIZE,
  1882. .maxauthsize = SHA256_DIGEST_SIZE,
  1883. .base = {
  1884. .cra_name = "authenc(hmac(sha256),cbc(aes))",
  1885. .cra_driver_name = "atmel-authenc-hmac-sha256-cbc-aes",
  1886. .cra_priority = ATMEL_AES_PRIORITY,
  1887. .cra_flags = CRYPTO_ALG_ASYNC,
  1888. .cra_blocksize = AES_BLOCK_SIZE,
  1889. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1890. .cra_alignmask = 0xf,
  1891. .cra_module = THIS_MODULE,
  1892. },
  1893. },
  1894. {
  1895. .setkey = atmel_aes_authenc_setkey,
  1896. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1897. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1898. .init = atmel_aes_authenc_hmac_sha384_init_tfm,
  1899. .exit = atmel_aes_authenc_exit_tfm,
  1900. .ivsize = AES_BLOCK_SIZE,
  1901. .maxauthsize = SHA384_DIGEST_SIZE,
  1902. .base = {
  1903. .cra_name = "authenc(hmac(sha384),cbc(aes))",
  1904. .cra_driver_name = "atmel-authenc-hmac-sha384-cbc-aes",
  1905. .cra_priority = ATMEL_AES_PRIORITY,
  1906. .cra_flags = CRYPTO_ALG_ASYNC,
  1907. .cra_blocksize = AES_BLOCK_SIZE,
  1908. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1909. .cra_alignmask = 0xf,
  1910. .cra_module = THIS_MODULE,
  1911. },
  1912. },
  1913. {
  1914. .setkey = atmel_aes_authenc_setkey,
  1915. .encrypt = atmel_aes_authenc_cbc_aes_encrypt,
  1916. .decrypt = atmel_aes_authenc_cbc_aes_decrypt,
  1917. .init = atmel_aes_authenc_hmac_sha512_init_tfm,
  1918. .exit = atmel_aes_authenc_exit_tfm,
  1919. .ivsize = AES_BLOCK_SIZE,
  1920. .maxauthsize = SHA512_DIGEST_SIZE,
  1921. .base = {
  1922. .cra_name = "authenc(hmac(sha512),cbc(aes))",
  1923. .cra_driver_name = "atmel-authenc-hmac-sha512-cbc-aes",
  1924. .cra_priority = ATMEL_AES_PRIORITY,
  1925. .cra_flags = CRYPTO_ALG_ASYNC,
  1926. .cra_blocksize = AES_BLOCK_SIZE,
  1927. .cra_ctxsize = sizeof(struct atmel_aes_authenc_ctx),
  1928. .cra_alignmask = 0xf,
  1929. .cra_module = THIS_MODULE,
  1930. },
  1931. },
  1932. };
  1933. #endif /* CONFIG_CRYPTO_DEV_ATMEL_AUTHENC */
  1934. /* Probe functions */
  1935. static int atmel_aes_buff_init(struct atmel_aes_dev *dd)
  1936. {
  1937. dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER);
  1938. dd->buflen = ATMEL_AES_BUFFER_SIZE;
  1939. dd->buflen &= ~(AES_BLOCK_SIZE - 1);
  1940. if (!dd->buf) {
  1941. dev_err(dd->dev, "unable to alloc pages.\n");
  1942. return -ENOMEM;
  1943. }
  1944. return 0;
  1945. }
  1946. static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd)
  1947. {
  1948. free_page((unsigned long)dd->buf);
  1949. }
  1950. static bool atmel_aes_filter(struct dma_chan *chan, void *slave)
  1951. {
  1952. struct at_dma_slave *sl = slave;
  1953. if (sl && sl->dma_dev == chan->device->dev) {
  1954. chan->private = sl;
  1955. return true;
  1956. } else {
  1957. return false;
  1958. }
  1959. }
  1960. static int atmel_aes_dma_init(struct atmel_aes_dev *dd,
  1961. struct crypto_platform_data *pdata)
  1962. {
  1963. struct at_dma_slave *slave;
  1964. dma_cap_mask_t mask;
  1965. dma_cap_zero(mask);
  1966. dma_cap_set(DMA_SLAVE, mask);
  1967. /* Try to grab 2 DMA channels */
  1968. slave = &pdata->dma_slave->rxdata;
  1969. dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
  1970. slave, dd->dev, "tx");
  1971. if (!dd->src.chan)
  1972. goto err_dma_in;
  1973. slave = &pdata->dma_slave->txdata;
  1974. dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter,
  1975. slave, dd->dev, "rx");
  1976. if (!dd->dst.chan)
  1977. goto err_dma_out;
  1978. return 0;
  1979. err_dma_out:
  1980. dma_release_channel(dd->src.chan);
  1981. err_dma_in:
  1982. dev_warn(dd->dev, "no DMA channel available\n");
  1983. return -ENODEV;
  1984. }
  1985. static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd)
  1986. {
  1987. dma_release_channel(dd->dst.chan);
  1988. dma_release_channel(dd->src.chan);
  1989. }
  1990. static void atmel_aes_queue_task(unsigned long data)
  1991. {
  1992. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  1993. atmel_aes_handle_queue(dd, NULL);
  1994. }
  1995. static void atmel_aes_done_task(unsigned long data)
  1996. {
  1997. struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data;
  1998. dd->is_async = true;
  1999. (void)dd->resume(dd);
  2000. }
  2001. static irqreturn_t atmel_aes_irq(int irq, void *dev_id)
  2002. {
  2003. struct atmel_aes_dev *aes_dd = dev_id;
  2004. u32 reg;
  2005. reg = atmel_aes_read(aes_dd, AES_ISR);
  2006. if (reg & atmel_aes_read(aes_dd, AES_IMR)) {
  2007. atmel_aes_write(aes_dd, AES_IDR, reg);
  2008. if (AES_FLAGS_BUSY & aes_dd->flags)
  2009. tasklet_schedule(&aes_dd->done_task);
  2010. else
  2011. dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n");
  2012. return IRQ_HANDLED;
  2013. }
  2014. return IRQ_NONE;
  2015. }
  2016. static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd)
  2017. {
  2018. int i;
  2019. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  2020. if (dd->caps.has_authenc)
  2021. for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++)
  2022. crypto_unregister_aead(&aes_authenc_algs[i]);
  2023. #endif
  2024. if (dd->caps.has_xts)
  2025. crypto_unregister_alg(&aes_xts_alg);
  2026. if (dd->caps.has_gcm)
  2027. crypto_unregister_aead(&aes_gcm_alg);
  2028. if (dd->caps.has_cfb64)
  2029. crypto_unregister_alg(&aes_cfb64_alg);
  2030. for (i = 0; i < ARRAY_SIZE(aes_algs); i++)
  2031. crypto_unregister_alg(&aes_algs[i]);
  2032. }
  2033. static int atmel_aes_register_algs(struct atmel_aes_dev *dd)
  2034. {
  2035. int err, i, j;
  2036. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  2037. err = crypto_register_alg(&aes_algs[i]);
  2038. if (err)
  2039. goto err_aes_algs;
  2040. }
  2041. if (dd->caps.has_cfb64) {
  2042. err = crypto_register_alg(&aes_cfb64_alg);
  2043. if (err)
  2044. goto err_aes_cfb64_alg;
  2045. }
  2046. if (dd->caps.has_gcm) {
  2047. err = crypto_register_aead(&aes_gcm_alg);
  2048. if (err)
  2049. goto err_aes_gcm_alg;
  2050. }
  2051. if (dd->caps.has_xts) {
  2052. err = crypto_register_alg(&aes_xts_alg);
  2053. if (err)
  2054. goto err_aes_xts_alg;
  2055. }
  2056. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  2057. if (dd->caps.has_authenc) {
  2058. for (i = 0; i < ARRAY_SIZE(aes_authenc_algs); i++) {
  2059. err = crypto_register_aead(&aes_authenc_algs[i]);
  2060. if (err)
  2061. goto err_aes_authenc_alg;
  2062. }
  2063. }
  2064. #endif
  2065. return 0;
  2066. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  2067. /* i = ARRAY_SIZE(aes_authenc_algs); */
  2068. err_aes_authenc_alg:
  2069. for (j = 0; j < i; j++)
  2070. crypto_unregister_aead(&aes_authenc_algs[j]);
  2071. crypto_unregister_alg(&aes_xts_alg);
  2072. #endif
  2073. err_aes_xts_alg:
  2074. crypto_unregister_aead(&aes_gcm_alg);
  2075. err_aes_gcm_alg:
  2076. crypto_unregister_alg(&aes_cfb64_alg);
  2077. err_aes_cfb64_alg:
  2078. i = ARRAY_SIZE(aes_algs);
  2079. err_aes_algs:
  2080. for (j = 0; j < i; j++)
  2081. crypto_unregister_alg(&aes_algs[j]);
  2082. return err;
  2083. }
  2084. static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
  2085. {
  2086. dd->caps.has_dualbuff = 0;
  2087. dd->caps.has_cfb64 = 0;
  2088. dd->caps.has_gcm = 0;
  2089. dd->caps.has_xts = 0;
  2090. dd->caps.has_authenc = 0;
  2091. dd->caps.max_burst_size = 1;
  2092. /* keep only major version number */
  2093. switch (dd->hw_version & 0xff0) {
  2094. case 0x500:
  2095. dd->caps.has_dualbuff = 1;
  2096. dd->caps.has_cfb64 = 1;
  2097. dd->caps.has_gcm = 1;
  2098. dd->caps.has_xts = 1;
  2099. dd->caps.has_authenc = 1;
  2100. dd->caps.max_burst_size = 4;
  2101. break;
  2102. case 0x200:
  2103. dd->caps.has_dualbuff = 1;
  2104. dd->caps.has_cfb64 = 1;
  2105. dd->caps.has_gcm = 1;
  2106. dd->caps.max_burst_size = 4;
  2107. break;
  2108. case 0x130:
  2109. dd->caps.has_dualbuff = 1;
  2110. dd->caps.has_cfb64 = 1;
  2111. dd->caps.max_burst_size = 4;
  2112. break;
  2113. case 0x120:
  2114. break;
  2115. default:
  2116. dev_warn(dd->dev,
  2117. "Unmanaged aes version, set minimum capabilities\n");
  2118. break;
  2119. }
  2120. }
  2121. #if defined(CONFIG_OF)
  2122. static const struct of_device_id atmel_aes_dt_ids[] = {
  2123. { .compatible = "atmel,at91sam9g46-aes" },
  2124. { /* sentinel */ }
  2125. };
  2126. MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids);
  2127. static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
  2128. {
  2129. struct device_node *np = pdev->dev.of_node;
  2130. struct crypto_platform_data *pdata;
  2131. if (!np) {
  2132. dev_err(&pdev->dev, "device node not found\n");
  2133. return ERR_PTR(-EINVAL);
  2134. }
  2135. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  2136. if (!pdata)
  2137. return ERR_PTR(-ENOMEM);
  2138. pdata->dma_slave = devm_kzalloc(&pdev->dev,
  2139. sizeof(*(pdata->dma_slave)),
  2140. GFP_KERNEL);
  2141. if (!pdata->dma_slave) {
  2142. devm_kfree(&pdev->dev, pdata);
  2143. return ERR_PTR(-ENOMEM);
  2144. }
  2145. return pdata;
  2146. }
  2147. #else
  2148. static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev)
  2149. {
  2150. return ERR_PTR(-EINVAL);
  2151. }
  2152. #endif
  2153. static int atmel_aes_probe(struct platform_device *pdev)
  2154. {
  2155. struct atmel_aes_dev *aes_dd;
  2156. struct crypto_platform_data *pdata;
  2157. struct device *dev = &pdev->dev;
  2158. struct resource *aes_res;
  2159. int err;
  2160. pdata = pdev->dev.platform_data;
  2161. if (!pdata) {
  2162. pdata = atmel_aes_of_init(pdev);
  2163. if (IS_ERR(pdata)) {
  2164. err = PTR_ERR(pdata);
  2165. goto aes_dd_err;
  2166. }
  2167. }
  2168. if (!pdata->dma_slave) {
  2169. err = -ENXIO;
  2170. goto aes_dd_err;
  2171. }
  2172. aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL);
  2173. if (aes_dd == NULL) {
  2174. err = -ENOMEM;
  2175. goto aes_dd_err;
  2176. }
  2177. aes_dd->dev = dev;
  2178. platform_set_drvdata(pdev, aes_dd);
  2179. INIT_LIST_HEAD(&aes_dd->list);
  2180. spin_lock_init(&aes_dd->lock);
  2181. tasklet_init(&aes_dd->done_task, atmel_aes_done_task,
  2182. (unsigned long)aes_dd);
  2183. tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task,
  2184. (unsigned long)aes_dd);
  2185. crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH);
  2186. /* Get the base address */
  2187. aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2188. if (!aes_res) {
  2189. dev_err(dev, "no MEM resource info\n");
  2190. err = -ENODEV;
  2191. goto res_err;
  2192. }
  2193. aes_dd->phys_base = aes_res->start;
  2194. /* Get the IRQ */
  2195. aes_dd->irq = platform_get_irq(pdev, 0);
  2196. if (aes_dd->irq < 0) {
  2197. dev_err(dev, "no IRQ resource info\n");
  2198. err = aes_dd->irq;
  2199. goto res_err;
  2200. }
  2201. err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq,
  2202. IRQF_SHARED, "atmel-aes", aes_dd);
  2203. if (err) {
  2204. dev_err(dev, "unable to request aes irq.\n");
  2205. goto res_err;
  2206. }
  2207. /* Initializing the clock */
  2208. aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk");
  2209. if (IS_ERR(aes_dd->iclk)) {
  2210. dev_err(dev, "clock initialization failed.\n");
  2211. err = PTR_ERR(aes_dd->iclk);
  2212. goto res_err;
  2213. }
  2214. aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res);
  2215. if (IS_ERR(aes_dd->io_base)) {
  2216. dev_err(dev, "can't ioremap\n");
  2217. err = PTR_ERR(aes_dd->io_base);
  2218. goto res_err;
  2219. }
  2220. err = clk_prepare(aes_dd->iclk);
  2221. if (err)
  2222. goto res_err;
  2223. err = atmel_aes_hw_version_init(aes_dd);
  2224. if (err)
  2225. goto iclk_unprepare;
  2226. atmel_aes_get_cap(aes_dd);
  2227. #if IS_ENABLED(CONFIG_CRYPTO_DEV_ATMEL_AUTHENC)
  2228. if (aes_dd->caps.has_authenc && !atmel_sha_authenc_is_ready()) {
  2229. err = -EPROBE_DEFER;
  2230. goto iclk_unprepare;
  2231. }
  2232. #endif
  2233. err = atmel_aes_buff_init(aes_dd);
  2234. if (err)
  2235. goto err_aes_buff;
  2236. err = atmel_aes_dma_init(aes_dd, pdata);
  2237. if (err)
  2238. goto err_aes_dma;
  2239. spin_lock(&atmel_aes.lock);
  2240. list_add_tail(&aes_dd->list, &atmel_aes.dev_list);
  2241. spin_unlock(&atmel_aes.lock);
  2242. err = atmel_aes_register_algs(aes_dd);
  2243. if (err)
  2244. goto err_algs;
  2245. dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n",
  2246. dma_chan_name(aes_dd->src.chan),
  2247. dma_chan_name(aes_dd->dst.chan));
  2248. return 0;
  2249. err_algs:
  2250. spin_lock(&atmel_aes.lock);
  2251. list_del(&aes_dd->list);
  2252. spin_unlock(&atmel_aes.lock);
  2253. atmel_aes_dma_cleanup(aes_dd);
  2254. err_aes_dma:
  2255. atmel_aes_buff_cleanup(aes_dd);
  2256. err_aes_buff:
  2257. iclk_unprepare:
  2258. clk_unprepare(aes_dd->iclk);
  2259. res_err:
  2260. tasklet_kill(&aes_dd->done_task);
  2261. tasklet_kill(&aes_dd->queue_task);
  2262. aes_dd_err:
  2263. if (err != -EPROBE_DEFER)
  2264. dev_err(dev, "initialization failed.\n");
  2265. return err;
  2266. }
  2267. static int atmel_aes_remove(struct platform_device *pdev)
  2268. {
  2269. struct atmel_aes_dev *aes_dd;
  2270. aes_dd = platform_get_drvdata(pdev);
  2271. if (!aes_dd)
  2272. return -ENODEV;
  2273. spin_lock(&atmel_aes.lock);
  2274. list_del(&aes_dd->list);
  2275. spin_unlock(&atmel_aes.lock);
  2276. atmel_aes_unregister_algs(aes_dd);
  2277. tasklet_kill(&aes_dd->done_task);
  2278. tasklet_kill(&aes_dd->queue_task);
  2279. atmel_aes_dma_cleanup(aes_dd);
  2280. atmel_aes_buff_cleanup(aes_dd);
  2281. clk_unprepare(aes_dd->iclk);
  2282. return 0;
  2283. }
  2284. static struct platform_driver atmel_aes_driver = {
  2285. .probe = atmel_aes_probe,
  2286. .remove = atmel_aes_remove,
  2287. .driver = {
  2288. .name = "atmel_aes",
  2289. .of_match_table = of_match_ptr(atmel_aes_dt_ids),
  2290. },
  2291. };
  2292. module_platform_driver(atmel_aes_driver);
  2293. MODULE_DESCRIPTION("Atmel AES hw acceleration support.");
  2294. MODULE_LICENSE("GPL v2");
  2295. MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique");