ctr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_ablkcipher *child;
  27. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  28. };
  29. struct crypto_rfc3686_req_ctx {
  30. u8 iv[CTR_RFC3686_BLOCK_SIZE];
  31. struct ablkcipher_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_ablkcipher *parent,
  204. const u8 *key, unsigned int keylen)
  205. {
  206. struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(parent);
  207. struct crypto_ablkcipher *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_ablkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  216. crypto_ablkcipher_set_flags(child, crypto_ablkcipher_get_flags(parent) &
  217. CRYPTO_TFM_REQ_MASK);
  218. err = crypto_ablkcipher_setkey(child, key, keylen);
  219. crypto_ablkcipher_set_flags(parent, crypto_ablkcipher_get_flags(child) &
  220. CRYPTO_TFM_RES_MASK);
  221. return err;
  222. }
  223. static int crypto_rfc3686_crypt(struct ablkcipher_request *req)
  224. {
  225. struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  226. struct crypto_rfc3686_ctx *ctx = crypto_ablkcipher_ctx(tfm);
  227. struct crypto_ablkcipher *child = ctx->child;
  228. unsigned long align = crypto_ablkcipher_alignmask(tfm);
  229. struct crypto_rfc3686_req_ctx *rctx =
  230. (void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
  231. struct ablkcipher_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->info, 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. ablkcipher_request_set_tfm(subreq, child);
  240. ablkcipher_request_set_callback(subreq, req->base.flags,
  241. req->base.complete, req->base.data);
  242. ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->nbytes,
  243. iv);
  244. return crypto_ablkcipher_encrypt(subreq);
  245. }
  246. static int crypto_rfc3686_init_tfm(struct crypto_tfm *tfm)
  247. {
  248. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  249. struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst);
  250. struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm);
  251. struct crypto_ablkcipher *cipher;
  252. unsigned long align;
  253. cipher = crypto_spawn_skcipher(spawn);
  254. if (IS_ERR(cipher))
  255. return PTR_ERR(cipher);
  256. ctx->child = cipher;
  257. align = crypto_tfm_alg_alignmask(tfm);
  258. align &= ~(crypto_tfm_ctx_alignment() - 1);
  259. tfm->crt_ablkcipher.reqsize = align +
  260. sizeof(struct crypto_rfc3686_req_ctx) +
  261. crypto_ablkcipher_reqsize(cipher);
  262. return 0;
  263. }
  264. static void crypto_rfc3686_exit_tfm(struct crypto_tfm *tfm)
  265. {
  266. struct crypto_rfc3686_ctx *ctx = crypto_tfm_ctx(tfm);
  267. crypto_free_ablkcipher(ctx->child);
  268. }
  269. static struct crypto_instance *crypto_rfc3686_alloc(struct rtattr **tb)
  270. {
  271. struct crypto_attr_type *algt;
  272. struct crypto_instance *inst;
  273. struct crypto_alg *alg;
  274. struct crypto_skcipher_spawn *spawn;
  275. const char *cipher_name;
  276. int err;
  277. algt = crypto_get_attr_type(tb);
  278. if (IS_ERR(algt))
  279. return ERR_CAST(algt);
  280. if ((algt->type ^ CRYPTO_ALG_TYPE_BLKCIPHER) & algt->mask)
  281. return ERR_PTR(-EINVAL);
  282. cipher_name = crypto_attr_alg_name(tb[1]);
  283. if (IS_ERR(cipher_name))
  284. return ERR_CAST(cipher_name);
  285. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  286. if (!inst)
  287. return ERR_PTR(-ENOMEM);
  288. spawn = crypto_instance_ctx(inst);
  289. crypto_set_skcipher_spawn(spawn, inst);
  290. err = crypto_grab_skcipher(spawn, cipher_name, 0,
  291. crypto_requires_sync(algt->type,
  292. algt->mask));
  293. if (err)
  294. goto err_free_inst;
  295. alg = crypto_skcipher_spawn_alg(spawn);
  296. /* We only support 16-byte blocks. */
  297. err = -EINVAL;
  298. if (alg->cra_ablkcipher.ivsize != CTR_RFC3686_BLOCK_SIZE)
  299. goto err_drop_spawn;
  300. /* Not a stream cipher? */
  301. if (alg->cra_blocksize != 1)
  302. goto err_drop_spawn;
  303. err = -ENAMETOOLONG;
  304. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "rfc3686(%s)",
  305. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  306. goto err_drop_spawn;
  307. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  308. "rfc3686(%s)", alg->cra_driver_name) >=
  309. CRYPTO_MAX_ALG_NAME)
  310. goto err_drop_spawn;
  311. inst->alg.cra_priority = alg->cra_priority;
  312. inst->alg.cra_blocksize = 1;
  313. inst->alg.cra_alignmask = alg->cra_alignmask;
  314. inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  315. (alg->cra_flags & CRYPTO_ALG_ASYNC);
  316. inst->alg.cra_type = &crypto_ablkcipher_type;
  317. inst->alg.cra_ablkcipher.ivsize = CTR_RFC3686_IV_SIZE;
  318. inst->alg.cra_ablkcipher.min_keysize =
  319. alg->cra_ablkcipher.min_keysize + CTR_RFC3686_NONCE_SIZE;
  320. inst->alg.cra_ablkcipher.max_keysize =
  321. alg->cra_ablkcipher.max_keysize + CTR_RFC3686_NONCE_SIZE;
  322. inst->alg.cra_ablkcipher.geniv = "seqiv";
  323. inst->alg.cra_ablkcipher.setkey = crypto_rfc3686_setkey;
  324. inst->alg.cra_ablkcipher.encrypt = crypto_rfc3686_crypt;
  325. inst->alg.cra_ablkcipher.decrypt = crypto_rfc3686_crypt;
  326. inst->alg.cra_ctxsize = sizeof(struct crypto_rfc3686_ctx);
  327. inst->alg.cra_init = crypto_rfc3686_init_tfm;
  328. inst->alg.cra_exit = crypto_rfc3686_exit_tfm;
  329. return inst;
  330. err_drop_spawn:
  331. crypto_drop_skcipher(spawn);
  332. err_free_inst:
  333. kfree(inst);
  334. return ERR_PTR(err);
  335. }
  336. static void crypto_rfc3686_free(struct crypto_instance *inst)
  337. {
  338. struct crypto_skcipher_spawn *spawn = crypto_instance_ctx(inst);
  339. crypto_drop_skcipher(spawn);
  340. kfree(inst);
  341. }
  342. static struct crypto_template crypto_rfc3686_tmpl = {
  343. .name = "rfc3686",
  344. .alloc = crypto_rfc3686_alloc,
  345. .free = crypto_rfc3686_free,
  346. .module = THIS_MODULE,
  347. };
  348. static int __init crypto_ctr_module_init(void)
  349. {
  350. int err;
  351. err = crypto_register_template(&crypto_ctr_tmpl);
  352. if (err)
  353. goto out;
  354. err = crypto_register_template(&crypto_rfc3686_tmpl);
  355. if (err)
  356. goto out_drop_ctr;
  357. out:
  358. return err;
  359. out_drop_ctr:
  360. crypto_unregister_template(&crypto_ctr_tmpl);
  361. goto out;
  362. }
  363. static void __exit crypto_ctr_module_exit(void)
  364. {
  365. crypto_unregister_template(&crypto_rfc3686_tmpl);
  366. crypto_unregister_template(&crypto_ctr_tmpl);
  367. }
  368. module_init(crypto_ctr_module_init);
  369. module_exit(crypto_ctr_module_exit);
  370. MODULE_LICENSE("GPL");
  371. MODULE_DESCRIPTION("CTR Counter block mode");
  372. MODULE_ALIAS_CRYPTO("rfc3686");
  373. MODULE_ALIAS_CRYPTO("ctr");