crypto4xx_alg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /**
  3. * AMCC SoC PPC4xx Crypto Driver
  4. *
  5. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  6. * All rights reserved. James Hsiao <jhsiao@amcc.com>
  7. *
  8. * This file implements the Linux crypto algorithms.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/spinlock_types.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/crypto.h>
  15. #include <linux/hash.h>
  16. #include <crypto/internal/hash.h>
  17. #include <linux/dma-mapping.h>
  18. #include <crypto/algapi.h>
  19. #include <crypto/aead.h>
  20. #include <crypto/aes.h>
  21. #include <crypto/gcm.h>
  22. #include <crypto/sha.h>
  23. #include <crypto/ctr.h>
  24. #include <crypto/skcipher.h>
  25. #include "crypto4xx_reg_def.h"
  26. #include "crypto4xx_core.h"
  27. #include "crypto4xx_sa.h"
  28. static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
  29. u32 save_iv, u32 ld_h, u32 ld_iv,
  30. u32 hdr_proc, u32 h, u32 c, u32 pad_type,
  31. u32 op_grp, u32 op, u32 dir)
  32. {
  33. sa->sa_command_0.w = 0;
  34. sa->sa_command_0.bf.save_hash_state = save_h;
  35. sa->sa_command_0.bf.save_iv = save_iv;
  36. sa->sa_command_0.bf.load_hash_state = ld_h;
  37. sa->sa_command_0.bf.load_iv = ld_iv;
  38. sa->sa_command_0.bf.hdr_proc = hdr_proc;
  39. sa->sa_command_0.bf.hash_alg = h;
  40. sa->sa_command_0.bf.cipher_alg = c;
  41. sa->sa_command_0.bf.pad_type = pad_type & 3;
  42. sa->sa_command_0.bf.extend_pad = pad_type >> 2;
  43. sa->sa_command_0.bf.op_group = op_grp;
  44. sa->sa_command_0.bf.opcode = op;
  45. sa->sa_command_0.bf.dir = dir;
  46. }
  47. static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
  48. u32 hmac_mc, u32 cfb, u32 esn,
  49. u32 sn_mask, u32 mute, u32 cp_pad,
  50. u32 cp_pay, u32 cp_hdr)
  51. {
  52. sa->sa_command_1.w = 0;
  53. sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
  54. sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
  55. sa->sa_command_1.bf.feedback_mode = cfb,
  56. sa->sa_command_1.bf.sa_rev = 1;
  57. sa->sa_command_1.bf.hmac_muting = hmac_mc;
  58. sa->sa_command_1.bf.extended_seq_num = esn;
  59. sa->sa_command_1.bf.seq_num_mask = sn_mask;
  60. sa->sa_command_1.bf.mutable_bit_proc = mute;
  61. sa->sa_command_1.bf.copy_pad = cp_pad;
  62. sa->sa_command_1.bf.copy_payload = cp_pay;
  63. sa->sa_command_1.bf.copy_hdr = cp_hdr;
  64. }
  65. static inline int crypto4xx_crypt(struct skcipher_request *req,
  66. const unsigned int ivlen, bool decrypt,
  67. bool check_blocksize)
  68. {
  69. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  70. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  71. __le32 iv[AES_IV_SIZE];
  72. if (check_blocksize && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE))
  73. return -EINVAL;
  74. if (ivlen)
  75. crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
  76. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  77. req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
  78. ctx->sa_len, 0, NULL);
  79. }
  80. int crypto4xx_encrypt_noiv_block(struct skcipher_request *req)
  81. {
  82. return crypto4xx_crypt(req, 0, false, true);
  83. }
  84. int crypto4xx_encrypt_iv_stream(struct skcipher_request *req)
  85. {
  86. return crypto4xx_crypt(req, AES_IV_SIZE, false, false);
  87. }
  88. int crypto4xx_decrypt_noiv_block(struct skcipher_request *req)
  89. {
  90. return crypto4xx_crypt(req, 0, true, true);
  91. }
  92. int crypto4xx_decrypt_iv_stream(struct skcipher_request *req)
  93. {
  94. return crypto4xx_crypt(req, AES_IV_SIZE, true, false);
  95. }
  96. int crypto4xx_encrypt_iv_block(struct skcipher_request *req)
  97. {
  98. return crypto4xx_crypt(req, AES_IV_SIZE, false, true);
  99. }
  100. int crypto4xx_decrypt_iv_block(struct skcipher_request *req)
  101. {
  102. return crypto4xx_crypt(req, AES_IV_SIZE, true, true);
  103. }
  104. /**
  105. * AES Functions
  106. */
  107. static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
  108. const u8 *key,
  109. unsigned int keylen,
  110. unsigned char cm,
  111. u8 fb)
  112. {
  113. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  114. struct dynamic_sa_ctl *sa;
  115. int rc;
  116. if (keylen != AES_KEYSIZE_256 &&
  117. keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
  118. crypto_skcipher_set_flags(cipher,
  119. CRYPTO_TFM_RES_BAD_KEY_LEN);
  120. return -EINVAL;
  121. }
  122. /* Create SA */
  123. if (ctx->sa_in || ctx->sa_out)
  124. crypto4xx_free_sa(ctx);
  125. rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
  126. if (rc)
  127. return rc;
  128. /* Setup SA */
  129. sa = ctx->sa_in;
  130. set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
  131. SA_NOT_SAVE_IV : SA_SAVE_IV),
  132. SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
  133. SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
  134. SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
  135. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  136. SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
  137. DIR_INBOUND);
  138. set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
  139. fb, SA_EXTENDED_SN_OFF,
  140. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  141. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  142. SA_NOT_COPY_HDR);
  143. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  144. key, keylen);
  145. sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
  146. sa->sa_command_1.bf.key_len = keylen >> 3;
  147. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  148. sa = ctx->sa_out;
  149. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  150. /*
  151. * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
  152. * it's the DIR_(IN|OUT)BOUND that matters
  153. */
  154. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
  155. return 0;
  156. }
  157. int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
  158. const u8 *key, unsigned int keylen)
  159. {
  160. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
  161. CRYPTO_FEEDBACK_MODE_NO_FB);
  162. }
  163. int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
  164. const u8 *key, unsigned int keylen)
  165. {
  166. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
  167. CRYPTO_FEEDBACK_MODE_128BIT_CFB);
  168. }
  169. int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
  170. const u8 *key, unsigned int keylen)
  171. {
  172. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
  173. CRYPTO_FEEDBACK_MODE_NO_FB);
  174. }
  175. int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
  176. const u8 *key, unsigned int keylen)
  177. {
  178. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
  179. CRYPTO_FEEDBACK_MODE_64BIT_OFB);
  180. }
  181. int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
  182. const u8 *key, unsigned int keylen)
  183. {
  184. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  185. int rc;
  186. rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
  187. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  188. if (rc)
  189. return rc;
  190. ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
  191. CTR_RFC3686_NONCE_SIZE]);
  192. return 0;
  193. }
  194. int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
  195. {
  196. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  197. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  198. __le32 iv[AES_IV_SIZE / 4] = {
  199. ctx->iv_nonce,
  200. cpu_to_le32p((u32 *) req->iv),
  201. cpu_to_le32p((u32 *) (req->iv + 4)),
  202. cpu_to_le32(1) };
  203. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  204. req->cryptlen, iv, AES_IV_SIZE,
  205. ctx->sa_out, ctx->sa_len, 0, NULL);
  206. }
  207. int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
  208. {
  209. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  210. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  211. __le32 iv[AES_IV_SIZE / 4] = {
  212. ctx->iv_nonce,
  213. cpu_to_le32p((u32 *) req->iv),
  214. cpu_to_le32p((u32 *) (req->iv + 4)),
  215. cpu_to_le32(1) };
  216. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  217. req->cryptlen, iv, AES_IV_SIZE,
  218. ctx->sa_out, ctx->sa_len, 0, NULL);
  219. }
  220. static int
  221. crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
  222. {
  223. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  224. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  225. size_t iv_len = crypto_skcipher_ivsize(cipher);
  226. unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
  227. unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
  228. AES_BLOCK_SIZE;
  229. /*
  230. * The hardware uses only the last 32-bits as the counter while the
  231. * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
  232. * the whole IV is a counter. So fallback if the counter is going to
  233. * overlow.
  234. */
  235. if (counter + nblks < counter) {
  236. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
  237. int ret;
  238. skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
  239. skcipher_request_set_callback(subreq, req->base.flags,
  240. NULL, NULL);
  241. skcipher_request_set_crypt(subreq, req->src, req->dst,
  242. req->cryptlen, req->iv);
  243. ret = encrypt ? crypto_skcipher_encrypt(subreq)
  244. : crypto_skcipher_decrypt(subreq);
  245. skcipher_request_zero(subreq);
  246. return ret;
  247. }
  248. return encrypt ? crypto4xx_encrypt_iv_stream(req)
  249. : crypto4xx_decrypt_iv_stream(req);
  250. }
  251. static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
  252. struct crypto_skcipher *cipher,
  253. const u8 *key,
  254. unsigned int keylen)
  255. {
  256. int rc;
  257. crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
  258. CRYPTO_TFM_REQ_MASK);
  259. crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
  260. crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  261. rc = crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
  262. crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
  263. crypto_skcipher_set_flags(cipher,
  264. crypto_sync_skcipher_get_flags(ctx->sw_cipher.cipher) &
  265. CRYPTO_TFM_RES_MASK);
  266. return rc;
  267. }
  268. int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
  269. const u8 *key, unsigned int keylen)
  270. {
  271. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  272. int rc;
  273. rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
  274. if (rc)
  275. return rc;
  276. return crypto4xx_setkey_aes(cipher, key, keylen,
  277. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  278. }
  279. int crypto4xx_encrypt_ctr(struct skcipher_request *req)
  280. {
  281. return crypto4xx_ctr_crypt(req, true);
  282. }
  283. int crypto4xx_decrypt_ctr(struct skcipher_request *req)
  284. {
  285. return crypto4xx_ctr_crypt(req, false);
  286. }
  287. static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
  288. unsigned int len,
  289. bool is_ccm, bool decrypt)
  290. {
  291. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  292. /* authsize has to be a multiple of 4 */
  293. if (aead->authsize & 3)
  294. return true;
  295. /*
  296. * hardware does not handle cases where plaintext
  297. * is less than a block.
  298. */
  299. if (len < AES_BLOCK_SIZE)
  300. return true;
  301. /* assoc len needs to be a multiple of 4 and <= 1020 */
  302. if (req->assoclen & 0x3 || req->assoclen > 1020)
  303. return true;
  304. /* CCM supports only counter field length of 2 and 4 bytes */
  305. if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
  306. return true;
  307. return false;
  308. }
  309. static int crypto4xx_aead_fallback(struct aead_request *req,
  310. struct crypto4xx_ctx *ctx, bool do_decrypt)
  311. {
  312. struct aead_request *subreq = aead_request_ctx(req);
  313. aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
  314. aead_request_set_callback(subreq, req->base.flags,
  315. req->base.complete, req->base.data);
  316. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  317. req->iv);
  318. aead_request_set_ad(subreq, req->assoclen);
  319. return do_decrypt ? crypto_aead_decrypt(subreq) :
  320. crypto_aead_encrypt(subreq);
  321. }
  322. static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
  323. struct crypto_aead *cipher,
  324. const u8 *key,
  325. unsigned int keylen)
  326. {
  327. int rc;
  328. crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
  329. crypto_aead_set_flags(ctx->sw_cipher.aead,
  330. crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  331. rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
  332. crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
  333. crypto_aead_set_flags(cipher,
  334. crypto_aead_get_flags(ctx->sw_cipher.aead) &
  335. CRYPTO_TFM_RES_MASK);
  336. return rc;
  337. }
  338. /**
  339. * AES-CCM Functions
  340. */
  341. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
  342. unsigned int keylen)
  343. {
  344. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  345. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  346. struct dynamic_sa_ctl *sa;
  347. int rc = 0;
  348. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  349. if (rc)
  350. return rc;
  351. if (ctx->sa_in || ctx->sa_out)
  352. crypto4xx_free_sa(ctx);
  353. rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
  354. if (rc)
  355. return rc;
  356. /* Setup SA */
  357. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  358. sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
  359. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  360. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  361. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  362. SA_CIPHER_ALG_AES,
  363. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  364. SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
  365. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  366. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  367. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  368. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  369. SA_NOT_COPY_HDR);
  370. sa->sa_command_1.bf.key_len = keylen >> 3;
  371. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
  372. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  373. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  374. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  375. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  376. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  377. SA_CIPHER_ALG_AES,
  378. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  379. SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
  380. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  381. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  382. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  383. SA_COPY_PAD, SA_COPY_PAYLOAD,
  384. SA_NOT_COPY_HDR);
  385. sa->sa_command_1.bf.key_len = keylen >> 3;
  386. return 0;
  387. }
  388. static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
  389. {
  390. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  391. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  392. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  393. __le32 iv[16];
  394. u32 tmp_sa[SA_AES128_CCM_LEN + 4];
  395. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
  396. unsigned int len = req->cryptlen;
  397. if (decrypt)
  398. len -= crypto_aead_authsize(aead);
  399. if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
  400. return crypto4xx_aead_fallback(req, ctx, decrypt);
  401. memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
  402. sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
  403. if (req->iv[0] == 1) {
  404. /* CRYPTO_MODE_AES_ICM */
  405. sa->sa_command_1.bf.crypto_mode9_8 = 1;
  406. }
  407. iv[3] = cpu_to_le32(0);
  408. crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
  409. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  410. len, iv, sizeof(iv),
  411. sa, ctx->sa_len, req->assoclen, rctx->dst);
  412. }
  413. int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
  414. {
  415. return crypto4xx_crypt_aes_ccm(req, false);
  416. }
  417. int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
  418. {
  419. return crypto4xx_crypt_aes_ccm(req, true);
  420. }
  421. int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
  422. unsigned int authsize)
  423. {
  424. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  425. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  426. return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
  427. }
  428. /**
  429. * AES-GCM Functions
  430. */
  431. static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
  432. {
  433. switch (keylen) {
  434. case 16:
  435. case 24:
  436. case 32:
  437. return 0;
  438. default:
  439. return -EINVAL;
  440. }
  441. }
  442. static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
  443. unsigned int keylen)
  444. {
  445. struct crypto_aes_ctx ctx;
  446. uint8_t src[16] = { 0 };
  447. int rc;
  448. rc = aes_expandkey(&ctx, key, keylen);
  449. if (rc) {
  450. pr_err("aes_expandkey() failed: %d\n", rc);
  451. return rc;
  452. }
  453. aes_encrypt(&ctx, src, src);
  454. crypto4xx_memcpy_to_le32(hash_start, src, 16);
  455. memzero_explicit(&ctx, sizeof(ctx));
  456. return 0;
  457. }
  458. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  459. const u8 *key, unsigned int keylen)
  460. {
  461. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  462. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  463. struct dynamic_sa_ctl *sa;
  464. int rc = 0;
  465. if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
  466. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  467. return -EINVAL;
  468. }
  469. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  470. if (rc)
  471. return rc;
  472. if (ctx->sa_in || ctx->sa_out)
  473. crypto4xx_free_sa(ctx);
  474. rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
  475. if (rc)
  476. return rc;
  477. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  478. sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
  479. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  480. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  481. SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
  482. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  483. SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
  484. DIR_INBOUND);
  485. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  486. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  487. SA_SEQ_MASK_ON, SA_MC_DISABLE,
  488. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  489. SA_NOT_COPY_HDR);
  490. sa->sa_command_1.bf.key_len = keylen >> 3;
  491. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  492. key, keylen);
  493. rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
  494. key, keylen);
  495. if (rc) {
  496. pr_err("GCM hash key setting failed = %d\n", rc);
  497. goto err;
  498. }
  499. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  500. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  501. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  502. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
  503. return 0;
  504. err:
  505. crypto4xx_free_sa(ctx);
  506. return rc;
  507. }
  508. static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
  509. bool decrypt)
  510. {
  511. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  512. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  513. __le32 iv[4];
  514. unsigned int len = req->cryptlen;
  515. if (decrypt)
  516. len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
  517. if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
  518. return crypto4xx_aead_fallback(req, ctx, decrypt);
  519. crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
  520. iv[3] = cpu_to_le32(1);
  521. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  522. len, iv, sizeof(iv),
  523. decrypt ? ctx->sa_in : ctx->sa_out,
  524. ctx->sa_len, req->assoclen, rctx->dst);
  525. }
  526. int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
  527. {
  528. return crypto4xx_crypt_aes_gcm(req, false);
  529. }
  530. int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
  531. {
  532. return crypto4xx_crypt_aes_gcm(req, true);
  533. }
  534. /**
  535. * HASH SHA1 Functions
  536. */
  537. static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
  538. unsigned int sa_len,
  539. unsigned char ha,
  540. unsigned char hm)
  541. {
  542. struct crypto_alg *alg = tfm->__crt_alg;
  543. struct crypto4xx_alg *my_alg;
  544. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  545. struct dynamic_sa_hash160 *sa;
  546. int rc;
  547. my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
  548. alg.u.hash);
  549. ctx->dev = my_alg->dev;
  550. /* Create SA */
  551. if (ctx->sa_in || ctx->sa_out)
  552. crypto4xx_free_sa(ctx);
  553. rc = crypto4xx_alloc_sa(ctx, sa_len);
  554. if (rc)
  555. return rc;
  556. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  557. sizeof(struct crypto4xx_ctx));
  558. sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
  559. set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  560. SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
  561. SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
  562. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  563. SA_OPCODE_HASH, DIR_INBOUND);
  564. set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
  565. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  566. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  567. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  568. SA_NOT_COPY_HDR);
  569. /* Need to zero hash digest in SA */
  570. memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
  571. memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
  572. return 0;
  573. }
  574. int crypto4xx_hash_init(struct ahash_request *req)
  575. {
  576. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  577. int ds;
  578. struct dynamic_sa_ctl *sa;
  579. sa = ctx->sa_in;
  580. ds = crypto_ahash_digestsize(
  581. __crypto_ahash_cast(req->base.tfm));
  582. sa->sa_command_0.bf.digest_len = ds >> 2;
  583. sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
  584. return 0;
  585. }
  586. int crypto4xx_hash_update(struct ahash_request *req)
  587. {
  588. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  589. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  590. struct scatterlist dst;
  591. unsigned int ds = crypto_ahash_digestsize(ahash);
  592. sg_init_one(&dst, req->result, ds);
  593. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  594. req->nbytes, NULL, 0, ctx->sa_in,
  595. ctx->sa_len, 0, NULL);
  596. }
  597. int crypto4xx_hash_final(struct ahash_request *req)
  598. {
  599. return 0;
  600. }
  601. int crypto4xx_hash_digest(struct ahash_request *req)
  602. {
  603. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  604. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  605. struct scatterlist dst;
  606. unsigned int ds = crypto_ahash_digestsize(ahash);
  607. sg_init_one(&dst, req->result, ds);
  608. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  609. req->nbytes, NULL, 0, ctx->sa_in,
  610. ctx->sa_len, 0, NULL);
  611. }
  612. /**
  613. * SHA1 Algorithm
  614. */
  615. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
  616. {
  617. return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
  618. SA_HASH_MODE_HASH);
  619. }