2 * Key Wrapping: RFC3394 / NIST SP800-38F
4 * Copyright (C) 2015, Stephan Mueller <smueller@chronox.de>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, and the entire permission notice in its entirety,
11 * including the disclaimer of warranties.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
19 * ALTERNATIVELY, this product may be distributed under the terms of
20 * the GNU General Public License, in which case the provisions of the GPL2
21 * are required INSTEAD OF the above restrictions. (This clause is
22 * necessary due to a potential bad interaction between the GPL and
23 * the restrictions contained in a BSD-style copyright.)
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
28 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
40 * Note for using key wrapping:
42 * * The result of the encryption operation is the ciphertext starting
43 * with the 2nd semiblock. The first semiblock is provided as the IV.
44 * The IV used to start the encryption operation is the default IV.
46 * * The input for the decryption is the first semiblock handed in as an
47 * IV. The ciphertext is the data starting with the 2nd semiblock. The
48 * return code of the decryption operation will be EBADMSG in case an
49 * integrity error occurs.
51 * To obtain the full result of an encryption as expected by SP800-38F, the
52 * caller must allocate a buffer of plaintext + 8 bytes:
54 * unsigned int datalen = ptlen + crypto_skcipher_ivsize(tfm);
57 * u8 *pt = data + crypto_skcipher_ivsize(tfm);
58 * <ensure that pt contains the plaintext of size ptlen>
59 * sg_init_one(&sg, ptdata, ptlen);
60 * skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv);
62 * ==> After encryption, data now contains full KW result as per SP800-38F.
64 * In case of decryption, ciphertext now already has the expected length
65 * and must be segmented appropriately:
67 * unsigned int datalen = CTLEN;
69 * <ensure that data contains full ciphertext>
71 * u8 *ct = data + crypto_skcipher_ivsize(tfm);
72 * unsigned int ctlen = datalen - crypto_skcipher_ivsize(tfm);
73 * sg_init_one(&sg, ctdata, ctlen);
74 * skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv);
76 * ==> After decryption (which hopefully does not return EBADMSG), the ct
77 * pointer now points to the plaintext of size ctlen.
79 * Note 2: KWP is not implemented as this would defy in-place operation.
80 * If somebody wants to wrap non-aligned data, he should simply pad
81 * the input with zeros to fill it up to the 8 byte boundary.
84 #include <linux/module.h>
85 #include <linux/crypto.h>
86 #include <linux/scatterlist.h>
87 #include <crypto/scatterwalk.h>
88 #include <crypto/internal/skcipher.h>
90 struct crypto_kw_ctx
{
91 struct crypto_cipher
*child
;
94 struct crypto_kw_block
{
101 * Fast forward the SGL to the "end" length minus SEMIBSIZE.
102 * The start in the SGL defined by the fast-forward is returned with
105 static void crypto_kw_scatterlist_ff(struct scatter_walk
*walk
,
106 struct scatterlist
*sg
,
109 unsigned int skip
= 0;
111 /* The caller should only operate on full SEMIBLOCKs. */
112 BUG_ON(end
< SEMIBSIZE
);
114 skip
= end
- SEMIBSIZE
;
116 if (sg
->length
> skip
) {
117 scatterwalk_start(walk
, sg
);
118 scatterwalk_advance(walk
, skip
);
127 static int crypto_kw_decrypt(struct blkcipher_desc
*desc
,
128 struct scatterlist
*dst
, struct scatterlist
*src
,
131 struct crypto_blkcipher
*tfm
= desc
->tfm
;
132 struct crypto_kw_ctx
*ctx
= crypto_blkcipher_ctx(tfm
);
133 struct crypto_cipher
*child
= ctx
->child
;
134 struct crypto_kw_block block
;
135 struct scatterlist
*lsrc
, *ldst
;
136 u64 t
= 6 * ((nbytes
) >> 3);
141 * Require at least 2 semiblocks (note, the 3rd semiblock that is
142 * required by SP800-38F is the IV.
144 if (nbytes
< (2 * SEMIBSIZE
) || nbytes
% SEMIBSIZE
)
147 /* Place the IV into block A */
148 memcpy(&block
.A
, desc
->info
, SEMIBSIZE
);
151 * src scatterlist is read-only. dst scatterlist is r/w. During the
152 * first loop, lsrc points to src and ldst to dst. For any
153 * subsequent round, the code operates on dst only.
158 for (i
= 0; i
< 6; i
++) {
159 struct scatter_walk src_walk
, dst_walk
;
160 unsigned int tmp_nbytes
= nbytes
;
163 /* move pointer by tmp_nbytes in the SGL */
164 crypto_kw_scatterlist_ff(&src_walk
, lsrc
, tmp_nbytes
);
165 /* get the source block */
166 scatterwalk_copychunks(&block
.R
, &src_walk
, SEMIBSIZE
,
169 /* perform KW operation: modify IV with counter */
170 block
.A
^= cpu_to_be64(t
);
172 /* perform KW operation: decrypt block */
173 crypto_cipher_decrypt_one(child
, (u8
*)&block
,
176 /* move pointer by tmp_nbytes in the SGL */
177 crypto_kw_scatterlist_ff(&dst_walk
, ldst
, tmp_nbytes
);
178 /* Copy block->R into place */
179 scatterwalk_copychunks(&block
.R
, &dst_walk
, SEMIBSIZE
,
182 tmp_nbytes
-= SEMIBSIZE
;
185 /* we now start to operate on the dst SGL only */
190 /* Perform authentication check */
191 if (block
.A
!= cpu_to_be64(0xa6a6a6a6a6a6a6a6))
194 memzero_explicit(&block
, sizeof(struct crypto_kw_block
));
199 static int crypto_kw_encrypt(struct blkcipher_desc
*desc
,
200 struct scatterlist
*dst
, struct scatterlist
*src
,
203 struct crypto_blkcipher
*tfm
= desc
->tfm
;
204 struct crypto_kw_ctx
*ctx
= crypto_blkcipher_ctx(tfm
);
205 struct crypto_cipher
*child
= ctx
->child
;
206 struct crypto_kw_block block
;
207 struct scatterlist
*lsrc
, *ldst
;
212 * Require at least 2 semiblocks (note, the 3rd semiblock that is
213 * required by SP800-38F is the IV that occupies the first semiblock.
214 * This means that the dst memory must be one semiblock larger than src.
215 * Also ensure that the given data is aligned to semiblock.
217 if (nbytes
< (2 * SEMIBSIZE
) || nbytes
% SEMIBSIZE
)
221 * Place the predefined IV into block A -- for encrypt, the caller
222 * does not need to provide an IV, but he needs to fetch the final IV.
224 block
.A
= cpu_to_be64(0xa6a6a6a6a6a6a6a6);
227 * src scatterlist is read-only. dst scatterlist is r/w. During the
228 * first loop, lsrc points to src and ldst to dst. For any
229 * subsequent round, the code operates on dst only.
234 for (i
= 0; i
< 6; i
++) {
235 struct scatter_walk src_walk
, dst_walk
;
236 unsigned int tmp_nbytes
= nbytes
;
238 scatterwalk_start(&src_walk
, lsrc
);
239 scatterwalk_start(&dst_walk
, ldst
);
242 /* get the source block */
243 scatterwalk_copychunks(&block
.R
, &src_walk
, SEMIBSIZE
,
246 /* perform KW operation: encrypt block */
247 crypto_cipher_encrypt_one(child
, (u8
*)&block
,
249 /* perform KW operation: modify IV with counter */
250 block
.A
^= cpu_to_be64(t
);
253 /* Copy block->R into place */
254 scatterwalk_copychunks(&block
.R
, &dst_walk
, SEMIBSIZE
,
257 tmp_nbytes
-= SEMIBSIZE
;
260 /* we now start to operate on the dst SGL only */
265 /* establish the IV for the caller to pick up */
266 memcpy(desc
->info
, &block
.A
, SEMIBSIZE
);
268 memzero_explicit(&block
, sizeof(struct crypto_kw_block
));
273 static int crypto_kw_setkey(struct crypto_tfm
*parent
, const u8
*key
,
276 struct crypto_kw_ctx
*ctx
= crypto_tfm_ctx(parent
);
277 struct crypto_cipher
*child
= ctx
->child
;
280 crypto_cipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
281 crypto_cipher_set_flags(child
, crypto_tfm_get_flags(parent
) &
282 CRYPTO_TFM_REQ_MASK
);
283 err
= crypto_cipher_setkey(child
, key
, keylen
);
284 crypto_tfm_set_flags(parent
, crypto_cipher_get_flags(child
) &
285 CRYPTO_TFM_RES_MASK
);
289 static int crypto_kw_init_tfm(struct crypto_tfm
*tfm
)
291 struct crypto_instance
*inst
= crypto_tfm_alg_instance(tfm
);
292 struct crypto_spawn
*spawn
= crypto_instance_ctx(inst
);
293 struct crypto_kw_ctx
*ctx
= crypto_tfm_ctx(tfm
);
294 struct crypto_cipher
*cipher
;
296 cipher
= crypto_spawn_cipher(spawn
);
298 return PTR_ERR(cipher
);
304 static void crypto_kw_exit_tfm(struct crypto_tfm
*tfm
)
306 struct crypto_kw_ctx
*ctx
= crypto_tfm_ctx(tfm
);
308 crypto_free_cipher(ctx
->child
);
311 static struct crypto_instance
*crypto_kw_alloc(struct rtattr
**tb
)
313 struct crypto_instance
*inst
= NULL
;
314 struct crypto_alg
*alg
= NULL
;
317 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_BLKCIPHER
);
321 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_CIPHER
,
322 CRYPTO_ALG_TYPE_MASK
);
324 return ERR_CAST(alg
);
326 inst
= ERR_PTR(-EINVAL
);
327 /* Section 5.1 requirement for KW */
328 if (alg
->cra_blocksize
!= sizeof(struct crypto_kw_block
))
331 inst
= crypto_alloc_instance("kw", alg
);
335 inst
->alg
.cra_flags
= CRYPTO_ALG_TYPE_BLKCIPHER
;
336 inst
->alg
.cra_priority
= alg
->cra_priority
;
337 inst
->alg
.cra_blocksize
= SEMIBSIZE
;
338 inst
->alg
.cra_alignmask
= 0;
339 inst
->alg
.cra_type
= &crypto_blkcipher_type
;
340 inst
->alg
.cra_blkcipher
.ivsize
= SEMIBSIZE
;
341 inst
->alg
.cra_blkcipher
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
;
342 inst
->alg
.cra_blkcipher
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
;
344 inst
->alg
.cra_ctxsize
= sizeof(struct crypto_kw_ctx
);
346 inst
->alg
.cra_init
= crypto_kw_init_tfm
;
347 inst
->alg
.cra_exit
= crypto_kw_exit_tfm
;
349 inst
->alg
.cra_blkcipher
.setkey
= crypto_kw_setkey
;
350 inst
->alg
.cra_blkcipher
.encrypt
= crypto_kw_encrypt
;
351 inst
->alg
.cra_blkcipher
.decrypt
= crypto_kw_decrypt
;
358 static void crypto_kw_free(struct crypto_instance
*inst
)
360 crypto_drop_spawn(crypto_instance_ctx(inst
));
364 static struct crypto_template crypto_kw_tmpl
= {
366 .alloc
= crypto_kw_alloc
,
367 .free
= crypto_kw_free
,
368 .module
= THIS_MODULE
,
371 static int __init
crypto_kw_init(void)
373 return crypto_register_template(&crypto_kw_tmpl
);
376 static void __exit
crypto_kw_exit(void)
378 crypto_unregister_template(&crypto_kw_tmpl
);
381 module_init(crypto_kw_init
);
382 module_exit(crypto_kw_exit
);
384 MODULE_LICENSE("Dual BSD/GPL");
385 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
386 MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NIST SP800-38F)");
387 MODULE_ALIAS_CRYPTO("kw");