nx-aes-ecb.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * AES ECB routines supporting the Power 7+ Nest Accelerators driver
  3. *
  4. * Copyright (C) 2011-2012 International Business Machines Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 only.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Author: Kent Yoder <yoder1@us.ibm.com>
  20. */
  21. #include <crypto/aes.h>
  22. #include <crypto/algapi.h>
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/crypto.h>
  26. #include <asm/vio.h>
  27. #include "nx_csbcpb.h"
  28. #include "nx.h"
  29. static int ecb_aes_nx_set_key(struct crypto_tfm *tfm,
  30. const u8 *in_key,
  31. unsigned int key_len)
  32. {
  33. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  34. struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
  35. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  36. switch (key_len) {
  37. case AES_KEYSIZE_128:
  38. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  39. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  40. break;
  41. case AES_KEYSIZE_192:
  42. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
  43. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
  44. break;
  45. case AES_KEYSIZE_256:
  46. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
  47. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
  48. break;
  49. default:
  50. return -EINVAL;
  51. }
  52. csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
  53. memcpy(csbcpb->cpb.aes_ecb.key, in_key, key_len);
  54. return 0;
  55. }
  56. static int ecb_aes_nx_crypt(struct blkcipher_desc *desc,
  57. struct scatterlist *dst,
  58. struct scatterlist *src,
  59. unsigned int nbytes,
  60. int enc)
  61. {
  62. struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
  63. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  64. unsigned long irq_flags;
  65. unsigned int processed = 0, to_process;
  66. int rc;
  67. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  68. if (enc)
  69. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  70. else
  71. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  72. do {
  73. to_process = nbytes - processed;
  74. rc = nx_build_sg_lists(nx_ctx, desc, dst, src, &to_process,
  75. processed, NULL);
  76. if (rc)
  77. goto out;
  78. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  79. rc = -EINVAL;
  80. goto out;
  81. }
  82. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  83. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  84. if (rc)
  85. goto out;
  86. atomic_inc(&(nx_ctx->stats->aes_ops));
  87. atomic64_add(csbcpb->csb.processed_byte_count,
  88. &(nx_ctx->stats->aes_bytes));
  89. processed += to_process;
  90. } while (processed < nbytes);
  91. out:
  92. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  93. return rc;
  94. }
  95. static int ecb_aes_nx_encrypt(struct blkcipher_desc *desc,
  96. struct scatterlist *dst,
  97. struct scatterlist *src,
  98. unsigned int nbytes)
  99. {
  100. return ecb_aes_nx_crypt(desc, dst, src, nbytes, 1);
  101. }
  102. static int ecb_aes_nx_decrypt(struct blkcipher_desc *desc,
  103. struct scatterlist *dst,
  104. struct scatterlist *src,
  105. unsigned int nbytes)
  106. {
  107. return ecb_aes_nx_crypt(desc, dst, src, nbytes, 0);
  108. }
  109. struct crypto_alg nx_ecb_aes_alg = {
  110. .cra_name = "ecb(aes)",
  111. .cra_driver_name = "ecb-aes-nx",
  112. .cra_priority = 300,
  113. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  114. .cra_blocksize = AES_BLOCK_SIZE,
  115. .cra_alignmask = 0xf,
  116. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  117. .cra_type = &crypto_blkcipher_type,
  118. .cra_module = THIS_MODULE,
  119. .cra_init = nx_crypto_ctx_aes_ecb_init,
  120. .cra_exit = nx_crypto_ctx_exit,
  121. .cra_blkcipher = {
  122. .min_keysize = AES_MIN_KEY_SIZE,
  123. .max_keysize = AES_MAX_KEY_SIZE,
  124. .setkey = ecb_aes_nx_set_key,
  125. .encrypt = ecb_aes_nx_encrypt,
  126. .decrypt = ecb_aes_nx_decrypt,
  127. }
  128. };