cc_cipher.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <crypto/algapi.h>
  6. #include <crypto/internal/skcipher.h>
  7. #include <crypto/des.h>
  8. #include <crypto/xts.h>
  9. #include <crypto/scatterwalk.h>
  10. #include "cc_driver.h"
  11. #include "cc_lli_defs.h"
  12. #include "cc_buffer_mgr.h"
  13. #include "cc_cipher.h"
  14. #include "cc_request_mgr.h"
  15. #define MAX_ABLKCIPHER_SEQ_LEN 6
  16. #define template_skcipher template_u.skcipher
  17. struct cc_cipher_handle {
  18. struct list_head alg_list;
  19. };
  20. struct cc_user_key_info {
  21. u8 *key;
  22. dma_addr_t key_dma_addr;
  23. };
  24. struct cc_hw_key_info {
  25. enum cc_hw_crypto_key key1_slot;
  26. enum cc_hw_crypto_key key2_slot;
  27. };
  28. struct cc_cipher_ctx {
  29. struct cc_drvdata *drvdata;
  30. int keylen;
  31. int key_round_number;
  32. int cipher_mode;
  33. int flow_mode;
  34. unsigned int flags;
  35. bool hw_key;
  36. struct cc_user_key_info user;
  37. struct cc_hw_key_info hw;
  38. struct crypto_shash *shash_tfm;
  39. };
  40. static void cc_cipher_complete(struct device *dev, void *cc_req, int err);
  41. static inline bool cc_is_hw_key(struct crypto_tfm *tfm)
  42. {
  43. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  44. return ctx_p->hw_key;
  45. }
  46. static int validate_keys_sizes(struct cc_cipher_ctx *ctx_p, u32 size)
  47. {
  48. switch (ctx_p->flow_mode) {
  49. case S_DIN_to_AES:
  50. switch (size) {
  51. case CC_AES_128_BIT_KEY_SIZE:
  52. case CC_AES_192_BIT_KEY_SIZE:
  53. if (ctx_p->cipher_mode != DRV_CIPHER_XTS &&
  54. ctx_p->cipher_mode != DRV_CIPHER_ESSIV &&
  55. ctx_p->cipher_mode != DRV_CIPHER_BITLOCKER)
  56. return 0;
  57. break;
  58. case CC_AES_256_BIT_KEY_SIZE:
  59. return 0;
  60. case (CC_AES_192_BIT_KEY_SIZE * 2):
  61. case (CC_AES_256_BIT_KEY_SIZE * 2):
  62. if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
  63. ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
  64. ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)
  65. return 0;
  66. break;
  67. default:
  68. break;
  69. }
  70. break;
  71. case S_DIN_to_DES:
  72. if (size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE)
  73. return 0;
  74. break;
  75. default:
  76. break;
  77. }
  78. return -EINVAL;
  79. }
  80. static int validate_data_size(struct cc_cipher_ctx *ctx_p,
  81. unsigned int size)
  82. {
  83. switch (ctx_p->flow_mode) {
  84. case S_DIN_to_AES:
  85. switch (ctx_p->cipher_mode) {
  86. case DRV_CIPHER_XTS:
  87. if (size >= AES_BLOCK_SIZE &&
  88. IS_ALIGNED(size, AES_BLOCK_SIZE))
  89. return 0;
  90. break;
  91. case DRV_CIPHER_CBC_CTS:
  92. if (size >= AES_BLOCK_SIZE)
  93. return 0;
  94. break;
  95. case DRV_CIPHER_OFB:
  96. case DRV_CIPHER_CTR:
  97. return 0;
  98. case DRV_CIPHER_ECB:
  99. case DRV_CIPHER_CBC:
  100. case DRV_CIPHER_ESSIV:
  101. case DRV_CIPHER_BITLOCKER:
  102. if (IS_ALIGNED(size, AES_BLOCK_SIZE))
  103. return 0;
  104. break;
  105. default:
  106. break;
  107. }
  108. break;
  109. case S_DIN_to_DES:
  110. if (IS_ALIGNED(size, DES_BLOCK_SIZE))
  111. return 0;
  112. break;
  113. default:
  114. break;
  115. }
  116. return -EINVAL;
  117. }
  118. static int cc_cipher_init(struct crypto_tfm *tfm)
  119. {
  120. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  121. struct cc_crypto_alg *cc_alg =
  122. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  123. skcipher_alg.base);
  124. struct device *dev = drvdata_to_dev(cc_alg->drvdata);
  125. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  126. dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
  127. crypto_tfm_alg_name(tfm));
  128. crypto_skcipher_set_reqsize(__crypto_skcipher_cast(tfm),
  129. sizeof(struct cipher_req_ctx));
  130. ctx_p->cipher_mode = cc_alg->cipher_mode;
  131. ctx_p->flow_mode = cc_alg->flow_mode;
  132. ctx_p->drvdata = cc_alg->drvdata;
  133. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  134. /* Alloc hash tfm for essiv */
  135. ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
  136. if (IS_ERR(ctx_p->shash_tfm)) {
  137. dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
  138. return PTR_ERR(ctx_p->shash_tfm);
  139. }
  140. }
  141. /* Allocate key buffer, cache line aligned */
  142. ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL);
  143. if (!ctx_p->user.key)
  144. goto free_shash;
  145. dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
  146. ctx_p->user.key);
  147. /* Map key buffer */
  148. ctx_p->user.key_dma_addr = dma_map_single(dev, (void *)ctx_p->user.key,
  149. max_key_buf_size,
  150. DMA_TO_DEVICE);
  151. if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
  152. dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
  153. max_key_buf_size, ctx_p->user.key);
  154. goto free_key;
  155. }
  156. dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
  157. max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
  158. return 0;
  159. free_key:
  160. kfree(ctx_p->user.key);
  161. free_shash:
  162. crypto_free_shash(ctx_p->shash_tfm);
  163. return -ENOMEM;
  164. }
  165. static void cc_cipher_exit(struct crypto_tfm *tfm)
  166. {
  167. struct crypto_alg *alg = tfm->__crt_alg;
  168. struct cc_crypto_alg *cc_alg =
  169. container_of(alg, struct cc_crypto_alg,
  170. skcipher_alg.base);
  171. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  172. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  173. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  174. dev_dbg(dev, "Clearing context @%p for %s\n",
  175. crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
  176. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  177. /* Free hash tfm for essiv */
  178. crypto_free_shash(ctx_p->shash_tfm);
  179. ctx_p->shash_tfm = NULL;
  180. }
  181. /* Unmap key buffer */
  182. dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
  183. DMA_TO_DEVICE);
  184. dev_dbg(dev, "Unmapped key buffer key_dma_addr=%pad\n",
  185. &ctx_p->user.key_dma_addr);
  186. /* Free key buffer in context */
  187. kzfree(ctx_p->user.key);
  188. dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
  189. }
  190. struct tdes_keys {
  191. u8 key1[DES_KEY_SIZE];
  192. u8 key2[DES_KEY_SIZE];
  193. u8 key3[DES_KEY_SIZE];
  194. };
  195. static enum cc_hw_crypto_key cc_slot_to_hw_key(int slot_num)
  196. {
  197. switch (slot_num) {
  198. case 0:
  199. return KFDE0_KEY;
  200. case 1:
  201. return KFDE1_KEY;
  202. case 2:
  203. return KFDE2_KEY;
  204. case 3:
  205. return KFDE3_KEY;
  206. }
  207. return END_OF_KEYS;
  208. }
  209. static int cc_cipher_sethkey(struct crypto_skcipher *sktfm, const u8 *key,
  210. unsigned int keylen)
  211. {
  212. struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
  213. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  214. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  215. struct cc_hkey_info hki;
  216. dev_dbg(dev, "Setting HW key in context @%p for %s. keylen=%u\n",
  217. ctx_p, crypto_tfm_alg_name(tfm), keylen);
  218. dump_byte_array("key", (u8 *)key, keylen);
  219. /* STAT_PHASE_0: Init and sanity checks */
  220. /* This check the size of the hardware key token */
  221. if (keylen != sizeof(hki)) {
  222. dev_err(dev, "Unsupported HW key size %d.\n", keylen);
  223. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  224. return -EINVAL;
  225. }
  226. if (ctx_p->flow_mode != S_DIN_to_AES) {
  227. dev_err(dev, "HW key not supported for non-AES flows\n");
  228. return -EINVAL;
  229. }
  230. memcpy(&hki, key, keylen);
  231. /* The real key len for crypto op is the size of the HW key
  232. * referenced by the HW key slot, not the hardware key token
  233. */
  234. keylen = hki.keylen;
  235. if (validate_keys_sizes(ctx_p, keylen)) {
  236. dev_err(dev, "Unsupported key size %d.\n", keylen);
  237. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  238. return -EINVAL;
  239. }
  240. ctx_p->hw.key1_slot = cc_slot_to_hw_key(hki.hw_key1);
  241. if (ctx_p->hw.key1_slot == END_OF_KEYS) {
  242. dev_err(dev, "Unsupported hw key1 number (%d)\n", hki.hw_key1);
  243. return -EINVAL;
  244. }
  245. if (ctx_p->cipher_mode == DRV_CIPHER_XTS ||
  246. ctx_p->cipher_mode == DRV_CIPHER_ESSIV ||
  247. ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER) {
  248. if (hki.hw_key1 == hki.hw_key2) {
  249. dev_err(dev, "Illegal hw key numbers (%d,%d)\n",
  250. hki.hw_key1, hki.hw_key2);
  251. return -EINVAL;
  252. }
  253. ctx_p->hw.key2_slot = cc_slot_to_hw_key(hki.hw_key2);
  254. if (ctx_p->hw.key2_slot == END_OF_KEYS) {
  255. dev_err(dev, "Unsupported hw key2 number (%d)\n",
  256. hki.hw_key2);
  257. return -EINVAL;
  258. }
  259. }
  260. ctx_p->keylen = keylen;
  261. ctx_p->hw_key = true;
  262. dev_dbg(dev, "cc_is_hw_key ret 0");
  263. return 0;
  264. }
  265. static int cc_cipher_setkey(struct crypto_skcipher *sktfm, const u8 *key,
  266. unsigned int keylen)
  267. {
  268. struct crypto_tfm *tfm = crypto_skcipher_tfm(sktfm);
  269. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  270. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  271. struct cc_crypto_alg *cc_alg =
  272. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  273. skcipher_alg.base);
  274. unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
  275. dev_dbg(dev, "Setting key in context @%p for %s. keylen=%u\n",
  276. ctx_p, crypto_tfm_alg_name(tfm), keylen);
  277. dump_byte_array("key", (u8 *)key, keylen);
  278. /* STAT_PHASE_0: Init and sanity checks */
  279. if (validate_keys_sizes(ctx_p, keylen)) {
  280. dev_err(dev, "Unsupported key size %d.\n", keylen);
  281. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  282. return -EINVAL;
  283. }
  284. ctx_p->hw_key = false;
  285. /*
  286. * Verify DES weak keys
  287. * Note that we're dropping the expanded key since the
  288. * HW does the expansion on its own.
  289. */
  290. if (ctx_p->flow_mode == S_DIN_to_DES) {
  291. u32 tmp[DES3_EDE_EXPKEY_WORDS];
  292. if (keylen == DES3_EDE_KEY_SIZE &&
  293. __des3_ede_setkey(tmp, &tfm->crt_flags, key,
  294. DES3_EDE_KEY_SIZE)) {
  295. dev_dbg(dev, "weak 3DES key");
  296. return -EINVAL;
  297. } else if (!des_ekey(tmp, key) &&
  298. (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_WEAK_KEY)) {
  299. tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
  300. dev_dbg(dev, "weak DES key");
  301. return -EINVAL;
  302. }
  303. }
  304. if (ctx_p->cipher_mode == DRV_CIPHER_XTS &&
  305. xts_check_key(tfm, key, keylen)) {
  306. dev_dbg(dev, "weak XTS key");
  307. return -EINVAL;
  308. }
  309. /* STAT_PHASE_1: Copy key to ctx */
  310. dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
  311. max_key_buf_size, DMA_TO_DEVICE);
  312. memcpy(ctx_p->user.key, key, keylen);
  313. if (keylen == 24)
  314. memset(ctx_p->user.key + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
  315. if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
  316. /* sha256 for key2 - use sw implementation */
  317. int key_len = keylen >> 1;
  318. int err;
  319. SHASH_DESC_ON_STACK(desc, ctx_p->shash_tfm);
  320. desc->tfm = ctx_p->shash_tfm;
  321. err = crypto_shash_digest(desc, ctx_p->user.key, key_len,
  322. ctx_p->user.key + key_len);
  323. if (err) {
  324. dev_err(dev, "Failed to hash ESSIV key.\n");
  325. return err;
  326. }
  327. }
  328. dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
  329. max_key_buf_size, DMA_TO_DEVICE);
  330. ctx_p->keylen = keylen;
  331. dev_dbg(dev, "return safely");
  332. return 0;
  333. }
  334. static void cc_setup_cipher_desc(struct crypto_tfm *tfm,
  335. struct cipher_req_ctx *req_ctx,
  336. unsigned int ivsize, unsigned int nbytes,
  337. struct cc_hw_desc desc[],
  338. unsigned int *seq_size)
  339. {
  340. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  341. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  342. int cipher_mode = ctx_p->cipher_mode;
  343. int flow_mode = ctx_p->flow_mode;
  344. int direction = req_ctx->gen_ctx.op_type;
  345. dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
  346. unsigned int key_len = ctx_p->keylen;
  347. dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
  348. unsigned int du_size = nbytes;
  349. struct cc_crypto_alg *cc_alg =
  350. container_of(tfm->__crt_alg, struct cc_crypto_alg,
  351. skcipher_alg.base);
  352. if (cc_alg->data_unit)
  353. du_size = cc_alg->data_unit;
  354. switch (cipher_mode) {
  355. case DRV_CIPHER_CBC:
  356. case DRV_CIPHER_CBC_CTS:
  357. case DRV_CIPHER_CTR:
  358. case DRV_CIPHER_OFB:
  359. /* Load cipher state */
  360. hw_desc_init(&desc[*seq_size]);
  361. set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
  362. NS_BIT);
  363. set_cipher_config0(&desc[*seq_size], direction);
  364. set_flow_mode(&desc[*seq_size], flow_mode);
  365. set_cipher_mode(&desc[*seq_size], cipher_mode);
  366. if (cipher_mode == DRV_CIPHER_CTR ||
  367. cipher_mode == DRV_CIPHER_OFB) {
  368. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
  369. } else {
  370. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
  371. }
  372. (*seq_size)++;
  373. /*FALLTHROUGH*/
  374. case DRV_CIPHER_ECB:
  375. /* Load key */
  376. hw_desc_init(&desc[*seq_size]);
  377. set_cipher_mode(&desc[*seq_size], cipher_mode);
  378. set_cipher_config0(&desc[*seq_size], direction);
  379. if (flow_mode == S_DIN_to_AES) {
  380. if (cc_is_hw_key(tfm)) {
  381. set_hw_crypto_key(&desc[*seq_size],
  382. ctx_p->hw.key1_slot);
  383. } else {
  384. set_din_type(&desc[*seq_size], DMA_DLLI,
  385. key_dma_addr, ((key_len == 24) ?
  386. AES_MAX_KEY_SIZE :
  387. key_len), NS_BIT);
  388. }
  389. set_key_size_aes(&desc[*seq_size], key_len);
  390. } else {
  391. /*des*/
  392. set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
  393. key_len, NS_BIT);
  394. set_key_size_des(&desc[*seq_size], key_len);
  395. }
  396. set_flow_mode(&desc[*seq_size], flow_mode);
  397. set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
  398. (*seq_size)++;
  399. break;
  400. case DRV_CIPHER_XTS:
  401. case DRV_CIPHER_ESSIV:
  402. case DRV_CIPHER_BITLOCKER:
  403. /* Load AES key */
  404. hw_desc_init(&desc[*seq_size]);
  405. set_cipher_mode(&desc[*seq_size], cipher_mode);
  406. set_cipher_config0(&desc[*seq_size], direction);
  407. if (cc_is_hw_key(tfm)) {
  408. set_hw_crypto_key(&desc[*seq_size],
  409. ctx_p->hw.key1_slot);
  410. } else {
  411. set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
  412. (key_len / 2), NS_BIT);
  413. }
  414. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  415. set_flow_mode(&desc[*seq_size], flow_mode);
  416. set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
  417. (*seq_size)++;
  418. /* load XEX key */
  419. hw_desc_init(&desc[*seq_size]);
  420. set_cipher_mode(&desc[*seq_size], cipher_mode);
  421. set_cipher_config0(&desc[*seq_size], direction);
  422. if (cc_is_hw_key(tfm)) {
  423. set_hw_crypto_key(&desc[*seq_size],
  424. ctx_p->hw.key2_slot);
  425. } else {
  426. set_din_type(&desc[*seq_size], DMA_DLLI,
  427. (key_dma_addr + (key_len / 2)),
  428. (key_len / 2), NS_BIT);
  429. }
  430. set_xex_data_unit_size(&desc[*seq_size], du_size);
  431. set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
  432. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  433. set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
  434. (*seq_size)++;
  435. /* Set state */
  436. hw_desc_init(&desc[*seq_size]);
  437. set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
  438. set_cipher_mode(&desc[*seq_size], cipher_mode);
  439. set_cipher_config0(&desc[*seq_size], direction);
  440. set_key_size_aes(&desc[*seq_size], (key_len / 2));
  441. set_flow_mode(&desc[*seq_size], flow_mode);
  442. set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
  443. CC_AES_BLOCK_SIZE, NS_BIT);
  444. (*seq_size)++;
  445. break;
  446. default:
  447. dev_err(dev, "Unsupported cipher mode (%d)\n", cipher_mode);
  448. }
  449. }
  450. static void cc_setup_cipher_data(struct crypto_tfm *tfm,
  451. struct cipher_req_ctx *req_ctx,
  452. struct scatterlist *dst,
  453. struct scatterlist *src, unsigned int nbytes,
  454. void *areq, struct cc_hw_desc desc[],
  455. unsigned int *seq_size)
  456. {
  457. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  458. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  459. unsigned int flow_mode = ctx_p->flow_mode;
  460. switch (ctx_p->flow_mode) {
  461. case S_DIN_to_AES:
  462. flow_mode = DIN_AES_DOUT;
  463. break;
  464. case S_DIN_to_DES:
  465. flow_mode = DIN_DES_DOUT;
  466. break;
  467. default:
  468. dev_err(dev, "invalid flow mode, flow_mode = %d\n", flow_mode);
  469. return;
  470. }
  471. /* Process */
  472. if (req_ctx->dma_buf_type == CC_DMA_BUF_DLLI) {
  473. dev_dbg(dev, " data params addr %pad length 0x%X\n",
  474. &sg_dma_address(src), nbytes);
  475. dev_dbg(dev, " data params addr %pad length 0x%X\n",
  476. &sg_dma_address(dst), nbytes);
  477. hw_desc_init(&desc[*seq_size]);
  478. set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
  479. nbytes, NS_BIT);
  480. set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
  481. nbytes, NS_BIT, (!areq ? 0 : 1));
  482. if (areq)
  483. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  484. set_flow_mode(&desc[*seq_size], flow_mode);
  485. (*seq_size)++;
  486. } else {
  487. /* bypass */
  488. dev_dbg(dev, " bypass params addr %pad length 0x%X addr 0x%08X\n",
  489. &req_ctx->mlli_params.mlli_dma_addr,
  490. req_ctx->mlli_params.mlli_len,
  491. (unsigned int)ctx_p->drvdata->mlli_sram_addr);
  492. hw_desc_init(&desc[*seq_size]);
  493. set_din_type(&desc[*seq_size], DMA_DLLI,
  494. req_ctx->mlli_params.mlli_dma_addr,
  495. req_ctx->mlli_params.mlli_len, NS_BIT);
  496. set_dout_sram(&desc[*seq_size],
  497. ctx_p->drvdata->mlli_sram_addr,
  498. req_ctx->mlli_params.mlli_len);
  499. set_flow_mode(&desc[*seq_size], BYPASS);
  500. (*seq_size)++;
  501. hw_desc_init(&desc[*seq_size]);
  502. set_din_type(&desc[*seq_size], DMA_MLLI,
  503. ctx_p->drvdata->mlli_sram_addr,
  504. req_ctx->in_mlli_nents, NS_BIT);
  505. if (req_ctx->out_nents == 0) {
  506. dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
  507. (unsigned int)ctx_p->drvdata->mlli_sram_addr,
  508. (unsigned int)ctx_p->drvdata->mlli_sram_addr);
  509. set_dout_mlli(&desc[*seq_size],
  510. ctx_p->drvdata->mlli_sram_addr,
  511. req_ctx->in_mlli_nents, NS_BIT,
  512. (!areq ? 0 : 1));
  513. } else {
  514. dev_dbg(dev, " din/dout params addr 0x%08X addr 0x%08X\n",
  515. (unsigned int)ctx_p->drvdata->mlli_sram_addr,
  516. (unsigned int)ctx_p->drvdata->mlli_sram_addr +
  517. (u32)LLI_ENTRY_BYTE_SIZE * req_ctx->in_nents);
  518. set_dout_mlli(&desc[*seq_size],
  519. (ctx_p->drvdata->mlli_sram_addr +
  520. (LLI_ENTRY_BYTE_SIZE *
  521. req_ctx->in_mlli_nents)),
  522. req_ctx->out_mlli_nents, NS_BIT,
  523. (!areq ? 0 : 1));
  524. }
  525. if (areq)
  526. set_queue_last_ind(ctx_p->drvdata, &desc[*seq_size]);
  527. set_flow_mode(&desc[*seq_size], flow_mode);
  528. (*seq_size)++;
  529. }
  530. }
  531. /*
  532. * Update a CTR-AES 128 bit counter
  533. */
  534. static void cc_update_ctr(u8 *ctr, unsigned int increment)
  535. {
  536. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
  537. IS_ALIGNED((unsigned long)ctr, 8)) {
  538. __be64 *high_be = (__be64 *)ctr;
  539. __be64 *low_be = high_be + 1;
  540. u64 orig_low = __be64_to_cpu(*low_be);
  541. u64 new_low = orig_low + (u64)increment;
  542. *low_be = __cpu_to_be64(new_low);
  543. if (new_low < orig_low)
  544. *high_be = __cpu_to_be64(__be64_to_cpu(*high_be) + 1);
  545. } else {
  546. u8 *pos = (ctr + AES_BLOCK_SIZE);
  547. u8 val;
  548. unsigned int size;
  549. for (; increment; increment--)
  550. for (size = AES_BLOCK_SIZE; size; size--) {
  551. val = *--pos + 1;
  552. *pos = val;
  553. if (val)
  554. break;
  555. }
  556. }
  557. }
  558. static void cc_cipher_complete(struct device *dev, void *cc_req, int err)
  559. {
  560. struct skcipher_request *req = (struct skcipher_request *)cc_req;
  561. struct scatterlist *dst = req->dst;
  562. struct scatterlist *src = req->src;
  563. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  564. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  565. struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
  566. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  567. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  568. unsigned int len;
  569. cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
  570. switch (ctx_p->cipher_mode) {
  571. case DRV_CIPHER_CBC:
  572. /*
  573. * The crypto API expects us to set the req->iv to the last
  574. * ciphertext block. For encrypt, simply copy from the result.
  575. * For decrypt, we must copy from a saved buffer since this
  576. * could be an in-place decryption operation and the src is
  577. * lost by this point.
  578. */
  579. if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT) {
  580. memcpy(req->iv, req_ctx->backup_info, ivsize);
  581. kzfree(req_ctx->backup_info);
  582. } else if (!err) {
  583. len = req->cryptlen - ivsize;
  584. scatterwalk_map_and_copy(req->iv, req->dst, len,
  585. ivsize, 0);
  586. }
  587. break;
  588. case DRV_CIPHER_CTR:
  589. /* Compute the counter of the last block */
  590. len = ALIGN(req->cryptlen, AES_BLOCK_SIZE) / AES_BLOCK_SIZE;
  591. cc_update_ctr((u8 *)req->iv, len);
  592. break;
  593. default:
  594. break;
  595. }
  596. kzfree(req_ctx->iv);
  597. skcipher_request_complete(req, err);
  598. }
  599. static int cc_cipher_process(struct skcipher_request *req,
  600. enum drv_crypto_direction direction)
  601. {
  602. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  603. struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
  604. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  605. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  606. struct scatterlist *dst = req->dst;
  607. struct scatterlist *src = req->src;
  608. unsigned int nbytes = req->cryptlen;
  609. void *iv = req->iv;
  610. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  611. struct device *dev = drvdata_to_dev(ctx_p->drvdata);
  612. struct cc_hw_desc desc[MAX_ABLKCIPHER_SEQ_LEN];
  613. struct cc_crypto_req cc_req = {};
  614. int rc;
  615. unsigned int seq_len = 0;
  616. gfp_t flags = cc_gfp_flags(&req->base);
  617. dev_dbg(dev, "%s req=%p iv=%p nbytes=%d\n",
  618. ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ?
  619. "Encrypt" : "Decrypt"), req, iv, nbytes);
  620. /* STAT_PHASE_0: Init and sanity checks */
  621. /* TODO: check data length according to mode */
  622. if (validate_data_size(ctx_p, nbytes)) {
  623. dev_err(dev, "Unsupported data size %d.\n", nbytes);
  624. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
  625. rc = -EINVAL;
  626. goto exit_process;
  627. }
  628. if (nbytes == 0) {
  629. /* No data to process is valid */
  630. rc = 0;
  631. goto exit_process;
  632. }
  633. /* The IV we are handed may be allocted from the stack so
  634. * we must copy it to a DMAable buffer before use.
  635. */
  636. req_ctx->iv = kmemdup(iv, ivsize, flags);
  637. if (!req_ctx->iv) {
  638. rc = -ENOMEM;
  639. goto exit_process;
  640. }
  641. /* Setup request structure */
  642. cc_req.user_cb = (void *)cc_cipher_complete;
  643. cc_req.user_arg = (void *)req;
  644. /* Setup request context */
  645. req_ctx->gen_ctx.op_type = direction;
  646. /* STAT_PHASE_1: Map buffers */
  647. rc = cc_map_cipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes,
  648. req_ctx->iv, src, dst, flags);
  649. if (rc) {
  650. dev_err(dev, "map_request() failed\n");
  651. goto exit_process;
  652. }
  653. /* STAT_PHASE_2: Create sequence */
  654. /* Setup processing */
  655. cc_setup_cipher_desc(tfm, req_ctx, ivsize, nbytes, desc, &seq_len);
  656. /* Data processing */
  657. cc_setup_cipher_data(tfm, req_ctx, dst, src, nbytes, req, desc,
  658. &seq_len);
  659. /* STAT_PHASE_3: Lock HW and push sequence */
  660. rc = cc_send_request(ctx_p->drvdata, &cc_req, desc, seq_len,
  661. &req->base);
  662. if (rc != -EINPROGRESS && rc != -EBUSY) {
  663. /* Failed to send the request or request completed
  664. * synchronously
  665. */
  666. cc_unmap_cipher_request(dev, req_ctx, ivsize, src, dst);
  667. }
  668. exit_process:
  669. if (rc != -EINPROGRESS && rc != -EBUSY) {
  670. kzfree(req_ctx->backup_info);
  671. kzfree(req_ctx->iv);
  672. }
  673. return rc;
  674. }
  675. static int cc_cipher_encrypt(struct skcipher_request *req)
  676. {
  677. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  678. memset(req_ctx, 0, sizeof(*req_ctx));
  679. return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_ENCRYPT);
  680. }
  681. static int cc_cipher_decrypt(struct skcipher_request *req)
  682. {
  683. struct crypto_skcipher *sk_tfm = crypto_skcipher_reqtfm(req);
  684. struct crypto_tfm *tfm = crypto_skcipher_tfm(sk_tfm);
  685. struct cc_cipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
  686. struct cipher_req_ctx *req_ctx = skcipher_request_ctx(req);
  687. unsigned int ivsize = crypto_skcipher_ivsize(sk_tfm);
  688. gfp_t flags = cc_gfp_flags(&req->base);
  689. unsigned int len;
  690. memset(req_ctx, 0, sizeof(*req_ctx));
  691. if ((ctx_p->cipher_mode == DRV_CIPHER_CBC) &&
  692. (req->cryptlen >= ivsize)) {
  693. /* Allocate and save the last IV sized bytes of the source,
  694. * which will be lost in case of in-place decryption.
  695. */
  696. req_ctx->backup_info = kzalloc(ivsize, flags);
  697. if (!req_ctx->backup_info)
  698. return -ENOMEM;
  699. len = req->cryptlen - ivsize;
  700. scatterwalk_map_and_copy(req_ctx->backup_info, req->src, len,
  701. ivsize, 0);
  702. }
  703. return cc_cipher_process(req, DRV_CRYPTO_DIRECTION_DECRYPT);
  704. }
  705. /* Block cipher alg */
  706. static const struct cc_alg_template skcipher_algs[] = {
  707. {
  708. .name = "xts(paes)",
  709. .driver_name = "xts-paes-ccree",
  710. .blocksize = AES_BLOCK_SIZE,
  711. .template_skcipher = {
  712. .setkey = cc_cipher_sethkey,
  713. .encrypt = cc_cipher_encrypt,
  714. .decrypt = cc_cipher_decrypt,
  715. .min_keysize = CC_HW_KEY_SIZE,
  716. .max_keysize = CC_HW_KEY_SIZE,
  717. .ivsize = AES_BLOCK_SIZE,
  718. },
  719. .cipher_mode = DRV_CIPHER_XTS,
  720. .flow_mode = S_DIN_to_AES,
  721. .min_hw_rev = CC_HW_REV_630,
  722. },
  723. {
  724. .name = "xts512(paes)",
  725. .driver_name = "xts-paes-du512-ccree",
  726. .blocksize = AES_BLOCK_SIZE,
  727. .template_skcipher = {
  728. .setkey = cc_cipher_sethkey,
  729. .encrypt = cc_cipher_encrypt,
  730. .decrypt = cc_cipher_decrypt,
  731. .min_keysize = CC_HW_KEY_SIZE,
  732. .max_keysize = CC_HW_KEY_SIZE,
  733. .ivsize = AES_BLOCK_SIZE,
  734. },
  735. .cipher_mode = DRV_CIPHER_XTS,
  736. .flow_mode = S_DIN_to_AES,
  737. .data_unit = 512,
  738. .min_hw_rev = CC_HW_REV_712,
  739. },
  740. {
  741. .name = "xts4096(paes)",
  742. .driver_name = "xts-paes-du4096-ccree",
  743. .blocksize = AES_BLOCK_SIZE,
  744. .template_skcipher = {
  745. .setkey = cc_cipher_sethkey,
  746. .encrypt = cc_cipher_encrypt,
  747. .decrypt = cc_cipher_decrypt,
  748. .min_keysize = CC_HW_KEY_SIZE,
  749. .max_keysize = CC_HW_KEY_SIZE,
  750. .ivsize = AES_BLOCK_SIZE,
  751. },
  752. .cipher_mode = DRV_CIPHER_XTS,
  753. .flow_mode = S_DIN_to_AES,
  754. .data_unit = 4096,
  755. .min_hw_rev = CC_HW_REV_712,
  756. },
  757. {
  758. .name = "essiv(paes)",
  759. .driver_name = "essiv-paes-ccree",
  760. .blocksize = AES_BLOCK_SIZE,
  761. .template_skcipher = {
  762. .setkey = cc_cipher_sethkey,
  763. .encrypt = cc_cipher_encrypt,
  764. .decrypt = cc_cipher_decrypt,
  765. .min_keysize = CC_HW_KEY_SIZE,
  766. .max_keysize = CC_HW_KEY_SIZE,
  767. .ivsize = AES_BLOCK_SIZE,
  768. },
  769. .cipher_mode = DRV_CIPHER_ESSIV,
  770. .flow_mode = S_DIN_to_AES,
  771. .min_hw_rev = CC_HW_REV_712,
  772. },
  773. {
  774. .name = "essiv512(paes)",
  775. .driver_name = "essiv-paes-du512-ccree",
  776. .blocksize = AES_BLOCK_SIZE,
  777. .template_skcipher = {
  778. .setkey = cc_cipher_sethkey,
  779. .encrypt = cc_cipher_encrypt,
  780. .decrypt = cc_cipher_decrypt,
  781. .min_keysize = CC_HW_KEY_SIZE,
  782. .max_keysize = CC_HW_KEY_SIZE,
  783. .ivsize = AES_BLOCK_SIZE,
  784. },
  785. .cipher_mode = DRV_CIPHER_ESSIV,
  786. .flow_mode = S_DIN_to_AES,
  787. .data_unit = 512,
  788. .min_hw_rev = CC_HW_REV_712,
  789. },
  790. {
  791. .name = "essiv4096(paes)",
  792. .driver_name = "essiv-paes-du4096-ccree",
  793. .blocksize = AES_BLOCK_SIZE,
  794. .template_skcipher = {
  795. .setkey = cc_cipher_sethkey,
  796. .encrypt = cc_cipher_encrypt,
  797. .decrypt = cc_cipher_decrypt,
  798. .min_keysize = CC_HW_KEY_SIZE,
  799. .max_keysize = CC_HW_KEY_SIZE,
  800. .ivsize = AES_BLOCK_SIZE,
  801. },
  802. .cipher_mode = DRV_CIPHER_ESSIV,
  803. .flow_mode = S_DIN_to_AES,
  804. .data_unit = 4096,
  805. .min_hw_rev = CC_HW_REV_712,
  806. },
  807. {
  808. .name = "bitlocker(paes)",
  809. .driver_name = "bitlocker-paes-ccree",
  810. .blocksize = AES_BLOCK_SIZE,
  811. .template_skcipher = {
  812. .setkey = cc_cipher_sethkey,
  813. .encrypt = cc_cipher_encrypt,
  814. .decrypt = cc_cipher_decrypt,
  815. .min_keysize = CC_HW_KEY_SIZE,
  816. .max_keysize = CC_HW_KEY_SIZE,
  817. .ivsize = AES_BLOCK_SIZE,
  818. },
  819. .cipher_mode = DRV_CIPHER_BITLOCKER,
  820. .flow_mode = S_DIN_to_AES,
  821. .min_hw_rev = CC_HW_REV_712,
  822. },
  823. {
  824. .name = "bitlocker512(paes)",
  825. .driver_name = "bitlocker-paes-du512-ccree",
  826. .blocksize = AES_BLOCK_SIZE,
  827. .template_skcipher = {
  828. .setkey = cc_cipher_sethkey,
  829. .encrypt = cc_cipher_encrypt,
  830. .decrypt = cc_cipher_decrypt,
  831. .min_keysize = CC_HW_KEY_SIZE,
  832. .max_keysize = CC_HW_KEY_SIZE,
  833. .ivsize = AES_BLOCK_SIZE,
  834. },
  835. .cipher_mode = DRV_CIPHER_BITLOCKER,
  836. .flow_mode = S_DIN_to_AES,
  837. .data_unit = 512,
  838. .min_hw_rev = CC_HW_REV_712,
  839. },
  840. {
  841. .name = "bitlocker4096(paes)",
  842. .driver_name = "bitlocker-paes-du4096-ccree",
  843. .blocksize = AES_BLOCK_SIZE,
  844. .template_skcipher = {
  845. .setkey = cc_cipher_sethkey,
  846. .encrypt = cc_cipher_encrypt,
  847. .decrypt = cc_cipher_decrypt,
  848. .min_keysize = CC_HW_KEY_SIZE,
  849. .max_keysize = CC_HW_KEY_SIZE,
  850. .ivsize = AES_BLOCK_SIZE,
  851. },
  852. .cipher_mode = DRV_CIPHER_BITLOCKER,
  853. .flow_mode = S_DIN_to_AES,
  854. .data_unit = 4096,
  855. .min_hw_rev = CC_HW_REV_712,
  856. },
  857. {
  858. .name = "ecb(paes)",
  859. .driver_name = "ecb-paes-ccree",
  860. .blocksize = AES_BLOCK_SIZE,
  861. .template_skcipher = {
  862. .setkey = cc_cipher_sethkey,
  863. .encrypt = cc_cipher_encrypt,
  864. .decrypt = cc_cipher_decrypt,
  865. .min_keysize = CC_HW_KEY_SIZE,
  866. .max_keysize = CC_HW_KEY_SIZE,
  867. .ivsize = 0,
  868. },
  869. .cipher_mode = DRV_CIPHER_ECB,
  870. .flow_mode = S_DIN_to_AES,
  871. .min_hw_rev = CC_HW_REV_712,
  872. },
  873. {
  874. .name = "cbc(paes)",
  875. .driver_name = "cbc-paes-ccree",
  876. .blocksize = AES_BLOCK_SIZE,
  877. .template_skcipher = {
  878. .setkey = cc_cipher_sethkey,
  879. .encrypt = cc_cipher_encrypt,
  880. .decrypt = cc_cipher_decrypt,
  881. .min_keysize = CC_HW_KEY_SIZE,
  882. .max_keysize = CC_HW_KEY_SIZE,
  883. .ivsize = AES_BLOCK_SIZE,
  884. },
  885. .cipher_mode = DRV_CIPHER_CBC,
  886. .flow_mode = S_DIN_to_AES,
  887. .min_hw_rev = CC_HW_REV_712,
  888. },
  889. {
  890. .name = "ofb(paes)",
  891. .driver_name = "ofb-paes-ccree",
  892. .blocksize = AES_BLOCK_SIZE,
  893. .template_skcipher = {
  894. .setkey = cc_cipher_sethkey,
  895. .encrypt = cc_cipher_encrypt,
  896. .decrypt = cc_cipher_decrypt,
  897. .min_keysize = CC_HW_KEY_SIZE,
  898. .max_keysize = CC_HW_KEY_SIZE,
  899. .ivsize = AES_BLOCK_SIZE,
  900. },
  901. .cipher_mode = DRV_CIPHER_OFB,
  902. .flow_mode = S_DIN_to_AES,
  903. .min_hw_rev = CC_HW_REV_712,
  904. },
  905. {
  906. .name = "cts(cbc(paes))",
  907. .driver_name = "cts-cbc-paes-ccree",
  908. .blocksize = AES_BLOCK_SIZE,
  909. .template_skcipher = {
  910. .setkey = cc_cipher_sethkey,
  911. .encrypt = cc_cipher_encrypt,
  912. .decrypt = cc_cipher_decrypt,
  913. .min_keysize = CC_HW_KEY_SIZE,
  914. .max_keysize = CC_HW_KEY_SIZE,
  915. .ivsize = AES_BLOCK_SIZE,
  916. },
  917. .cipher_mode = DRV_CIPHER_CBC_CTS,
  918. .flow_mode = S_DIN_to_AES,
  919. .min_hw_rev = CC_HW_REV_712,
  920. },
  921. {
  922. .name = "ctr(paes)",
  923. .driver_name = "ctr-paes-ccree",
  924. .blocksize = 1,
  925. .template_skcipher = {
  926. .setkey = cc_cipher_sethkey,
  927. .encrypt = cc_cipher_encrypt,
  928. .decrypt = cc_cipher_decrypt,
  929. .min_keysize = CC_HW_KEY_SIZE,
  930. .max_keysize = CC_HW_KEY_SIZE,
  931. .ivsize = AES_BLOCK_SIZE,
  932. },
  933. .cipher_mode = DRV_CIPHER_CTR,
  934. .flow_mode = S_DIN_to_AES,
  935. .min_hw_rev = CC_HW_REV_712,
  936. },
  937. {
  938. .name = "xts(aes)",
  939. .driver_name = "xts-aes-ccree",
  940. .blocksize = AES_BLOCK_SIZE,
  941. .template_skcipher = {
  942. .setkey = cc_cipher_setkey,
  943. .encrypt = cc_cipher_encrypt,
  944. .decrypt = cc_cipher_decrypt,
  945. .min_keysize = AES_MIN_KEY_SIZE * 2,
  946. .max_keysize = AES_MAX_KEY_SIZE * 2,
  947. .ivsize = AES_BLOCK_SIZE,
  948. },
  949. .cipher_mode = DRV_CIPHER_XTS,
  950. .flow_mode = S_DIN_to_AES,
  951. .min_hw_rev = CC_HW_REV_630,
  952. },
  953. {
  954. .name = "xts512(aes)",
  955. .driver_name = "xts-aes-du512-ccree",
  956. .blocksize = AES_BLOCK_SIZE,
  957. .template_skcipher = {
  958. .setkey = cc_cipher_setkey,
  959. .encrypt = cc_cipher_encrypt,
  960. .decrypt = cc_cipher_decrypt,
  961. .min_keysize = AES_MIN_KEY_SIZE * 2,
  962. .max_keysize = AES_MAX_KEY_SIZE * 2,
  963. .ivsize = AES_BLOCK_SIZE,
  964. },
  965. .cipher_mode = DRV_CIPHER_XTS,
  966. .flow_mode = S_DIN_to_AES,
  967. .data_unit = 512,
  968. .min_hw_rev = CC_HW_REV_712,
  969. },
  970. {
  971. .name = "xts4096(aes)",
  972. .driver_name = "xts-aes-du4096-ccree",
  973. .blocksize = AES_BLOCK_SIZE,
  974. .template_skcipher = {
  975. .setkey = cc_cipher_setkey,
  976. .encrypt = cc_cipher_encrypt,
  977. .decrypt = cc_cipher_decrypt,
  978. .min_keysize = AES_MIN_KEY_SIZE * 2,
  979. .max_keysize = AES_MAX_KEY_SIZE * 2,
  980. .ivsize = AES_BLOCK_SIZE,
  981. },
  982. .cipher_mode = DRV_CIPHER_XTS,
  983. .flow_mode = S_DIN_to_AES,
  984. .data_unit = 4096,
  985. .min_hw_rev = CC_HW_REV_712,
  986. },
  987. {
  988. .name = "essiv(aes)",
  989. .driver_name = "essiv-aes-ccree",
  990. .blocksize = AES_BLOCK_SIZE,
  991. .template_skcipher = {
  992. .setkey = cc_cipher_setkey,
  993. .encrypt = cc_cipher_encrypt,
  994. .decrypt = cc_cipher_decrypt,
  995. .min_keysize = AES_MIN_KEY_SIZE * 2,
  996. .max_keysize = AES_MAX_KEY_SIZE * 2,
  997. .ivsize = AES_BLOCK_SIZE,
  998. },
  999. .cipher_mode = DRV_CIPHER_ESSIV,
  1000. .flow_mode = S_DIN_to_AES,
  1001. .min_hw_rev = CC_HW_REV_712,
  1002. },
  1003. {
  1004. .name = "essiv512(aes)",
  1005. .driver_name = "essiv-aes-du512-ccree",
  1006. .blocksize = AES_BLOCK_SIZE,
  1007. .template_skcipher = {
  1008. .setkey = cc_cipher_setkey,
  1009. .encrypt = cc_cipher_encrypt,
  1010. .decrypt = cc_cipher_decrypt,
  1011. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1012. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1013. .ivsize = AES_BLOCK_SIZE,
  1014. },
  1015. .cipher_mode = DRV_CIPHER_ESSIV,
  1016. .flow_mode = S_DIN_to_AES,
  1017. .data_unit = 512,
  1018. .min_hw_rev = CC_HW_REV_712,
  1019. },
  1020. {
  1021. .name = "essiv4096(aes)",
  1022. .driver_name = "essiv-aes-du4096-ccree",
  1023. .blocksize = AES_BLOCK_SIZE,
  1024. .template_skcipher = {
  1025. .setkey = cc_cipher_setkey,
  1026. .encrypt = cc_cipher_encrypt,
  1027. .decrypt = cc_cipher_decrypt,
  1028. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1029. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1030. .ivsize = AES_BLOCK_SIZE,
  1031. },
  1032. .cipher_mode = DRV_CIPHER_ESSIV,
  1033. .flow_mode = S_DIN_to_AES,
  1034. .data_unit = 4096,
  1035. .min_hw_rev = CC_HW_REV_712,
  1036. },
  1037. {
  1038. .name = "bitlocker(aes)",
  1039. .driver_name = "bitlocker-aes-ccree",
  1040. .blocksize = AES_BLOCK_SIZE,
  1041. .template_skcipher = {
  1042. .setkey = cc_cipher_setkey,
  1043. .encrypt = cc_cipher_encrypt,
  1044. .decrypt = cc_cipher_decrypt,
  1045. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1046. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1047. .ivsize = AES_BLOCK_SIZE,
  1048. },
  1049. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1050. .flow_mode = S_DIN_to_AES,
  1051. .min_hw_rev = CC_HW_REV_712,
  1052. },
  1053. {
  1054. .name = "bitlocker512(aes)",
  1055. .driver_name = "bitlocker-aes-du512-ccree",
  1056. .blocksize = AES_BLOCK_SIZE,
  1057. .template_skcipher = {
  1058. .setkey = cc_cipher_setkey,
  1059. .encrypt = cc_cipher_encrypt,
  1060. .decrypt = cc_cipher_decrypt,
  1061. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1062. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1063. .ivsize = AES_BLOCK_SIZE,
  1064. },
  1065. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1066. .flow_mode = S_DIN_to_AES,
  1067. .data_unit = 512,
  1068. .min_hw_rev = CC_HW_REV_712,
  1069. },
  1070. {
  1071. .name = "bitlocker4096(aes)",
  1072. .driver_name = "bitlocker-aes-du4096-ccree",
  1073. .blocksize = AES_BLOCK_SIZE,
  1074. .template_skcipher = {
  1075. .setkey = cc_cipher_setkey,
  1076. .encrypt = cc_cipher_encrypt,
  1077. .decrypt = cc_cipher_decrypt,
  1078. .min_keysize = AES_MIN_KEY_SIZE * 2,
  1079. .max_keysize = AES_MAX_KEY_SIZE * 2,
  1080. .ivsize = AES_BLOCK_SIZE,
  1081. },
  1082. .cipher_mode = DRV_CIPHER_BITLOCKER,
  1083. .flow_mode = S_DIN_to_AES,
  1084. .data_unit = 4096,
  1085. .min_hw_rev = CC_HW_REV_712,
  1086. },
  1087. {
  1088. .name = "ecb(aes)",
  1089. .driver_name = "ecb-aes-ccree",
  1090. .blocksize = AES_BLOCK_SIZE,
  1091. .template_skcipher = {
  1092. .setkey = cc_cipher_setkey,
  1093. .encrypt = cc_cipher_encrypt,
  1094. .decrypt = cc_cipher_decrypt,
  1095. .min_keysize = AES_MIN_KEY_SIZE,
  1096. .max_keysize = AES_MAX_KEY_SIZE,
  1097. .ivsize = 0,
  1098. },
  1099. .cipher_mode = DRV_CIPHER_ECB,
  1100. .flow_mode = S_DIN_to_AES,
  1101. .min_hw_rev = CC_HW_REV_630,
  1102. },
  1103. {
  1104. .name = "cbc(aes)",
  1105. .driver_name = "cbc-aes-ccree",
  1106. .blocksize = AES_BLOCK_SIZE,
  1107. .template_skcipher = {
  1108. .setkey = cc_cipher_setkey,
  1109. .encrypt = cc_cipher_encrypt,
  1110. .decrypt = cc_cipher_decrypt,
  1111. .min_keysize = AES_MIN_KEY_SIZE,
  1112. .max_keysize = AES_MAX_KEY_SIZE,
  1113. .ivsize = AES_BLOCK_SIZE,
  1114. },
  1115. .cipher_mode = DRV_CIPHER_CBC,
  1116. .flow_mode = S_DIN_to_AES,
  1117. .min_hw_rev = CC_HW_REV_630,
  1118. },
  1119. {
  1120. .name = "ofb(aes)",
  1121. .driver_name = "ofb-aes-ccree",
  1122. .blocksize = AES_BLOCK_SIZE,
  1123. .template_skcipher = {
  1124. .setkey = cc_cipher_setkey,
  1125. .encrypt = cc_cipher_encrypt,
  1126. .decrypt = cc_cipher_decrypt,
  1127. .min_keysize = AES_MIN_KEY_SIZE,
  1128. .max_keysize = AES_MAX_KEY_SIZE,
  1129. .ivsize = AES_BLOCK_SIZE,
  1130. },
  1131. .cipher_mode = DRV_CIPHER_OFB,
  1132. .flow_mode = S_DIN_to_AES,
  1133. .min_hw_rev = CC_HW_REV_630,
  1134. },
  1135. {
  1136. .name = "cts(cbc(aes))",
  1137. .driver_name = "cts-cbc-aes-ccree",
  1138. .blocksize = AES_BLOCK_SIZE,
  1139. .template_skcipher = {
  1140. .setkey = cc_cipher_setkey,
  1141. .encrypt = cc_cipher_encrypt,
  1142. .decrypt = cc_cipher_decrypt,
  1143. .min_keysize = AES_MIN_KEY_SIZE,
  1144. .max_keysize = AES_MAX_KEY_SIZE,
  1145. .ivsize = AES_BLOCK_SIZE,
  1146. },
  1147. .cipher_mode = DRV_CIPHER_CBC_CTS,
  1148. .flow_mode = S_DIN_to_AES,
  1149. .min_hw_rev = CC_HW_REV_630,
  1150. },
  1151. {
  1152. .name = "ctr(aes)",
  1153. .driver_name = "ctr-aes-ccree",
  1154. .blocksize = 1,
  1155. .template_skcipher = {
  1156. .setkey = cc_cipher_setkey,
  1157. .encrypt = cc_cipher_encrypt,
  1158. .decrypt = cc_cipher_decrypt,
  1159. .min_keysize = AES_MIN_KEY_SIZE,
  1160. .max_keysize = AES_MAX_KEY_SIZE,
  1161. .ivsize = AES_BLOCK_SIZE,
  1162. },
  1163. .cipher_mode = DRV_CIPHER_CTR,
  1164. .flow_mode = S_DIN_to_AES,
  1165. .min_hw_rev = CC_HW_REV_630,
  1166. },
  1167. {
  1168. .name = "cbc(des3_ede)",
  1169. .driver_name = "cbc-3des-ccree",
  1170. .blocksize = DES3_EDE_BLOCK_SIZE,
  1171. .template_skcipher = {
  1172. .setkey = cc_cipher_setkey,
  1173. .encrypt = cc_cipher_encrypt,
  1174. .decrypt = cc_cipher_decrypt,
  1175. .min_keysize = DES3_EDE_KEY_SIZE,
  1176. .max_keysize = DES3_EDE_KEY_SIZE,
  1177. .ivsize = DES3_EDE_BLOCK_SIZE,
  1178. },
  1179. .cipher_mode = DRV_CIPHER_CBC,
  1180. .flow_mode = S_DIN_to_DES,
  1181. .min_hw_rev = CC_HW_REV_630,
  1182. },
  1183. {
  1184. .name = "ecb(des3_ede)",
  1185. .driver_name = "ecb-3des-ccree",
  1186. .blocksize = DES3_EDE_BLOCK_SIZE,
  1187. .template_skcipher = {
  1188. .setkey = cc_cipher_setkey,
  1189. .encrypt = cc_cipher_encrypt,
  1190. .decrypt = cc_cipher_decrypt,
  1191. .min_keysize = DES3_EDE_KEY_SIZE,
  1192. .max_keysize = DES3_EDE_KEY_SIZE,
  1193. .ivsize = 0,
  1194. },
  1195. .cipher_mode = DRV_CIPHER_ECB,
  1196. .flow_mode = S_DIN_to_DES,
  1197. .min_hw_rev = CC_HW_REV_630,
  1198. },
  1199. {
  1200. .name = "cbc(des)",
  1201. .driver_name = "cbc-des-ccree",
  1202. .blocksize = DES_BLOCK_SIZE,
  1203. .template_skcipher = {
  1204. .setkey = cc_cipher_setkey,
  1205. .encrypt = cc_cipher_encrypt,
  1206. .decrypt = cc_cipher_decrypt,
  1207. .min_keysize = DES_KEY_SIZE,
  1208. .max_keysize = DES_KEY_SIZE,
  1209. .ivsize = DES_BLOCK_SIZE,
  1210. },
  1211. .cipher_mode = DRV_CIPHER_CBC,
  1212. .flow_mode = S_DIN_to_DES,
  1213. .min_hw_rev = CC_HW_REV_630,
  1214. },
  1215. {
  1216. .name = "ecb(des)",
  1217. .driver_name = "ecb-des-ccree",
  1218. .blocksize = DES_BLOCK_SIZE,
  1219. .template_skcipher = {
  1220. .setkey = cc_cipher_setkey,
  1221. .encrypt = cc_cipher_encrypt,
  1222. .decrypt = cc_cipher_decrypt,
  1223. .min_keysize = DES_KEY_SIZE,
  1224. .max_keysize = DES_KEY_SIZE,
  1225. .ivsize = 0,
  1226. },
  1227. .cipher_mode = DRV_CIPHER_ECB,
  1228. .flow_mode = S_DIN_to_DES,
  1229. .min_hw_rev = CC_HW_REV_630,
  1230. },
  1231. };
  1232. static struct cc_crypto_alg *cc_create_alg(const struct cc_alg_template *tmpl,
  1233. struct device *dev)
  1234. {
  1235. struct cc_crypto_alg *t_alg;
  1236. struct skcipher_alg *alg;
  1237. t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
  1238. if (!t_alg)
  1239. return ERR_PTR(-ENOMEM);
  1240. alg = &t_alg->skcipher_alg;
  1241. memcpy(alg, &tmpl->template_skcipher, sizeof(*alg));
  1242. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", tmpl->name);
  1243. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  1244. tmpl->driver_name);
  1245. alg->base.cra_module = THIS_MODULE;
  1246. alg->base.cra_priority = CC_CRA_PRIO;
  1247. alg->base.cra_blocksize = tmpl->blocksize;
  1248. alg->base.cra_alignmask = 0;
  1249. alg->base.cra_ctxsize = sizeof(struct cc_cipher_ctx);
  1250. alg->base.cra_init = cc_cipher_init;
  1251. alg->base.cra_exit = cc_cipher_exit;
  1252. alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
  1253. t_alg->cipher_mode = tmpl->cipher_mode;
  1254. t_alg->flow_mode = tmpl->flow_mode;
  1255. t_alg->data_unit = tmpl->data_unit;
  1256. return t_alg;
  1257. }
  1258. int cc_cipher_free(struct cc_drvdata *drvdata)
  1259. {
  1260. struct cc_crypto_alg *t_alg, *n;
  1261. struct cc_cipher_handle *cipher_handle = drvdata->cipher_handle;
  1262. if (cipher_handle) {
  1263. /* Remove registered algs */
  1264. list_for_each_entry_safe(t_alg, n, &cipher_handle->alg_list,
  1265. entry) {
  1266. crypto_unregister_skcipher(&t_alg->skcipher_alg);
  1267. list_del(&t_alg->entry);
  1268. kfree(t_alg);
  1269. }
  1270. kfree(cipher_handle);
  1271. drvdata->cipher_handle = NULL;
  1272. }
  1273. return 0;
  1274. }
  1275. int cc_cipher_alloc(struct cc_drvdata *drvdata)
  1276. {
  1277. struct cc_cipher_handle *cipher_handle;
  1278. struct cc_crypto_alg *t_alg;
  1279. struct device *dev = drvdata_to_dev(drvdata);
  1280. int rc = -ENOMEM;
  1281. int alg;
  1282. cipher_handle = kmalloc(sizeof(*cipher_handle), GFP_KERNEL);
  1283. if (!cipher_handle)
  1284. return -ENOMEM;
  1285. INIT_LIST_HEAD(&cipher_handle->alg_list);
  1286. drvdata->cipher_handle = cipher_handle;
  1287. /* Linux crypto */
  1288. dev_dbg(dev, "Number of algorithms = %zu\n",
  1289. ARRAY_SIZE(skcipher_algs));
  1290. for (alg = 0; alg < ARRAY_SIZE(skcipher_algs); alg++) {
  1291. if (skcipher_algs[alg].min_hw_rev > drvdata->hw_rev)
  1292. continue;
  1293. dev_dbg(dev, "creating %s\n", skcipher_algs[alg].driver_name);
  1294. t_alg = cc_create_alg(&skcipher_algs[alg], dev);
  1295. if (IS_ERR(t_alg)) {
  1296. rc = PTR_ERR(t_alg);
  1297. dev_err(dev, "%s alg allocation failed\n",
  1298. skcipher_algs[alg].driver_name);
  1299. goto fail0;
  1300. }
  1301. t_alg->drvdata = drvdata;
  1302. dev_dbg(dev, "registering %s\n",
  1303. skcipher_algs[alg].driver_name);
  1304. rc = crypto_register_skcipher(&t_alg->skcipher_alg);
  1305. dev_dbg(dev, "%s alg registration rc = %x\n",
  1306. t_alg->skcipher_alg.base.cra_driver_name, rc);
  1307. if (rc) {
  1308. dev_err(dev, "%s alg registration failed\n",
  1309. t_alg->skcipher_alg.base.cra_driver_name);
  1310. kfree(t_alg);
  1311. goto fail0;
  1312. } else {
  1313. list_add_tail(&t_alg->entry,
  1314. &cipher_handle->alg_list);
  1315. dev_dbg(dev, "Registered %s\n",
  1316. t_alg->skcipher_alg.base.cra_driver_name);
  1317. }
  1318. }
  1319. return 0;
  1320. fail0:
  1321. cc_cipher_free(drvdata);
  1322. return rc;
  1323. }