jh7110-aes.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * StarFive AES acceleration driver
  4. *
  5. * Copyright (c) 2022 StarFive Technology
  6. */
  7. #include <crypto/engine.h>
  8. #include <crypto/gcm.h>
  9. #include <crypto/internal/aead.h>
  10. #include <crypto/internal/skcipher.h>
  11. #include <crypto/scatterwalk.h>
  12. #include "jh7110-cryp.h"
  13. #include <linux/err.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #define STARFIVE_AES_REGS_OFFSET 0x100
  19. #define STARFIVE_AES_AESDIO0R (STARFIVE_AES_REGS_OFFSET + 0x0)
  20. #define STARFIVE_AES_KEY0 (STARFIVE_AES_REGS_OFFSET + 0x4)
  21. #define STARFIVE_AES_KEY1 (STARFIVE_AES_REGS_OFFSET + 0x8)
  22. #define STARFIVE_AES_KEY2 (STARFIVE_AES_REGS_OFFSET + 0xC)
  23. #define STARFIVE_AES_KEY3 (STARFIVE_AES_REGS_OFFSET + 0x10)
  24. #define STARFIVE_AES_KEY4 (STARFIVE_AES_REGS_OFFSET + 0x14)
  25. #define STARFIVE_AES_KEY5 (STARFIVE_AES_REGS_OFFSET + 0x18)
  26. #define STARFIVE_AES_KEY6 (STARFIVE_AES_REGS_OFFSET + 0x1C)
  27. #define STARFIVE_AES_KEY7 (STARFIVE_AES_REGS_OFFSET + 0x20)
  28. #define STARFIVE_AES_CSR (STARFIVE_AES_REGS_OFFSET + 0x24)
  29. #define STARFIVE_AES_IV0 (STARFIVE_AES_REGS_OFFSET + 0x28)
  30. #define STARFIVE_AES_IV1 (STARFIVE_AES_REGS_OFFSET + 0x2C)
  31. #define STARFIVE_AES_IV2 (STARFIVE_AES_REGS_OFFSET + 0x30)
  32. #define STARFIVE_AES_IV3 (STARFIVE_AES_REGS_OFFSET + 0x34)
  33. #define STARFIVE_AES_NONCE0 (STARFIVE_AES_REGS_OFFSET + 0x3C)
  34. #define STARFIVE_AES_NONCE1 (STARFIVE_AES_REGS_OFFSET + 0x40)
  35. #define STARFIVE_AES_NONCE2 (STARFIVE_AES_REGS_OFFSET + 0x44)
  36. #define STARFIVE_AES_NONCE3 (STARFIVE_AES_REGS_OFFSET + 0x48)
  37. #define STARFIVE_AES_ALEN0 (STARFIVE_AES_REGS_OFFSET + 0x4C)
  38. #define STARFIVE_AES_ALEN1 (STARFIVE_AES_REGS_OFFSET + 0x50)
  39. #define STARFIVE_AES_MLEN0 (STARFIVE_AES_REGS_OFFSET + 0x54)
  40. #define STARFIVE_AES_MLEN1 (STARFIVE_AES_REGS_OFFSET + 0x58)
  41. #define STARFIVE_AES_IVLEN (STARFIVE_AES_REGS_OFFSET + 0x5C)
  42. #define FLG_MODE_MASK GENMASK(2, 0)
  43. #define FLG_ENCRYPT BIT(4)
  44. /* Misc */
  45. #define CCM_B0_ADATA 0x40
  46. #define AES_BLOCK_32 (AES_BLOCK_SIZE / sizeof(u32))
  47. static inline int starfive_aes_wait_busy(struct starfive_cryp_dev *cryp)
  48. {
  49. u32 status;
  50. return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
  51. !(status & STARFIVE_AES_BUSY), 10, 100000);
  52. }
  53. static inline int starfive_aes_wait_keydone(struct starfive_cryp_dev *cryp)
  54. {
  55. u32 status;
  56. return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
  57. (status & STARFIVE_AES_KEY_DONE), 10, 100000);
  58. }
  59. static inline int starfive_aes_wait_gcmdone(struct starfive_cryp_dev *cryp)
  60. {
  61. u32 status;
  62. return readl_relaxed_poll_timeout(cryp->base + STARFIVE_AES_CSR, status,
  63. (status & STARFIVE_AES_GCM_DONE), 10, 100000);
  64. }
  65. static inline int is_gcm(struct starfive_cryp_dev *cryp)
  66. {
  67. return (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_GCM;
  68. }
  69. static inline bool is_encrypt(struct starfive_cryp_dev *cryp)
  70. {
  71. return cryp->flags & FLG_ENCRYPT;
  72. }
  73. static void starfive_aes_aead_hw_start(struct starfive_cryp_ctx *ctx, u32 hw_mode)
  74. {
  75. struct starfive_cryp_dev *cryp = ctx->cryp;
  76. unsigned int value;
  77. switch (hw_mode) {
  78. case STARFIVE_AES_MODE_GCM:
  79. value = readl(ctx->cryp->base + STARFIVE_AES_CSR);
  80. value |= STARFIVE_AES_GCM_START;
  81. writel(value, cryp->base + STARFIVE_AES_CSR);
  82. starfive_aes_wait_gcmdone(cryp);
  83. break;
  84. case STARFIVE_AES_MODE_CCM:
  85. value = readl(ctx->cryp->base + STARFIVE_AES_CSR);
  86. value |= STARFIVE_AES_CCM_START;
  87. writel(value, cryp->base + STARFIVE_AES_CSR);
  88. break;
  89. }
  90. }
  91. static inline void starfive_aes_set_alen(struct starfive_cryp_ctx *ctx)
  92. {
  93. struct starfive_cryp_dev *cryp = ctx->cryp;
  94. writel(upper_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN0);
  95. writel(lower_32_bits(cryp->assoclen), cryp->base + STARFIVE_AES_ALEN1);
  96. }
  97. static inline void starfive_aes_set_mlen(struct starfive_cryp_ctx *ctx)
  98. {
  99. struct starfive_cryp_dev *cryp = ctx->cryp;
  100. writel(upper_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN0);
  101. writel(lower_32_bits(cryp->total_in), cryp->base + STARFIVE_AES_MLEN1);
  102. }
  103. static inline int starfive_aes_ccm_check_iv(const u8 *iv)
  104. {
  105. /* 2 <= L <= 8, so 1 <= L' <= 7. */
  106. if (iv[0] < 1 || iv[0] > 7)
  107. return -EINVAL;
  108. return 0;
  109. }
  110. static int starfive_aes_write_iv(struct starfive_cryp_ctx *ctx, u32 *iv)
  111. {
  112. struct starfive_cryp_dev *cryp = ctx->cryp;
  113. writel(iv[0], cryp->base + STARFIVE_AES_IV0);
  114. writel(iv[1], cryp->base + STARFIVE_AES_IV1);
  115. writel(iv[2], cryp->base + STARFIVE_AES_IV2);
  116. if (is_gcm(cryp)) {
  117. if (starfive_aes_wait_gcmdone(cryp))
  118. return -ETIMEDOUT;
  119. return 0;
  120. }
  121. writel(iv[3], cryp->base + STARFIVE_AES_IV3);
  122. return 0;
  123. }
  124. static inline void starfive_aes_get_iv(struct starfive_cryp_dev *cryp, u32 *iv)
  125. {
  126. iv[0] = readl(cryp->base + STARFIVE_AES_IV0);
  127. iv[1] = readl(cryp->base + STARFIVE_AES_IV1);
  128. iv[2] = readl(cryp->base + STARFIVE_AES_IV2);
  129. iv[3] = readl(cryp->base + STARFIVE_AES_IV3);
  130. }
  131. static inline void starfive_aes_write_nonce(struct starfive_cryp_ctx *ctx, u32 *nonce)
  132. {
  133. struct starfive_cryp_dev *cryp = ctx->cryp;
  134. writel(nonce[0], cryp->base + STARFIVE_AES_NONCE0);
  135. writel(nonce[1], cryp->base + STARFIVE_AES_NONCE1);
  136. writel(nonce[2], cryp->base + STARFIVE_AES_NONCE2);
  137. writel(nonce[3], cryp->base + STARFIVE_AES_NONCE3);
  138. }
  139. static int starfive_aes_write_key(struct starfive_cryp_ctx *ctx)
  140. {
  141. struct starfive_cryp_dev *cryp = ctx->cryp;
  142. u32 *key = (u32 *)ctx->key;
  143. if (ctx->keylen >= AES_KEYSIZE_128) {
  144. writel(key[0], cryp->base + STARFIVE_AES_KEY0);
  145. writel(key[1], cryp->base + STARFIVE_AES_KEY1);
  146. writel(key[2], cryp->base + STARFIVE_AES_KEY2);
  147. writel(key[3], cryp->base + STARFIVE_AES_KEY3);
  148. }
  149. if (ctx->keylen >= AES_KEYSIZE_192) {
  150. writel(key[4], cryp->base + STARFIVE_AES_KEY4);
  151. writel(key[5], cryp->base + STARFIVE_AES_KEY5);
  152. }
  153. if (ctx->keylen >= AES_KEYSIZE_256) {
  154. writel(key[6], cryp->base + STARFIVE_AES_KEY6);
  155. writel(key[7], cryp->base + STARFIVE_AES_KEY7);
  156. }
  157. if (starfive_aes_wait_keydone(cryp))
  158. return -ETIMEDOUT;
  159. return 0;
  160. }
  161. static int starfive_aes_ccm_init(struct starfive_cryp_ctx *ctx)
  162. {
  163. struct starfive_cryp_dev *cryp = ctx->cryp;
  164. u8 iv[AES_BLOCK_SIZE], b0[AES_BLOCK_SIZE];
  165. unsigned int textlen;
  166. memcpy(iv, cryp->req.areq->iv, AES_BLOCK_SIZE);
  167. memset(iv + AES_BLOCK_SIZE - 1 - iv[0], 0, iv[0] + 1);
  168. /* Build B0 */
  169. memcpy(b0, iv, AES_BLOCK_SIZE);
  170. b0[0] |= (8 * ((cryp->authsize - 2) / 2));
  171. if (cryp->assoclen)
  172. b0[0] |= CCM_B0_ADATA;
  173. textlen = cryp->total_in;
  174. b0[AES_BLOCK_SIZE - 2] = textlen >> 8;
  175. b0[AES_BLOCK_SIZE - 1] = textlen & 0xFF;
  176. starfive_aes_write_nonce(ctx, (u32 *)b0);
  177. return 0;
  178. }
  179. static int starfive_aes_hw_init(struct starfive_cryp_ctx *ctx)
  180. {
  181. struct starfive_cryp_request_ctx *rctx = ctx->rctx;
  182. struct starfive_cryp_dev *cryp = ctx->cryp;
  183. u32 hw_mode;
  184. /* reset */
  185. rctx->csr.aes.v = 0;
  186. rctx->csr.aes.aesrst = 1;
  187. writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR);
  188. /* csr setup */
  189. hw_mode = cryp->flags & FLG_MODE_MASK;
  190. rctx->csr.aes.v = 0;
  191. switch (ctx->keylen) {
  192. case AES_KEYSIZE_128:
  193. rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_128;
  194. break;
  195. case AES_KEYSIZE_192:
  196. rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_192;
  197. break;
  198. case AES_KEYSIZE_256:
  199. rctx->csr.aes.keymode = STARFIVE_AES_KEYMODE_256;
  200. break;
  201. }
  202. rctx->csr.aes.mode = hw_mode;
  203. rctx->csr.aes.cmode = !is_encrypt(cryp);
  204. rctx->csr.aes.stmode = STARFIVE_AES_MODE_XFB_1;
  205. if (cryp->side_chan) {
  206. rctx->csr.aes.delay_aes = 1;
  207. rctx->csr.aes.vaes_start = 1;
  208. }
  209. writel(rctx->csr.aes.v, cryp->base + STARFIVE_AES_CSR);
  210. cryp->err = starfive_aes_write_key(ctx);
  211. if (cryp->err)
  212. return cryp->err;
  213. switch (hw_mode) {
  214. case STARFIVE_AES_MODE_GCM:
  215. starfive_aes_set_alen(ctx);
  216. starfive_aes_set_mlen(ctx);
  217. writel(GCM_AES_IV_SIZE, cryp->base + STARFIVE_AES_IVLEN);
  218. starfive_aes_aead_hw_start(ctx, hw_mode);
  219. starfive_aes_write_iv(ctx, (void *)cryp->req.areq->iv);
  220. break;
  221. case STARFIVE_AES_MODE_CCM:
  222. starfive_aes_set_alen(ctx);
  223. starfive_aes_set_mlen(ctx);
  224. starfive_aes_ccm_init(ctx);
  225. starfive_aes_aead_hw_start(ctx, hw_mode);
  226. break;
  227. case STARFIVE_AES_MODE_CBC:
  228. case STARFIVE_AES_MODE_CTR:
  229. starfive_aes_write_iv(ctx, (void *)cryp->req.sreq->iv);
  230. break;
  231. default:
  232. break;
  233. }
  234. return cryp->err;
  235. }
  236. static int starfive_aes_read_authtag(struct starfive_cryp_ctx *ctx)
  237. {
  238. struct starfive_cryp_dev *cryp = ctx->cryp;
  239. struct starfive_cryp_request_ctx *rctx = ctx->rctx;
  240. int i;
  241. if (starfive_aes_wait_busy(cryp))
  242. return dev_err_probe(cryp->dev, -ETIMEDOUT,
  243. "Timeout waiting for tag generation.");
  244. if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_GCM) {
  245. cryp->tag_out[0] = readl(cryp->base + STARFIVE_AES_NONCE0);
  246. cryp->tag_out[1] = readl(cryp->base + STARFIVE_AES_NONCE1);
  247. cryp->tag_out[2] = readl(cryp->base + STARFIVE_AES_NONCE2);
  248. cryp->tag_out[3] = readl(cryp->base + STARFIVE_AES_NONCE3);
  249. } else {
  250. for (i = 0; i < AES_BLOCK_32; i++)
  251. cryp->tag_out[i] = readl(cryp->base + STARFIVE_AES_AESDIO0R);
  252. }
  253. if (is_encrypt(cryp)) {
  254. scatterwalk_map_and_copy(cryp->tag_out, rctx->out_sg,
  255. cryp->total_in, cryp->authsize, 1);
  256. } else {
  257. if (crypto_memneq(cryp->tag_in, cryp->tag_out, cryp->authsize))
  258. return -EBADMSG;
  259. }
  260. return 0;
  261. }
  262. static void starfive_aes_finish_req(struct starfive_cryp_ctx *ctx)
  263. {
  264. struct starfive_cryp_dev *cryp = ctx->cryp;
  265. int err = cryp->err;
  266. if (!err && cryp->authsize)
  267. err = starfive_aes_read_authtag(ctx);
  268. if (!err && ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC ||
  269. (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CTR))
  270. starfive_aes_get_iv(cryp, (void *)cryp->req.sreq->iv);
  271. if (cryp->authsize)
  272. crypto_finalize_aead_request(cryp->engine, cryp->req.areq, err);
  273. else
  274. crypto_finalize_skcipher_request(cryp->engine, cryp->req.sreq,
  275. err);
  276. }
  277. static int starfive_aes_gcm_write_adata(struct starfive_cryp_ctx *ctx)
  278. {
  279. struct starfive_cryp_dev *cryp = ctx->cryp;
  280. struct starfive_cryp_request_ctx *rctx = ctx->rctx;
  281. u32 *buffer;
  282. int total_len, loop;
  283. total_len = ALIGN(cryp->assoclen, AES_BLOCK_SIZE) / sizeof(unsigned int);
  284. buffer = (u32 *)rctx->adata;
  285. for (loop = 0; loop < total_len; loop += 4) {
  286. writel(*buffer, cryp->base + STARFIVE_AES_NONCE0);
  287. buffer++;
  288. writel(*buffer, cryp->base + STARFIVE_AES_NONCE1);
  289. buffer++;
  290. writel(*buffer, cryp->base + STARFIVE_AES_NONCE2);
  291. buffer++;
  292. writel(*buffer, cryp->base + STARFIVE_AES_NONCE3);
  293. buffer++;
  294. }
  295. if (starfive_aes_wait_gcmdone(cryp))
  296. return dev_err_probe(cryp->dev, -ETIMEDOUT,
  297. "Timeout processing gcm aad block");
  298. return 0;
  299. }
  300. static int starfive_aes_ccm_write_adata(struct starfive_cryp_ctx *ctx)
  301. {
  302. struct starfive_cryp_dev *cryp = ctx->cryp;
  303. struct starfive_cryp_request_ctx *rctx = ctx->rctx;
  304. u32 *buffer;
  305. u8 *ci;
  306. int total_len, loop;
  307. total_len = cryp->assoclen;
  308. ci = rctx->adata;
  309. writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R);
  310. ci++;
  311. writeb(*ci, cryp->base + STARFIVE_AES_AESDIO0R);
  312. ci++;
  313. total_len -= 2;
  314. buffer = (u32 *)ci;
  315. for (loop = 0; loop < 3; loop++, buffer++)
  316. writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R);
  317. total_len -= 12;
  318. while (total_len > 0) {
  319. for (loop = 0; loop < AES_BLOCK_32; loop++, buffer++)
  320. writel(*buffer, cryp->base + STARFIVE_AES_AESDIO0R);
  321. total_len -= AES_BLOCK_SIZE;
  322. }
  323. if (starfive_aes_wait_busy(cryp))
  324. return dev_err_probe(cryp->dev, -ETIMEDOUT,
  325. "Timeout processing ccm aad block");
  326. return 0;
  327. }
  328. static void starfive_aes_dma_done(void *param)
  329. {
  330. struct starfive_cryp_dev *cryp = param;
  331. complete(&cryp->dma_done);
  332. }
  333. static void starfive_aes_dma_init(struct starfive_cryp_dev *cryp)
  334. {
  335. cryp->cfg_in.direction = DMA_MEM_TO_DEV;
  336. cryp->cfg_in.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES;
  337. cryp->cfg_in.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  338. cryp->cfg_in.src_maxburst = cryp->dma_maxburst;
  339. cryp->cfg_in.dst_maxburst = cryp->dma_maxburst;
  340. cryp->cfg_in.dst_addr = cryp->phys_base + STARFIVE_ALG_FIFO_OFFSET;
  341. dmaengine_slave_config(cryp->tx, &cryp->cfg_in);
  342. cryp->cfg_out.direction = DMA_DEV_TO_MEM;
  343. cryp->cfg_out.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  344. cryp->cfg_out.dst_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES;
  345. cryp->cfg_out.src_maxburst = 4;
  346. cryp->cfg_out.dst_maxburst = 4;
  347. cryp->cfg_out.src_addr = cryp->phys_base + STARFIVE_ALG_FIFO_OFFSET;
  348. dmaengine_slave_config(cryp->rx, &cryp->cfg_out);
  349. init_completion(&cryp->dma_done);
  350. }
  351. static int starfive_aes_dma_xfer(struct starfive_cryp_dev *cryp,
  352. struct scatterlist *src,
  353. struct scatterlist *dst,
  354. int len)
  355. {
  356. struct dma_async_tx_descriptor *in_desc, *out_desc;
  357. union starfive_alg_cr alg_cr;
  358. int ret = 0, in_save, out_save;
  359. alg_cr.v = 0;
  360. alg_cr.start = 1;
  361. alg_cr.aes_dma_en = 1;
  362. writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);
  363. in_save = sg_dma_len(src);
  364. out_save = sg_dma_len(dst);
  365. writel(ALIGN(len, AES_BLOCK_SIZE), cryp->base + STARFIVE_DMA_IN_LEN_OFFSET);
  366. writel(ALIGN(len, AES_BLOCK_SIZE), cryp->base + STARFIVE_DMA_OUT_LEN_OFFSET);
  367. sg_dma_len(src) = ALIGN(len, AES_BLOCK_SIZE);
  368. sg_dma_len(dst) = ALIGN(len, AES_BLOCK_SIZE);
  369. out_desc = dmaengine_prep_slave_sg(cryp->rx, dst, 1, DMA_DEV_TO_MEM,
  370. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  371. if (!out_desc) {
  372. ret = -EINVAL;
  373. goto dma_err;
  374. }
  375. out_desc->callback = starfive_aes_dma_done;
  376. out_desc->callback_param = cryp;
  377. reinit_completion(&cryp->dma_done);
  378. dmaengine_submit(out_desc);
  379. dma_async_issue_pending(cryp->rx);
  380. in_desc = dmaengine_prep_slave_sg(cryp->tx, src, 1, DMA_MEM_TO_DEV,
  381. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  382. if (!in_desc) {
  383. ret = -EINVAL;
  384. goto dma_err;
  385. }
  386. dmaengine_submit(in_desc);
  387. dma_async_issue_pending(cryp->tx);
  388. if (!wait_for_completion_timeout(&cryp->dma_done,
  389. msecs_to_jiffies(1000)))
  390. ret = -ETIMEDOUT;
  391. dma_err:
  392. sg_dma_len(src) = in_save;
  393. sg_dma_len(dst) = out_save;
  394. alg_cr.v = 0;
  395. alg_cr.clear = 1;
  396. writel(alg_cr.v, cryp->base + STARFIVE_ALG_CR_OFFSET);
  397. return ret;
  398. }
  399. static int starfive_aes_map_sg(struct starfive_cryp_dev *cryp,
  400. struct scatterlist *src,
  401. struct scatterlist *dst)
  402. {
  403. struct scatterlist *stsg, *dtsg;
  404. struct scatterlist _src[2], _dst[2];
  405. unsigned int remain = cryp->total_in;
  406. unsigned int len, src_nents, dst_nents;
  407. int ret;
  408. if (src == dst) {
  409. for (stsg = src, dtsg = dst; remain > 0;
  410. stsg = sg_next(stsg), dtsg = sg_next(dtsg)) {
  411. src_nents = dma_map_sg(cryp->dev, stsg, 1, DMA_BIDIRECTIONAL);
  412. if (src_nents == 0)
  413. return -ENOMEM;
  414. dst_nents = src_nents;
  415. len = min(sg_dma_len(stsg), remain);
  416. ret = starfive_aes_dma_xfer(cryp, stsg, dtsg, len);
  417. dma_unmap_sg(cryp->dev, stsg, 1, DMA_BIDIRECTIONAL);
  418. if (ret)
  419. return ret;
  420. remain -= len;
  421. }
  422. } else {
  423. for (stsg = src, dtsg = dst;;) {
  424. src_nents = dma_map_sg(cryp->dev, stsg, 1, DMA_TO_DEVICE);
  425. if (src_nents == 0)
  426. return -ENOMEM;
  427. dst_nents = dma_map_sg(cryp->dev, dtsg, 1, DMA_FROM_DEVICE);
  428. if (dst_nents == 0)
  429. return -ENOMEM;
  430. len = min(sg_dma_len(stsg), sg_dma_len(dtsg));
  431. len = min(len, remain);
  432. ret = starfive_aes_dma_xfer(cryp, stsg, dtsg, len);
  433. dma_unmap_sg(cryp->dev, stsg, 1, DMA_TO_DEVICE);
  434. dma_unmap_sg(cryp->dev, dtsg, 1, DMA_FROM_DEVICE);
  435. if (ret)
  436. return ret;
  437. remain -= len;
  438. if (remain == 0)
  439. break;
  440. if (sg_dma_len(stsg) - len) {
  441. stsg = scatterwalk_ffwd(_src, stsg, len);
  442. dtsg = sg_next(dtsg);
  443. } else if (sg_dma_len(dtsg) - len) {
  444. dtsg = scatterwalk_ffwd(_dst, dtsg, len);
  445. stsg = sg_next(stsg);
  446. } else {
  447. stsg = sg_next(stsg);
  448. dtsg = sg_next(dtsg);
  449. }
  450. }
  451. }
  452. return 0;
  453. }
  454. static int starfive_aes_do_one_req(struct crypto_engine *engine, void *areq)
  455. {
  456. struct skcipher_request *req =
  457. container_of(areq, struct skcipher_request, base);
  458. struct starfive_cryp_ctx *ctx =
  459. crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  460. struct starfive_cryp_request_ctx *rctx = skcipher_request_ctx(req);
  461. struct starfive_cryp_dev *cryp = ctx->cryp;
  462. int ret;
  463. cryp->req.sreq = req;
  464. cryp->total_in = req->cryptlen;
  465. cryp->total_out = req->cryptlen;
  466. cryp->assoclen = 0;
  467. cryp->authsize = 0;
  468. rctx->in_sg = req->src;
  469. rctx->out_sg = req->dst;
  470. ctx->rctx = rctx;
  471. ret = starfive_aes_hw_init(ctx);
  472. if (ret)
  473. return ret;
  474. if (!cryp->total_in)
  475. goto finish_req;
  476. starfive_aes_dma_init(cryp);
  477. ret = starfive_aes_map_sg(cryp, rctx->in_sg, rctx->out_sg);
  478. if (ret)
  479. return ret;
  480. finish_req:
  481. starfive_aes_finish_req(ctx);
  482. return 0;
  483. }
  484. static int starfive_aes_init_tfm(struct crypto_skcipher *tfm,
  485. const char *alg_name)
  486. {
  487. struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
  488. ctx->cryp = starfive_cryp_find_dev(ctx);
  489. if (!ctx->cryp)
  490. return -ENODEV;
  491. ctx->skcipher_fbk = crypto_alloc_skcipher(alg_name, 0,
  492. CRYPTO_ALG_NEED_FALLBACK);
  493. if (IS_ERR(ctx->skcipher_fbk))
  494. return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->skcipher_fbk),
  495. "%s() failed to allocate fallback for %s\n",
  496. __func__, alg_name);
  497. crypto_skcipher_set_reqsize(tfm, sizeof(struct starfive_cryp_request_ctx) +
  498. crypto_skcipher_reqsize(ctx->skcipher_fbk));
  499. return 0;
  500. }
  501. static void starfive_aes_exit_tfm(struct crypto_skcipher *tfm)
  502. {
  503. struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
  504. crypto_free_skcipher(ctx->skcipher_fbk);
  505. }
  506. static int starfive_aes_aead_do_one_req(struct crypto_engine *engine, void *areq)
  507. {
  508. struct aead_request *req =
  509. container_of(areq, struct aead_request, base);
  510. struct starfive_cryp_ctx *ctx =
  511. crypto_aead_ctx(crypto_aead_reqtfm(req));
  512. struct starfive_cryp_dev *cryp = ctx->cryp;
  513. struct starfive_cryp_request_ctx *rctx = aead_request_ctx(req);
  514. struct scatterlist _src[2], _dst[2];
  515. int ret;
  516. cryp->req.areq = req;
  517. cryp->assoclen = req->assoclen;
  518. cryp->authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
  519. rctx->in_sg = scatterwalk_ffwd(_src, req->src, cryp->assoclen);
  520. if (req->src == req->dst)
  521. rctx->out_sg = rctx->in_sg;
  522. else
  523. rctx->out_sg = scatterwalk_ffwd(_dst, req->dst, cryp->assoclen);
  524. if (is_encrypt(cryp)) {
  525. cryp->total_in = req->cryptlen;
  526. cryp->total_out = req->cryptlen;
  527. } else {
  528. cryp->total_in = req->cryptlen - cryp->authsize;
  529. cryp->total_out = cryp->total_in;
  530. scatterwalk_map_and_copy(cryp->tag_in, req->src,
  531. cryp->total_in + cryp->assoclen,
  532. cryp->authsize, 0);
  533. }
  534. if (cryp->assoclen) {
  535. rctx->adata = kzalloc(cryp->assoclen + AES_BLOCK_SIZE, GFP_KERNEL);
  536. if (!rctx->adata)
  537. return -ENOMEM;
  538. if (sg_copy_to_buffer(req->src, sg_nents_for_len(req->src, cryp->assoclen),
  539. rctx->adata, cryp->assoclen) != cryp->assoclen) {
  540. kfree(rctx->adata);
  541. return -EINVAL;
  542. }
  543. }
  544. if (cryp->total_in)
  545. sg_zero_buffer(rctx->in_sg, sg_nents(rctx->in_sg),
  546. sg_dma_len(rctx->in_sg) - cryp->total_in,
  547. cryp->total_in);
  548. ctx->rctx = rctx;
  549. ret = starfive_aes_hw_init(ctx);
  550. if (ret) {
  551. if (cryp->assoclen)
  552. kfree(rctx->adata);
  553. return ret;
  554. }
  555. if (!cryp->assoclen)
  556. goto write_text;
  557. if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CCM)
  558. ret = starfive_aes_ccm_write_adata(ctx);
  559. else
  560. ret = starfive_aes_gcm_write_adata(ctx);
  561. kfree(rctx->adata);
  562. if (ret)
  563. return ret;
  564. write_text:
  565. if (!cryp->total_in)
  566. goto finish_req;
  567. starfive_aes_dma_init(cryp);
  568. ret = starfive_aes_map_sg(cryp, rctx->in_sg, rctx->out_sg);
  569. if (ret)
  570. return ret;
  571. finish_req:
  572. starfive_aes_finish_req(ctx);
  573. return 0;
  574. }
  575. static int starfive_aes_aead_init_tfm(struct crypto_aead *tfm,
  576. const char *alg_name)
  577. {
  578. struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  579. ctx->cryp = starfive_cryp_find_dev(ctx);
  580. if (!ctx->cryp)
  581. return -ENODEV;
  582. ctx->aead_fbk = crypto_alloc_aead(alg_name, 0,
  583. CRYPTO_ALG_NEED_FALLBACK);
  584. if (IS_ERR(ctx->aead_fbk))
  585. return dev_err_probe(ctx->cryp->dev, PTR_ERR(ctx->aead_fbk),
  586. "%s() failed to allocate fallback for %s\n",
  587. __func__, alg_name);
  588. crypto_aead_set_reqsize(tfm, sizeof(struct starfive_cryp_request_ctx) +
  589. crypto_aead_reqsize(ctx->aead_fbk));
  590. return 0;
  591. }
  592. static void starfive_aes_aead_exit_tfm(struct crypto_aead *tfm)
  593. {
  594. struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  595. crypto_free_aead(ctx->aead_fbk);
  596. }
  597. static bool starfive_aes_check_unaligned(struct starfive_cryp_dev *cryp,
  598. struct scatterlist *src,
  599. struct scatterlist *dst)
  600. {
  601. struct scatterlist *tsg;
  602. int i;
  603. for_each_sg(src, tsg, sg_nents(src), i)
  604. if (!IS_ALIGNED(tsg->offset, sizeof(u32)) ||
  605. (!IS_ALIGNED(tsg->length, AES_BLOCK_SIZE) &&
  606. !sg_is_last(tsg)))
  607. return true;
  608. if (src != dst)
  609. for_each_sg(dst, tsg, sg_nents(dst), i)
  610. if (!IS_ALIGNED(tsg->offset, sizeof(u32)) ||
  611. (!IS_ALIGNED(tsg->length, AES_BLOCK_SIZE) &&
  612. !sg_is_last(tsg)))
  613. return true;
  614. return false;
  615. }
  616. static int starfive_aes_do_fallback(struct skcipher_request *req, bool enc)
  617. {
  618. struct starfive_cryp_ctx *ctx =
  619. crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  620. struct skcipher_request *subreq = skcipher_request_ctx(req);
  621. skcipher_request_set_tfm(subreq, ctx->skcipher_fbk);
  622. skcipher_request_set_callback(subreq, req->base.flags,
  623. req->base.complete,
  624. req->base.data);
  625. skcipher_request_set_crypt(subreq, req->src, req->dst,
  626. req->cryptlen, req->iv);
  627. return enc ? crypto_skcipher_encrypt(subreq) :
  628. crypto_skcipher_decrypt(subreq);
  629. }
  630. static int starfive_aes_crypt(struct skcipher_request *req, unsigned long flags)
  631. {
  632. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  633. struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
  634. struct starfive_cryp_dev *cryp = ctx->cryp;
  635. unsigned int blocksize_align = crypto_skcipher_blocksize(tfm) - 1;
  636. cryp->flags = flags;
  637. if ((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_ECB ||
  638. (cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CBC)
  639. if (req->cryptlen & blocksize_align)
  640. return -EINVAL;
  641. if (starfive_aes_check_unaligned(cryp, req->src, req->dst))
  642. return starfive_aes_do_fallback(req, is_encrypt(cryp));
  643. return crypto_transfer_skcipher_request_to_engine(cryp->engine, req);
  644. }
  645. static int starfive_aes_aead_do_fallback(struct aead_request *req, bool enc)
  646. {
  647. struct starfive_cryp_ctx *ctx =
  648. crypto_aead_ctx(crypto_aead_reqtfm(req));
  649. struct aead_request *subreq = aead_request_ctx(req);
  650. aead_request_set_tfm(subreq, ctx->aead_fbk);
  651. aead_request_set_callback(subreq, req->base.flags,
  652. req->base.complete,
  653. req->base.data);
  654. aead_request_set_crypt(subreq, req->src, req->dst,
  655. req->cryptlen, req->iv);
  656. aead_request_set_ad(subreq, req->assoclen);
  657. return enc ? crypto_aead_encrypt(subreq) :
  658. crypto_aead_decrypt(subreq);
  659. }
  660. static int starfive_aes_aead_crypt(struct aead_request *req, unsigned long flags)
  661. {
  662. struct starfive_cryp_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  663. struct starfive_cryp_dev *cryp = ctx->cryp;
  664. struct scatterlist *src, *dst, _src[2], _dst[2];
  665. cryp->flags = flags;
  666. /* aes-ccm does not support tag verification for non-aligned text,
  667. * use fallback for ccm decryption instead.
  668. */
  669. if (((cryp->flags & FLG_MODE_MASK) == STARFIVE_AES_MODE_CCM) &&
  670. !is_encrypt(cryp))
  671. return starfive_aes_aead_do_fallback(req, 0);
  672. src = scatterwalk_ffwd(_src, req->src, req->assoclen);
  673. if (req->src == req->dst)
  674. dst = src;
  675. else
  676. dst = scatterwalk_ffwd(_dst, req->dst, req->assoclen);
  677. if (starfive_aes_check_unaligned(cryp, src, dst))
  678. return starfive_aes_aead_do_fallback(req, is_encrypt(cryp));
  679. return crypto_transfer_aead_request_to_engine(cryp->engine, req);
  680. }
  681. static int starfive_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  682. unsigned int keylen)
  683. {
  684. struct starfive_cryp_ctx *ctx = crypto_skcipher_ctx(tfm);
  685. if (!key || !keylen)
  686. return -EINVAL;
  687. if (keylen != AES_KEYSIZE_128 &&
  688. keylen != AES_KEYSIZE_192 &&
  689. keylen != AES_KEYSIZE_256)
  690. return -EINVAL;
  691. memcpy(ctx->key, key, keylen);
  692. ctx->keylen = keylen;
  693. return crypto_skcipher_setkey(ctx->skcipher_fbk, key, keylen);
  694. }
  695. static int starfive_aes_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  696. unsigned int keylen)
  697. {
  698. struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  699. if (!key || !keylen)
  700. return -EINVAL;
  701. if (keylen != AES_KEYSIZE_128 &&
  702. keylen != AES_KEYSIZE_192 &&
  703. keylen != AES_KEYSIZE_256)
  704. return -EINVAL;
  705. memcpy(ctx->key, key, keylen);
  706. ctx->keylen = keylen;
  707. return crypto_aead_setkey(ctx->aead_fbk, key, keylen);
  708. }
  709. static int starfive_aes_gcm_setauthsize(struct crypto_aead *tfm,
  710. unsigned int authsize)
  711. {
  712. struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  713. int ret;
  714. ret = crypto_gcm_check_authsize(authsize);
  715. if (ret)
  716. return ret;
  717. return crypto_aead_setauthsize(ctx->aead_fbk, authsize);
  718. }
  719. static int starfive_aes_ccm_setauthsize(struct crypto_aead *tfm,
  720. unsigned int authsize)
  721. {
  722. struct starfive_cryp_ctx *ctx = crypto_aead_ctx(tfm);
  723. switch (authsize) {
  724. case 4:
  725. case 6:
  726. case 8:
  727. case 10:
  728. case 12:
  729. case 14:
  730. case 16:
  731. break;
  732. default:
  733. return -EINVAL;
  734. }
  735. return crypto_aead_setauthsize(ctx->aead_fbk, authsize);
  736. }
  737. static int starfive_aes_ecb_encrypt(struct skcipher_request *req)
  738. {
  739. return starfive_aes_crypt(req, STARFIVE_AES_MODE_ECB | FLG_ENCRYPT);
  740. }
  741. static int starfive_aes_ecb_decrypt(struct skcipher_request *req)
  742. {
  743. return starfive_aes_crypt(req, STARFIVE_AES_MODE_ECB);
  744. }
  745. static int starfive_aes_cbc_encrypt(struct skcipher_request *req)
  746. {
  747. return starfive_aes_crypt(req, STARFIVE_AES_MODE_CBC | FLG_ENCRYPT);
  748. }
  749. static int starfive_aes_cbc_decrypt(struct skcipher_request *req)
  750. {
  751. return starfive_aes_crypt(req, STARFIVE_AES_MODE_CBC);
  752. }
  753. static int starfive_aes_ctr_encrypt(struct skcipher_request *req)
  754. {
  755. return starfive_aes_crypt(req, STARFIVE_AES_MODE_CTR | FLG_ENCRYPT);
  756. }
  757. static int starfive_aes_ctr_decrypt(struct skcipher_request *req)
  758. {
  759. return starfive_aes_crypt(req, STARFIVE_AES_MODE_CTR);
  760. }
  761. static int starfive_aes_gcm_encrypt(struct aead_request *req)
  762. {
  763. return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_GCM | FLG_ENCRYPT);
  764. }
  765. static int starfive_aes_gcm_decrypt(struct aead_request *req)
  766. {
  767. return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_GCM);
  768. }
  769. static int starfive_aes_ccm_encrypt(struct aead_request *req)
  770. {
  771. int ret;
  772. ret = starfive_aes_ccm_check_iv(req->iv);
  773. if (ret)
  774. return ret;
  775. return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_CCM | FLG_ENCRYPT);
  776. }
  777. static int starfive_aes_ccm_decrypt(struct aead_request *req)
  778. {
  779. int ret;
  780. ret = starfive_aes_ccm_check_iv(req->iv);
  781. if (ret)
  782. return ret;
  783. return starfive_aes_aead_crypt(req, STARFIVE_AES_MODE_CCM);
  784. }
  785. static int starfive_aes_ecb_init_tfm(struct crypto_skcipher *tfm)
  786. {
  787. return starfive_aes_init_tfm(tfm, "ecb(aes-lib)");
  788. }
  789. static int starfive_aes_cbc_init_tfm(struct crypto_skcipher *tfm)
  790. {
  791. return starfive_aes_init_tfm(tfm, "cbc(aes-lib)");
  792. }
  793. static int starfive_aes_ctr_init_tfm(struct crypto_skcipher *tfm)
  794. {
  795. return starfive_aes_init_tfm(tfm, "ctr(aes-lib)");
  796. }
  797. static int starfive_aes_ccm_init_tfm(struct crypto_aead *tfm)
  798. {
  799. return starfive_aes_aead_init_tfm(tfm, "ccm_base(ctr(aes-lib),cbcmac(aes-lib))");
  800. }
  801. static int starfive_aes_gcm_init_tfm(struct crypto_aead *tfm)
  802. {
  803. return starfive_aes_aead_init_tfm(tfm, "gcm_base(ctr(aes-lib),ghash-generic)");
  804. }
  805. static struct skcipher_engine_alg skcipher_algs[] = {
  806. {
  807. .base.init = starfive_aes_ecb_init_tfm,
  808. .base.exit = starfive_aes_exit_tfm,
  809. .base.setkey = starfive_aes_setkey,
  810. .base.encrypt = starfive_aes_ecb_encrypt,
  811. .base.decrypt = starfive_aes_ecb_decrypt,
  812. .base.min_keysize = AES_MIN_KEY_SIZE,
  813. .base.max_keysize = AES_MAX_KEY_SIZE,
  814. .base.base = {
  815. .cra_name = "ecb(aes)",
  816. .cra_driver_name = "starfive-ecb-aes",
  817. .cra_priority = 200,
  818. .cra_flags = CRYPTO_ALG_ASYNC |
  819. CRYPTO_ALG_NEED_FALLBACK,
  820. .cra_blocksize = AES_BLOCK_SIZE,
  821. .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
  822. .cra_alignmask = 0xf,
  823. .cra_module = THIS_MODULE,
  824. },
  825. .op = {
  826. .do_one_request = starfive_aes_do_one_req,
  827. },
  828. }, {
  829. .base.init = starfive_aes_cbc_init_tfm,
  830. .base.exit = starfive_aes_exit_tfm,
  831. .base.setkey = starfive_aes_setkey,
  832. .base.encrypt = starfive_aes_cbc_encrypt,
  833. .base.decrypt = starfive_aes_cbc_decrypt,
  834. .base.min_keysize = AES_MIN_KEY_SIZE,
  835. .base.max_keysize = AES_MAX_KEY_SIZE,
  836. .base.ivsize = AES_BLOCK_SIZE,
  837. .base.base = {
  838. .cra_name = "cbc(aes)",
  839. .cra_driver_name = "starfive-cbc-aes",
  840. .cra_priority = 200,
  841. .cra_flags = CRYPTO_ALG_ASYNC |
  842. CRYPTO_ALG_NEED_FALLBACK,
  843. .cra_blocksize = AES_BLOCK_SIZE,
  844. .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
  845. .cra_alignmask = 0xf,
  846. .cra_module = THIS_MODULE,
  847. },
  848. .op = {
  849. .do_one_request = starfive_aes_do_one_req,
  850. },
  851. }, {
  852. .base.init = starfive_aes_ctr_init_tfm,
  853. .base.exit = starfive_aes_exit_tfm,
  854. .base.setkey = starfive_aes_setkey,
  855. .base.encrypt = starfive_aes_ctr_encrypt,
  856. .base.decrypt = starfive_aes_ctr_decrypt,
  857. .base.min_keysize = AES_MIN_KEY_SIZE,
  858. .base.max_keysize = AES_MAX_KEY_SIZE,
  859. .base.ivsize = AES_BLOCK_SIZE,
  860. .base.base = {
  861. .cra_name = "ctr(aes)",
  862. .cra_driver_name = "starfive-ctr-aes",
  863. .cra_priority = 200,
  864. .cra_flags = CRYPTO_ALG_ASYNC |
  865. CRYPTO_ALG_NEED_FALLBACK,
  866. .cra_blocksize = 1,
  867. .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
  868. .cra_alignmask = 0xf,
  869. .cra_module = THIS_MODULE,
  870. },
  871. .op = {
  872. .do_one_request = starfive_aes_do_one_req,
  873. },
  874. },
  875. };
  876. static struct aead_engine_alg aead_algs[] = {
  877. {
  878. .base.setkey = starfive_aes_aead_setkey,
  879. .base.setauthsize = starfive_aes_gcm_setauthsize,
  880. .base.encrypt = starfive_aes_gcm_encrypt,
  881. .base.decrypt = starfive_aes_gcm_decrypt,
  882. .base.init = starfive_aes_gcm_init_tfm,
  883. .base.exit = starfive_aes_aead_exit_tfm,
  884. .base.ivsize = GCM_AES_IV_SIZE,
  885. .base.maxauthsize = AES_BLOCK_SIZE,
  886. .base.base = {
  887. .cra_name = "gcm(aes)",
  888. .cra_driver_name = "starfive-gcm-aes",
  889. .cra_priority = 200,
  890. .cra_flags = CRYPTO_ALG_ASYNC |
  891. CRYPTO_ALG_NEED_FALLBACK,
  892. .cra_blocksize = 1,
  893. .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
  894. .cra_alignmask = 0xf,
  895. .cra_module = THIS_MODULE,
  896. },
  897. .op = {
  898. .do_one_request = starfive_aes_aead_do_one_req,
  899. },
  900. }, {
  901. .base.setkey = starfive_aes_aead_setkey,
  902. .base.setauthsize = starfive_aes_ccm_setauthsize,
  903. .base.encrypt = starfive_aes_ccm_encrypt,
  904. .base.decrypt = starfive_aes_ccm_decrypt,
  905. .base.init = starfive_aes_ccm_init_tfm,
  906. .base.exit = starfive_aes_aead_exit_tfm,
  907. .base.ivsize = AES_BLOCK_SIZE,
  908. .base.maxauthsize = AES_BLOCK_SIZE,
  909. .base.base = {
  910. .cra_name = "ccm(aes)",
  911. .cra_driver_name = "starfive-ccm-aes",
  912. .cra_priority = 200,
  913. .cra_flags = CRYPTO_ALG_ASYNC |
  914. CRYPTO_ALG_NEED_FALLBACK,
  915. .cra_blocksize = 1,
  916. .cra_ctxsize = sizeof(struct starfive_cryp_ctx),
  917. .cra_alignmask = 0xf,
  918. .cra_module = THIS_MODULE,
  919. },
  920. .op = {
  921. .do_one_request = starfive_aes_aead_do_one_req,
  922. },
  923. },
  924. };
  925. int starfive_aes_register_algs(void)
  926. {
  927. int ret;
  928. ret = crypto_engine_register_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
  929. if (ret)
  930. return ret;
  931. ret = crypto_engine_register_aeads(aead_algs, ARRAY_SIZE(aead_algs));
  932. if (ret)
  933. crypto_engine_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
  934. return ret;
  935. }
  936. void starfive_aes_unregister_algs(void)
  937. {
  938. crypto_engine_unregister_aeads(aead_algs, ARRAY_SIZE(aead_algs));
  939. crypto_engine_unregister_skciphers(skcipher_algs, ARRAY_SIZE(skcipher_algs));
  940. }