keywrap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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, ptdata, 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, ctdata, ctlen);
  73. * skcipher_request_set_crypt(req, &sg, &sg, ptlen, 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/skcipher.h>
  87. struct crypto_kw_ctx {
  88. struct crypto_cipher *child;
  89. };
  90. struct crypto_kw_block {
  91. #define SEMIBSIZE 8
  92. __be64 A;
  93. __be64 R;
  94. };
  95. /*
  96. * Fast forward the SGL to the "end" length minus SEMIBSIZE.
  97. * The start in the SGL defined by the fast-forward is returned with
  98. * the walk variable
  99. */
  100. static void crypto_kw_scatterlist_ff(struct scatter_walk *walk,
  101. struct scatterlist *sg,
  102. unsigned int end)
  103. {
  104. unsigned int skip = 0;
  105. /* The caller should only operate on full SEMIBLOCKs. */
  106. BUG_ON(end < SEMIBSIZE);
  107. skip = end - SEMIBSIZE;
  108. while (sg) {
  109. if (sg->length > skip) {
  110. scatterwalk_start(walk, sg);
  111. scatterwalk_advance(walk, skip);
  112. break;
  113. } else
  114. skip -= sg->length;
  115. sg = sg_next(sg);
  116. }
  117. }
  118. static int crypto_kw_decrypt(struct blkcipher_desc *desc,
  119. struct scatterlist *dst, struct scatterlist *src,
  120. unsigned int nbytes)
  121. {
  122. struct crypto_blkcipher *tfm = desc->tfm;
  123. struct crypto_kw_ctx *ctx = crypto_blkcipher_ctx(tfm);
  124. struct crypto_cipher *child = ctx->child;
  125. struct crypto_kw_block block;
  126. struct scatterlist *lsrc, *ldst;
  127. u64 t = 6 * ((nbytes) >> 3);
  128. unsigned int i;
  129. int ret = 0;
  130. /*
  131. * Require at least 2 semiblocks (note, the 3rd semiblock that is
  132. * required by SP800-38F is the IV.
  133. */
  134. if (nbytes < (2 * SEMIBSIZE) || nbytes % SEMIBSIZE)
  135. return -EINVAL;
  136. /* Place the IV into block A */
  137. memcpy(&block.A, desc->info, SEMIBSIZE);
  138. /*
  139. * src scatterlist is read-only. dst scatterlist is r/w. During the
  140. * first loop, lsrc points to src and ldst to dst. For any
  141. * subsequent round, the code operates on dst only.
  142. */
  143. lsrc = src;
  144. ldst = dst;
  145. for (i = 0; i < 6; i++) {
  146. struct scatter_walk src_walk, dst_walk;
  147. unsigned int tmp_nbytes = nbytes;
  148. while (tmp_nbytes) {
  149. /* move pointer by tmp_nbytes in the SGL */
  150. crypto_kw_scatterlist_ff(&src_walk, lsrc, tmp_nbytes);
  151. /* get the source block */
  152. scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
  153. false);
  154. /* perform KW operation: modify IV with counter */
  155. block.A ^= cpu_to_be64(t);
  156. t--;
  157. /* perform KW operation: decrypt block */
  158. crypto_cipher_decrypt_one(child, (u8*)&block,
  159. (u8*)&block);
  160. /* move pointer by tmp_nbytes in the SGL */
  161. crypto_kw_scatterlist_ff(&dst_walk, ldst, tmp_nbytes);
  162. /* Copy block->R into place */
  163. scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
  164. true);
  165. tmp_nbytes -= SEMIBSIZE;
  166. }
  167. /* we now start to operate on the dst SGL only */
  168. lsrc = dst;
  169. ldst = dst;
  170. }
  171. /* Perform authentication check */
  172. if (block.A != cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL))
  173. ret = -EBADMSG;
  174. memzero_explicit(&block, sizeof(struct crypto_kw_block));
  175. return ret;
  176. }
  177. static int crypto_kw_encrypt(struct blkcipher_desc *desc,
  178. struct scatterlist *dst, struct scatterlist *src,
  179. unsigned int nbytes)
  180. {
  181. struct crypto_blkcipher *tfm = desc->tfm;
  182. struct crypto_kw_ctx *ctx = crypto_blkcipher_ctx(tfm);
  183. struct crypto_cipher *child = ctx->child;
  184. struct crypto_kw_block block;
  185. struct scatterlist *lsrc, *ldst;
  186. u64 t = 1;
  187. unsigned int i;
  188. /*
  189. * Require at least 2 semiblocks (note, the 3rd semiblock that is
  190. * required by SP800-38F is the IV that occupies the first semiblock.
  191. * This means that the dst memory must be one semiblock larger than src.
  192. * Also ensure that the given data is aligned to semiblock.
  193. */
  194. if (nbytes < (2 * SEMIBSIZE) || nbytes % SEMIBSIZE)
  195. return -EINVAL;
  196. /*
  197. * Place the predefined IV into block A -- for encrypt, the caller
  198. * does not need to provide an IV, but he needs to fetch the final IV.
  199. */
  200. block.A = cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL);
  201. /*
  202. * src scatterlist is read-only. dst scatterlist is r/w. During the
  203. * first loop, lsrc points to src and ldst to dst. For any
  204. * subsequent round, the code operates on dst only.
  205. */
  206. lsrc = src;
  207. ldst = dst;
  208. for (i = 0; i < 6; i++) {
  209. struct scatter_walk src_walk, dst_walk;
  210. unsigned int tmp_nbytes = nbytes;
  211. scatterwalk_start(&src_walk, lsrc);
  212. scatterwalk_start(&dst_walk, ldst);
  213. while (tmp_nbytes) {
  214. /* get the source block */
  215. scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
  216. false);
  217. /* perform KW operation: encrypt block */
  218. crypto_cipher_encrypt_one(child, (u8 *)&block,
  219. (u8 *)&block);
  220. /* perform KW operation: modify IV with counter */
  221. block.A ^= cpu_to_be64(t);
  222. t++;
  223. /* Copy block->R into place */
  224. scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
  225. true);
  226. tmp_nbytes -= SEMIBSIZE;
  227. }
  228. /* we now start to operate on the dst SGL only */
  229. lsrc = dst;
  230. ldst = dst;
  231. }
  232. /* establish the IV for the caller to pick up */
  233. memcpy(desc->info, &block.A, SEMIBSIZE);
  234. memzero_explicit(&block, sizeof(struct crypto_kw_block));
  235. return 0;
  236. }
  237. static int crypto_kw_setkey(struct crypto_tfm *parent, const u8 *key,
  238. unsigned int keylen)
  239. {
  240. struct crypto_kw_ctx *ctx = crypto_tfm_ctx(parent);
  241. struct crypto_cipher *child = ctx->child;
  242. int err;
  243. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  244. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  245. CRYPTO_TFM_REQ_MASK);
  246. err = crypto_cipher_setkey(child, key, keylen);
  247. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  248. CRYPTO_TFM_RES_MASK);
  249. return err;
  250. }
  251. static int crypto_kw_init_tfm(struct crypto_tfm *tfm)
  252. {
  253. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  254. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  255. struct crypto_kw_ctx *ctx = crypto_tfm_ctx(tfm);
  256. struct crypto_cipher *cipher;
  257. cipher = crypto_spawn_cipher(spawn);
  258. if (IS_ERR(cipher))
  259. return PTR_ERR(cipher);
  260. ctx->child = cipher;
  261. return 0;
  262. }
  263. static void crypto_kw_exit_tfm(struct crypto_tfm *tfm)
  264. {
  265. struct crypto_kw_ctx *ctx = crypto_tfm_ctx(tfm);
  266. crypto_free_cipher(ctx->child);
  267. }
  268. static struct crypto_instance *crypto_kw_alloc(struct rtattr **tb)
  269. {
  270. struct crypto_instance *inst = NULL;
  271. struct crypto_alg *alg = NULL;
  272. int err;
  273. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  274. if (err)
  275. return ERR_PTR(err);
  276. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  277. CRYPTO_ALG_TYPE_MASK);
  278. if (IS_ERR(alg))
  279. return ERR_CAST(alg);
  280. inst = ERR_PTR(-EINVAL);
  281. /* Section 5.1 requirement for KW */
  282. if (alg->cra_blocksize != sizeof(struct crypto_kw_block))
  283. goto err;
  284. inst = crypto_alloc_instance("kw", alg);
  285. if (IS_ERR(inst))
  286. goto err;
  287. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  288. inst->alg.cra_priority = alg->cra_priority;
  289. inst->alg.cra_blocksize = SEMIBSIZE;
  290. inst->alg.cra_alignmask = 0;
  291. inst->alg.cra_type = &crypto_blkcipher_type;
  292. inst->alg.cra_blkcipher.ivsize = SEMIBSIZE;
  293. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  294. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  295. inst->alg.cra_ctxsize = sizeof(struct crypto_kw_ctx);
  296. inst->alg.cra_init = crypto_kw_init_tfm;
  297. inst->alg.cra_exit = crypto_kw_exit_tfm;
  298. inst->alg.cra_blkcipher.setkey = crypto_kw_setkey;
  299. inst->alg.cra_blkcipher.encrypt = crypto_kw_encrypt;
  300. inst->alg.cra_blkcipher.decrypt = crypto_kw_decrypt;
  301. err:
  302. crypto_mod_put(alg);
  303. return inst;
  304. }
  305. static void crypto_kw_free(struct crypto_instance *inst)
  306. {
  307. crypto_drop_spawn(crypto_instance_ctx(inst));
  308. kfree(inst);
  309. }
  310. static struct crypto_template crypto_kw_tmpl = {
  311. .name = "kw",
  312. .alloc = crypto_kw_alloc,
  313. .free = crypto_kw_free,
  314. .module = THIS_MODULE,
  315. };
  316. static int __init crypto_kw_init(void)
  317. {
  318. return crypto_register_template(&crypto_kw_tmpl);
  319. }
  320. static void __exit crypto_kw_exit(void)
  321. {
  322. crypto_unregister_template(&crypto_kw_tmpl);
  323. }
  324. module_init(crypto_kw_init);
  325. module_exit(crypto_kw_exit);
  326. MODULE_LICENSE("Dual BSD/GPL");
  327. MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
  328. MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NIST SP800-38F)");
  329. MODULE_ALIAS_CRYPTO("kw");