1e28facdeSStephan Mueller /* 2e28facdeSStephan Mueller * Key Wrapping: RFC3394 / NIST SP800-38F 3e28facdeSStephan Mueller * 4e28facdeSStephan Mueller * Copyright (C) 2015, Stephan Mueller <smueller@chronox.de> 5e28facdeSStephan Mueller * 6e28facdeSStephan Mueller * Redistribution and use in source and binary forms, with or without 7e28facdeSStephan Mueller * modification, are permitted provided that the following conditions 8e28facdeSStephan Mueller * are met: 9e28facdeSStephan Mueller * 1. Redistributions of source code must retain the above copyright 10e28facdeSStephan Mueller * notice, and the entire permission notice in its entirety, 11e28facdeSStephan Mueller * including the disclaimer of warranties. 12e28facdeSStephan Mueller * 2. Redistributions in binary form must reproduce the above copyright 13e28facdeSStephan Mueller * notice, this list of conditions and the following disclaimer in the 14e28facdeSStephan Mueller * documentation and/or other materials provided with the distribution. 15e28facdeSStephan Mueller * 3. The name of the author may not be used to endorse or promote 16e28facdeSStephan Mueller * products derived from this software without specific prior 17e28facdeSStephan Mueller * written permission. 18e28facdeSStephan Mueller * 19e28facdeSStephan Mueller * ALTERNATIVELY, this product may be distributed under the terms of 20e28facdeSStephan Mueller * the GNU General Public License, in which case the provisions of the GPL2 21e28facdeSStephan Mueller * are required INSTEAD OF the above restrictions. (This clause is 22e28facdeSStephan Mueller * necessary due to a potential bad interaction between the GPL and 23e28facdeSStephan Mueller * the restrictions contained in a BSD-style copyright.) 24e28facdeSStephan Mueller * 25e28facdeSStephan Mueller * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 26e28facdeSStephan Mueller * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27e28facdeSStephan Mueller * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF 28e28facdeSStephan Mueller * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 29e28facdeSStephan Mueller * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30e28facdeSStephan Mueller * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 31e28facdeSStephan Mueller * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 32e28facdeSStephan Mueller * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33e28facdeSStephan Mueller * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34e28facdeSStephan Mueller * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 35e28facdeSStephan Mueller * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH 36e28facdeSStephan Mueller * DAMAGE. 37e28facdeSStephan Mueller */ 38e28facdeSStephan Mueller 39e28facdeSStephan Mueller /* 40e28facdeSStephan Mueller * Note for using key wrapping: 41e28facdeSStephan Mueller * 42e28facdeSStephan Mueller * * The result of the encryption operation is the ciphertext starting 43e28facdeSStephan Mueller * with the 2nd semiblock. The first semiblock is provided as the IV. 44e28facdeSStephan Mueller * The IV used to start the encryption operation is the default IV. 45e28facdeSStephan Mueller * 46e28facdeSStephan Mueller * * The input for the decryption is the first semiblock handed in as an 47e28facdeSStephan Mueller * IV. The ciphertext is the data starting with the 2nd semiblock. The 48e28facdeSStephan Mueller * return code of the decryption operation will be EBADMSG in case an 49e28facdeSStephan Mueller * integrity error occurs. 50e28facdeSStephan Mueller * 51e28facdeSStephan Mueller * To obtain the full result of an encryption as expected by SP800-38F, the 52e28facdeSStephan Mueller * caller must allocate a buffer of plaintext + 8 bytes: 53e28facdeSStephan Mueller * 54e28facdeSStephan Mueller * unsigned int datalen = ptlen + crypto_skcipher_ivsize(tfm); 55e28facdeSStephan Mueller * u8 data[datalen]; 56e28facdeSStephan Mueller * u8 *iv = data; 57e28facdeSStephan Mueller * u8 *pt = data + crypto_skcipher_ivsize(tfm); 58e28facdeSStephan Mueller * <ensure that pt contains the plaintext of size ptlen> 596b611d98SEric Biggers * sg_init_one(&sg, pt, ptlen); 60e28facdeSStephan Mueller * skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv); 61e28facdeSStephan Mueller * 62e28facdeSStephan Mueller * ==> After encryption, data now contains full KW result as per SP800-38F. 63e28facdeSStephan Mueller * 64e28facdeSStephan Mueller * In case of decryption, ciphertext now already has the expected length 65e28facdeSStephan Mueller * and must be segmented appropriately: 66e28facdeSStephan Mueller * 67e28facdeSStephan Mueller * unsigned int datalen = CTLEN; 68e28facdeSStephan Mueller * u8 data[datalen]; 69e28facdeSStephan Mueller * <ensure that data contains full ciphertext> 70e28facdeSStephan Mueller * u8 *iv = data; 71e28facdeSStephan Mueller * u8 *ct = data + crypto_skcipher_ivsize(tfm); 72e28facdeSStephan Mueller * unsigned int ctlen = datalen - crypto_skcipher_ivsize(tfm); 736b611d98SEric Biggers * sg_init_one(&sg, ct, ctlen); 746b611d98SEric Biggers * skcipher_request_set_crypt(req, &sg, &sg, ctlen, iv); 75e28facdeSStephan Mueller * 76e28facdeSStephan Mueller * ==> After decryption (which hopefully does not return EBADMSG), the ct 77e28facdeSStephan Mueller * pointer now points to the plaintext of size ctlen. 78e28facdeSStephan Mueller * 79e28facdeSStephan Mueller * Note 2: KWP is not implemented as this would defy in-place operation. 80e28facdeSStephan Mueller * If somebody wants to wrap non-aligned data, he should simply pad 81e28facdeSStephan Mueller * the input with zeros to fill it up to the 8 byte boundary. 82e28facdeSStephan Mueller */ 83e28facdeSStephan Mueller 84e28facdeSStephan Mueller #include <linux/module.h> 85e28facdeSStephan Mueller #include <linux/crypto.h> 86e28facdeSStephan Mueller #include <linux/scatterlist.h> 87e28facdeSStephan Mueller #include <crypto/scatterwalk.h> 88*0eb76ba2SArd Biesheuvel #include <crypto/internal/cipher.h> 89e28facdeSStephan Mueller #include <crypto/internal/skcipher.h> 90e28facdeSStephan Mueller 91e28facdeSStephan Mueller struct crypto_kw_block { 92e28facdeSStephan Mueller #define SEMIBSIZE 8 939e49451dSStephan Mueller __be64 A; 949e49451dSStephan Mueller __be64 R; 95e28facdeSStephan Mueller }; 96e28facdeSStephan Mueller 97e28facdeSStephan Mueller /* 98e28facdeSStephan Mueller * Fast forward the SGL to the "end" length minus SEMIBSIZE. 99e28facdeSStephan Mueller * The start in the SGL defined by the fast-forward is returned with 100e28facdeSStephan Mueller * the walk variable 101e28facdeSStephan Mueller */ 102e28facdeSStephan Mueller static void crypto_kw_scatterlist_ff(struct scatter_walk *walk, 103e28facdeSStephan Mueller struct scatterlist *sg, 104e28facdeSStephan Mueller unsigned int end) 105e28facdeSStephan Mueller { 106e28facdeSStephan Mueller unsigned int skip = 0; 107e28facdeSStephan Mueller 108e28facdeSStephan Mueller /* The caller should only operate on full SEMIBLOCKs. */ 109e28facdeSStephan Mueller BUG_ON(end < SEMIBSIZE); 110e28facdeSStephan Mueller 111e28facdeSStephan Mueller skip = end - SEMIBSIZE; 112e28facdeSStephan Mueller while (sg) { 113e28facdeSStephan Mueller if (sg->length > skip) { 114e28facdeSStephan Mueller scatterwalk_start(walk, sg); 115e28facdeSStephan Mueller scatterwalk_advance(walk, skip); 116e28facdeSStephan Mueller break; 117e28facdeSStephan Mueller } else 118e28facdeSStephan Mueller skip -= sg->length; 119e28facdeSStephan Mueller 120e28facdeSStephan Mueller sg = sg_next(sg); 121e28facdeSStephan Mueller } 122e28facdeSStephan Mueller } 123e28facdeSStephan Mueller 1246b611d98SEric Biggers static int crypto_kw_decrypt(struct skcipher_request *req) 125e28facdeSStephan Mueller { 1266b611d98SEric Biggers struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1276b611d98SEric Biggers struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); 1289e49451dSStephan Mueller struct crypto_kw_block block; 1296b611d98SEric Biggers struct scatterlist *src, *dst; 1306b611d98SEric Biggers u64 t = 6 * ((req->cryptlen) >> 3); 1319e49451dSStephan Mueller unsigned int i; 132e28facdeSStephan Mueller int ret = 0; 133e28facdeSStephan Mueller 134e28facdeSStephan Mueller /* 135e28facdeSStephan Mueller * Require at least 2 semiblocks (note, the 3rd semiblock that is 136e28facdeSStephan Mueller * required by SP800-38F is the IV. 137e28facdeSStephan Mueller */ 1386b611d98SEric Biggers if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE) 139e28facdeSStephan Mueller return -EINVAL; 140e28facdeSStephan Mueller 141e28facdeSStephan Mueller /* Place the IV into block A */ 1426b611d98SEric Biggers memcpy(&block.A, req->iv, SEMIBSIZE); 143e28facdeSStephan Mueller 144e28facdeSStephan Mueller /* 145e28facdeSStephan Mueller * src scatterlist is read-only. dst scatterlist is r/w. During the 1466b611d98SEric Biggers * first loop, src points to req->src and dst to req->dst. For any 1476b611d98SEric Biggers * subsequent round, the code operates on req->dst only. 148e28facdeSStephan Mueller */ 1496b611d98SEric Biggers src = req->src; 1506b611d98SEric Biggers dst = req->dst; 151e28facdeSStephan Mueller 152e28facdeSStephan Mueller for (i = 0; i < 6; i++) { 153e28facdeSStephan Mueller struct scatter_walk src_walk, dst_walk; 1546b611d98SEric Biggers unsigned int nbytes = req->cryptlen; 155e28facdeSStephan Mueller 1566b611d98SEric Biggers while (nbytes) { 1576b611d98SEric Biggers /* move pointer by nbytes in the SGL */ 1586b611d98SEric Biggers crypto_kw_scatterlist_ff(&src_walk, src, nbytes); 159e28facdeSStephan Mueller /* get the source block */ 1609e49451dSStephan Mueller scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE, 161e28facdeSStephan Mueller false); 162e28facdeSStephan Mueller 163e28facdeSStephan Mueller /* perform KW operation: modify IV with counter */ 1649e49451dSStephan Mueller block.A ^= cpu_to_be64(t); 165e28facdeSStephan Mueller t--; 166e28facdeSStephan Mueller /* perform KW operation: decrypt block */ 1676b611d98SEric Biggers crypto_cipher_decrypt_one(cipher, (u8 *)&block, 1689e49451dSStephan Mueller (u8 *)&block); 169e28facdeSStephan Mueller 1706b611d98SEric Biggers /* move pointer by nbytes in the SGL */ 1716b611d98SEric Biggers crypto_kw_scatterlist_ff(&dst_walk, dst, nbytes); 172e28facdeSStephan Mueller /* Copy block->R into place */ 1739e49451dSStephan Mueller scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE, 174e28facdeSStephan Mueller true); 175e28facdeSStephan Mueller 1766b611d98SEric Biggers nbytes -= SEMIBSIZE; 177e28facdeSStephan Mueller } 178e28facdeSStephan Mueller 179e28facdeSStephan Mueller /* we now start to operate on the dst SGL only */ 1806b611d98SEric Biggers src = req->dst; 1816b611d98SEric Biggers dst = req->dst; 182e28facdeSStephan Mueller } 183e28facdeSStephan Mueller 184e28facdeSStephan Mueller /* Perform authentication check */ 185c9683276SGeert Uytterhoeven if (block.A != cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL)) 186e28facdeSStephan Mueller ret = -EBADMSG; 187e28facdeSStephan Mueller 1889e49451dSStephan Mueller memzero_explicit(&block, sizeof(struct crypto_kw_block)); 189e28facdeSStephan Mueller 190e28facdeSStephan Mueller return ret; 191e28facdeSStephan Mueller } 192e28facdeSStephan Mueller 1936b611d98SEric Biggers static int crypto_kw_encrypt(struct skcipher_request *req) 194e28facdeSStephan Mueller { 1956b611d98SEric Biggers struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1966b611d98SEric Biggers struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); 1979e49451dSStephan Mueller struct crypto_kw_block block; 1986b611d98SEric Biggers struct scatterlist *src, *dst; 1999e49451dSStephan Mueller u64 t = 1; 2009e49451dSStephan Mueller unsigned int i; 201e28facdeSStephan Mueller 202e28facdeSStephan Mueller /* 203e28facdeSStephan Mueller * Require at least 2 semiblocks (note, the 3rd semiblock that is 204e28facdeSStephan Mueller * required by SP800-38F is the IV that occupies the first semiblock. 205e28facdeSStephan Mueller * This means that the dst memory must be one semiblock larger than src. 206e28facdeSStephan Mueller * Also ensure that the given data is aligned to semiblock. 207e28facdeSStephan Mueller */ 2086b611d98SEric Biggers if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE) 209e28facdeSStephan Mueller return -EINVAL; 210e28facdeSStephan Mueller 211e28facdeSStephan Mueller /* 212e28facdeSStephan Mueller * Place the predefined IV into block A -- for encrypt, the caller 213e28facdeSStephan Mueller * does not need to provide an IV, but he needs to fetch the final IV. 214e28facdeSStephan Mueller */ 215c9683276SGeert Uytterhoeven block.A = cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL); 216e28facdeSStephan Mueller 217e28facdeSStephan Mueller /* 218e28facdeSStephan Mueller * src scatterlist is read-only. dst scatterlist is r/w. During the 2196b611d98SEric Biggers * first loop, src points to req->src and dst to req->dst. For any 2206b611d98SEric Biggers * subsequent round, the code operates on req->dst only. 221e28facdeSStephan Mueller */ 2226b611d98SEric Biggers src = req->src; 2236b611d98SEric Biggers dst = req->dst; 224e28facdeSStephan Mueller 225e28facdeSStephan Mueller for (i = 0; i < 6; i++) { 226e28facdeSStephan Mueller struct scatter_walk src_walk, dst_walk; 2276b611d98SEric Biggers unsigned int nbytes = req->cryptlen; 228e28facdeSStephan Mueller 2296b611d98SEric Biggers scatterwalk_start(&src_walk, src); 2306b611d98SEric Biggers scatterwalk_start(&dst_walk, dst); 231e28facdeSStephan Mueller 2326b611d98SEric Biggers while (nbytes) { 233e28facdeSStephan Mueller /* get the source block */ 2349e49451dSStephan Mueller scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE, 235e28facdeSStephan Mueller false); 236e28facdeSStephan Mueller 237e28facdeSStephan Mueller /* perform KW operation: encrypt block */ 2386b611d98SEric Biggers crypto_cipher_encrypt_one(cipher, (u8 *)&block, 2399e49451dSStephan Mueller (u8 *)&block); 240e28facdeSStephan Mueller /* perform KW operation: modify IV with counter */ 2419e49451dSStephan Mueller block.A ^= cpu_to_be64(t); 242e28facdeSStephan Mueller t++; 243e28facdeSStephan Mueller 244e28facdeSStephan Mueller /* Copy block->R into place */ 2459e49451dSStephan Mueller scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE, 246e28facdeSStephan Mueller true); 247e28facdeSStephan Mueller 2486b611d98SEric Biggers nbytes -= SEMIBSIZE; 249e28facdeSStephan Mueller } 250e28facdeSStephan Mueller 251e28facdeSStephan Mueller /* we now start to operate on the dst SGL only */ 2526b611d98SEric Biggers src = req->dst; 2536b611d98SEric Biggers dst = req->dst; 254e28facdeSStephan Mueller } 255e28facdeSStephan Mueller 256e28facdeSStephan Mueller /* establish the IV for the caller to pick up */ 2576b611d98SEric Biggers memcpy(req->iv, &block.A, SEMIBSIZE); 258e28facdeSStephan Mueller 2599e49451dSStephan Mueller memzero_explicit(&block, sizeof(struct crypto_kw_block)); 260e28facdeSStephan Mueller 261e28facdeSStephan Mueller return 0; 262e28facdeSStephan Mueller } 263e28facdeSStephan Mueller 2646b611d98SEric Biggers static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb) 265e28facdeSStephan Mueller { 2666b611d98SEric Biggers struct skcipher_instance *inst; 2676b611d98SEric Biggers struct crypto_alg *alg; 268e28facdeSStephan Mueller int err; 269e28facdeSStephan Mueller 270b3c16bfcSHerbert Xu inst = skcipher_alloc_instance_simple(tmpl, tb); 2716b611d98SEric Biggers if (IS_ERR(inst)) 2726b611d98SEric Biggers return PTR_ERR(inst); 273e28facdeSStephan Mueller 274b3c16bfcSHerbert Xu alg = skcipher_ialg_simple(inst); 275b3c16bfcSHerbert Xu 2766b611d98SEric Biggers err = -EINVAL; 277e28facdeSStephan Mueller /* Section 5.1 requirement for KW */ 278e28facdeSStephan Mueller if (alg->cra_blocksize != sizeof(struct crypto_kw_block)) 2796b611d98SEric Biggers goto out_free_inst; 280e28facdeSStephan Mueller 2816b611d98SEric Biggers inst->alg.base.cra_blocksize = SEMIBSIZE; 2826b611d98SEric Biggers inst->alg.base.cra_alignmask = 0; 2836b611d98SEric Biggers inst->alg.ivsize = SEMIBSIZE; 284e28facdeSStephan Mueller 2856b611d98SEric Biggers inst->alg.encrypt = crypto_kw_encrypt; 2866b611d98SEric Biggers inst->alg.decrypt = crypto_kw_decrypt; 287e28facdeSStephan Mueller 2886b611d98SEric Biggers err = skcipher_register_instance(tmpl, inst); 289b3c16bfcSHerbert Xu if (err) { 2906b611d98SEric Biggers out_free_inst: 2916b611d98SEric Biggers inst->free(inst); 292b3c16bfcSHerbert Xu } 293b3c16bfcSHerbert Xu 2946b611d98SEric Biggers return err; 295e28facdeSStephan Mueller } 296e28facdeSStephan Mueller 297e28facdeSStephan Mueller static struct crypto_template crypto_kw_tmpl = { 298e28facdeSStephan Mueller .name = "kw", 2996b611d98SEric Biggers .create = crypto_kw_create, 300e28facdeSStephan Mueller .module = THIS_MODULE, 301e28facdeSStephan Mueller }; 302e28facdeSStephan Mueller 303e28facdeSStephan Mueller static int __init crypto_kw_init(void) 304e28facdeSStephan Mueller { 305e28facdeSStephan Mueller return crypto_register_template(&crypto_kw_tmpl); 306e28facdeSStephan Mueller } 307e28facdeSStephan Mueller 308e28facdeSStephan Mueller static void __exit crypto_kw_exit(void) 309e28facdeSStephan Mueller { 310e28facdeSStephan Mueller crypto_unregister_template(&crypto_kw_tmpl); 311e28facdeSStephan Mueller } 312e28facdeSStephan Mueller 313c4741b23SEric Biggers subsys_initcall(crypto_kw_init); 314e28facdeSStephan Mueller module_exit(crypto_kw_exit); 315e28facdeSStephan Mueller 316e28facdeSStephan Mueller MODULE_LICENSE("Dual BSD/GPL"); 317e28facdeSStephan Mueller MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>"); 318e28facdeSStephan Mueller MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NIST SP800-38F)"); 319e28facdeSStephan Mueller MODULE_ALIAS_CRYPTO("kw"); 320*0eb76ba2SArd Biesheuvel MODULE_IMPORT_NS(CRYPTO_INTERNAL); 321