xctr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * XCTR: XOR Counter mode - Adapted from ctr.c
  4. *
  5. * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
  6. * Copyright 2021 Google LLC
  7. */
  8. /*
  9. * XCTR mode is a blockcipher mode of operation used to implement HCTR2. XCTR is
  10. * closely related to the CTR mode of operation; the main difference is that CTR
  11. * generates the keystream using E(CTR + IV) whereas XCTR generates the
  12. * keystream using E(CTR ^ IV). This allows implementations to avoid dealing
  13. * with multi-limb integers (as is required in CTR mode). XCTR is also specified
  14. * using little-endian arithmetic which makes it slightly faster on LE machines.
  15. *
  16. * See the HCTR2 paper for more details:
  17. * Length-preserving encryption with HCTR2
  18. * (https://eprint.iacr.org/2021/1441.pdf)
  19. */
  20. #include <crypto/algapi.h>
  21. #include <crypto/internal/cipher.h>
  22. #include <crypto/internal/skcipher.h>
  23. #include <linux/err.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. /* For now this implementation is limited to 16-byte blocks for simplicity */
  29. #define XCTR_BLOCKSIZE 16
  30. static void crypto_xctr_crypt_final(struct skcipher_walk *walk,
  31. struct crypto_cipher *tfm, u32 byte_ctr)
  32. {
  33. u8 keystream[XCTR_BLOCKSIZE];
  34. const u8 *src = walk->src.virt.addr;
  35. u8 *dst = walk->dst.virt.addr;
  36. unsigned int nbytes = walk->nbytes;
  37. __le32 ctr32 = cpu_to_le32(byte_ctr / XCTR_BLOCKSIZE + 1);
  38. crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
  39. crypto_cipher_encrypt_one(tfm, keystream, walk->iv);
  40. crypto_xor_cpy(dst, keystream, src, nbytes);
  41. crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
  42. }
  43. static int crypto_xctr_crypt_segment(struct skcipher_walk *walk,
  44. struct crypto_cipher *tfm, u32 byte_ctr)
  45. {
  46. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  47. crypto_cipher_alg(tfm)->cia_encrypt;
  48. const u8 *src = walk->src.virt.addr;
  49. u8 *dst = walk->dst.virt.addr;
  50. unsigned int nbytes = walk->nbytes;
  51. __le32 ctr32 = cpu_to_le32(byte_ctr / XCTR_BLOCKSIZE + 1);
  52. do {
  53. crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
  54. fn(crypto_cipher_tfm(tfm), dst, walk->iv);
  55. crypto_xor(dst, src, XCTR_BLOCKSIZE);
  56. crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
  57. le32_add_cpu(&ctr32, 1);
  58. src += XCTR_BLOCKSIZE;
  59. dst += XCTR_BLOCKSIZE;
  60. } while ((nbytes -= XCTR_BLOCKSIZE) >= XCTR_BLOCKSIZE);
  61. return nbytes;
  62. }
  63. static int crypto_xctr_crypt_inplace(struct skcipher_walk *walk,
  64. struct crypto_cipher *tfm, u32 byte_ctr)
  65. {
  66. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  67. crypto_cipher_alg(tfm)->cia_encrypt;
  68. unsigned long alignmask = crypto_cipher_alignmask(tfm);
  69. unsigned int nbytes = walk->nbytes;
  70. u8 *data = walk->src.virt.addr;
  71. u8 tmp[XCTR_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
  72. u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
  73. __le32 ctr32 = cpu_to_le32(byte_ctr / XCTR_BLOCKSIZE + 1);
  74. do {
  75. crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
  76. fn(crypto_cipher_tfm(tfm), keystream, walk->iv);
  77. crypto_xor(data, keystream, XCTR_BLOCKSIZE);
  78. crypto_xor(walk->iv, (u8 *)&ctr32, sizeof(ctr32));
  79. le32_add_cpu(&ctr32, 1);
  80. data += XCTR_BLOCKSIZE;
  81. } while ((nbytes -= XCTR_BLOCKSIZE) >= XCTR_BLOCKSIZE);
  82. return nbytes;
  83. }
  84. static int crypto_xctr_crypt(struct skcipher_request *req)
  85. {
  86. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  87. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  88. struct skcipher_walk walk;
  89. unsigned int nbytes;
  90. int err;
  91. u32 byte_ctr = 0;
  92. err = skcipher_walk_virt(&walk, req, false);
  93. while (walk.nbytes >= XCTR_BLOCKSIZE) {
  94. if (walk.src.virt.addr == walk.dst.virt.addr)
  95. nbytes = crypto_xctr_crypt_inplace(&walk, cipher,
  96. byte_ctr);
  97. else
  98. nbytes = crypto_xctr_crypt_segment(&walk, cipher,
  99. byte_ctr);
  100. byte_ctr += walk.nbytes - nbytes;
  101. err = skcipher_walk_done(&walk, nbytes);
  102. }
  103. if (walk.nbytes) {
  104. crypto_xctr_crypt_final(&walk, cipher, byte_ctr);
  105. err = skcipher_walk_done(&walk, 0);
  106. }
  107. return err;
  108. }
  109. static int crypto_xctr_create(struct crypto_template *tmpl, struct rtattr **tb)
  110. {
  111. struct skcipher_instance *inst;
  112. struct crypto_alg *alg;
  113. int err;
  114. inst = skcipher_alloc_instance_simple(tmpl, tb);
  115. if (IS_ERR(inst))
  116. return PTR_ERR(inst);
  117. alg = skcipher_ialg_simple(inst);
  118. /* Block size must be 16 bytes. */
  119. err = -EINVAL;
  120. if (alg->cra_blocksize != XCTR_BLOCKSIZE)
  121. goto out_free_inst;
  122. /* XCTR mode is a stream cipher. */
  123. inst->alg.base.cra_blocksize = 1;
  124. /*
  125. * To simplify the implementation, configure the skcipher walk to only
  126. * give a partial block at the very end, never earlier.
  127. */
  128. inst->alg.chunksize = alg->cra_blocksize;
  129. inst->alg.encrypt = crypto_xctr_crypt;
  130. inst->alg.decrypt = crypto_xctr_crypt;
  131. err = skcipher_register_instance(tmpl, inst);
  132. if (err) {
  133. out_free_inst:
  134. inst->free(inst);
  135. }
  136. return err;
  137. }
  138. static struct crypto_template crypto_xctr_tmpl = {
  139. .name = "xctr",
  140. .create = crypto_xctr_create,
  141. .module = THIS_MODULE,
  142. };
  143. static int __init crypto_xctr_module_init(void)
  144. {
  145. return crypto_register_template(&crypto_xctr_tmpl);
  146. }
  147. static void __exit crypto_xctr_module_exit(void)
  148. {
  149. crypto_unregister_template(&crypto_xctr_tmpl);
  150. }
  151. subsys_initcall(crypto_xctr_module_init);
  152. module_exit(crypto_xctr_module_exit);
  153. MODULE_LICENSE("GPL");
  154. MODULE_DESCRIPTION("XCTR block cipher mode of operation");
  155. MODULE_ALIAS_CRYPTO("xctr");
  156. MODULE_IMPORT_NS(CRYPTO_INTERNAL);