nx-aes-ecb.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AES ECB 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 ecb_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 = (struct nx_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_ECB;
  41. memcpy(csbcpb->cpb.aes_ecb.key, in_key, key_len);
  42. return 0;
  43. }
  44. static int ecb_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, NULL, req->dst, req->src,
  61. &to_process, processed, NULL);
  62. if (rc)
  63. goto out;
  64. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  65. rc = -EINVAL;
  66. goto out;
  67. }
  68. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  69. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  70. if (rc)
  71. goto out;
  72. atomic_inc(&(nx_ctx->stats->aes_ops));
  73. atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
  74. &(nx_ctx->stats->aes_bytes));
  75. processed += to_process;
  76. } while (processed < req->cryptlen);
  77. out:
  78. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  79. return rc;
  80. }
  81. static int ecb_aes_nx_encrypt(struct skcipher_request *req)
  82. {
  83. return ecb_aes_nx_crypt(req, 1);
  84. }
  85. static int ecb_aes_nx_decrypt(struct skcipher_request *req)
  86. {
  87. return ecb_aes_nx_crypt(req, 0);
  88. }
  89. struct skcipher_alg nx_ecb_aes_alg = {
  90. .base.cra_name = "ecb(aes)",
  91. .base.cra_driver_name = "ecb-aes-nx",
  92. .base.cra_priority = 300,
  93. .base.cra_blocksize = AES_BLOCK_SIZE,
  94. .base.cra_alignmask = 0xf,
  95. .base.cra_ctxsize = sizeof(struct nx_crypto_ctx),
  96. .base.cra_module = THIS_MODULE,
  97. .init = nx_crypto_ctx_aes_ecb_init,
  98. .exit = nx_crypto_ctx_skcipher_exit,
  99. .min_keysize = AES_MIN_KEY_SIZE,
  100. .max_keysize = AES_MAX_KEY_SIZE,
  101. .setkey = ecb_aes_nx_set_key,
  102. .encrypt = ecb_aes_nx_encrypt,
  103. .decrypt = ecb_aes_nx_decrypt,
  104. };