xcbc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C)2006 USAGI/WIDE Project
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author:
  19. * Kazunori Miyazawa <miyazawa@linux-ipv6.org>
  20. */
  21. #include <crypto/internal/hash.h>
  22. #include <linux/err.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
  26. 0x02020202, 0x02020202, 0x02020202, 0x02020202,
  27. 0x03030303, 0x03030303, 0x03030303, 0x03030303};
  28. /*
  29. * +------------------------
  30. * | <parent tfm>
  31. * +------------------------
  32. * | xcbc_tfm_ctx
  33. * +------------------------
  34. * | consts (block size * 2)
  35. * +------------------------
  36. */
  37. struct xcbc_tfm_ctx {
  38. struct crypto_cipher *child;
  39. u8 ctx[];
  40. };
  41. /*
  42. * +------------------------
  43. * | <shash desc>
  44. * +------------------------
  45. * | xcbc_desc_ctx
  46. * +------------------------
  47. * | odds (block size)
  48. * +------------------------
  49. * | prev (block size)
  50. * +------------------------
  51. */
  52. struct xcbc_desc_ctx {
  53. unsigned int len;
  54. u8 ctx[];
  55. };
  56. static int crypto_xcbc_digest_setkey(struct crypto_shash *parent,
  57. const u8 *inkey, unsigned int keylen)
  58. {
  59. unsigned long alignmask = crypto_shash_alignmask(parent);
  60. struct xcbc_tfm_ctx *ctx = crypto_shash_ctx(parent);
  61. int bs = crypto_shash_blocksize(parent);
  62. u8 *consts = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
  63. int err = 0;
  64. u8 key1[bs];
  65. if ((err = crypto_cipher_setkey(ctx->child, inkey, keylen)))
  66. return err;
  67. crypto_cipher_encrypt_one(ctx->child, consts, (u8 *)ks + bs);
  68. crypto_cipher_encrypt_one(ctx->child, consts + bs, (u8 *)ks + bs * 2);
  69. crypto_cipher_encrypt_one(ctx->child, key1, (u8 *)ks);
  70. return crypto_cipher_setkey(ctx->child, key1, bs);
  71. }
  72. static int crypto_xcbc_digest_init(struct shash_desc *pdesc)
  73. {
  74. unsigned long alignmask = crypto_shash_alignmask(pdesc->tfm);
  75. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  76. int bs = crypto_shash_blocksize(pdesc->tfm);
  77. u8 *prev = PTR_ALIGN(&ctx->ctx[0], alignmask + 1) + bs;
  78. ctx->len = 0;
  79. memset(prev, 0, bs);
  80. return 0;
  81. }
  82. static int crypto_xcbc_digest_update(struct shash_desc *pdesc, const u8 *p,
  83. unsigned int len)
  84. {
  85. struct crypto_shash *parent = pdesc->tfm;
  86. unsigned long alignmask = crypto_shash_alignmask(parent);
  87. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  88. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  89. struct crypto_cipher *tfm = tctx->child;
  90. int bs = crypto_shash_blocksize(parent);
  91. u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
  92. u8 *prev = odds + bs;
  93. /* checking the data can fill the block */
  94. if ((ctx->len + len) <= bs) {
  95. memcpy(odds + ctx->len, p, len);
  96. ctx->len += len;
  97. return 0;
  98. }
  99. /* filling odds with new data and encrypting it */
  100. memcpy(odds + ctx->len, p, bs - ctx->len);
  101. len -= bs - ctx->len;
  102. p += bs - ctx->len;
  103. crypto_xor(prev, odds, bs);
  104. crypto_cipher_encrypt_one(tfm, prev, prev);
  105. /* clearing the length */
  106. ctx->len = 0;
  107. /* encrypting the rest of data */
  108. while (len > bs) {
  109. crypto_xor(prev, p, bs);
  110. crypto_cipher_encrypt_one(tfm, prev, prev);
  111. p += bs;
  112. len -= bs;
  113. }
  114. /* keeping the surplus of blocksize */
  115. if (len) {
  116. memcpy(odds, p, len);
  117. ctx->len = len;
  118. }
  119. return 0;
  120. }
  121. static int crypto_xcbc_digest_final(struct shash_desc *pdesc, u8 *out)
  122. {
  123. struct crypto_shash *parent = pdesc->tfm;
  124. unsigned long alignmask = crypto_shash_alignmask(parent);
  125. struct xcbc_tfm_ctx *tctx = crypto_shash_ctx(parent);
  126. struct xcbc_desc_ctx *ctx = shash_desc_ctx(pdesc);
  127. struct crypto_cipher *tfm = tctx->child;
  128. int bs = crypto_shash_blocksize(parent);
  129. u8 *consts = PTR_ALIGN(&tctx->ctx[0], alignmask + 1);
  130. u8 *odds = PTR_ALIGN(&ctx->ctx[0], alignmask + 1);
  131. u8 *prev = odds + bs;
  132. unsigned int offset = 0;
  133. if (ctx->len != bs) {
  134. unsigned int rlen;
  135. u8 *p = odds + ctx->len;
  136. *p = 0x80;
  137. p++;
  138. rlen = bs - ctx->len -1;
  139. if (rlen)
  140. memset(p, 0, rlen);
  141. offset += bs;
  142. }
  143. crypto_xor(prev, odds, bs);
  144. crypto_xor(prev, consts + offset, bs);
  145. crypto_cipher_encrypt_one(tfm, out, prev);
  146. return 0;
  147. }
  148. static int xcbc_init_tfm(struct crypto_tfm *tfm)
  149. {
  150. struct crypto_cipher *cipher;
  151. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  152. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  153. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  154. cipher = crypto_spawn_cipher(spawn);
  155. if (IS_ERR(cipher))
  156. return PTR_ERR(cipher);
  157. ctx->child = cipher;
  158. return 0;
  159. };
  160. static void xcbc_exit_tfm(struct crypto_tfm *tfm)
  161. {
  162. struct xcbc_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
  163. crypto_free_cipher(ctx->child);
  164. }
  165. static int xcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  166. {
  167. struct shash_instance *inst;
  168. struct crypto_alg *alg;
  169. unsigned long alignmask;
  170. int err;
  171. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  172. if (err)
  173. return err;
  174. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  175. CRYPTO_ALG_TYPE_MASK);
  176. if (IS_ERR(alg))
  177. return PTR_ERR(alg);
  178. switch(alg->cra_blocksize) {
  179. case 16:
  180. break;
  181. default:
  182. goto out_put_alg;
  183. }
  184. inst = shash_alloc_instance("xcbc", alg);
  185. err = PTR_ERR(inst);
  186. if (IS_ERR(inst))
  187. goto out_put_alg;
  188. err = crypto_init_spawn(shash_instance_ctx(inst), alg,
  189. shash_crypto_instance(inst),
  190. CRYPTO_ALG_TYPE_MASK);
  191. if (err)
  192. goto out_free_inst;
  193. alignmask = alg->cra_alignmask | 3;
  194. inst->alg.base.cra_alignmask = alignmask;
  195. inst->alg.base.cra_priority = alg->cra_priority;
  196. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  197. inst->alg.digestsize = alg->cra_blocksize;
  198. inst->alg.descsize = ALIGN(sizeof(struct xcbc_desc_ctx),
  199. crypto_tfm_ctx_alignment()) +
  200. (alignmask &
  201. ~(crypto_tfm_ctx_alignment() - 1)) +
  202. alg->cra_blocksize * 2;
  203. inst->alg.base.cra_ctxsize = ALIGN(sizeof(struct xcbc_tfm_ctx),
  204. alignmask + 1) +
  205. alg->cra_blocksize * 2;
  206. inst->alg.base.cra_init = xcbc_init_tfm;
  207. inst->alg.base.cra_exit = xcbc_exit_tfm;
  208. inst->alg.init = crypto_xcbc_digest_init;
  209. inst->alg.update = crypto_xcbc_digest_update;
  210. inst->alg.final = crypto_xcbc_digest_final;
  211. inst->alg.setkey = crypto_xcbc_digest_setkey;
  212. err = shash_register_instance(tmpl, inst);
  213. if (err) {
  214. out_free_inst:
  215. shash_free_instance(shash_crypto_instance(inst));
  216. }
  217. out_put_alg:
  218. crypto_mod_put(alg);
  219. return err;
  220. }
  221. static struct crypto_template crypto_xcbc_tmpl = {
  222. .name = "xcbc",
  223. .create = xcbc_create,
  224. .free = shash_free_instance,
  225. .module = THIS_MODULE,
  226. };
  227. static int __init crypto_xcbc_module_init(void)
  228. {
  229. return crypto_register_template(&crypto_xcbc_tmpl);
  230. }
  231. static void __exit crypto_xcbc_module_exit(void)
  232. {
  233. crypto_unregister_template(&crypto_xcbc_tmpl);
  234. }
  235. module_init(crypto_xcbc_module_init);
  236. module_exit(crypto_xcbc_module_exit);
  237. MODULE_LICENSE("GPL");
  238. MODULE_DESCRIPTION("XCBC keyed hash algorithm");
  239. MODULE_ALIAS_CRYPTO("xcbc");