xcbc.c 7.3 KB

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