nx-aes-cbc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * AES CBC 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 cbc_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 = 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_CBC;
  53. memcpy(csbcpb->cpb.aes_cbc.key, in_key, key_len);
  54. return 0;
  55. }
  56. static int cbc_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. int rc;
  65. if (nbytes > nx_ctx->ap->databytelen)
  66. return -EINVAL;
  67. if (enc)
  68. NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
  69. else
  70. NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
  71. rc = nx_build_sg_lists(nx_ctx, desc, dst, src, nbytes,
  72. csbcpb->cpb.aes_cbc.iv);
  73. if (rc)
  74. goto out;
  75. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  76. rc = -EINVAL;
  77. goto out;
  78. }
  79. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  80. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  81. if (rc)
  82. goto out;
  83. atomic_inc(&(nx_ctx->stats->aes_ops));
  84. atomic64_add(csbcpb->csb.processed_byte_count,
  85. &(nx_ctx->stats->aes_bytes));
  86. out:
  87. return rc;
  88. }
  89. static int cbc_aes_nx_encrypt(struct blkcipher_desc *desc,
  90. struct scatterlist *dst,
  91. struct scatterlist *src,
  92. unsigned int nbytes)
  93. {
  94. return cbc_aes_nx_crypt(desc, dst, src, nbytes, 1);
  95. }
  96. static int cbc_aes_nx_decrypt(struct blkcipher_desc *desc,
  97. struct scatterlist *dst,
  98. struct scatterlist *src,
  99. unsigned int nbytes)
  100. {
  101. return cbc_aes_nx_crypt(desc, dst, src, nbytes, 0);
  102. }
  103. struct crypto_alg nx_cbc_aes_alg = {
  104. .cra_name = "cbc(aes)",
  105. .cra_driver_name = "cbc-aes-nx",
  106. .cra_priority = 300,
  107. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  108. .cra_blocksize = AES_BLOCK_SIZE,
  109. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  110. .cra_type = &crypto_blkcipher_type,
  111. .cra_module = THIS_MODULE,
  112. .cra_init = nx_crypto_ctx_aes_cbc_init,
  113. .cra_exit = nx_crypto_ctx_exit,
  114. .cra_blkcipher = {
  115. .min_keysize = AES_MIN_KEY_SIZE,
  116. .max_keysize = AES_MAX_KEY_SIZE,
  117. .ivsize = AES_BLOCK_SIZE,
  118. .setkey = cbc_aes_nx_set_key,
  119. .encrypt = cbc_aes_nx_encrypt,
  120. .decrypt = cbc_aes_nx_decrypt,
  121. }
  122. };