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