ctr.c 13 KB

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