crypto4xx_alg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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/sha1.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 && keylen != AES_KEYSIZE_192 &&
  117. keylen != AES_KEYSIZE_128)
  118. return -EINVAL;
  119. /* Create SA */
  120. if (ctx->sa_in || ctx->sa_out)
  121. crypto4xx_free_sa(ctx);
  122. rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
  123. if (rc)
  124. return rc;
  125. /* Setup SA */
  126. sa = ctx->sa_in;
  127. set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
  128. SA_NOT_SAVE_IV : SA_SAVE_IV),
  129. SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
  130. SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
  131. SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
  132. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  133. SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
  134. DIR_INBOUND);
  135. set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
  136. fb, SA_EXTENDED_SN_OFF,
  137. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  138. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  139. SA_NOT_COPY_HDR);
  140. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  141. key, keylen);
  142. sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
  143. sa->sa_command_1.bf.key_len = keylen >> 3;
  144. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  145. sa = ctx->sa_out;
  146. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  147. /*
  148. * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
  149. * it's the DIR_(IN|OUT)BOUND that matters
  150. */
  151. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
  152. return 0;
  153. }
  154. int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
  155. const u8 *key, unsigned int keylen)
  156. {
  157. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
  158. CRYPTO_FEEDBACK_MODE_NO_FB);
  159. }
  160. int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
  161. const u8 *key, unsigned int keylen)
  162. {
  163. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
  164. CRYPTO_FEEDBACK_MODE_NO_FB);
  165. }
  166. int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
  167. const u8 *key, unsigned int keylen)
  168. {
  169. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  170. int rc;
  171. rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
  172. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  173. if (rc)
  174. return rc;
  175. ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
  176. CTR_RFC3686_NONCE_SIZE]);
  177. return 0;
  178. }
  179. int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
  180. {
  181. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  182. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  183. __le32 iv[AES_IV_SIZE / 4] = {
  184. ctx->iv_nonce,
  185. cpu_to_le32p((u32 *) req->iv),
  186. cpu_to_le32p((u32 *) (req->iv + 4)),
  187. cpu_to_le32(1) };
  188. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  189. req->cryptlen, iv, AES_IV_SIZE,
  190. ctx->sa_out, ctx->sa_len, 0, NULL);
  191. }
  192. int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
  193. {
  194. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  195. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  196. __le32 iv[AES_IV_SIZE / 4] = {
  197. ctx->iv_nonce,
  198. cpu_to_le32p((u32 *) req->iv),
  199. cpu_to_le32p((u32 *) (req->iv + 4)),
  200. cpu_to_le32(1) };
  201. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  202. req->cryptlen, iv, AES_IV_SIZE,
  203. ctx->sa_out, ctx->sa_len, 0, NULL);
  204. }
  205. static int
  206. crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
  207. {
  208. struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
  209. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  210. size_t iv_len = crypto_skcipher_ivsize(cipher);
  211. unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
  212. unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
  213. AES_BLOCK_SIZE;
  214. /*
  215. * The hardware uses only the last 32-bits as the counter while the
  216. * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
  217. * the whole IV is a counter. So fallback if the counter is going to
  218. * overlow.
  219. */
  220. if (counter + nblks < counter) {
  221. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
  222. int ret;
  223. skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
  224. skcipher_request_set_callback(subreq, req->base.flags,
  225. NULL, NULL);
  226. skcipher_request_set_crypt(subreq, req->src, req->dst,
  227. req->cryptlen, req->iv);
  228. ret = encrypt ? crypto_skcipher_encrypt(subreq)
  229. : crypto_skcipher_decrypt(subreq);
  230. skcipher_request_zero(subreq);
  231. return ret;
  232. }
  233. return encrypt ? crypto4xx_encrypt_iv_stream(req)
  234. : crypto4xx_decrypt_iv_stream(req);
  235. }
  236. static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
  237. struct crypto_skcipher *cipher,
  238. const u8 *key,
  239. unsigned int keylen)
  240. {
  241. crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
  242. CRYPTO_TFM_REQ_MASK);
  243. crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
  244. crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  245. return crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
  246. }
  247. int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
  248. const u8 *key, unsigned int keylen)
  249. {
  250. struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
  251. int rc;
  252. rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
  253. if (rc)
  254. return rc;
  255. return crypto4xx_setkey_aes(cipher, key, keylen,
  256. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  257. }
  258. int crypto4xx_encrypt_ctr(struct skcipher_request *req)
  259. {
  260. return crypto4xx_ctr_crypt(req, true);
  261. }
  262. int crypto4xx_decrypt_ctr(struct skcipher_request *req)
  263. {
  264. return crypto4xx_ctr_crypt(req, false);
  265. }
  266. static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
  267. unsigned int len,
  268. bool is_ccm, bool decrypt)
  269. {
  270. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  271. /* authsize has to be a multiple of 4 */
  272. if (aead->authsize & 3)
  273. return true;
  274. /*
  275. * hardware does not handle cases where plaintext
  276. * is less than a block.
  277. */
  278. if (len < AES_BLOCK_SIZE)
  279. return true;
  280. /* assoc len needs to be a multiple of 4 and <= 1020 */
  281. if (req->assoclen & 0x3 || req->assoclen > 1020)
  282. return true;
  283. /* CCM supports only counter field length of 2 and 4 bytes */
  284. if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
  285. return true;
  286. return false;
  287. }
  288. static int crypto4xx_aead_fallback(struct aead_request *req,
  289. struct crypto4xx_ctx *ctx, bool do_decrypt)
  290. {
  291. struct aead_request *subreq = aead_request_ctx(req);
  292. aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
  293. aead_request_set_callback(subreq, req->base.flags,
  294. req->base.complete, req->base.data);
  295. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  296. req->iv);
  297. aead_request_set_ad(subreq, req->assoclen);
  298. return do_decrypt ? crypto_aead_decrypt(subreq) :
  299. crypto_aead_encrypt(subreq);
  300. }
  301. static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
  302. struct crypto_aead *cipher,
  303. const u8 *key,
  304. unsigned int keylen)
  305. {
  306. crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
  307. crypto_aead_set_flags(ctx->sw_cipher.aead,
  308. crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  309. return crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
  310. }
  311. /*
  312. * AES-CCM Functions
  313. */
  314. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
  315. unsigned int keylen)
  316. {
  317. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  318. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  319. struct dynamic_sa_ctl *sa;
  320. int rc = 0;
  321. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  322. if (rc)
  323. return rc;
  324. if (ctx->sa_in || ctx->sa_out)
  325. crypto4xx_free_sa(ctx);
  326. rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
  327. if (rc)
  328. return rc;
  329. /* Setup SA */
  330. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  331. sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
  332. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  333. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  334. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  335. SA_CIPHER_ALG_AES,
  336. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  337. SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
  338. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  339. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  340. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  341. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  342. SA_NOT_COPY_HDR);
  343. sa->sa_command_1.bf.key_len = keylen >> 3;
  344. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
  345. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  346. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  347. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  348. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  349. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  350. SA_CIPHER_ALG_AES,
  351. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  352. SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
  353. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  354. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  355. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  356. SA_COPY_PAD, SA_COPY_PAYLOAD,
  357. SA_NOT_COPY_HDR);
  358. sa->sa_command_1.bf.key_len = keylen >> 3;
  359. return 0;
  360. }
  361. static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
  362. {
  363. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  364. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  365. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  366. __le32 iv[16];
  367. u32 tmp_sa[SA_AES128_CCM_LEN + 4];
  368. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
  369. unsigned int len = req->cryptlen;
  370. if (decrypt)
  371. len -= crypto_aead_authsize(aead);
  372. if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
  373. return crypto4xx_aead_fallback(req, ctx, decrypt);
  374. memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
  375. sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
  376. if (req->iv[0] == 1) {
  377. /* CRYPTO_MODE_AES_ICM */
  378. sa->sa_command_1.bf.crypto_mode9_8 = 1;
  379. }
  380. iv[3] = cpu_to_le32(0);
  381. crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
  382. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  383. len, iv, sizeof(iv),
  384. sa, ctx->sa_len, req->assoclen, rctx->dst);
  385. }
  386. int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
  387. {
  388. return crypto4xx_crypt_aes_ccm(req, false);
  389. }
  390. int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
  391. {
  392. return crypto4xx_crypt_aes_ccm(req, true);
  393. }
  394. int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
  395. unsigned int authsize)
  396. {
  397. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  398. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  399. return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
  400. }
  401. /*
  402. * AES-GCM Functions
  403. */
  404. static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
  405. {
  406. switch (keylen) {
  407. case 16:
  408. case 24:
  409. case 32:
  410. return 0;
  411. default:
  412. return -EINVAL;
  413. }
  414. }
  415. static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
  416. unsigned int keylen)
  417. {
  418. struct crypto_aes_ctx ctx;
  419. uint8_t src[16] = { 0 };
  420. int rc;
  421. rc = aes_expandkey(&ctx, key, keylen);
  422. if (rc) {
  423. pr_err("aes_expandkey() failed: %d\n", rc);
  424. return rc;
  425. }
  426. aes_encrypt(&ctx, src, src);
  427. crypto4xx_memcpy_to_le32(hash_start, src, 16);
  428. memzero_explicit(&ctx, sizeof(ctx));
  429. return 0;
  430. }
  431. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  432. const u8 *key, unsigned int keylen)
  433. {
  434. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  435. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  436. struct dynamic_sa_ctl *sa;
  437. int rc = 0;
  438. if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0)
  439. return -EINVAL;
  440. rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
  441. if (rc)
  442. return rc;
  443. if (ctx->sa_in || ctx->sa_out)
  444. crypto4xx_free_sa(ctx);
  445. rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
  446. if (rc)
  447. return rc;
  448. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  449. sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
  450. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  451. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  452. SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
  453. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  454. SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
  455. DIR_INBOUND);
  456. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  457. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  458. SA_SEQ_MASK_ON, SA_MC_DISABLE,
  459. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  460. SA_NOT_COPY_HDR);
  461. sa->sa_command_1.bf.key_len = keylen >> 3;
  462. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  463. key, keylen);
  464. rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
  465. key, keylen);
  466. if (rc) {
  467. pr_err("GCM hash key setting failed = %d\n", rc);
  468. goto err;
  469. }
  470. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  471. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  472. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  473. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
  474. return 0;
  475. err:
  476. crypto4xx_free_sa(ctx);
  477. return rc;
  478. }
  479. static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
  480. bool decrypt)
  481. {
  482. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  483. struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
  484. __le32 iv[4];
  485. unsigned int len = req->cryptlen;
  486. if (decrypt)
  487. len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
  488. if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
  489. return crypto4xx_aead_fallback(req, ctx, decrypt);
  490. crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
  491. iv[3] = cpu_to_le32(1);
  492. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  493. len, iv, sizeof(iv),
  494. decrypt ? ctx->sa_in : ctx->sa_out,
  495. ctx->sa_len, req->assoclen, rctx->dst);
  496. }
  497. int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
  498. {
  499. return crypto4xx_crypt_aes_gcm(req, false);
  500. }
  501. int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
  502. {
  503. return crypto4xx_crypt_aes_gcm(req, true);
  504. }
  505. /*
  506. * HASH SHA1 Functions
  507. */
  508. static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
  509. unsigned int sa_len,
  510. unsigned char ha,
  511. unsigned char hm)
  512. {
  513. struct crypto_alg *alg = tfm->__crt_alg;
  514. struct crypto4xx_alg *my_alg;
  515. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  516. struct dynamic_sa_hash160 *sa;
  517. int rc;
  518. my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
  519. alg.u.hash);
  520. ctx->dev = my_alg->dev;
  521. /* Create SA */
  522. if (ctx->sa_in || ctx->sa_out)
  523. crypto4xx_free_sa(ctx);
  524. rc = crypto4xx_alloc_sa(ctx, sa_len);
  525. if (rc)
  526. return rc;
  527. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  528. sizeof(struct crypto4xx_ctx));
  529. sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
  530. set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  531. SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
  532. SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
  533. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  534. SA_OPCODE_HASH, DIR_INBOUND);
  535. set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
  536. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  537. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  538. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  539. SA_NOT_COPY_HDR);
  540. /* Need to zero hash digest in SA */
  541. memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
  542. memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
  543. return 0;
  544. }
  545. int crypto4xx_hash_init(struct ahash_request *req)
  546. {
  547. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  548. int ds;
  549. struct dynamic_sa_ctl *sa;
  550. sa = ctx->sa_in;
  551. ds = crypto_ahash_digestsize(
  552. __crypto_ahash_cast(req->base.tfm));
  553. sa->sa_command_0.bf.digest_len = ds >> 2;
  554. sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
  555. return 0;
  556. }
  557. int crypto4xx_hash_update(struct ahash_request *req)
  558. {
  559. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  560. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  561. struct scatterlist dst;
  562. unsigned int ds = crypto_ahash_digestsize(ahash);
  563. sg_init_one(&dst, req->result, ds);
  564. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  565. req->nbytes, NULL, 0, ctx->sa_in,
  566. ctx->sa_len, 0, NULL);
  567. }
  568. int crypto4xx_hash_final(struct ahash_request *req)
  569. {
  570. return 0;
  571. }
  572. int crypto4xx_hash_digest(struct ahash_request *req)
  573. {
  574. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  575. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  576. struct scatterlist dst;
  577. unsigned int ds = crypto_ahash_digestsize(ahash);
  578. sg_init_one(&dst, req->result, ds);
  579. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  580. req->nbytes, NULL, 0, ctx->sa_in,
  581. ctx->sa_len, 0, NULL);
  582. }
  583. /*
  584. * SHA1 Algorithm
  585. */
  586. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
  587. {
  588. return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
  589. SA_HASH_MODE_HASH);
  590. }