nx-aes-xcbc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AES XCBC routines supporting the Power 7+ Nest Accelerators driver
  4. *
  5. * Copyright (C) 2011-2012 International Business Machines Inc.
  6. *
  7. * Author: Kent Yoder <[email protected]>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <crypto/aes.h>
  11. #include <crypto/algapi.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/crypto.h>
  15. #include <asm/vio.h>
  16. #include "nx_csbcpb.h"
  17. #include "nx.h"
  18. struct xcbc_state {
  19. u8 state[AES_BLOCK_SIZE];
  20. unsigned int count;
  21. u8 buffer[AES_BLOCK_SIZE];
  22. };
  23. static int nx_xcbc_set_key(struct crypto_shash *desc,
  24. const u8 *in_key,
  25. unsigned int key_len)
  26. {
  27. struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc);
  28. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  29. switch (key_len) {
  30. case AES_KEYSIZE_128:
  31. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  32. break;
  33. default:
  34. return -EINVAL;
  35. }
  36. memcpy(csbcpb->cpb.aes_xcbc.key, in_key, key_len);
  37. return 0;
  38. }
  39. /*
  40. * Based on RFC 3566, for a zero-length message:
  41. *
  42. * n = 1
  43. * K1 = E(K, 0x01010101010101010101010101010101)
  44. * K3 = E(K, 0x03030303030303030303030303030303)
  45. * E[0] = 0x00000000000000000000000000000000
  46. * M[1] = 0x80000000000000000000000000000000 (0 length message with padding)
  47. * E[1] = (K1, M[1] ^ E[0] ^ K3)
  48. * Tag = M[1]
  49. */
  50. static int nx_xcbc_empty(struct shash_desc *desc, u8 *out)
  51. {
  52. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  53. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  54. struct nx_sg *in_sg, *out_sg;
  55. u8 keys[2][AES_BLOCK_SIZE];
  56. u8 key[32];
  57. int rc = 0;
  58. int len;
  59. /* Change to ECB mode */
  60. csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
  61. memcpy(key, csbcpb->cpb.aes_xcbc.key, AES_BLOCK_SIZE);
  62. memcpy(csbcpb->cpb.aes_ecb.key, key, AES_BLOCK_SIZE);
  63. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  64. /* K1 and K3 base patterns */
  65. memset(keys[0], 0x01, sizeof(keys[0]));
  66. memset(keys[1], 0x03, sizeof(keys[1]));
  67. len = sizeof(keys);
  68. /* Generate K1 and K3 encrypting the patterns */
  69. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys, &len,
  70. nx_ctx->ap->sglen);
  71. if (len != sizeof(keys))
  72. return -EINVAL;
  73. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *) keys, &len,
  74. nx_ctx->ap->sglen);
  75. if (len != sizeof(keys))
  76. return -EINVAL;
  77. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  78. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  79. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  80. if (rc)
  81. goto out;
  82. atomic_inc(&(nx_ctx->stats->aes_ops));
  83. /* XOr K3 with the padding for a 0 length message */
  84. keys[1][0] ^= 0x80;
  85. len = sizeof(keys[1]);
  86. /* Encrypt the final result */
  87. memcpy(csbcpb->cpb.aes_ecb.key, keys[0], AES_BLOCK_SIZE);
  88. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *) keys[1], &len,
  89. nx_ctx->ap->sglen);
  90. if (len != sizeof(keys[1]))
  91. return -EINVAL;
  92. len = AES_BLOCK_SIZE;
  93. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
  94. nx_ctx->ap->sglen);
  95. if (len != AES_BLOCK_SIZE)
  96. return -EINVAL;
  97. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  98. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  99. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  100. if (rc)
  101. goto out;
  102. atomic_inc(&(nx_ctx->stats->aes_ops));
  103. out:
  104. /* Restore XCBC mode */
  105. csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
  106. memcpy(csbcpb->cpb.aes_xcbc.key, key, AES_BLOCK_SIZE);
  107. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  108. return rc;
  109. }
  110. static int nx_crypto_ctx_aes_xcbc_init2(struct crypto_tfm *tfm)
  111. {
  112. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  113. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  114. int err;
  115. err = nx_crypto_ctx_aes_xcbc_init(tfm);
  116. if (err)
  117. return err;
  118. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  119. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  120. csbcpb->cpb.hdr.mode = NX_MODE_AES_XCBC_MAC;
  121. return 0;
  122. }
  123. static int nx_xcbc_init(struct shash_desc *desc)
  124. {
  125. struct xcbc_state *sctx = shash_desc_ctx(desc);
  126. memset(sctx, 0, sizeof *sctx);
  127. return 0;
  128. }
  129. static int nx_xcbc_update(struct shash_desc *desc,
  130. const u8 *data,
  131. unsigned int len)
  132. {
  133. struct xcbc_state *sctx = shash_desc_ctx(desc);
  134. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  135. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  136. struct nx_sg *in_sg;
  137. struct nx_sg *out_sg;
  138. u32 to_process = 0, leftover, total;
  139. unsigned int max_sg_len;
  140. unsigned long irq_flags;
  141. int rc = 0;
  142. int data_len;
  143. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  144. total = sctx->count + len;
  145. /* 2 cases for total data len:
  146. * 1: <= AES_BLOCK_SIZE: copy into state, return 0
  147. * 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover
  148. */
  149. if (total <= AES_BLOCK_SIZE) {
  150. memcpy(sctx->buffer + sctx->count, data, len);
  151. sctx->count += len;
  152. goto out;
  153. }
  154. in_sg = nx_ctx->in_sg;
  155. max_sg_len = min_t(u64, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  156. nx_ctx->ap->sglen);
  157. max_sg_len = min_t(u64, max_sg_len,
  158. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  159. data_len = AES_BLOCK_SIZE;
  160. out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
  161. &len, nx_ctx->ap->sglen);
  162. if (data_len != AES_BLOCK_SIZE) {
  163. rc = -EINVAL;
  164. goto out;
  165. }
  166. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  167. do {
  168. to_process = total - to_process;
  169. to_process = to_process & ~(AES_BLOCK_SIZE - 1);
  170. leftover = total - to_process;
  171. /* the hardware will not accept a 0 byte operation for this
  172. * algorithm and the operation MUST be finalized to be correct.
  173. * So if we happen to get an update that falls on a block sized
  174. * boundary, we must save off the last block to finalize with
  175. * later. */
  176. if (!leftover) {
  177. to_process -= AES_BLOCK_SIZE;
  178. leftover = AES_BLOCK_SIZE;
  179. }
  180. if (sctx->count) {
  181. data_len = sctx->count;
  182. in_sg = nx_build_sg_list(nx_ctx->in_sg,
  183. (u8 *) sctx->buffer,
  184. &data_len,
  185. max_sg_len);
  186. if (data_len != sctx->count) {
  187. rc = -EINVAL;
  188. goto out;
  189. }
  190. }
  191. data_len = to_process - sctx->count;
  192. in_sg = nx_build_sg_list(in_sg,
  193. (u8 *) data,
  194. &data_len,
  195. max_sg_len);
  196. if (data_len != to_process - sctx->count) {
  197. rc = -EINVAL;
  198. goto out;
  199. }
  200. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
  201. sizeof(struct nx_sg);
  202. /* we've hit the nx chip previously and we're updating again,
  203. * so copy over the partial digest */
  204. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  205. memcpy(csbcpb->cpb.aes_xcbc.cv,
  206. csbcpb->cpb.aes_xcbc.out_cv_mac,
  207. AES_BLOCK_SIZE);
  208. }
  209. NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
  210. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  211. rc = -EINVAL;
  212. goto out;
  213. }
  214. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  215. if (rc)
  216. goto out;
  217. atomic_inc(&(nx_ctx->stats->aes_ops));
  218. /* everything after the first update is continuation */
  219. NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
  220. total -= to_process;
  221. data += to_process - sctx->count;
  222. sctx->count = 0;
  223. in_sg = nx_ctx->in_sg;
  224. } while (leftover > AES_BLOCK_SIZE);
  225. /* copy the leftover back into the state struct */
  226. memcpy(sctx->buffer, data, leftover);
  227. sctx->count = leftover;
  228. out:
  229. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  230. return rc;
  231. }
  232. static int nx_xcbc_final(struct shash_desc *desc, u8 *out)
  233. {
  234. struct xcbc_state *sctx = shash_desc_ctx(desc);
  235. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
  236. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  237. struct nx_sg *in_sg, *out_sg;
  238. unsigned long irq_flags;
  239. int rc = 0;
  240. int len;
  241. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  242. if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
  243. /* we've hit the nx chip previously, now we're finalizing,
  244. * so copy over the partial digest */
  245. memcpy(csbcpb->cpb.aes_xcbc.cv,
  246. csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  247. } else if (sctx->count == 0) {
  248. /*
  249. * we've never seen an update, so this is a 0 byte op. The
  250. * hardware cannot handle a 0 byte op, so just ECB to
  251. * generate the hash.
  252. */
  253. rc = nx_xcbc_empty(desc, out);
  254. goto out;
  255. }
  256. /* final is represented by continuing the operation and indicating that
  257. * this is not an intermediate operation */
  258. NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
  259. len = sctx->count;
  260. in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buffer,
  261. &len, nx_ctx->ap->sglen);
  262. if (len != sctx->count) {
  263. rc = -EINVAL;
  264. goto out;
  265. }
  266. len = AES_BLOCK_SIZE;
  267. out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len,
  268. nx_ctx->ap->sglen);
  269. if (len != AES_BLOCK_SIZE) {
  270. rc = -EINVAL;
  271. goto out;
  272. }
  273. nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
  274. nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
  275. if (!nx_ctx->op.outlen) {
  276. rc = -EINVAL;
  277. goto out;
  278. }
  279. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0);
  280. if (rc)
  281. goto out;
  282. atomic_inc(&(nx_ctx->stats->aes_ops));
  283. memcpy(out, csbcpb->cpb.aes_xcbc.out_cv_mac, AES_BLOCK_SIZE);
  284. out:
  285. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  286. return rc;
  287. }
  288. struct shash_alg nx_shash_aes_xcbc_alg = {
  289. .digestsize = AES_BLOCK_SIZE,
  290. .init = nx_xcbc_init,
  291. .update = nx_xcbc_update,
  292. .final = nx_xcbc_final,
  293. .setkey = nx_xcbc_set_key,
  294. .descsize = sizeof(struct xcbc_state),
  295. .statesize = sizeof(struct xcbc_state),
  296. .base = {
  297. .cra_name = "xcbc(aes)",
  298. .cra_driver_name = "xcbc-aes-nx",
  299. .cra_priority = 300,
  300. .cra_blocksize = AES_BLOCK_SIZE,
  301. .cra_module = THIS_MODULE,
  302. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  303. .cra_init = nx_crypto_ctx_aes_xcbc_init2,
  304. .cra_exit = nx_crypto_ctx_exit,
  305. }
  306. };