ctr.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/random.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/slab.h>
  20. struct ctr_instance_ctx {
  21. struct crypto_spawn alg;
  22. unsigned int noncesize;
  23. unsigned int ivsize;
  24. unsigned int countersize;
  25. };
  26. struct crypto_ctr_ctx {
  27. struct crypto_cipher *child;
  28. u8 *nonce;
  29. };
  30. static int crypto_ctr_setkey(struct crypto_tfm *parent, const u8 *key,
  31. unsigned int keylen)
  32. {
  33. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(parent);
  34. struct crypto_cipher *child = ctx->child;
  35. struct ctr_instance_ctx *ictx =
  36. crypto_instance_ctx(crypto_tfm_alg_instance(parent));
  37. unsigned int noncelen = ictx->noncesize;
  38. int err = 0;
  39. /* the nonce is stored in bytes at end of key */
  40. if (keylen < noncelen)
  41. return -EINVAL;
  42. memcpy(ctx->nonce, key + (keylen - noncelen), noncelen);
  43. keylen -= noncelen;
  44. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  45. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  46. CRYPTO_TFM_REQ_MASK);
  47. err = crypto_cipher_setkey(child, key, keylen);
  48. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  49. CRYPTO_TFM_RES_MASK);
  50. return err;
  51. }
  52. static void crypto_ctr_crypt_final(struct blkcipher_walk *walk,
  53. struct crypto_cipher *tfm, u8 *ctrblk,
  54. unsigned int countersize)
  55. {
  56. unsigned int bsize = crypto_cipher_blocksize(tfm);
  57. u8 *keystream = ctrblk + bsize;
  58. u8 *src = walk->src.virt.addr;
  59. u8 *dst = walk->dst.virt.addr;
  60. unsigned int nbytes = walk->nbytes;
  61. crypto_cipher_encrypt_one(tfm, keystream, ctrblk);
  62. crypto_xor(keystream, src, nbytes);
  63. memcpy(dst, keystream, nbytes);
  64. }
  65. static int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
  66. struct crypto_cipher *tfm, u8 *ctrblk,
  67. unsigned int countersize)
  68. {
  69. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  70. crypto_cipher_alg(tfm)->cia_encrypt;
  71. unsigned int bsize = crypto_cipher_blocksize(tfm);
  72. u8 *src = walk->src.virt.addr;
  73. u8 *dst = walk->dst.virt.addr;
  74. unsigned int nbytes = walk->nbytes;
  75. do {
  76. /* create keystream */
  77. fn(crypto_cipher_tfm(tfm), dst, ctrblk);
  78. crypto_xor(dst, src, bsize);
  79. /* increment counter in counterblock */
  80. crypto_inc(ctrblk + bsize - countersize, countersize);
  81. src += bsize;
  82. dst += bsize;
  83. } while ((nbytes -= bsize) >= bsize);
  84. return nbytes;
  85. }
  86. static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
  87. struct crypto_cipher *tfm, u8 *ctrblk,
  88. unsigned int countersize)
  89. {
  90. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  91. crypto_cipher_alg(tfm)->cia_encrypt;
  92. unsigned int bsize = crypto_cipher_blocksize(tfm);
  93. unsigned int nbytes = walk->nbytes;
  94. u8 *src = walk->src.virt.addr;
  95. u8 *keystream = ctrblk + bsize;
  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 - countersize, countersize);
  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. struct ctr_instance_ctx *ictx =
  116. crypto_instance_ctx(crypto_tfm_alg_instance(&tfm->base));
  117. unsigned long alignmask = crypto_cipher_alignmask(child) |
  118. (__alignof__(u32) - 1);
  119. u8 cblk[bsize * 2 + alignmask];
  120. u8 *counterblk = (u8 *)ALIGN((unsigned long)cblk, alignmask + 1);
  121. int err;
  122. blkcipher_walk_init(&walk, dst, src, nbytes);
  123. err = blkcipher_walk_virt_block(desc, &walk, bsize);
  124. /* set up counter block */
  125. memset(counterblk, 0 , bsize);
  126. memcpy(counterblk, ctx->nonce, ictx->noncesize);
  127. memcpy(counterblk + ictx->noncesize, walk.iv, ictx->ivsize);
  128. /* initialize counter portion of counter block */
  129. crypto_inc(counterblk + bsize - ictx->countersize, ictx->countersize);
  130. while (walk.nbytes >= bsize) {
  131. if (walk.src.virt.addr == walk.dst.virt.addr)
  132. nbytes = crypto_ctr_crypt_inplace(&walk, child,
  133. counterblk,
  134. ictx->countersize);
  135. else
  136. nbytes = crypto_ctr_crypt_segment(&walk, child,
  137. counterblk,
  138. ictx->countersize);
  139. err = blkcipher_walk_done(desc, &walk, nbytes);
  140. }
  141. if (walk.nbytes) {
  142. crypto_ctr_crypt_final(&walk, child, counterblk,
  143. ictx->countersize);
  144. err = blkcipher_walk_done(desc, &walk, 0);
  145. }
  146. return err;
  147. }
  148. static int crypto_ctr_init_tfm(struct crypto_tfm *tfm)
  149. {
  150. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  151. struct ctr_instance_ctx *ictx = crypto_instance_ctx(inst);
  152. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
  153. struct crypto_cipher *cipher;
  154. ctx->nonce = kzalloc(ictx->noncesize, GFP_KERNEL);
  155. if (!ctx->nonce)
  156. return -ENOMEM;
  157. cipher = crypto_spawn_cipher(&ictx->alg);
  158. if (IS_ERR(cipher))
  159. return PTR_ERR(cipher);
  160. ctx->child = cipher;
  161. return 0;
  162. }
  163. static void crypto_ctr_exit_tfm(struct crypto_tfm *tfm)
  164. {
  165. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
  166. kfree(ctx->nonce);
  167. crypto_free_cipher(ctx->child);
  168. }
  169. static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
  170. {
  171. struct crypto_instance *inst;
  172. struct crypto_alg *alg;
  173. struct ctr_instance_ctx *ictx;
  174. unsigned int noncesize;
  175. unsigned int ivsize;
  176. unsigned int countersize;
  177. int err;
  178. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  179. if (err)
  180. return ERR_PTR(err);
  181. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  182. CRYPTO_ALG_TYPE_MASK);
  183. if (IS_ERR(alg))
  184. return ERR_PTR(PTR_ERR(alg));
  185. err = crypto_attr_u32(tb[2], &noncesize);
  186. if (err)
  187. goto out_put_alg;
  188. err = crypto_attr_u32(tb[3], &ivsize);
  189. if (err)
  190. goto out_put_alg;
  191. err = crypto_attr_u32(tb[4], &countersize);
  192. if (err)
  193. goto out_put_alg;
  194. /* verify size of nonce + iv + counter
  195. * counter must be >= 4 bytes.
  196. */
  197. err = -EINVAL;
  198. if (((noncesize + ivsize + countersize) < alg->cra_blocksize) ||
  199. ((noncesize + ivsize) > alg->cra_blocksize) ||
  200. (countersize > alg->cra_blocksize) || (countersize < 4))
  201. goto out_put_alg;
  202. /* If this is false we'd fail the alignment of crypto_inc. */
  203. if ((alg->cra_blocksize - countersize) % 4)
  204. goto out_put_alg;
  205. inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
  206. err = -ENOMEM;
  207. if (!inst)
  208. goto out_put_alg;
  209. err = -ENAMETOOLONG;
  210. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  211. "ctr(%s,%u,%u,%u)", alg->cra_name, noncesize,
  212. ivsize, countersize) >= CRYPTO_MAX_ALG_NAME) {
  213. goto err_free_inst;
  214. }
  215. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  216. "ctr(%s,%u,%u,%u)", alg->cra_driver_name, noncesize,
  217. ivsize, countersize) >= CRYPTO_MAX_ALG_NAME) {
  218. goto err_free_inst;
  219. }
  220. ictx = crypto_instance_ctx(inst);
  221. ictx->noncesize = noncesize;
  222. ictx->ivsize = ivsize;
  223. ictx->countersize = countersize;
  224. err = crypto_init_spawn(&ictx->alg, alg, inst,
  225. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  226. if (err)
  227. goto err_free_inst;
  228. err = 0;
  229. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  230. inst->alg.cra_priority = alg->cra_priority;
  231. inst->alg.cra_blocksize = 1;
  232. inst->alg.cra_alignmask = alg->cra_alignmask | (__alignof__(u32) - 1);
  233. inst->alg.cra_type = &crypto_blkcipher_type;
  234. inst->alg.cra_blkcipher.ivsize = ivsize;
  235. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize
  236. + noncesize;
  237. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize
  238. + noncesize;
  239. inst->alg.cra_ctxsize = sizeof(struct crypto_ctr_ctx);
  240. inst->alg.cra_init = crypto_ctr_init_tfm;
  241. inst->alg.cra_exit = crypto_ctr_exit_tfm;
  242. inst->alg.cra_blkcipher.setkey = crypto_ctr_setkey;
  243. inst->alg.cra_blkcipher.encrypt = crypto_ctr_crypt;
  244. inst->alg.cra_blkcipher.decrypt = crypto_ctr_crypt;
  245. err_free_inst:
  246. if (err)
  247. kfree(inst);
  248. out_put_alg:
  249. crypto_mod_put(alg);
  250. if (err)
  251. inst = ERR_PTR(err);
  252. return inst;
  253. }
  254. static void crypto_ctr_free(struct crypto_instance *inst)
  255. {
  256. struct ctr_instance_ctx *ictx = crypto_instance_ctx(inst);
  257. crypto_drop_spawn(&ictx->alg);
  258. kfree(inst);
  259. }
  260. static struct crypto_template crypto_ctr_tmpl = {
  261. .name = "ctr",
  262. .alloc = crypto_ctr_alloc,
  263. .free = crypto_ctr_free,
  264. .module = THIS_MODULE,
  265. };
  266. static int __init crypto_ctr_module_init(void)
  267. {
  268. return crypto_register_template(&crypto_ctr_tmpl);
  269. }
  270. static void __exit crypto_ctr_module_exit(void)
  271. {
  272. crypto_unregister_template(&crypto_ctr_tmpl);
  273. }
  274. module_init(crypto_ctr_module_init);
  275. module_exit(crypto_ctr_module_exit);
  276. MODULE_LICENSE("GPL");
  277. MODULE_DESCRIPTION("CTR Counter block mode");