hash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Cryptographic Hash operations.
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/seq_file.h>
  16. #include "internal.h"
  17. static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type,
  18. u32 mask)
  19. {
  20. return alg->cra_ctxsize;
  21. }
  22. static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key,
  23. unsigned int keylen)
  24. {
  25. struct crypto_tfm *tfm = crypto_hash_tfm(crt);
  26. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  27. unsigned long alignmask = crypto_hash_alignmask(crt);
  28. int ret;
  29. u8 *buffer, *alignbuffer;
  30. unsigned long absize;
  31. absize = keylen + alignmask;
  32. buffer = kmalloc(absize, GFP_ATOMIC);
  33. if (!buffer)
  34. return -ENOMEM;
  35. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  36. memcpy(alignbuffer, key, keylen);
  37. ret = alg->setkey(crt, alignbuffer, keylen);
  38. memset(alignbuffer, 0, keylen);
  39. kfree(buffer);
  40. return ret;
  41. }
  42. static int hash_setkey(struct crypto_hash *crt, const u8 *key,
  43. unsigned int keylen)
  44. {
  45. struct crypto_tfm *tfm = crypto_hash_tfm(crt);
  46. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  47. unsigned long alignmask = crypto_hash_alignmask(crt);
  48. if ((unsigned long)key & alignmask)
  49. return hash_setkey_unaligned(crt, key, keylen);
  50. return alg->setkey(crt, key, keylen);
  51. }
  52. static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  53. {
  54. struct hash_tfm *crt = &tfm->crt_hash;
  55. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  56. if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
  57. return -EINVAL;
  58. crt->init = alg->init;
  59. crt->update = alg->update;
  60. crt->final = alg->final;
  61. crt->digest = alg->digest;
  62. crt->setkey = hash_setkey;
  63. crt->digestsize = alg->digestsize;
  64. return 0;
  65. }
  66. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  67. __attribute__ ((unused));
  68. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  69. {
  70. seq_printf(m, "type : hash\n");
  71. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  72. seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
  73. }
  74. const struct crypto_type crypto_hash_type = {
  75. .ctxsize = crypto_hash_ctxsize,
  76. .init = crypto_init_hash_ops,
  77. #ifdef CONFIG_PROC_FS
  78. .show = crypto_hash_show,
  79. #endif
  80. };
  81. EXPORT_SYMBOL_GPL(crypto_hash_type);
  82. MODULE_LICENSE("GPL");
  83. MODULE_DESCRIPTION("Generic cryptographic hash type");