cc_cipher.c 39 KB

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