keywrap.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Key Wrapping: RFC3394 / NIST SP800-38F
  3. *
  4. * Copyright (C) 2015, Stephan Mueller <smueller@chronox.de>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  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
  17. * written permission.
  18. *
  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.)
  24. *
  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
  36. * DAMAGE.
  37. */
  38. /*
  39. * Note for using key wrapping:
  40. *
  41. * * The result of the encryption operation is the ciphertext starting
  42. * with the 2nd semiblock. The first semiblock is provided as the IV.
  43. * The IV used to start the encryption operation is the default IV.
  44. *
  45. * * The input for the decryption is the first semiblock handed in as an
  46. * IV. The ciphertext is the data starting with the 2nd semiblock. The
  47. * return code of the decryption operation will be EBADMSG in case an
  48. * integrity error occurs.
  49. *
  50. * To obtain the full result of an encryption as expected by SP800-38F, the
  51. * caller must allocate a buffer of plaintext + 8 bytes:
  52. *
  53. * unsigned int datalen = ptlen + crypto_skcipher_ivsize(tfm);
  54. * u8 data[datalen];
  55. * u8 *iv = data;
  56. * u8 *pt = data + crypto_skcipher_ivsize(tfm);
  57. * <ensure that pt contains the plaintext of size ptlen>
  58. * sg_init_one(&sg, pt, ptlen);
  59. * skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv);
  60. *
  61. * ==> After encryption, data now contains full KW result as per SP800-38F.
  62. *
  63. * In case of decryption, ciphertext now already has the expected length
  64. * and must be segmented appropriately:
  65. *
  66. * unsigned int datalen = CTLEN;
  67. * u8 data[datalen];
  68. * <ensure that data contains full ciphertext>
  69. * u8 *iv = data;
  70. * u8 *ct = data + crypto_skcipher_ivsize(tfm);
  71. * unsigned int ctlen = datalen - crypto_skcipher_ivsize(tfm);
  72. * sg_init_one(&sg, ct, ctlen);
  73. * skcipher_request_set_crypt(req, &sg, &sg, ctlen, iv);
  74. *
  75. * ==> After decryption (which hopefully does not return EBADMSG), the ct
  76. * pointer now points to the plaintext of size ctlen.
  77. *
  78. * Note 2: KWP is not implemented as this would defy in-place operation.
  79. * If somebody wants to wrap non-aligned data, he should simply pad
  80. * the input with zeros to fill it up to the 8 byte boundary.
  81. */
  82. #include <linux/module.h>
  83. #include <linux/crypto.h>
  84. #include <linux/scatterlist.h>
  85. #include <crypto/scatterwalk.h>
  86. #include <crypto/internal/cipher.h>
  87. #include <crypto/internal/skcipher.h>
  88. struct crypto_kw_block {
  89. #define SEMIBSIZE 8
  90. __be64 A;
  91. __be64 R;
  92. };
  93. /*
  94. * Fast forward the SGL to the "end" length minus SEMIBSIZE.
  95. * The start in the SGL defined by the fast-forward is returned with
  96. * the walk variable
  97. */
  98. static void crypto_kw_scatterlist_ff(struct scatter_walk *walk,
  99. struct scatterlist *sg,
  100. unsigned int end)
  101. {
  102. unsigned int skip = 0;
  103. /* The caller should only operate on full SEMIBLOCKs. */
  104. BUG_ON(end < SEMIBSIZE);
  105. skip = end - SEMIBSIZE;
  106. while (sg) {
  107. if (sg->length > skip) {
  108. scatterwalk_start(walk, sg);
  109. scatterwalk_advance(walk, skip);
  110. break;
  111. } else
  112. skip -= sg->length;
  113. sg = sg_next(sg);
  114. }
  115. }
  116. static int crypto_kw_decrypt(struct skcipher_request *req)
  117. {
  118. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  119. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  120. struct crypto_kw_block block;
  121. struct scatterlist *src, *dst;
  122. u64 t = 6 * ((req->cryptlen) >> 3);
  123. unsigned int i;
  124. int ret = 0;
  125. /*
  126. * Require at least 2 semiblocks (note, the 3rd semiblock that is
  127. * required by SP800-38F is the IV.
  128. */
  129. if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE)
  130. return -EINVAL;
  131. /* Place the IV into block A */
  132. memcpy(&block.A, req->iv, SEMIBSIZE);
  133. /*
  134. * src scatterlist is read-only. dst scatterlist is r/w. During the
  135. * first loop, src points to req->src and dst to req->dst. For any
  136. * subsequent round, the code operates on req->dst only.
  137. */
  138. src = req->src;
  139. dst = req->dst;
  140. for (i = 0; i < 6; i++) {
  141. struct scatter_walk src_walk, dst_walk;
  142. unsigned int nbytes = req->cryptlen;
  143. while (nbytes) {
  144. /* move pointer by nbytes in the SGL */
  145. crypto_kw_scatterlist_ff(&src_walk, src, nbytes);
  146. /* get the source block */
  147. scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
  148. false);
  149. /* perform KW operation: modify IV with counter */
  150. block.A ^= cpu_to_be64(t);
  151. t--;
  152. /* perform KW operation: decrypt block */
  153. crypto_cipher_decrypt_one(cipher, (u8 *)&block,
  154. (u8 *)&block);
  155. /* move pointer by nbytes in the SGL */
  156. crypto_kw_scatterlist_ff(&dst_walk, dst, nbytes);
  157. /* Copy block->R into place */
  158. scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
  159. true);
  160. nbytes -= SEMIBSIZE;
  161. }
  162. /* we now start to operate on the dst SGL only */
  163. src = req->dst;
  164. dst = req->dst;
  165. }
  166. /* Perform authentication check */
  167. if (block.A != cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL))
  168. ret = -EBADMSG;
  169. memzero_explicit(&block, sizeof(struct crypto_kw_block));
  170. return ret;
  171. }
  172. static int crypto_kw_encrypt(struct skcipher_request *req)
  173. {
  174. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  175. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  176. struct crypto_kw_block block;
  177. struct scatterlist *src, *dst;
  178. u64 t = 1;
  179. unsigned int i;
  180. /*
  181. * Require at least 2 semiblocks (note, the 3rd semiblock that is
  182. * required by SP800-38F is the IV that occupies the first semiblock.
  183. * This means that the dst memory must be one semiblock larger than src.
  184. * Also ensure that the given data is aligned to semiblock.
  185. */
  186. if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE)
  187. return -EINVAL;
  188. /*
  189. * Place the predefined IV into block A -- for encrypt, the caller
  190. * does not need to provide an IV, but he needs to fetch the final IV.
  191. */
  192. block.A = cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL);
  193. /*
  194. * src scatterlist is read-only. dst scatterlist is r/w. During the
  195. * first loop, src points to req->src and dst to req->dst. For any
  196. * subsequent round, the code operates on req->dst only.
  197. */
  198. src = req->src;
  199. dst = req->dst;
  200. for (i = 0; i < 6; i++) {
  201. struct scatter_walk src_walk, dst_walk;
  202. unsigned int nbytes = req->cryptlen;
  203. scatterwalk_start(&src_walk, src);
  204. scatterwalk_start(&dst_walk, dst);
  205. while (nbytes) {
  206. /* get the source block */
  207. scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
  208. false);
  209. /* perform KW operation: encrypt block */
  210. crypto_cipher_encrypt_one(cipher, (u8 *)&block,
  211. (u8 *)&block);
  212. /* perform KW operation: modify IV with counter */
  213. block.A ^= cpu_to_be64(t);
  214. t++;
  215. /* Copy block->R into place */
  216. scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
  217. true);
  218. nbytes -= SEMIBSIZE;
  219. }
  220. /* we now start to operate on the dst SGL only */
  221. src = req->dst;
  222. dst = req->dst;
  223. }
  224. /* establish the IV for the caller to pick up */
  225. memcpy(req->iv, &block.A, SEMIBSIZE);
  226. memzero_explicit(&block, sizeof(struct crypto_kw_block));
  227. return 0;
  228. }
  229. static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb)
  230. {
  231. struct skcipher_instance *inst;
  232. struct crypto_alg *alg;
  233. int err;
  234. inst = skcipher_alloc_instance_simple(tmpl, tb);
  235. if (IS_ERR(inst))
  236. return PTR_ERR(inst);
  237. alg = skcipher_ialg_simple(inst);
  238. err = -EINVAL;
  239. /* Section 5.1 requirement for KW */
  240. if (alg->cra_blocksize != sizeof(struct crypto_kw_block))
  241. goto out_free_inst;
  242. inst->alg.base.cra_blocksize = SEMIBSIZE;
  243. inst->alg.base.cra_alignmask = 0;
  244. inst->alg.ivsize = SEMIBSIZE;
  245. inst->alg.encrypt = crypto_kw_encrypt;
  246. inst->alg.decrypt = crypto_kw_decrypt;
  247. err = skcipher_register_instance(tmpl, inst);
  248. if (err) {
  249. out_free_inst:
  250. inst->free(inst);
  251. }
  252. return err;
  253. }
  254. static struct crypto_template crypto_kw_tmpl = {
  255. .name = "kw",
  256. .create = crypto_kw_create,
  257. .module = THIS_MODULE,
  258. };
  259. static int __init crypto_kw_init(void)
  260. {
  261. return crypto_register_template(&crypto_kw_tmpl);
  262. }
  263. static void __exit crypto_kw_exit(void)
  264. {
  265. crypto_unregister_template(&crypto_kw_tmpl);
  266. }
  267. subsys_initcall(crypto_kw_init);
  268. module_exit(crypto_kw_exit);
  269. MODULE_LICENSE("Dual BSD/GPL");
  270. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  271. MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NIST SP800-38F)");
  272. MODULE_ALIAS_CRYPTO("kw");
  273. MODULE_IMPORT_NS(CRYPTO_INTERNAL);