ctr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 int crypto_ctr_crypt_segment(struct blkcipher_walk *walk,
  53. struct crypto_cipher *tfm, u8 *ctrblk,
  54. unsigned int countersize)
  55. {
  56. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  57. crypto_cipher_alg(tfm)->cia_encrypt;
  58. unsigned int bsize = crypto_cipher_blocksize(tfm);
  59. unsigned long alignmask = crypto_cipher_alignmask(tfm) |
  60. (__alignof__(u32) - 1);
  61. u8 ks[bsize + alignmask];
  62. u8 *keystream = (u8 *)ALIGN((unsigned long)ks, alignmask + 1);
  63. u8 *src = walk->src.virt.addr;
  64. u8 *dst = walk->dst.virt.addr;
  65. unsigned int nbytes = walk->nbytes;
  66. do {
  67. /* create keystream */
  68. fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
  69. crypto_xor(keystream, src, min(nbytes, bsize));
  70. /* copy result into dst */
  71. memcpy(dst, keystream, min(nbytes, bsize));
  72. /* increment counter in counterblock */
  73. crypto_inc(ctrblk + bsize - countersize, countersize);
  74. if (nbytes < bsize)
  75. break;
  76. src += bsize;
  77. dst += bsize;
  78. nbytes -= bsize;
  79. } while (nbytes);
  80. return 0;
  81. }
  82. static int crypto_ctr_crypt_inplace(struct blkcipher_walk *walk,
  83. struct crypto_cipher *tfm, u8 *ctrblk,
  84. unsigned int countersize)
  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. (__alignof__(u32) - 1);
  91. unsigned int nbytes = walk->nbytes;
  92. u8 *src = walk->src.virt.addr;
  93. u8 ks[bsize + alignmask];
  94. u8 *keystream = (u8 *)ALIGN((unsigned long)ks, alignmask + 1);
  95. do {
  96. /* create keystream */
  97. fn(crypto_cipher_tfm(tfm), keystream, ctrblk);
  98. crypto_xor(src, keystream, min(nbytes, bsize));
  99. /* increment counter in counterblock */
  100. crypto_inc(ctrblk + bsize - countersize, countersize);
  101. if (nbytes < bsize)
  102. break;
  103. src += bsize;
  104. nbytes -= bsize;
  105. } while (nbytes);
  106. return 0;
  107. }
  108. static int crypto_ctr_crypt(struct blkcipher_desc *desc,
  109. struct scatterlist *dst, struct scatterlist *src,
  110. unsigned int nbytes)
  111. {
  112. struct blkcipher_walk walk;
  113. struct crypto_blkcipher *tfm = desc->tfm;
  114. struct crypto_ctr_ctx *ctx = crypto_blkcipher_ctx(tfm);
  115. struct crypto_cipher *child = ctx->child;
  116. unsigned int bsize = crypto_cipher_blocksize(child);
  117. struct ctr_instance_ctx *ictx =
  118. crypto_instance_ctx(crypto_tfm_alg_instance(&tfm->base));
  119. unsigned long alignmask = crypto_cipher_alignmask(child) |
  120. (__alignof__(u32) - 1);
  121. u8 cblk[bsize + alignmask];
  122. u8 *counterblk = (u8 *)ALIGN((unsigned long)cblk, alignmask + 1);
  123. int err;
  124. blkcipher_walk_init(&walk, dst, src, nbytes);
  125. err = blkcipher_walk_virt_block(desc, &walk, bsize);
  126. /* set up counter block */
  127. memset(counterblk, 0 , bsize);
  128. memcpy(counterblk, ctx->nonce, ictx->noncesize);
  129. memcpy(counterblk + ictx->noncesize, walk.iv, ictx->ivsize);
  130. /* initialize counter portion of counter block */
  131. crypto_inc(counterblk + bsize - ictx->countersize, ictx->countersize);
  132. while (walk.nbytes) {
  133. if (walk.src.virt.addr == walk.dst.virt.addr)
  134. nbytes = crypto_ctr_crypt_inplace(&walk, child,
  135. counterblk,
  136. ictx->countersize);
  137. else
  138. nbytes = crypto_ctr_crypt_segment(&walk, child,
  139. counterblk,
  140. ictx->countersize);
  141. err = blkcipher_walk_done(desc, &walk, nbytes);
  142. }
  143. return err;
  144. }
  145. static int crypto_ctr_init_tfm(struct crypto_tfm *tfm)
  146. {
  147. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  148. struct ctr_instance_ctx *ictx = crypto_instance_ctx(inst);
  149. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
  150. struct crypto_cipher *cipher;
  151. ctx->nonce = kzalloc(ictx->noncesize, GFP_KERNEL);
  152. if (!ctx->nonce)
  153. return -ENOMEM;
  154. cipher = crypto_spawn_cipher(&ictx->alg);
  155. if (IS_ERR(cipher))
  156. return PTR_ERR(cipher);
  157. ctx->child = cipher;
  158. return 0;
  159. }
  160. static void crypto_ctr_exit_tfm(struct crypto_tfm *tfm)
  161. {
  162. struct crypto_ctr_ctx *ctx = crypto_tfm_ctx(tfm);
  163. kfree(ctx->nonce);
  164. crypto_free_cipher(ctx->child);
  165. }
  166. static struct crypto_instance *crypto_ctr_alloc(struct rtattr **tb)
  167. {
  168. struct crypto_instance *inst;
  169. struct crypto_alg *alg;
  170. struct ctr_instance_ctx *ictx;
  171. unsigned int noncesize;
  172. unsigned int ivsize;
  173. unsigned int countersize;
  174. int err;
  175. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  176. if (err)
  177. return ERR_PTR(err);
  178. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_CIPHER,
  179. CRYPTO_ALG_TYPE_MASK);
  180. if (IS_ERR(alg))
  181. return ERR_PTR(PTR_ERR(alg));
  182. err = crypto_attr_u32(tb[2], &noncesize);
  183. if (err)
  184. goto out_put_alg;
  185. err = crypto_attr_u32(tb[3], &ivsize);
  186. if (err)
  187. goto out_put_alg;
  188. err = crypto_attr_u32(tb[4], &countersize);
  189. if (err)
  190. goto out_put_alg;
  191. /* verify size of nonce + iv + counter
  192. * counter must be >= 4 bytes.
  193. */
  194. err = -EINVAL;
  195. if (((noncesize + ivsize + countersize) < alg->cra_blocksize) ||
  196. ((noncesize + ivsize) > alg->cra_blocksize) ||
  197. (countersize > alg->cra_blocksize) || (countersize < 4))
  198. goto out_put_alg;
  199. /* If this is false we'd fail the alignment of crypto_inc. */
  200. if ((alg->cra_blocksize - countersize) % 4)
  201. goto out_put_alg;
  202. inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
  203. err = -ENOMEM;
  204. if (!inst)
  205. goto out_put_alg;
  206. err = -ENAMETOOLONG;
  207. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
  208. "ctr(%s,%u,%u,%u)", alg->cra_name, noncesize,
  209. ivsize, countersize) >= CRYPTO_MAX_ALG_NAME) {
  210. goto err_free_inst;
  211. }
  212. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  213. "ctr(%s,%u,%u,%u)", alg->cra_driver_name, noncesize,
  214. ivsize, countersize) >= CRYPTO_MAX_ALG_NAME) {
  215. goto err_free_inst;
  216. }
  217. ictx = crypto_instance_ctx(inst);
  218. ictx->noncesize = noncesize;
  219. ictx->ivsize = ivsize;
  220. ictx->countersize = countersize;
  221. err = crypto_init_spawn(&ictx->alg, alg, inst,
  222. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  223. if (err)
  224. goto err_free_inst;
  225. err = 0;
  226. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  227. inst->alg.cra_priority = alg->cra_priority;
  228. inst->alg.cra_blocksize = 1;
  229. inst->alg.cra_alignmask = __alignof__(u32) - 1;
  230. inst->alg.cra_type = &crypto_blkcipher_type;
  231. inst->alg.cra_blkcipher.ivsize = ivsize;
  232. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize
  233. + noncesize;
  234. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize
  235. + noncesize;
  236. inst->alg.cra_ctxsize = sizeof(struct crypto_ctr_ctx);
  237. inst->alg.cra_init = crypto_ctr_init_tfm;
  238. inst->alg.cra_exit = crypto_ctr_exit_tfm;
  239. inst->alg.cra_blkcipher.setkey = crypto_ctr_setkey;
  240. inst->alg.cra_blkcipher.encrypt = crypto_ctr_crypt;
  241. inst->alg.cra_blkcipher.decrypt = crypto_ctr_crypt;
  242. err_free_inst:
  243. if (err)
  244. kfree(inst);
  245. out_put_alg:
  246. crypto_mod_put(alg);
  247. if (err)
  248. inst = ERR_PTR(err);
  249. return inst;
  250. }
  251. static void crypto_ctr_free(struct crypto_instance *inst)
  252. {
  253. struct ctr_instance_ctx *ictx = crypto_instance_ctx(inst);
  254. crypto_drop_spawn(&ictx->alg);
  255. kfree(inst);
  256. }
  257. static struct crypto_template crypto_ctr_tmpl = {
  258. .name = "ctr",
  259. .alloc = crypto_ctr_alloc,
  260. .free = crypto_ctr_free,
  261. .module = THIS_MODULE,
  262. };
  263. static int __init crypto_ctr_module_init(void)
  264. {
  265. return crypto_register_template(&crypto_ctr_tmpl);
  266. }
  267. static void __exit crypto_ctr_module_exit(void)
  268. {
  269. crypto_unregister_template(&crypto_ctr_tmpl);
  270. }
  271. module_init(crypto_ctr_module_init);
  272. module_exit(crypto_ctr_module_exit);
  273. MODULE_LICENSE("GPL");
  274. MODULE_DESCRIPTION("CTR Counter block mode");