common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/types.h>
  16. #include <crypto/scatterwalk.h>
  17. #include <crypto/sha.h>
  18. #include "cipher.h"
  19. #include "common.h"
  20. #include "core.h"
  21. #include "regs-v5.h"
  22. #include "sha.h"
  23. #define QCE_SECTOR_SIZE 512
  24. static inline u32 qce_read(struct qce_device *qce, u32 offset)
  25. {
  26. return readl(qce->base + offset);
  27. }
  28. static inline void qce_write(struct qce_device *qce, u32 offset, u32 val)
  29. {
  30. writel(val, qce->base + offset);
  31. }
  32. static inline void qce_write_array(struct qce_device *qce, u32 offset,
  33. const u32 *val, unsigned int len)
  34. {
  35. int i;
  36. for (i = 0; i < len; i++)
  37. qce_write(qce, offset + i * sizeof(u32), val[i]);
  38. }
  39. static inline void
  40. qce_clear_array(struct qce_device *qce, u32 offset, unsigned int len)
  41. {
  42. int i;
  43. for (i = 0; i < len; i++)
  44. qce_write(qce, offset + i * sizeof(u32), 0);
  45. }
  46. static u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size)
  47. {
  48. u32 cfg = 0;
  49. if (IS_AES(flags)) {
  50. if (aes_key_size == AES_KEYSIZE_128)
  51. cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT;
  52. else if (aes_key_size == AES_KEYSIZE_256)
  53. cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT;
  54. }
  55. if (IS_AES(flags))
  56. cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT;
  57. else if (IS_DES(flags) || IS_3DES(flags))
  58. cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT;
  59. if (IS_DES(flags))
  60. cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT;
  61. if (IS_3DES(flags))
  62. cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT;
  63. switch (flags & QCE_MODE_MASK) {
  64. case QCE_MODE_ECB:
  65. cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT;
  66. break;
  67. case QCE_MODE_CBC:
  68. cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT;
  69. break;
  70. case QCE_MODE_CTR:
  71. cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT;
  72. break;
  73. case QCE_MODE_XTS:
  74. cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT;
  75. break;
  76. case QCE_MODE_CCM:
  77. cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT;
  78. cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT;
  79. break;
  80. default:
  81. return ~0;
  82. }
  83. return cfg;
  84. }
  85. static u32 qce_auth_cfg(unsigned long flags, u32 key_size)
  86. {
  87. u32 cfg = 0;
  88. if (IS_AES(flags) && (IS_CCM(flags) || IS_CMAC(flags)))
  89. cfg |= AUTH_ALG_AES << AUTH_ALG_SHIFT;
  90. else
  91. cfg |= AUTH_ALG_SHA << AUTH_ALG_SHIFT;
  92. if (IS_CCM(flags) || IS_CMAC(flags)) {
  93. if (key_size == AES_KEYSIZE_128)
  94. cfg |= AUTH_KEY_SZ_AES128 << AUTH_KEY_SIZE_SHIFT;
  95. else if (key_size == AES_KEYSIZE_256)
  96. cfg |= AUTH_KEY_SZ_AES256 << AUTH_KEY_SIZE_SHIFT;
  97. }
  98. if (IS_SHA1(flags) || IS_SHA1_HMAC(flags))
  99. cfg |= AUTH_SIZE_SHA1 << AUTH_SIZE_SHIFT;
  100. else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags))
  101. cfg |= AUTH_SIZE_SHA256 << AUTH_SIZE_SHIFT;
  102. else if (IS_CMAC(flags))
  103. cfg |= AUTH_SIZE_ENUM_16_BYTES << AUTH_SIZE_SHIFT;
  104. if (IS_SHA1(flags) || IS_SHA256(flags))
  105. cfg |= AUTH_MODE_HASH << AUTH_MODE_SHIFT;
  106. else if (IS_SHA1_HMAC(flags) || IS_SHA256_HMAC(flags) ||
  107. IS_CBC(flags) || IS_CTR(flags))
  108. cfg |= AUTH_MODE_HMAC << AUTH_MODE_SHIFT;
  109. else if (IS_AES(flags) && IS_CCM(flags))
  110. cfg |= AUTH_MODE_CCM << AUTH_MODE_SHIFT;
  111. else if (IS_AES(flags) && IS_CMAC(flags))
  112. cfg |= AUTH_MODE_CMAC << AUTH_MODE_SHIFT;
  113. if (IS_SHA(flags) || IS_SHA_HMAC(flags))
  114. cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT;
  115. if (IS_CCM(flags))
  116. cfg |= QCE_MAX_NONCE_WORDS << AUTH_NONCE_NUM_WORDS_SHIFT;
  117. if (IS_CBC(flags) || IS_CTR(flags) || IS_CCM(flags) ||
  118. IS_CMAC(flags))
  119. cfg |= BIT(AUTH_LAST_SHIFT) | BIT(AUTH_FIRST_SHIFT);
  120. return cfg;
  121. }
  122. static u32 qce_config_reg(struct qce_device *qce, int little)
  123. {
  124. u32 beats = (qce->burst_size >> 3) - 1;
  125. u32 pipe_pair = qce->pipe_pair_id;
  126. u32 config;
  127. config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK;
  128. config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) |
  129. BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT);
  130. config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK;
  131. config &= ~HIGH_SPD_EN_N_SHIFT;
  132. if (little)
  133. config |= BIT(LITTLE_ENDIAN_MODE_SHIFT);
  134. return config;
  135. }
  136. void qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len)
  137. {
  138. __be32 *d = dst;
  139. const u8 *s = src;
  140. unsigned int n;
  141. n = len / sizeof(u32);
  142. for (; n > 0; n--) {
  143. *d = cpu_to_be32p((const __u32 *) s);
  144. s += sizeof(__u32);
  145. d++;
  146. }
  147. }
  148. static void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize)
  149. {
  150. u8 swap[QCE_AES_IV_LENGTH];
  151. u32 i, j;
  152. if (ivsize > QCE_AES_IV_LENGTH)
  153. return;
  154. memset(swap, 0, QCE_AES_IV_LENGTH);
  155. for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1;
  156. i < QCE_AES_IV_LENGTH; i++, j--)
  157. swap[i] = src[j];
  158. qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH);
  159. }
  160. static void qce_xtskey(struct qce_device *qce, const u8 *enckey,
  161. unsigned int enckeylen, unsigned int cryptlen)
  162. {
  163. u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0};
  164. unsigned int xtsklen = enckeylen / (2 * sizeof(u32));
  165. unsigned int xtsdusize;
  166. qce_cpu_to_be32p_array((__be32 *)xtskey, enckey + enckeylen / 2,
  167. enckeylen / 2);
  168. qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen);
  169. /* xts du size 512B */
  170. xtsdusize = min_t(u32, QCE_SECTOR_SIZE, cryptlen);
  171. qce_write(qce, REG_ENCR_XTS_DU_SIZE, xtsdusize);
  172. }
  173. static void qce_setup_config(struct qce_device *qce)
  174. {
  175. u32 config;
  176. /* get big endianness */
  177. config = qce_config_reg(qce, 0);
  178. /* clear status */
  179. qce_write(qce, REG_STATUS, 0);
  180. qce_write(qce, REG_CONFIG, config);
  181. }
  182. static inline void qce_crypto_go(struct qce_device *qce)
  183. {
  184. qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT));
  185. }
  186. static int qce_setup_regs_ahash(struct crypto_async_request *async_req,
  187. u32 totallen, u32 offset)
  188. {
  189. struct ahash_request *req = ahash_request_cast(async_req);
  190. struct crypto_ahash *ahash = __crypto_ahash_cast(async_req->tfm);
  191. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  192. struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
  193. struct qce_device *qce = tmpl->qce;
  194. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  195. unsigned int blocksize = crypto_tfm_alg_blocksize(async_req->tfm);
  196. __be32 auth[SHA256_DIGEST_SIZE / sizeof(__be32)] = {0};
  197. __be32 mackey[QCE_SHA_HMAC_KEY_SIZE / sizeof(__be32)] = {0};
  198. u32 auth_cfg = 0, config;
  199. unsigned int iv_words;
  200. /* if not the last, the size has to be on the block boundary */
  201. if (!rctx->last_blk && req->nbytes % blocksize)
  202. return -EINVAL;
  203. qce_setup_config(qce);
  204. if (IS_CMAC(rctx->flags)) {
  205. qce_write(qce, REG_AUTH_SEG_CFG, 0);
  206. qce_write(qce, REG_ENCR_SEG_CFG, 0);
  207. qce_write(qce, REG_ENCR_SEG_SIZE, 0);
  208. qce_clear_array(qce, REG_AUTH_IV0, 16);
  209. qce_clear_array(qce, REG_AUTH_KEY0, 16);
  210. qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
  211. auth_cfg = qce_auth_cfg(rctx->flags, rctx->authklen);
  212. }
  213. if (IS_SHA_HMAC(rctx->flags) || IS_CMAC(rctx->flags)) {
  214. u32 authkey_words = rctx->authklen / sizeof(u32);
  215. qce_cpu_to_be32p_array(mackey, rctx->authkey, rctx->authklen);
  216. qce_write_array(qce, REG_AUTH_KEY0, (u32 *)mackey,
  217. authkey_words);
  218. }
  219. if (IS_CMAC(rctx->flags))
  220. goto go_proc;
  221. if (rctx->first_blk)
  222. memcpy(auth, rctx->digest, digestsize);
  223. else
  224. qce_cpu_to_be32p_array(auth, rctx->digest, digestsize);
  225. iv_words = (IS_SHA1(rctx->flags) || IS_SHA1_HMAC(rctx->flags)) ? 5 : 8;
  226. qce_write_array(qce, REG_AUTH_IV0, (u32 *)auth, iv_words);
  227. if (rctx->first_blk)
  228. qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
  229. else
  230. qce_write_array(qce, REG_AUTH_BYTECNT0,
  231. (u32 *)rctx->byte_count, 2);
  232. auth_cfg = qce_auth_cfg(rctx->flags, 0);
  233. if (rctx->last_blk)
  234. auth_cfg |= BIT(AUTH_LAST_SHIFT);
  235. else
  236. auth_cfg &= ~BIT(AUTH_LAST_SHIFT);
  237. if (rctx->first_blk)
  238. auth_cfg |= BIT(AUTH_FIRST_SHIFT);
  239. else
  240. auth_cfg &= ~BIT(AUTH_FIRST_SHIFT);
  241. go_proc:
  242. qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
  243. qce_write(qce, REG_AUTH_SEG_SIZE, req->nbytes);
  244. qce_write(qce, REG_AUTH_SEG_START, 0);
  245. qce_write(qce, REG_ENCR_SEG_CFG, 0);
  246. qce_write(qce, REG_SEG_SIZE, req->nbytes);
  247. /* get little endianness */
  248. config = qce_config_reg(qce, 1);
  249. qce_write(qce, REG_CONFIG, config);
  250. qce_crypto_go(qce);
  251. return 0;
  252. }
  253. static int qce_setup_regs_ablkcipher(struct crypto_async_request *async_req,
  254. u32 totallen, u32 offset)
  255. {
  256. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  257. struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
  258. struct qce_cipher_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
  259. struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm);
  260. struct qce_device *qce = tmpl->qce;
  261. __be32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(__be32)] = {0};
  262. __be32 enciv[QCE_MAX_IV_SIZE / sizeof(__be32)] = {0};
  263. unsigned int enckey_words, enciv_words;
  264. unsigned int keylen;
  265. u32 encr_cfg = 0, auth_cfg = 0, config;
  266. unsigned int ivsize = rctx->ivsize;
  267. unsigned long flags = rctx->flags;
  268. qce_setup_config(qce);
  269. if (IS_XTS(flags))
  270. keylen = ctx->enc_keylen / 2;
  271. else
  272. keylen = ctx->enc_keylen;
  273. qce_cpu_to_be32p_array(enckey, ctx->enc_key, keylen);
  274. enckey_words = keylen / sizeof(u32);
  275. qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
  276. encr_cfg = qce_encr_cfg(flags, keylen);
  277. if (IS_DES(flags)) {
  278. enciv_words = 2;
  279. enckey_words = 2;
  280. } else if (IS_3DES(flags)) {
  281. enciv_words = 2;
  282. enckey_words = 6;
  283. } else if (IS_AES(flags)) {
  284. if (IS_XTS(flags))
  285. qce_xtskey(qce, ctx->enc_key, ctx->enc_keylen,
  286. rctx->cryptlen);
  287. enciv_words = 4;
  288. } else {
  289. return -EINVAL;
  290. }
  291. qce_write_array(qce, REG_ENCR_KEY0, (u32 *)enckey, enckey_words);
  292. if (!IS_ECB(flags)) {
  293. if (IS_XTS(flags))
  294. qce_xts_swapiv(enciv, rctx->iv, ivsize);
  295. else
  296. qce_cpu_to_be32p_array(enciv, rctx->iv, ivsize);
  297. qce_write_array(qce, REG_CNTR0_IV0, (u32 *)enciv, enciv_words);
  298. }
  299. if (IS_ENCRYPT(flags))
  300. encr_cfg |= BIT(ENCODE_SHIFT);
  301. qce_write(qce, REG_ENCR_SEG_CFG, encr_cfg);
  302. qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen);
  303. qce_write(qce, REG_ENCR_SEG_START, offset & 0xffff);
  304. if (IS_CTR(flags)) {
  305. qce_write(qce, REG_CNTR_MASK, ~0);
  306. qce_write(qce, REG_CNTR_MASK0, ~0);
  307. qce_write(qce, REG_CNTR_MASK1, ~0);
  308. qce_write(qce, REG_CNTR_MASK2, ~0);
  309. }
  310. qce_write(qce, REG_SEG_SIZE, totallen);
  311. /* get little endianness */
  312. config = qce_config_reg(qce, 1);
  313. qce_write(qce, REG_CONFIG, config);
  314. qce_crypto_go(qce);
  315. return 0;
  316. }
  317. int qce_start(struct crypto_async_request *async_req, u32 type, u32 totallen,
  318. u32 offset)
  319. {
  320. switch (type) {
  321. case CRYPTO_ALG_TYPE_ABLKCIPHER:
  322. return qce_setup_regs_ablkcipher(async_req, totallen, offset);
  323. case CRYPTO_ALG_TYPE_AHASH:
  324. return qce_setup_regs_ahash(async_req, totallen, offset);
  325. default:
  326. return -EINVAL;
  327. }
  328. }
  329. #define STATUS_ERRORS \
  330. (BIT(SW_ERR_SHIFT) | BIT(AXI_ERR_SHIFT) | BIT(HSD_ERR_SHIFT))
  331. int qce_check_status(struct qce_device *qce, u32 *status)
  332. {
  333. int ret = 0;
  334. *status = qce_read(qce, REG_STATUS);
  335. /*
  336. * Don't use result dump status. The operation may not be complete.
  337. * Instead, use the status we just read from device. In case, we need to
  338. * use result_status from result dump the result_status needs to be byte
  339. * swapped, since we set the device to little endian.
  340. */
  341. if (*status & STATUS_ERRORS || !(*status & BIT(OPERATION_DONE_SHIFT)))
  342. ret = -ENXIO;
  343. return ret;
  344. }
  345. void qce_get_version(struct qce_device *qce, u32 *major, u32 *minor, u32 *step)
  346. {
  347. u32 val;
  348. val = qce_read(qce, REG_VERSION);
  349. *major = (val & CORE_MAJOR_REV_MASK) >> CORE_MAJOR_REV_SHIFT;
  350. *minor = (val & CORE_MINOR_REV_MASK) >> CORE_MINOR_REV_SHIFT;
  351. *step = (val & CORE_STEP_REV_MASK) >> CORE_STEP_REV_SHIFT;
  352. }