xcbc.c 7.5 KB

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