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