xcbc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 <linux/crypto.h>
  22. #include <linux/err.h>
  23. #include <linux/hardirq.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/rtnetlink.h>
  27. #include <linux/slab.h>
  28. #include <linux/scatterlist.h>
  29. #include "internal.h"
  30. static u_int32_t ks[12] = {0x01010101, 0x01010101, 0x01010101, 0x01010101,
  31. 0x02020202, 0x02020202, 0x02020202, 0x02020202,
  32. 0x03030303, 0x03030303, 0x03030303, 0x03030303};
  33. /*
  34. * +------------------------
  35. * | <parent tfm>
  36. * +------------------------
  37. * | crypto_xcbc_ctx
  38. * +------------------------
  39. * | odds (block size)
  40. * +------------------------
  41. * | prev (block size)
  42. * +------------------------
  43. * | key (block size)
  44. * +------------------------
  45. * | consts (block size * 3)
  46. * +------------------------
  47. */
  48. struct crypto_xcbc_ctx {
  49. struct crypto_cipher *child;
  50. u8 *odds;
  51. u8 *prev;
  52. u8 *key;
  53. u8 *consts;
  54. void (*xor)(u8 *a, const u8 *b, unsigned int bs);
  55. unsigned int keylen;
  56. unsigned int len;
  57. };
  58. static void xor_128(u8 *a, const u8 *b, unsigned int bs)
  59. {
  60. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  61. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  62. ((u32 *)a)[2] ^= ((u32 *)b)[2];
  63. ((u32 *)a)[3] ^= ((u32 *)b)[3];
  64. }
  65. static int _crypto_xcbc_digest_setkey(struct crypto_hash *parent,
  66. struct crypto_xcbc_ctx *ctx)
  67. {
  68. int bs = crypto_hash_blocksize(parent);
  69. int err = 0;
  70. u8 key1[bs];
  71. if ((err = crypto_cipher_setkey(ctx->child, ctx->key, ctx->keylen)))
  72. return err;
  73. crypto_cipher_encrypt_one(ctx->child, key1, ctx->consts);
  74. return crypto_cipher_setkey(ctx->child, key1, bs);
  75. }
  76. static int crypto_xcbc_digest_setkey(struct crypto_hash *parent,
  77. const u8 *inkey, unsigned int keylen)
  78. {
  79. struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent);
  80. if (keylen != crypto_cipher_blocksize(ctx->child))
  81. return -EINVAL;
  82. ctx->keylen = keylen;
  83. memcpy(ctx->key, inkey, keylen);
  84. ctx->consts = (u8*)ks;
  85. return _crypto_xcbc_digest_setkey(parent, ctx);
  86. }
  87. static int crypto_xcbc_digest_init(struct hash_desc *pdesc)
  88. {
  89. struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(pdesc->tfm);
  90. int bs = crypto_hash_blocksize(pdesc->tfm);
  91. ctx->len = 0;
  92. memset(ctx->odds, 0, bs);
  93. memset(ctx->prev, 0, bs);
  94. return 0;
  95. }
  96. static int crypto_xcbc_digest_update2(struct hash_desc *pdesc,
  97. struct scatterlist *sg,
  98. unsigned int nbytes)
  99. {
  100. struct crypto_hash *parent = pdesc->tfm;
  101. struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent);
  102. struct crypto_cipher *tfm = ctx->child;
  103. int bs = crypto_hash_blocksize(parent);
  104. unsigned int i = 0;
  105. do {
  106. struct page *pg = sg[i].page;
  107. unsigned int offset = sg[i].offset;
  108. unsigned int slen = sg[i].length;
  109. while (slen > 0) {
  110. unsigned int len = min(slen, ((unsigned int)(PAGE_SIZE)) - offset);
  111. char *p = crypto_kmap(pg, 0) + offset;
  112. /* checking the data can fill the block */
  113. if ((ctx->len + len) <= bs) {
  114. memcpy(ctx->odds + ctx->len, p, len);
  115. ctx->len += len;
  116. slen -= len;
  117. /* checking the rest of the page */
  118. if (len + offset >= PAGE_SIZE) {
  119. offset = 0;
  120. pg++;
  121. } else
  122. offset += len;
  123. crypto_kunmap(p, 0);
  124. crypto_yield(pdesc->flags);
  125. continue;
  126. }
  127. /* filling odds with new data and encrypting it */
  128. memcpy(ctx->odds + ctx->len, p, bs - ctx->len);
  129. len -= bs - ctx->len;
  130. p += bs - ctx->len;
  131. ctx->xor(ctx->prev, ctx->odds, bs);
  132. crypto_cipher_encrypt_one(tfm, ctx->prev, ctx->prev);
  133. /* clearing the length */
  134. ctx->len = 0;
  135. /* encrypting the rest of data */
  136. while (len > bs) {
  137. ctx->xor(ctx->prev, p, bs);
  138. crypto_cipher_encrypt_one(tfm, ctx->prev,
  139. ctx->prev);
  140. p += bs;
  141. len -= bs;
  142. }
  143. /* keeping the surplus of blocksize */
  144. if (len) {
  145. memcpy(ctx->odds, p, len);
  146. ctx->len = len;
  147. }
  148. crypto_kunmap(p, 0);
  149. crypto_yield(pdesc->flags);
  150. slen -= min(slen, ((unsigned int)(PAGE_SIZE)) - offset);
  151. offset = 0;
  152. pg++;
  153. }
  154. nbytes-=sg[i].length;
  155. i++;
  156. } while (nbytes>0);
  157. return 0;
  158. }
  159. static int crypto_xcbc_digest_update(struct hash_desc *pdesc,
  160. struct scatterlist *sg,
  161. unsigned int nbytes)
  162. {
  163. if (WARN_ON_ONCE(in_irq()))
  164. return -EDEADLK;
  165. return crypto_xcbc_digest_update2(pdesc, sg, nbytes);
  166. }
  167. static int crypto_xcbc_digest_final(struct hash_desc *pdesc, u8 *out)
  168. {
  169. struct crypto_hash *parent = pdesc->tfm;
  170. struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(parent);
  171. struct crypto_cipher *tfm = ctx->child;
  172. int bs = crypto_hash_blocksize(parent);
  173. int err = 0;
  174. if (ctx->len == bs) {
  175. u8 key2[bs];
  176. if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
  177. return err;
  178. crypto_cipher_encrypt_one(tfm, key2,
  179. (u8 *)(ctx->consts + bs));
  180. ctx->xor(ctx->prev, ctx->odds, bs);
  181. ctx->xor(ctx->prev, key2, bs);
  182. _crypto_xcbc_digest_setkey(parent, ctx);
  183. crypto_cipher_encrypt_one(tfm, out, ctx->prev);
  184. } else {
  185. u8 key3[bs];
  186. unsigned int rlen;
  187. u8 *p = ctx->odds + ctx->len;
  188. *p = 0x80;
  189. p++;
  190. rlen = bs - ctx->len -1;
  191. if (rlen)
  192. memset(p, 0, rlen);
  193. if ((err = crypto_cipher_setkey(tfm, ctx->key, ctx->keylen)) != 0)
  194. return err;
  195. crypto_cipher_encrypt_one(tfm, key3,
  196. (u8 *)(ctx->consts + bs * 2));
  197. ctx->xor(ctx->prev, ctx->odds, bs);
  198. ctx->xor(ctx->prev, key3, bs);
  199. _crypto_xcbc_digest_setkey(parent, ctx);
  200. crypto_cipher_encrypt_one(tfm, out, ctx->prev);
  201. }
  202. return 0;
  203. }
  204. static int crypto_xcbc_digest(struct hash_desc *pdesc,
  205. struct scatterlist *sg, unsigned int nbytes, u8 *out)
  206. {
  207. if (WARN_ON_ONCE(in_irq()))
  208. return -EDEADLK;
  209. crypto_xcbc_digest_init(pdesc);
  210. crypto_xcbc_digest_update2(pdesc, sg, nbytes);
  211. return crypto_xcbc_digest_final(pdesc, out);
  212. }
  213. static int xcbc_init_tfm(struct crypto_tfm *tfm)
  214. {
  215. struct crypto_cipher *cipher;
  216. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  217. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  218. struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm));
  219. int bs = crypto_hash_blocksize(__crypto_hash_cast(tfm));
  220. cipher = crypto_spawn_cipher(spawn);
  221. if (IS_ERR(cipher))
  222. return PTR_ERR(cipher);
  223. switch(bs) {
  224. case 16:
  225. ctx->xor = xor_128;
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. ctx->child = cipher;
  231. ctx->odds = (u8*)(ctx+1);
  232. ctx->prev = ctx->odds + bs;
  233. ctx->key = ctx->prev + bs;
  234. return 0;
  235. };
  236. static void xcbc_exit_tfm(struct crypto_tfm *tfm)
  237. {
  238. struct crypto_xcbc_ctx *ctx = crypto_hash_ctx_aligned(__crypto_hash_cast(tfm));
  239. crypto_free_cipher(ctx->child);
  240. }
  241. static struct crypto_instance *xcbc_alloc(void *param, unsigned int len)
  242. {
  243. struct crypto_instance *inst;
  244. struct crypto_alg *alg;
  245. alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
  246. CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
  247. if (IS_ERR(alg))
  248. return ERR_PTR(PTR_ERR(alg));
  249. switch(alg->cra_blocksize) {
  250. case 16:
  251. break;
  252. default:
  253. return ERR_PTR(PTR_ERR(alg));
  254. }
  255. inst = crypto_alloc_instance("xcbc", alg);
  256. if (IS_ERR(inst))
  257. goto out_put_alg;
  258. inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH;
  259. inst->alg.cra_priority = alg->cra_priority;
  260. inst->alg.cra_blocksize = alg->cra_blocksize;
  261. inst->alg.cra_alignmask = alg->cra_alignmask;
  262. inst->alg.cra_type = &crypto_hash_type;
  263. inst->alg.cra_hash.digestsize =
  264. (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  265. CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
  266. alg->cra_blocksize;
  267. inst->alg.cra_ctxsize = sizeof(struct crypto_xcbc_ctx) +
  268. ALIGN(inst->alg.cra_blocksize * 3, sizeof(void *));
  269. inst->alg.cra_init = xcbc_init_tfm;
  270. inst->alg.cra_exit = xcbc_exit_tfm;
  271. inst->alg.cra_hash.init = crypto_xcbc_digest_init;
  272. inst->alg.cra_hash.update = crypto_xcbc_digest_update;
  273. inst->alg.cra_hash.final = crypto_xcbc_digest_final;
  274. inst->alg.cra_hash.digest = crypto_xcbc_digest;
  275. inst->alg.cra_hash.setkey = crypto_xcbc_digest_setkey;
  276. out_put_alg:
  277. crypto_mod_put(alg);
  278. return inst;
  279. }
  280. static void xcbc_free(struct crypto_instance *inst)
  281. {
  282. crypto_drop_spawn(crypto_instance_ctx(inst));
  283. kfree(inst);
  284. }
  285. static struct crypto_template crypto_xcbc_tmpl = {
  286. .name = "xcbc",
  287. .alloc = xcbc_alloc,
  288. .free = xcbc_free,
  289. .module = THIS_MODULE,
  290. };
  291. static int __init crypto_xcbc_module_init(void)
  292. {
  293. return crypto_register_template(&crypto_xcbc_tmpl);
  294. }
  295. static void __exit crypto_xcbc_module_exit(void)
  296. {
  297. crypto_unregister_template(&crypto_xcbc_tmpl);
  298. }
  299. module_init(crypto_xcbc_module_init);
  300. module_exit(crypto_xcbc_module_exit);
  301. MODULE_LICENSE("GPL");
  302. MODULE_DESCRIPTION("XCBC keyed hash algorithm");