nx-aes-ecb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. u32 max_sg_len;
  67. int rc;
  68. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  69. max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg),
  70. nx_ctx->ap->sglen);
  71. if (enc)
  72. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  73. else
  74. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  75. do {
  76. to_process = min_t(u64, nbytes - processed,
  77. nx_ctx->ap->databytelen);
  78. to_process = min_t(u64, to_process,
  79. NX_PAGE_SIZE * (max_sg_len - 1));
  80. to_process = to_process & ~(AES_BLOCK_SIZE - 1);
  81. rc = nx_build_sg_lists(nx_ctx, desc, dst, src, to_process,
  82. processed, NULL);
  83. if (rc)
  84. goto out;
  85. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  86. rc = -EINVAL;
  87. goto out;
  88. }
  89. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  90. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  91. if (rc)
  92. goto out;
  93. atomic_inc(&(nx_ctx->stats->aes_ops));
  94. atomic64_add(csbcpb->csb.processed_byte_count,
  95. &(nx_ctx->stats->aes_bytes));
  96. processed += to_process;
  97. } while (processed < nbytes);
  98. out:
  99. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  100. return rc;
  101. }
  102. static int ecb_aes_nx_encrypt(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, 1);
  108. }
  109. static int ecb_aes_nx_decrypt(struct blkcipher_desc *desc,
  110. struct scatterlist *dst,
  111. struct scatterlist *src,
  112. unsigned int nbytes)
  113. {
  114. return ecb_aes_nx_crypt(desc, dst, src, nbytes, 0);
  115. }
  116. struct crypto_alg nx_ecb_aes_alg = {
  117. .cra_name = "ecb(aes)",
  118. .cra_driver_name = "ecb-aes-nx",
  119. .cra_priority = 300,
  120. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  121. .cra_blocksize = AES_BLOCK_SIZE,
  122. .cra_alignmask = 0xf,
  123. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  124. .cra_type = &crypto_blkcipher_type,
  125. .cra_module = THIS_MODULE,
  126. .cra_init = nx_crypto_ctx_aes_ecb_init,
  127. .cra_exit = nx_crypto_ctx_exit,
  128. .cra_blkcipher = {
  129. .min_keysize = AES_MIN_KEY_SIZE,
  130. .max_keysize = AES_MAX_KEY_SIZE,
  131. .setkey = ecb_aes_nx_set_key,
  132. .encrypt = ecb_aes_nx_encrypt,
  133. .decrypt = ecb_aes_nx_decrypt,
  134. }
  135. };