crypto4xx_alg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /**
  2. * AMCC SoC PPC4xx Crypto Driver
  3. *
  4. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  5. * All rights reserved. James Hsiao <jhsiao@amcc.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * This file implements the Linux crypto algorithms.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/spinlock_types.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/crypto.h>
  24. #include <linux/hash.h>
  25. #include <crypto/internal/hash.h>
  26. #include <linux/dma-mapping.h>
  27. #include <crypto/algapi.h>
  28. #include <crypto/aead.h>
  29. #include <crypto/aes.h>
  30. #include <crypto/gcm.h>
  31. #include <crypto/sha.h>
  32. #include <crypto/ctr.h>
  33. #include "crypto4xx_reg_def.h"
  34. #include "crypto4xx_core.h"
  35. #include "crypto4xx_sa.h"
  36. static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
  37. u32 save_iv, u32 ld_h, u32 ld_iv,
  38. u32 hdr_proc, u32 h, u32 c, u32 pad_type,
  39. u32 op_grp, u32 op, u32 dir)
  40. {
  41. sa->sa_command_0.w = 0;
  42. sa->sa_command_0.bf.save_hash_state = save_h;
  43. sa->sa_command_0.bf.save_iv = save_iv;
  44. sa->sa_command_0.bf.load_hash_state = ld_h;
  45. sa->sa_command_0.bf.load_iv = ld_iv;
  46. sa->sa_command_0.bf.hdr_proc = hdr_proc;
  47. sa->sa_command_0.bf.hash_alg = h;
  48. sa->sa_command_0.bf.cipher_alg = c;
  49. sa->sa_command_0.bf.pad_type = pad_type & 3;
  50. sa->sa_command_0.bf.extend_pad = pad_type >> 2;
  51. sa->sa_command_0.bf.op_group = op_grp;
  52. sa->sa_command_0.bf.opcode = op;
  53. sa->sa_command_0.bf.dir = dir;
  54. }
  55. static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
  56. u32 hmac_mc, u32 cfb, u32 esn,
  57. u32 sn_mask, u32 mute, u32 cp_pad,
  58. u32 cp_pay, u32 cp_hdr)
  59. {
  60. sa->sa_command_1.w = 0;
  61. sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
  62. sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
  63. sa->sa_command_1.bf.feedback_mode = cfb,
  64. sa->sa_command_1.bf.sa_rev = 1;
  65. sa->sa_command_1.bf.hmac_muting = hmac_mc;
  66. sa->sa_command_1.bf.extended_seq_num = esn;
  67. sa->sa_command_1.bf.seq_num_mask = sn_mask;
  68. sa->sa_command_1.bf.mutable_bit_proc = mute;
  69. sa->sa_command_1.bf.copy_pad = cp_pad;
  70. sa->sa_command_1.bf.copy_payload = cp_pay;
  71. sa->sa_command_1.bf.copy_hdr = cp_hdr;
  72. }
  73. int crypto4xx_encrypt(struct ablkcipher_request *req)
  74. {
  75. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  76. unsigned int ivlen = crypto_ablkcipher_ivsize(
  77. crypto_ablkcipher_reqtfm(req));
  78. __le32 iv[ivlen];
  79. if (ivlen)
  80. crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
  81. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  82. req->nbytes, iv, ivlen, ctx->sa_out, ctx->sa_len, 0);
  83. }
  84. int crypto4xx_decrypt(struct ablkcipher_request *req)
  85. {
  86. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  87. unsigned int ivlen = crypto_ablkcipher_ivsize(
  88. crypto_ablkcipher_reqtfm(req));
  89. __le32 iv[ivlen];
  90. if (ivlen)
  91. crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
  92. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  93. req->nbytes, iv, ivlen, ctx->sa_in, ctx->sa_len, 0);
  94. }
  95. /**
  96. * AES Functions
  97. */
  98. static int crypto4xx_setkey_aes(struct crypto_ablkcipher *cipher,
  99. const u8 *key,
  100. unsigned int keylen,
  101. unsigned char cm,
  102. u8 fb)
  103. {
  104. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  105. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  106. struct dynamic_sa_ctl *sa;
  107. int rc;
  108. if (keylen != AES_KEYSIZE_256 &&
  109. keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
  110. crypto_ablkcipher_set_flags(cipher,
  111. CRYPTO_TFM_RES_BAD_KEY_LEN);
  112. return -EINVAL;
  113. }
  114. /* Create SA */
  115. if (ctx->sa_in || ctx->sa_out)
  116. crypto4xx_free_sa(ctx);
  117. rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
  118. if (rc)
  119. return rc;
  120. /* Setup SA */
  121. sa = ctx->sa_in;
  122. set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, SA_NOT_SAVE_IV,
  123. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  124. SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
  125. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  126. SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
  127. DIR_INBOUND);
  128. set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
  129. fb, SA_EXTENDED_SN_OFF,
  130. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  131. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  132. SA_NOT_COPY_HDR);
  133. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  134. key, keylen);
  135. sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
  136. sa->sa_command_1.bf.key_len = keylen >> 3;
  137. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  138. sa = ctx->sa_out;
  139. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  140. return 0;
  141. }
  142. int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
  143. const u8 *key, unsigned int keylen)
  144. {
  145. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
  146. CRYPTO_FEEDBACK_MODE_NO_FB);
  147. }
  148. int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
  149. const u8 *key, unsigned int keylen)
  150. {
  151. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
  152. CRYPTO_FEEDBACK_MODE_128BIT_CFB);
  153. }
  154. int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
  155. const u8 *key, unsigned int keylen)
  156. {
  157. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
  158. CRYPTO_FEEDBACK_MODE_NO_FB);
  159. }
  160. int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
  161. const u8 *key, unsigned int keylen)
  162. {
  163. return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
  164. CRYPTO_FEEDBACK_MODE_64BIT_OFB);
  165. }
  166. int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
  167. const u8 *key, unsigned int keylen)
  168. {
  169. struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
  170. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  171. int rc;
  172. rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
  173. CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
  174. if (rc)
  175. return rc;
  176. ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
  177. CTR_RFC3686_NONCE_SIZE]);
  178. return 0;
  179. }
  180. int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
  181. {
  182. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  183. __le32 iv[AES_IV_SIZE / 4] = {
  184. ctx->iv_nonce,
  185. cpu_to_le32p((u32 *) req->info),
  186. cpu_to_le32p((u32 *) (req->info + 4)),
  187. cpu_to_le32(1) };
  188. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  189. req->nbytes, iv, AES_IV_SIZE,
  190. ctx->sa_out, ctx->sa_len, 0);
  191. }
  192. int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
  193. {
  194. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  195. __le32 iv[AES_IV_SIZE / 4] = {
  196. ctx->iv_nonce,
  197. cpu_to_le32p((u32 *) req->info),
  198. cpu_to_le32p((u32 *) (req->info + 4)),
  199. cpu_to_le32(1) };
  200. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  201. req->nbytes, iv, AES_IV_SIZE,
  202. ctx->sa_out, ctx->sa_len, 0);
  203. }
  204. static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
  205. bool is_ccm, bool decrypt)
  206. {
  207. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  208. /* authsize has to be a multiple of 4 */
  209. if (aead->authsize & 3)
  210. return true;
  211. /*
  212. * hardware does not handle cases where cryptlen
  213. * is less than a block
  214. */
  215. if (req->cryptlen < AES_BLOCK_SIZE)
  216. return true;
  217. /* assoc len needs to be a multiple of 4 */
  218. if (req->assoclen & 0x3)
  219. return true;
  220. /* CCM supports only counter field length of 2 and 4 bytes */
  221. if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
  222. return true;
  223. return false;
  224. }
  225. static int crypto4xx_aead_fallback(struct aead_request *req,
  226. struct crypto4xx_ctx *ctx, bool do_decrypt)
  227. {
  228. char aead_req_data[sizeof(struct aead_request) +
  229. crypto_aead_reqsize(ctx->sw_cipher.aead)]
  230. __aligned(__alignof__(struct aead_request));
  231. struct aead_request *subreq = (void *) aead_req_data;
  232. memset(subreq, 0, sizeof(aead_req_data));
  233. aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
  234. aead_request_set_callback(subreq, req->base.flags,
  235. req->base.complete, req->base.data);
  236. aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  237. req->iv);
  238. aead_request_set_ad(subreq, req->assoclen);
  239. return do_decrypt ? crypto_aead_decrypt(subreq) :
  240. crypto_aead_encrypt(subreq);
  241. }
  242. static int crypto4xx_setup_fallback(struct crypto4xx_ctx *ctx,
  243. struct crypto_aead *cipher,
  244. const u8 *key,
  245. unsigned int keylen)
  246. {
  247. int rc;
  248. crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
  249. crypto_aead_set_flags(ctx->sw_cipher.aead,
  250. crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
  251. rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
  252. crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
  253. crypto_aead_set_flags(cipher,
  254. crypto_aead_get_flags(ctx->sw_cipher.aead) &
  255. CRYPTO_TFM_RES_MASK);
  256. return rc;
  257. }
  258. /**
  259. * AES-CCM Functions
  260. */
  261. int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
  262. unsigned int keylen)
  263. {
  264. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  265. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  266. struct dynamic_sa_ctl *sa;
  267. int rc = 0;
  268. rc = crypto4xx_setup_fallback(ctx, cipher, key, keylen);
  269. if (rc)
  270. return rc;
  271. if (ctx->sa_in || ctx->sa_out)
  272. crypto4xx_free_sa(ctx);
  273. rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
  274. if (rc)
  275. return rc;
  276. /* Setup SA */
  277. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  278. sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
  279. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  280. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  281. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  282. SA_CIPHER_ALG_AES,
  283. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  284. SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
  285. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  286. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  287. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  288. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  289. SA_NOT_COPY_HDR);
  290. sa->sa_command_1.bf.key_len = keylen >> 3;
  291. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
  292. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  293. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  294. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  295. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  296. SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
  297. SA_CIPHER_ALG_AES,
  298. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  299. SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
  300. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  301. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  302. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  303. SA_COPY_PAD, SA_COPY_PAYLOAD,
  304. SA_NOT_COPY_HDR);
  305. sa->sa_command_1.bf.key_len = keylen >> 3;
  306. return 0;
  307. }
  308. static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
  309. {
  310. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  311. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  312. unsigned int len = req->cryptlen;
  313. __le32 iv[16];
  314. u32 tmp_sa[ctx->sa_len * 4];
  315. struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
  316. if (crypto4xx_aead_need_fallback(req, true, decrypt))
  317. return crypto4xx_aead_fallback(req, ctx, decrypt);
  318. if (decrypt)
  319. len -= crypto_aead_authsize(aead);
  320. memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, sizeof(tmp_sa));
  321. sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
  322. if (req->iv[0] == 1) {
  323. /* CRYPTO_MODE_AES_ICM */
  324. sa->sa_command_1.bf.crypto_mode9_8 = 1;
  325. }
  326. iv[3] = cpu_to_le32(0);
  327. crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
  328. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  329. len, iv, sizeof(iv),
  330. sa, ctx->sa_len, req->assoclen);
  331. }
  332. int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
  333. {
  334. return crypto4xx_crypt_aes_ccm(req, false);
  335. }
  336. int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
  337. {
  338. return crypto4xx_crypt_aes_ccm(req, true);
  339. }
  340. int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
  341. unsigned int authsize)
  342. {
  343. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  344. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  345. return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
  346. }
  347. /**
  348. * AES-GCM Functions
  349. */
  350. static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
  351. {
  352. switch (keylen) {
  353. case 16:
  354. case 24:
  355. case 32:
  356. return 0;
  357. default:
  358. return -EINVAL;
  359. }
  360. }
  361. static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
  362. unsigned int keylen)
  363. {
  364. struct crypto_cipher *aes_tfm = NULL;
  365. uint8_t src[16] = { 0 };
  366. int rc = 0;
  367. aes_tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC |
  368. CRYPTO_ALG_NEED_FALLBACK);
  369. if (IS_ERR(aes_tfm)) {
  370. rc = PTR_ERR(aes_tfm);
  371. pr_warn("could not load aes cipher driver: %d\n", rc);
  372. return rc;
  373. }
  374. rc = crypto_cipher_setkey(aes_tfm, key, keylen);
  375. if (rc) {
  376. pr_err("setkey() failed: %d\n", rc);
  377. goto out;
  378. }
  379. crypto_cipher_encrypt_one(aes_tfm, src, src);
  380. crypto4xx_memcpy_to_le32(hash_start, src, 16);
  381. out:
  382. crypto_free_cipher(aes_tfm);
  383. return rc;
  384. }
  385. int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
  386. const u8 *key, unsigned int keylen)
  387. {
  388. struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
  389. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  390. struct dynamic_sa_ctl *sa;
  391. int rc = 0;
  392. if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
  393. crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
  394. return -EINVAL;
  395. }
  396. rc = crypto4xx_setup_fallback(ctx, cipher, key, keylen);
  397. if (rc)
  398. return rc;
  399. if (ctx->sa_in || ctx->sa_out)
  400. crypto4xx_free_sa(ctx);
  401. rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
  402. if (rc)
  403. return rc;
  404. sa = (struct dynamic_sa_ctl *) ctx->sa_in;
  405. sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
  406. set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  407. SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
  408. SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
  409. SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
  410. SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
  411. DIR_INBOUND);
  412. set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
  413. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  414. SA_SEQ_MASK_ON, SA_MC_DISABLE,
  415. SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
  416. SA_NOT_COPY_HDR);
  417. sa->sa_command_1.bf.key_len = keylen >> 3;
  418. crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
  419. key, keylen);
  420. rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
  421. key, keylen);
  422. if (rc) {
  423. pr_err("GCM hash key setting failed = %d\n", rc);
  424. goto err;
  425. }
  426. memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
  427. sa = (struct dynamic_sa_ctl *) ctx->sa_out;
  428. sa->sa_command_0.bf.dir = DIR_OUTBOUND;
  429. sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
  430. return 0;
  431. err:
  432. crypto4xx_free_sa(ctx);
  433. return rc;
  434. }
  435. static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
  436. bool decrypt)
  437. {
  438. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  439. unsigned int len = req->cryptlen;
  440. __le32 iv[4];
  441. if (crypto4xx_aead_need_fallback(req, false, decrypt))
  442. return crypto4xx_aead_fallback(req, ctx, decrypt);
  443. crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
  444. iv[3] = cpu_to_le32(1);
  445. if (decrypt)
  446. len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
  447. return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
  448. len, iv, sizeof(iv),
  449. decrypt ? ctx->sa_in : ctx->sa_out,
  450. ctx->sa_len, req->assoclen);
  451. }
  452. int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
  453. {
  454. return crypto4xx_crypt_aes_gcm(req, false);
  455. }
  456. int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
  457. {
  458. return crypto4xx_crypt_aes_gcm(req, true);
  459. }
  460. /**
  461. * HASH SHA1 Functions
  462. */
  463. static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
  464. unsigned int sa_len,
  465. unsigned char ha,
  466. unsigned char hm)
  467. {
  468. struct crypto_alg *alg = tfm->__crt_alg;
  469. struct crypto4xx_alg *my_alg;
  470. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
  471. struct dynamic_sa_hash160 *sa;
  472. int rc;
  473. my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
  474. alg.u.hash);
  475. ctx->dev = my_alg->dev;
  476. /* Create SA */
  477. if (ctx->sa_in || ctx->sa_out)
  478. crypto4xx_free_sa(ctx);
  479. rc = crypto4xx_alloc_sa(ctx, sa_len);
  480. if (rc)
  481. return rc;
  482. crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  483. sizeof(struct crypto4xx_ctx));
  484. sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
  485. set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
  486. SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
  487. SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
  488. SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
  489. SA_OPCODE_HASH, DIR_INBOUND);
  490. set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
  491. CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
  492. SA_SEQ_MASK_OFF, SA_MC_ENABLE,
  493. SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
  494. SA_NOT_COPY_HDR);
  495. /* Need to zero hash digest in SA */
  496. memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
  497. memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
  498. return 0;
  499. }
  500. int crypto4xx_hash_init(struct ahash_request *req)
  501. {
  502. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  503. int ds;
  504. struct dynamic_sa_ctl *sa;
  505. sa = ctx->sa_in;
  506. ds = crypto_ahash_digestsize(
  507. __crypto_ahash_cast(req->base.tfm));
  508. sa->sa_command_0.bf.digest_len = ds >> 2;
  509. sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
  510. return 0;
  511. }
  512. int crypto4xx_hash_update(struct ahash_request *req)
  513. {
  514. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  515. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  516. struct scatterlist dst;
  517. unsigned int ds = crypto_ahash_digestsize(ahash);
  518. sg_init_one(&dst, req->result, ds);
  519. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  520. req->nbytes, NULL, 0, ctx->sa_in,
  521. ctx->sa_len, 0);
  522. }
  523. int crypto4xx_hash_final(struct ahash_request *req)
  524. {
  525. return 0;
  526. }
  527. int crypto4xx_hash_digest(struct ahash_request *req)
  528. {
  529. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  530. struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  531. struct scatterlist dst;
  532. unsigned int ds = crypto_ahash_digestsize(ahash);
  533. sg_init_one(&dst, req->result, ds);
  534. return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
  535. req->nbytes, NULL, 0, ctx->sa_in,
  536. ctx->sa_len, 0);
  537. }
  538. /**
  539. * SHA1 Algorithm
  540. */
  541. int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
  542. {
  543. return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
  544. SA_HASH_MODE_HASH);
  545. }