omap-aes-gcm.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for OMAP AES GCM HW acceleration.
  5. *
  6. * Copyright (c) 2016 Texas Instruments Incorporated
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmaengine.h>
  17. #include <linux/omap-dma.h>
  18. #include <linux/interrupt.h>
  19. #include <crypto/aes.h>
  20. #include <crypto/gcm.h>
  21. #include <crypto/scatterwalk.h>
  22. #include <crypto/skcipher.h>
  23. #include <crypto/internal/aead.h>
  24. #include "omap-crypto.h"
  25. #include "omap-aes.h"
  26. static int omap_aes_gcm_handle_queue(struct omap_aes_dev *dd,
  27. struct aead_request *req);
  28. static void omap_aes_gcm_finish_req(struct omap_aes_dev *dd, int ret)
  29. {
  30. struct aead_request *req = dd->aead_req;
  31. dd->flags &= ~FLAGS_BUSY;
  32. dd->in_sg = NULL;
  33. dd->out_sg = NULL;
  34. req->base.complete(&req->base, ret);
  35. }
  36. static void omap_aes_gcm_done_task(struct omap_aes_dev *dd)
  37. {
  38. u8 *tag;
  39. int alen, clen, i, ret = 0, nsg;
  40. struct omap_aes_reqctx *rctx;
  41. alen = ALIGN(dd->assoc_len, AES_BLOCK_SIZE);
  42. clen = ALIGN(dd->total, AES_BLOCK_SIZE);
  43. rctx = aead_request_ctx(dd->aead_req);
  44. nsg = !!(dd->assoc_len && dd->total);
  45. dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
  46. DMA_FROM_DEVICE);
  47. dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
  48. dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, DMA_FROM_DEVICE);
  49. omap_aes_crypt_dma_stop(dd);
  50. omap_crypto_cleanup(dd->out_sg, dd->orig_out,
  51. dd->aead_req->assoclen, dd->total,
  52. FLAGS_OUT_DATA_ST_SHIFT, dd->flags);
  53. if (dd->flags & FLAGS_ENCRYPT)
  54. scatterwalk_map_and_copy(rctx->auth_tag,
  55. dd->aead_req->dst,
  56. dd->total + dd->aead_req->assoclen,
  57. dd->authsize, 1);
  58. omap_crypto_cleanup(&dd->in_sgl[0], NULL, 0, alen,
  59. FLAGS_ASSOC_DATA_ST_SHIFT, dd->flags);
  60. omap_crypto_cleanup(&dd->in_sgl[nsg], NULL, 0, clen,
  61. FLAGS_IN_DATA_ST_SHIFT, dd->flags);
  62. if (!(dd->flags & FLAGS_ENCRYPT)) {
  63. tag = (u8 *)rctx->auth_tag;
  64. for (i = 0; i < dd->authsize; i++) {
  65. if (tag[i]) {
  66. dev_err(dd->dev, "GCM decryption: Tag Message is wrong\n");
  67. ret = -EBADMSG;
  68. }
  69. }
  70. }
  71. omap_aes_gcm_finish_req(dd, ret);
  72. omap_aes_gcm_handle_queue(dd, NULL);
  73. }
  74. static int omap_aes_gcm_copy_buffers(struct omap_aes_dev *dd,
  75. struct aead_request *req)
  76. {
  77. int alen, clen, cryptlen, assoclen, ret;
  78. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  79. unsigned int authlen = crypto_aead_authsize(aead);
  80. struct scatterlist *tmp, sg_arr[2];
  81. int nsg;
  82. u16 flags;
  83. assoclen = req->assoclen;
  84. cryptlen = req->cryptlen;
  85. if (dd->flags & FLAGS_RFC4106_GCM)
  86. assoclen -= 8;
  87. if (!(dd->flags & FLAGS_ENCRYPT))
  88. cryptlen -= authlen;
  89. alen = ALIGN(assoclen, AES_BLOCK_SIZE);
  90. clen = ALIGN(cryptlen, AES_BLOCK_SIZE);
  91. nsg = !!(assoclen && cryptlen);
  92. omap_aes_clear_copy_flags(dd);
  93. sg_init_table(dd->in_sgl, nsg + 1);
  94. if (assoclen) {
  95. tmp = req->src;
  96. ret = omap_crypto_align_sg(&tmp, assoclen,
  97. AES_BLOCK_SIZE, dd->in_sgl,
  98. OMAP_CRYPTO_COPY_DATA |
  99. OMAP_CRYPTO_ZERO_BUF |
  100. OMAP_CRYPTO_FORCE_SINGLE_ENTRY,
  101. FLAGS_ASSOC_DATA_ST_SHIFT,
  102. &dd->flags);
  103. }
  104. if (cryptlen) {
  105. tmp = scatterwalk_ffwd(sg_arr, req->src, req->assoclen);
  106. ret = omap_crypto_align_sg(&tmp, cryptlen,
  107. AES_BLOCK_SIZE, &dd->in_sgl[nsg],
  108. OMAP_CRYPTO_COPY_DATA |
  109. OMAP_CRYPTO_ZERO_BUF |
  110. OMAP_CRYPTO_FORCE_SINGLE_ENTRY,
  111. FLAGS_IN_DATA_ST_SHIFT,
  112. &dd->flags);
  113. }
  114. dd->in_sg = dd->in_sgl;
  115. dd->total = cryptlen;
  116. dd->assoc_len = assoclen;
  117. dd->authsize = authlen;
  118. dd->out_sg = req->dst;
  119. dd->orig_out = req->dst;
  120. dd->out_sg = scatterwalk_ffwd(sg_arr, req->dst, assoclen);
  121. flags = 0;
  122. if (req->src == req->dst || dd->out_sg == sg_arr)
  123. flags |= OMAP_CRYPTO_FORCE_COPY;
  124. ret = omap_crypto_align_sg(&dd->out_sg, cryptlen,
  125. AES_BLOCK_SIZE, &dd->out_sgl,
  126. flags,
  127. FLAGS_OUT_DATA_ST_SHIFT, &dd->flags);
  128. if (ret)
  129. return ret;
  130. dd->in_sg_len = sg_nents_for_len(dd->in_sg, alen + clen);
  131. dd->out_sg_len = sg_nents_for_len(dd->out_sg, clen);
  132. return 0;
  133. }
  134. static void omap_aes_gcm_complete(struct crypto_async_request *req, int err)
  135. {
  136. struct omap_aes_gcm_result *res = req->data;
  137. if (err == -EINPROGRESS)
  138. return;
  139. res->err = err;
  140. complete(&res->completion);
  141. }
  142. static int do_encrypt_iv(struct aead_request *req, u32 *tag, u32 *iv)
  143. {
  144. struct scatterlist iv_sg, tag_sg;
  145. struct skcipher_request *sk_req;
  146. struct omap_aes_gcm_result result;
  147. struct omap_aes_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  148. int ret = 0;
  149. sk_req = skcipher_request_alloc(ctx->ctr, GFP_KERNEL);
  150. if (!sk_req) {
  151. pr_err("skcipher: Failed to allocate request\n");
  152. return -ENOMEM;
  153. }
  154. init_completion(&result.completion);
  155. sg_init_one(&iv_sg, iv, AES_BLOCK_SIZE);
  156. sg_init_one(&tag_sg, tag, AES_BLOCK_SIZE);
  157. skcipher_request_set_callback(sk_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  158. omap_aes_gcm_complete, &result);
  159. ret = crypto_skcipher_setkey(ctx->ctr, (u8 *)ctx->key, ctx->keylen);
  160. skcipher_request_set_crypt(sk_req, &iv_sg, &tag_sg, AES_BLOCK_SIZE,
  161. NULL);
  162. ret = crypto_skcipher_encrypt(sk_req);
  163. switch (ret) {
  164. case 0:
  165. break;
  166. case -EINPROGRESS:
  167. case -EBUSY:
  168. ret = wait_for_completion_interruptible(&result.completion);
  169. if (!ret) {
  170. ret = result.err;
  171. if (!ret) {
  172. reinit_completion(&result.completion);
  173. break;
  174. }
  175. }
  176. /* fall through */
  177. default:
  178. pr_err("Encryption of IV failed for GCM mode\n");
  179. break;
  180. }
  181. skcipher_request_free(sk_req);
  182. return ret;
  183. }
  184. void omap_aes_gcm_dma_out_callback(void *data)
  185. {
  186. struct omap_aes_dev *dd = data;
  187. struct omap_aes_reqctx *rctx;
  188. int i, val;
  189. u32 *auth_tag, tag[4];
  190. if (!(dd->flags & FLAGS_ENCRYPT))
  191. scatterwalk_map_and_copy(tag, dd->aead_req->src,
  192. dd->total + dd->aead_req->assoclen,
  193. dd->authsize, 0);
  194. rctx = aead_request_ctx(dd->aead_req);
  195. auth_tag = (u32 *)rctx->auth_tag;
  196. for (i = 0; i < 4; i++) {
  197. val = omap_aes_read(dd, AES_REG_TAG_N(dd, i));
  198. auth_tag[i] = val ^ auth_tag[i];
  199. if (!(dd->flags & FLAGS_ENCRYPT))
  200. auth_tag[i] = auth_tag[i] ^ tag[i];
  201. }
  202. omap_aes_gcm_done_task(dd);
  203. }
  204. static int omap_aes_gcm_handle_queue(struct omap_aes_dev *dd,
  205. struct aead_request *req)
  206. {
  207. struct omap_aes_ctx *ctx;
  208. struct aead_request *backlog;
  209. struct omap_aes_reqctx *rctx;
  210. unsigned long flags;
  211. int err, ret = 0;
  212. spin_lock_irqsave(&dd->lock, flags);
  213. if (req)
  214. ret = aead_enqueue_request(&dd->aead_queue, req);
  215. if (dd->flags & FLAGS_BUSY) {
  216. spin_unlock_irqrestore(&dd->lock, flags);
  217. return ret;
  218. }
  219. backlog = aead_get_backlog(&dd->aead_queue);
  220. req = aead_dequeue_request(&dd->aead_queue);
  221. if (req)
  222. dd->flags |= FLAGS_BUSY;
  223. spin_unlock_irqrestore(&dd->lock, flags);
  224. if (!req)
  225. return ret;
  226. if (backlog)
  227. backlog->base.complete(&backlog->base, -EINPROGRESS);
  228. ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  229. rctx = aead_request_ctx(req);
  230. dd->ctx = ctx;
  231. rctx->dd = dd;
  232. dd->aead_req = req;
  233. rctx->mode &= FLAGS_MODE_MASK;
  234. dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
  235. err = omap_aes_gcm_copy_buffers(dd, req);
  236. if (err)
  237. return err;
  238. err = omap_aes_write_ctrl(dd);
  239. if (!err)
  240. err = omap_aes_crypt_dma_start(dd);
  241. if (err) {
  242. omap_aes_gcm_finish_req(dd, err);
  243. omap_aes_gcm_handle_queue(dd, NULL);
  244. }
  245. return ret;
  246. }
  247. static int omap_aes_gcm_crypt(struct aead_request *req, unsigned long mode)
  248. {
  249. struct omap_aes_reqctx *rctx = aead_request_ctx(req);
  250. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  251. unsigned int authlen = crypto_aead_authsize(aead);
  252. struct omap_aes_dev *dd;
  253. __be32 counter = cpu_to_be32(1);
  254. int err, assoclen;
  255. memset(rctx->auth_tag, 0, sizeof(rctx->auth_tag));
  256. memcpy(rctx->iv + GCM_AES_IV_SIZE, &counter, 4);
  257. err = do_encrypt_iv(req, (u32 *)rctx->auth_tag, (u32 *)rctx->iv);
  258. if (err)
  259. return err;
  260. if (mode & FLAGS_RFC4106_GCM)
  261. assoclen = req->assoclen - 8;
  262. else
  263. assoclen = req->assoclen;
  264. if (assoclen + req->cryptlen == 0) {
  265. scatterwalk_map_and_copy(rctx->auth_tag, req->dst, 0, authlen,
  266. 1);
  267. return 0;
  268. }
  269. dd = omap_aes_find_dev(rctx);
  270. if (!dd)
  271. return -ENODEV;
  272. rctx->mode = mode;
  273. return omap_aes_gcm_handle_queue(dd, req);
  274. }
  275. int omap_aes_gcm_encrypt(struct aead_request *req)
  276. {
  277. struct omap_aes_reqctx *rctx = aead_request_ctx(req);
  278. memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
  279. return omap_aes_gcm_crypt(req, FLAGS_ENCRYPT | FLAGS_GCM);
  280. }
  281. int omap_aes_gcm_decrypt(struct aead_request *req)
  282. {
  283. struct omap_aes_reqctx *rctx = aead_request_ctx(req);
  284. memcpy(rctx->iv, req->iv, GCM_AES_IV_SIZE);
  285. return omap_aes_gcm_crypt(req, FLAGS_GCM);
  286. }
  287. int omap_aes_4106gcm_encrypt(struct aead_request *req)
  288. {
  289. struct omap_aes_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  290. struct omap_aes_reqctx *rctx = aead_request_ctx(req);
  291. memcpy(rctx->iv, ctx->nonce, 4);
  292. memcpy(rctx->iv + 4, req->iv, 8);
  293. return omap_aes_gcm_crypt(req, FLAGS_ENCRYPT | FLAGS_GCM |
  294. FLAGS_RFC4106_GCM);
  295. }
  296. int omap_aes_4106gcm_decrypt(struct aead_request *req)
  297. {
  298. struct omap_aes_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
  299. struct omap_aes_reqctx *rctx = aead_request_ctx(req);
  300. memcpy(rctx->iv, ctx->nonce, 4);
  301. memcpy(rctx->iv + 4, req->iv, 8);
  302. return omap_aes_gcm_crypt(req, FLAGS_GCM | FLAGS_RFC4106_GCM);
  303. }
  304. int omap_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  305. unsigned int keylen)
  306. {
  307. struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
  308. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  309. keylen != AES_KEYSIZE_256)
  310. return -EINVAL;
  311. memcpy(ctx->key, key, keylen);
  312. ctx->keylen = keylen;
  313. return 0;
  314. }
  315. int omap_aes_4106gcm_setkey(struct crypto_aead *tfm, const u8 *key,
  316. unsigned int keylen)
  317. {
  318. struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
  319. if (keylen < 4)
  320. return -EINVAL;
  321. keylen -= 4;
  322. if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
  323. keylen != AES_KEYSIZE_256)
  324. return -EINVAL;
  325. memcpy(ctx->key, key, keylen);
  326. memcpy(ctx->nonce, key + keylen, 4);
  327. ctx->keylen = keylen;
  328. return 0;
  329. }