hash.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/seq_file.h>
  15. #include "internal.h"
  16. static unsigned int crypto_hash_ctxsize(struct crypto_alg *alg, u32 type,
  17. u32 mask)
  18. {
  19. return alg->cra_ctxsize;
  20. }
  21. static int hash_setkey_unaligned(struct crypto_hash *crt, const u8 *key,
  22. unsigned int keylen)
  23. {
  24. struct crypto_tfm *tfm = crypto_hash_tfm(crt);
  25. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  26. unsigned long alignmask = crypto_hash_alignmask(crt);
  27. int ret;
  28. u8 *buffer, *alignbuffer;
  29. unsigned long absize;
  30. absize = keylen + alignmask;
  31. buffer = kmalloc(absize, GFP_ATOMIC);
  32. if (!buffer)
  33. return -ENOMEM;
  34. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  35. memcpy(alignbuffer, key, keylen);
  36. ret = alg->setkey(crt, alignbuffer, keylen);
  37. memset(alignbuffer, 0, absize);
  38. kfree(buffer);
  39. return ret;
  40. }
  41. static int hash_setkey(struct crypto_hash *crt, const u8 *key,
  42. unsigned int keylen)
  43. {
  44. struct crypto_tfm *tfm = crypto_hash_tfm(crt);
  45. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  46. unsigned long alignmask = crypto_hash_alignmask(crt);
  47. if ((unsigned long)key & alignmask)
  48. return hash_setkey_unaligned(crt, key, keylen);
  49. return alg->setkey(crt, key, keylen);
  50. }
  51. static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  52. {
  53. struct hash_tfm *crt = &tfm->crt_hash;
  54. struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
  55. if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
  56. return -EINVAL;
  57. crt->init = alg->init;
  58. crt->update = alg->update;
  59. crt->final = alg->final;
  60. crt->digest = alg->digest;
  61. crt->setkey = hash_setkey;
  62. crt->digestsize = alg->digestsize;
  63. return 0;
  64. }
  65. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  66. __attribute__ ((unused));
  67. static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
  68. {
  69. seq_printf(m, "type : hash\n");
  70. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  71. seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
  72. }
  73. const struct crypto_type crypto_hash_type = {
  74. .ctxsize = crypto_hash_ctxsize,
  75. .init = crypto_init_hash_ops,
  76. #ifdef CONFIG_PROC_FS
  77. .show = crypto_hash_show,
  78. #endif
  79. };
  80. EXPORT_SYMBOL_GPL(crypto_hash_type);
  81. MODULE_LICENSE("GPL");
  82. MODULE_DESCRIPTION("Generic cryptographic hash type");