nx-aes-cbc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AES CBC 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/aes.h>
  10. #include <crypto/algapi.h>
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/crypto.h>
  14. #include <asm/vio.h>
  15. #include "nx_csbcpb.h"
  16. #include "nx.h"
  17. static int cbc_aes_nx_set_key(struct crypto_skcipher *tfm,
  18. const u8 *in_key,
  19. unsigned int key_len)
  20. {
  21. struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
  22. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  23. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  24. switch (key_len) {
  25. case AES_KEYSIZE_128:
  26. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  27. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  28. break;
  29. case AES_KEYSIZE_192:
  30. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
  31. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
  32. break;
  33. case AES_KEYSIZE_256:
  34. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
  35. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
  36. break;
  37. default:
  38. return -EINVAL;
  39. }
  40. csbcpb->cpb.hdr.mode = NX_MODE_AES_CBC;
  41. memcpy(csbcpb->cpb.aes_cbc.key, in_key, key_len);
  42. return 0;
  43. }
  44. static int cbc_aes_nx_crypt(struct skcipher_request *req,
  45. int enc)
  46. {
  47. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  48. struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
  49. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  50. unsigned long irq_flags;
  51. unsigned int processed = 0, to_process;
  52. int rc;
  53. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  54. if (enc)
  55. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  56. else
  57. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  58. do {
  59. to_process = req->cryptlen - processed;
  60. rc = nx_build_sg_lists(nx_ctx, req->iv, req->dst, req->src,
  61. &to_process, processed,
  62. csbcpb->cpb.aes_cbc.iv);
  63. if (rc)
  64. goto out;
  65. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  66. rc = -EINVAL;
  67. goto out;
  68. }
  69. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  70. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  71. if (rc)
  72. goto out;
  73. memcpy(req->iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
  74. atomic_inc(&(nx_ctx->stats->aes_ops));
  75. atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
  76. &(nx_ctx->stats->aes_bytes));
  77. processed += to_process;
  78. } while (processed < req->cryptlen);
  79. out:
  80. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  81. return rc;
  82. }
  83. static int cbc_aes_nx_encrypt(struct skcipher_request *req)
  84. {
  85. return cbc_aes_nx_crypt(req, 1);
  86. }
  87. static int cbc_aes_nx_decrypt(struct skcipher_request *req)
  88. {
  89. return cbc_aes_nx_crypt(req, 0);
  90. }
  91. struct skcipher_alg nx_cbc_aes_alg = {
  92. .base.cra_name = "cbc(aes)",
  93. .base.cra_driver_name = "cbc-aes-nx",
  94. .base.cra_priority = 300,
  95. .base.cra_blocksize = AES_BLOCK_SIZE,
  96. .base.cra_ctxsize = sizeof(struct nx_crypto_ctx),
  97. .base.cra_alignmask = 0xf,
  98. .base.cra_module = THIS_MODULE,
  99. .init = nx_crypto_ctx_aes_cbc_init,
  100. .exit = nx_crypto_ctx_skcipher_exit,
  101. .min_keysize = AES_MIN_KEY_SIZE,
  102. .max_keysize = AES_MAX_KEY_SIZE,
  103. .ivsize = AES_BLOCK_SIZE,
  104. .setkey = cbc_aes_nx_set_key,
  105. .encrypt = cbc_aes_nx_encrypt,
  106. .decrypt = cbc_aes_nx_decrypt,
  107. };