atmel-aes.c 61 KB

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