nx-aes-ctr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AES CTR routines supporting the Power 7+ Nest Accelerators driver
  4. *
  5. * Copyright (C) 2011-2012 International Business Machines Inc.
  6. *
  7. * Author: Kent Yoder <[email protected]>
  8. */
  9. #include <crypto/aes.h>
  10. #include <crypto/ctr.h>
  11. #include <crypto/algapi.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/crypto.h>
  15. #include <asm/vio.h>
  16. #include "nx_csbcpb.h"
  17. #include "nx.h"
  18. static int ctr_aes_nx_set_key(struct crypto_skcipher *tfm,
  19. const u8 *in_key,
  20. unsigned int key_len)
  21. {
  22. struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
  23. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  24. nx_ctx_init(nx_ctx, HCOP_FC_AES);
  25. switch (key_len) {
  26. case AES_KEYSIZE_128:
  27. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
  28. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
  29. break;
  30. case AES_KEYSIZE_192:
  31. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
  32. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
  33. break;
  34. case AES_KEYSIZE_256:
  35. NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
  36. nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
  37. break;
  38. default:
  39. return -EINVAL;
  40. }
  41. csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR;
  42. memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len);
  43. return 0;
  44. }
  45. static int ctr3686_aes_nx_set_key(struct crypto_skcipher *tfm,
  46. const u8 *in_key,
  47. unsigned int key_len)
  48. {
  49. struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
  50. if (key_len < CTR_RFC3686_NONCE_SIZE)
  51. return -EINVAL;
  52. memcpy(nx_ctx->priv.ctr.nonce,
  53. in_key + key_len - CTR_RFC3686_NONCE_SIZE,
  54. CTR_RFC3686_NONCE_SIZE);
  55. key_len -= CTR_RFC3686_NONCE_SIZE;
  56. return ctr_aes_nx_set_key(tfm, in_key, key_len);
  57. }
  58. static int ctr_aes_nx_crypt(struct skcipher_request *req, u8 *iv)
  59. {
  60. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  61. struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
  62. struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
  63. unsigned long irq_flags;
  64. unsigned int processed = 0, to_process;
  65. int rc;
  66. spin_lock_irqsave(&nx_ctx->lock, irq_flags);
  67. do {
  68. to_process = req->cryptlen - processed;
  69. rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src,
  70. &to_process, processed,
  71. csbcpb->cpb.aes_ctr.iv);
  72. if (rc)
  73. goto out;
  74. if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
  75. rc = -EINVAL;
  76. goto out;
  77. }
  78. rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
  79. req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
  80. if (rc)
  81. goto out;
  82. memcpy(iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
  83. atomic_inc(&(nx_ctx->stats->aes_ops));
  84. atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
  85. &(nx_ctx->stats->aes_bytes));
  86. processed += to_process;
  87. } while (processed < req->cryptlen);
  88. out:
  89. spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
  90. return rc;
  91. }
  92. static int ctr3686_aes_nx_crypt(struct skcipher_request *req)
  93. {
  94. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  95. struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
  96. u8 iv[16];
  97. memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE);
  98. memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
  99. iv[12] = iv[13] = iv[14] = 0;
  100. iv[15] = 1;
  101. return ctr_aes_nx_crypt(req, iv);
  102. }
  103. struct skcipher_alg nx_ctr3686_aes_alg = {
  104. .base.cra_name = "rfc3686(ctr(aes))",
  105. .base.cra_driver_name = "rfc3686-ctr-aes-nx",
  106. .base.cra_priority = 300,
  107. .base.cra_blocksize = 1,
  108. .base.cra_ctxsize = sizeof(struct nx_crypto_ctx),
  109. .base.cra_module = THIS_MODULE,
  110. .init = nx_crypto_ctx_aes_ctr_init,
  111. .exit = nx_crypto_ctx_skcipher_exit,
  112. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  113. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  114. .ivsize = CTR_RFC3686_IV_SIZE,
  115. .setkey = ctr3686_aes_nx_set_key,
  116. .encrypt = ctr3686_aes_nx_crypt,
  117. .decrypt = ctr3686_aes_nx_crypt,
  118. .chunksize = AES_BLOCK_SIZE,
  119. };