nx-aes-ctr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.iv,
  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. int rc;
  78. if (nbytes > nx_ctx->ap->databytelen)
  79. return -EINVAL;
  80. rc = nx_build_sg_lists(nx_ctx, desc, dst, src, nbytes,
  81. csbcpb->cpb.aes_ctr.iv);
  82. if (rc)
  83. goto out;
  84. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  85. rc = -EINVAL;
  86. goto out;
  87. }
  88. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  89. desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  90. if (rc)
  91. goto out;
  92. atomic_inc(&(nx_ctx->stats->aes_ops));
  93. atomic64_add(csbcpb->csb.processed_byte_count,
  94. &(nx_ctx->stats->aes_bytes));
  95. out:
  96. return rc;
  97. }
  98. static int ctr3686_aes_nx_crypt(struct blkcipher_desc *desc,
  99. struct scatterlist *dst,
  100. struct scatterlist *src,
  101. unsigned int nbytes)
  102. {
  103. struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
  104. u8 *iv = nx_ctx->priv.ctr.iv;
  105. memcpy(iv + CTR_RFC3686_NONCE_SIZE,
  106. desc->info, CTR_RFC3686_IV_SIZE);
  107. iv[15] = 1;
  108. desc->info = nx_ctx->priv.ctr.iv;
  109. return ctr_aes_nx_crypt(desc, dst, src, nbytes);
  110. }
  111. struct crypto_alg nx_ctr_aes_alg = {
  112. .cra_name = "ctr(aes)",
  113. .cra_driver_name = "ctr-aes-nx",
  114. .cra_priority = 300,
  115. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  116. .cra_blocksize = 1,
  117. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  118. .cra_type = &crypto_blkcipher_type,
  119. .cra_module = THIS_MODULE,
  120. .cra_init = nx_crypto_ctx_aes_ctr_init,
  121. .cra_exit = nx_crypto_ctx_exit,
  122. .cra_blkcipher = {
  123. .min_keysize = AES_MIN_KEY_SIZE,
  124. .max_keysize = AES_MAX_KEY_SIZE,
  125. .ivsize = AES_BLOCK_SIZE,
  126. .setkey = ctr_aes_nx_set_key,
  127. .encrypt = ctr_aes_nx_crypt,
  128. .decrypt = ctr_aes_nx_crypt,
  129. }
  130. };
  131. struct crypto_alg nx_ctr3686_aes_alg = {
  132. .cra_name = "rfc3686(ctr(aes))",
  133. .cra_driver_name = "rfc3686-ctr-aes-nx",
  134. .cra_priority = 300,
  135. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  136. .cra_blocksize = 1,
  137. .cra_ctxsize = sizeof(struct nx_crypto_ctx),
  138. .cra_type = &crypto_blkcipher_type,
  139. .cra_module = THIS_MODULE,
  140. .cra_init = nx_crypto_ctx_aes_ctr_init,
  141. .cra_exit = nx_crypto_ctx_exit,
  142. .cra_blkcipher = {
  143. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  144. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  145. .ivsize = CTR_RFC3686_IV_SIZE,
  146. .geniv = "seqiv",
  147. .setkey = ctr3686_aes_nx_set_key,
  148. .encrypt = ctr3686_aes_nx_crypt,
  149. .decrypt = ctr3686_aes_nx_crypt,
  150. }
  151. };