ctr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * CTR: Counter mode
  3. *
  4. * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/algapi.h>
  13. #include <crypto/ctr.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/random.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/slab.h>
  22. struct crypto_ctr_ctx {
  23. struct crypto_cipher *child;
  24. };
  25. struct crypto_rfc3686_ctx {
  26. struct crypto_skcipher *child;
  27. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  28. };
  29. struct crypto_rfc3686_req_ctx {
  30. u8 iv[CTR_RFC3686_BLOCK_SIZE];
  31. struct skcipher_request subreq CRYPTO_MINALIGN_ATTR;
  32. };
  33. static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
  34. unsigned int keylen)
  35. {
  36. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(parent);
  37. struct crypto_cipher *child = ctx->child;
  38. int err;
  39. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  40. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  41. CRYPTO_TFM_REQ_MASK);
  42. err = crypto_cipher_setkey(child, key, keylen);
  43. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  44. CRYPTO_TFM_RES_MASK);
  45. return err;
  46. }
  47. static void crypto_ctr_crypt_final(struct blkcipher_walk *walk,
  48. struct crypto_cipher *tfm)
  49. {
  50. unsigned int bsize = crypto_cipher_blocksize(tfm);
  51. unsigned long alignmask = crypto_cipher_alignmask(tfm);
  52. u8 *ctrblk = walk->iv;
  53. u8 tmp[bsize + alignmask];
  54. u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
  55. u8 *src = walk->src.virt.addr;
  56. u8 *dst = walk->dst.virt.addr;
  57. unsigned int nbytes = walk->nbytes;
  58. crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
  59. crypto_xor(keystream, src, nbytes);
  60. memcpy(dst, keystream, nbytes);
  61. crypto_inc(ctrblk, bsize);
  62. }
  63. static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
  64. struct crypto_cipher *tfm)
  65. {
  66. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  67. crypto_cipher_alg(tfm)->cia_encrypt;
  68. unsigned int bsize = crypto_cipher_blocksize(tfm);
  69. u8 *ctrblk = walk->iv;
  70. u8 *src = walk->src.virt.addr;
  71. u8 *dst = walk->dst.virt.addr;
  72. unsigned int nbytes = walk->nbytes;
  73. do {
  74. /* create keystream */
  75. fn(crypto_cipher_tfm(tfm), dst, ctrblk);
  76. crypto_xor(dst, src, bsize);
  77. /* increment counter in counterblock */
  78. crypto_inc(ctrblk, bsize);
  79. src += bsize;
  80. dst += bsize;
  81. } while ((nbytes -= bsize) >= bsize);
  82. return nbytes;
  83. }
  84. static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
  85. struct crypto_cipher *tfm)
  86. {
  87. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  88. crypto_cipher_alg(tfm)->cia_encrypt;
  89. unsigned int bsize = crypto_cipher_blocksize(tfm);
  90. unsigned long alignmask = crypto_cipher_alignmask(tfm);
  91. unsigned int nbytes = walk->nbytes;
  92. u8 *ctrblk = walk->iv;
  93. u8 *src = walk->src.virt.addr;
  94. u8 tmp[bsize + alignmask];
  95. u8 *keystream = PTR_ALIGN(tmp + 0, alignmask + 1);
  96. do {
  97. /* create keystream */
  98. fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
  99. crypto_xor(src, keystream, bsize);
  100. /* increment counter in counterblock */
  101. crypto_inc(ctrblk, bsize);
  102. src += bsize;
  103. } while ((nbytes -= bsize) >= bsize);
  104. return nbytes;
  105. }
  106. static int crypto_ctr_crypt(struct blkcipher_desc *desc,
  107. struct scatterlist *dst, struct scatterlist *src,
  108. unsigned int nbytes)
  109. {
  110. struct blkcipher_walk walk;
  111. struct crypto_blkcipher *tfm = desc->tfm;
  112. struct crypto_ctr_ctx *ctx = crypto_blkcipher_ctx(tfm);
  113. struct crypto_cipher *child = ctx->child;
  114. unsigned int bsize = crypto_cipher_blocksize(child);
  115. int err;
  116. blkcipher_walk_init(&walk, dst, src, nbytes);
  117. err = blkcipher_walk_virt_block(desc, &walk, bsize);
  118. while (walk.nbytes >= bsize) {
  119. if (walk.src.virt.addr == walk.dst.virt.addr)
  120. nbytes = crypto_ctr_crypt_inplace(&walk, child);
  121. else
  122. nbytes = crypto_ctr_crypt_segment(&walk, child);
  123. err = blkcipher_walk_done(desc, &walk, nbytes);
  124. }
  125. if (walk.nbytes) {
  126. crypto_ctr_crypt_final(&walk, child);
  127. err = blkcipher_walk_done(desc, &walk, 0);
  128. }
  129. return err;
  130. }
  131. static int crypto_ctr_init_tfm(struct crypto_tfm *tfm)
  132. {
  133. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  134. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  135. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
  136. struct crypto_cipher *cipher;
  137. cipher = crypto_spawn_cipher(spawn);
  138. if (IS_ERR(cipher))
  139. return PTR_ERR(cipher);
  140. ctx->child = cipher;
  141. return 0;
  142. }
  143. static void crypto_ctr_exit_tfm(struct crypto_tfm *tfm)
  144. {
  145. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
  146. crypto_free_cipher(ctx->child);
  147. }
  148. static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
  149. {
  150. struct crypto_instance *inst;
  151. struct crypto_alg *alg;
  152. int err;
  153. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  154. if (err)
  155. return ERR_PTR(err);
  156. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  157. CRYPTO_ALG_TYPE_MASK);
  158. if (IS_ERR(alg))
  159. return ERR_CAST(alg);
  160. /* Block size must be >= 4 bytes. */
  161. err = -EINVAL;
  162. if (alg->cra_blocksize < 4)
  163. goto out_put_alg;
  164. /* If this is false we'd fail the alignment of crypto_inc. */
  165. if (alg->cra_blocksize % 4)
  166. goto out_put_alg;
  167. inst = crypto_alloc_instance("ctr", alg);
  168. if (IS_ERR(inst))
  169. goto out;
  170. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  171. inst->alg.cra_priority = alg->cra_priority;
  172. inst->alg.cra_blocksize = 1;
  173. inst->alg.cra_alignmask = alg->cra_alignmask | (__alignof__(u32) - 1);
  174. inst->alg.cra_type = &crypto_blkcipher_type;
  175. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  176. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  177. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  178. inst->alg.cra_ctxsize = sizeof(struct crypto_ctr_ctx);
  179. inst->alg.cra_init = crypto_ctr_init_tfm;
  180. inst->alg.cra_exit = crypto_ctr_exit_tfm;
  181. inst->alg.cra_blkcipher.setkey = crypto_ctr_setkey;
  182. inst->alg.cra_blkcipher.encrypt = crypto_ctr_crypt;
  183. inst->alg.cra_blkcipher.decrypt = crypto_ctr_crypt;
  184. inst->alg.cra_blkcipher.geniv = "chainiv";
  185. out:
  186. crypto_mod_put(alg);
  187. return inst;
  188. out_put_alg:
  189. inst = ERR_PTR(err);
  190. goto out;
  191. }
  192. static void crypto_ctr_free(struct crypto_instance *inst)
  193. {
  194. crypto_drop_spawn(crypto_instance_ctx(inst));
  195. kfree(inst);
  196. }
  197. static struct crypto_template crypto_ctr_tmpl = {
  198. .name = "ctr",
  199. .alloc = crypto_ctr_alloc,
  200. .free = crypto_ctr_free,
  201. .module = THIS_MODULE,
  202. };
  203. static int crypto_rfc3686_setkey(struct crypto_skcipher *parent,
  204. const u8 *key, unsigned int keylen)
  205. {
  206. struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(parent);
  207. struct crypto_skcipher *child = ctx->child;
  208. int err;
  209. /* the nonce is stored in bytes at end of key */
  210. if (keylen < CTR_RFC3686_NONCE_SIZE)
  211. return -EINVAL;
  212. memcpy(ctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
  213. CTR_RFC3686_NONCE_SIZE);
  214. keylen -= CTR_RFC3686_NONCE_SIZE;
  215. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  216. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  217. CRYPTO_TFM_REQ_MASK);
  218. err = crypto_skcipher_setkey(child, key, keylen);
  219. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  220. CRYPTO_TFM_RES_MASK);
  221. return err;
  222. }
  223. static int crypto_rfc3686_crypt(struct skcipher_request *req)
  224. {
  225. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  226. struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
  227. struct crypto_skcipher *child = ctx->child;
  228. unsigned long align = crypto_skcipher_alignmask(tfm);
  229. struct crypto_rfc3686_req_ctx *rctx =
  230. (void *)PTR_ALIGN((u8 *)skcipher_request_ctx(req), align + 1);
  231. struct skcipher_request *subreq = &rctx->subreq;
  232. u8 *iv = rctx->iv;
  233. /* set up counter block */
  234. memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
  235. memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE);
  236. /* initialize counter portion of counter block */
  237. *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
  238. cpu_to_be32(1);
  239. skcipher_request_set_tfm(subreq, child);
  240. skcipher_request_set_callback(subreq, req->base.flags,
  241. req->base.complete, req->base.data);
  242. skcipher_request_set_crypt(subreq, req->src, req->dst,
  243. req->cryptlen, iv);
  244. return crypto_skcipher_encrypt(subreq);
  245. }
  246. static int crypto_rfc3686_init_tfm(struct crypto_skcipher *tfm)
  247. {
  248. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  249. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  250. struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
  251. struct crypto_skcipher *cipher;
  252. unsigned long align;
  253. unsigned int reqsize;
  254. cipher = crypto_spawn_skcipher2(spawn);
  255. if (IS_ERR(cipher))
  256. return PTR_ERR(cipher);
  257. ctx->child = cipher;
  258. align = crypto_skcipher_alignmask(tfm);
  259. align &= ~(crypto_tfm_ctx_alignment() - 1);
  260. reqsize = align + sizeof(struct crypto_rfc3686_req_ctx) +
  261. crypto_skcipher_reqsize(cipher);
  262. crypto_skcipher_set_reqsize(tfm, reqsize);
  263. return 0;
  264. }
  265. static void crypto_rfc3686_exit_tfm(struct crypto_skcipher *tfm)
  266. {
  267. struct crypto_rfc3686_ctx *ctx = crypto_skcipher_ctx(tfm);
  268. crypto_free_skcipher(ctx->child);
  269. }
  270. static void crypto_rfc3686_free(struct skcipher_instance *inst)
  271. {
  272. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  273. crypto_drop_skcipher(spawn);
  274. kfree(inst);
  275. }
  276. static int crypto_rfc3686_create(struct crypto_template *tmpl,
  277. struct rtattr **tb)
  278. {
  279. struct crypto_attr_type *algt;
  280. struct skcipher_instance *inst;
  281. struct skcipher_alg *alg;
  282. struct crypto_skcipher_spawn *spawn;
  283. const char *cipher_name;
  284. int err;
  285. algt = crypto_get_attr_type(tb);
  286. if (IS_ERR(algt))
  287. return PTR_ERR(algt);
  288. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  289. return -EINVAL;
  290. cipher_name = crypto_attr_alg_name(tb[1]);
  291. if (IS_ERR(cipher_name))
  292. return PTR_ERR(cipher_name);
  293. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  294. if (!inst)
  295. return -ENOMEM;
  296. spawn = skcipher_instance_ctx(inst);
  297. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  298. err = crypto_grab_skcipher2(spawn, cipher_name, 0,
  299. crypto_requires_sync(algt->type,
  300. algt->mask));
  301. if (err)
  302. goto err_free_inst;
  303. alg = crypto_spawn_skcipher_alg(spawn);
  304. /* We only support 16-byte blocks. */
  305. err = -EINVAL;
  306. if (crypto_skcipher_alg_ivsize(alg) != CTR_RFC3686_BLOCK_SIZE)
  307. goto err_drop_spawn;
  308. /* Not a stream cipher? */
  309. if (alg->base.cra_blocksize != 1)
  310. goto err_drop_spawn;
  311. err = -ENAMETOOLONG;
  312. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  313. "rfc3686(%s)", alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
  314. goto err_drop_spawn;
  315. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  316. "rfc3686(%s)", alg->base.cra_driver_name) >=
  317. CRYPTO_MAX_ALG_NAME)
  318. goto err_drop_spawn;
  319. inst->alg.base.cra_priority = alg->base.cra_priority;
  320. inst->alg.base.cra_blocksize = 1;
  321. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  322. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  323. inst->alg.ivsize = CTR_RFC3686_IV_SIZE;
  324. inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
  325. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
  326. CTR_RFC3686_NONCE_SIZE;
  327. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
  328. CTR_RFC3686_NONCE_SIZE;
  329. inst->alg.setkey = crypto_rfc3686_setkey;
  330. inst->alg.encrypt = crypto_rfc3686_crypt;
  331. inst->alg.decrypt = crypto_rfc3686_crypt;
  332. inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
  333. inst->alg.init = crypto_rfc3686_init_tfm;
  334. inst->alg.exit = crypto_rfc3686_exit_tfm;
  335. inst->free = crypto_rfc3686_free;
  336. err = skcipher_register_instance(tmpl, inst);
  337. if (err)
  338. goto err_drop_spawn;
  339. out:
  340. return err;
  341. err_drop_spawn:
  342. crypto_drop_skcipher(spawn);
  343. err_free_inst:
  344. kfree(inst);
  345. goto out;
  346. }
  347. static struct crypto_template crypto_rfc3686_tmpl = {
  348. .name = "rfc3686",
  349. .create = crypto_rfc3686_create,
  350. .module = THIS_MODULE,
  351. };
  352. static int __init crypto_ctr_module_init(void)
  353. {
  354. int err;
  355. err = crypto_register_template(&crypto_ctr_tmpl);
  356. if (err)
  357. goto out;
  358. err = crypto_register_template(&crypto_rfc3686_tmpl);
  359. if (err)
  360. goto out_drop_ctr;
  361. out:
  362. return err;
  363. out_drop_ctr:
  364. crypto_unregister_template(&crypto_ctr_tmpl);
  365. goto out;
  366. }
  367. static void __exit crypto_ctr_module_exit(void)
  368. {
  369. crypto_unregister_template(&crypto_rfc3686_tmpl);
  370. crypto_unregister_template(&crypto_ctr_tmpl);
  371. }
  372. module_init(crypto_ctr_module_init);
  373. module_exit(crypto_ctr_module_exit);
  374. MODULE_LICENSE("GPL");
  375. MODULE_DESCRIPTION("CTR Counter block mode");
  376. MODULE_ALIAS_CRYPTO("rfc3686");
  377. MODULE_ALIAS_CRYPTO("ctr");