cc_cipher.c 39 KB

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