nx-aes-ctr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * AES CTR 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/ctr.h>
  23. #include <crypto/algapi.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/crypto.h>
  27. #include <asm/vio.h>
  28. #include "nx_csbcpb.h"
  29. #include "nx.h"
  30. static int ctr_aes_nx_set_key(struct crypto_tfm *tfm,
  31. const u8 *in_key,
  32. unsigned int key_len)
  33. {
  34. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  35. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  36. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  37. switch (key_len) {
  38. case AES_KEYSIZE_128:
  39. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  40. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  41. break;
  42. case AES_KEYSIZE_192:
  43. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
  44. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
  45. break;
  46. case AES_KEYSIZE_256:
  47. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
  48. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
  49. break;
  50. default:
  51. return -EINVAL;
  52. }
  53. csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR;
  54. memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len);
  55. return 0;
  56. }
  57. static int ctr3686_aes_nx_set_key(struct crypto_tfm *tfm,
  58. const u8 *in_key,
  59. unsigned int key_len)
  60. {
  61. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  62. if (key_len < CTR_RFC3686_NONCE_SIZE)
  63. return -EINVAL;
  64. memcpy(nx_ctx->priv.ctr.nonce,
  65. in_key + key_len - CTR_RFC3686_NONCE_SIZE,
  66. CTR_RFC3686_NONCE_SIZE);
  67. key_len -= CTR_RFC3686_NONCE_SIZE;
  68. return ctr_aes_nx_set_key(tfm, in_key, key_len);
  69. }
  70. static int ctr_aes_nx_crypt(struct blkcipher_desc *desc,
  71. struct scatterlist *dst,
  72. struct scatterlist *src,
  73. unsigned int nbytes)
  74. {
  75. struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
  76. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  77. unsigned long irq_flags;
  78. unsigned int processed = 0, to_process;
  79. int rc;
  80. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  81. do {
  82. to_process = nbytes - processed;
  83. rc = nx_build_sg_lists(nx_ctx, desc, dst, src, &to_process,
  84. processed, csbcpb->cpb.aes_ctr.iv);
  85. if (rc)
  86. goto out;
  87. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  88. rc = -EINVAL;
  89. goto out;
  90. }
  91. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  92. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  93. if (rc)
  94. goto out;
  95. memcpy(desc->info, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
  96. atomic_inc(&(nx_ctx->stats->aes_ops));
  97. atomic64_add(csbcpb->csb.processed_byte_count,
  98. &(nx_ctx->stats->aes_bytes));
  99. processed += to_process;
  100. } while (processed < nbytes);
  101. out:
  102. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  103. return rc;
  104. }
  105. static int ctr3686_aes_nx_crypt(struct blkcipher_desc *desc,
  106. struct scatterlist *dst,
  107. struct scatterlist *src,
  108. unsigned int nbytes)
  109. {
  110. struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
  111. u8 iv[16];
  112. memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_IV_SIZE);
  113. memcpy(iv + CTR_RFC3686_NONCE_SIZE,
  114. desc->info, CTR_RFC3686_IV_SIZE);
  115. iv[12] = iv[13] = iv[14] = 0;
  116. iv[15] = 1;
  117. desc->info = iv;
  118. return ctr_aes_nx_crypt(desc, dst, src, nbytes);
  119. }
  120. struct crypto_alg nx_ctr3686_aes_alg = {
  121. .cra_name = "rfc3686(ctr(aes))",
  122. .cra_driver_name = "rfc3686-ctr-aes-nx",
  123. .cra_priority = 300,
  124. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  125. .cra_blocksize = 1,
  126. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  127. .cra_type = &crypto_blkcipher_type,
  128. .cra_module = THIS_MODULE,
  129. .cra_init = nx_crypto_ctx_aes_ctr_init,
  130. .cra_exit = nx_crypto_ctx_exit,
  131. .cra_blkcipher = {
  132. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  133. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  134. .ivsize = CTR_RFC3686_IV_SIZE,
  135. .geniv = "seqiv",
  136. .setkey = ctr3686_aes_nx_set_key,
  137. .encrypt = ctr3686_aes_nx_crypt,
  138. .decrypt = ctr3686_aes_nx_crypt,
  139. }
  140. };