ahash.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Asynchronous Cryptographic Hash operations.
  3. *
  4. * This is the asynchronous version of hash.c with notification of
  5. * completion via a callback.
  6. *
  7. * Copyright (c) 2008 Loc Ho <lho@amcc.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/algapi.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/seq_file.h>
  22. #include "internal.h"
  23. static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
  24. unsigned int keylen)
  25. {
  26. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  27. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  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 = ahash->setkey(tfm, alignbuffer, keylen);
  38. memset(alignbuffer, 0, keylen);
  39. kfree(buffer);
  40. return ret;
  41. }
  42. static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  43. unsigned int keylen)
  44. {
  45. struct ahash_alg *ahash = crypto_ahash_alg(tfm);
  46. unsigned long alignmask = crypto_ahash_alignmask(tfm);
  47. if ((unsigned long)key & alignmask)
  48. return ahash_setkey_unaligned(tfm, key, keylen);
  49. return ahash->setkey(tfm, key, keylen);
  50. }
  51. static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
  52. u32 mask)
  53. {
  54. return alg->cra_ctxsize;
  55. }
  56. static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  57. {
  58. struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
  59. struct ahash_tfm *crt = &tfm->crt_ahash;
  60. if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
  61. return -EINVAL;
  62. crt->init = alg->init;
  63. crt->update = alg->update;
  64. crt->final = alg->final;
  65. crt->digest = alg->digest;
  66. crt->setkey = ahash_setkey;
  67. crt->base = __crypto_ahash_cast(tfm);
  68. crt->digestsize = alg->digestsize;
  69. return 0;
  70. }
  71. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  72. __attribute__ ((unused));
  73. static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
  74. {
  75. seq_printf(m, "type : ahash\n");
  76. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  77. "yes" : "no");
  78. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  79. seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
  80. }
  81. const struct crypto_type crypto_ahash_type = {
  82. .ctxsize = crypto_ahash_ctxsize,
  83. .init = crypto_init_ahash_ops,
  84. #ifdef CONFIG_PROC_FS
  85. .show = crypto_ahash_show,
  86. #endif
  87. };
  88. EXPORT_SYMBOL_GPL(crypto_ahash_type);
  89. MODULE_LICENSE("GPL");
  90. MODULE_DESCRIPTION("Asynchronous cryptographic hash type");