digest.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Digest operations.
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <linux/crypto.h>
  15. #include <linux/mm.h>
  16. #include <linux/errno.h>
  17. #include <linux/highmem.h>
  18. #include <asm/scatterlist.h>
  19. #include "internal.h"
  20. static void init(struct crypto_tfm *tfm)
  21. {
  22. tfm->__crt_alg->cra_digest.dia_init(tfm);
  23. }
  24. static void update(struct crypto_tfm *tfm,
  25. struct scatterlist *sg, unsigned int nsg)
  26. {
  27. unsigned int i;
  28. unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
  29. for (i = 0; i < nsg; i++) {
  30. struct page *pg = sg[i].page;
  31. unsigned int offset = sg[i].offset;
  32. unsigned int l = sg[i].length;
  33. do {
  34. unsigned int bytes_from_page = min(l, ((unsigned int)
  35. (PAGE_SIZE)) -
  36. offset);
  37. char *src = crypto_kmap(pg, 0);
  38. char *p = src + offset;
  39. if (unlikely(offset & alignmask)) {
  40. unsigned int bytes =
  41. alignmask + 1 - (offset & alignmask);
  42. bytes = min(bytes, bytes_from_page);
  43. tfm->__crt_alg->cra_digest.dia_update(tfm, p,
  44. bytes);
  45. p += bytes;
  46. bytes_from_page -= bytes;
  47. l -= bytes;
  48. }
  49. tfm->__crt_alg->cra_digest.dia_update(tfm, p,
  50. bytes_from_page);
  51. crypto_kunmap(src, 0);
  52. crypto_yield(tfm);
  53. offset = 0;
  54. pg++;
  55. l -= bytes_from_page;
  56. } while (l > 0);
  57. }
  58. }
  59. static void final(struct crypto_tfm *tfm, u8 *out)
  60. {
  61. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  62. if (unlikely((unsigned long)out & alignmask)) {
  63. unsigned int size = crypto_tfm_alg_digestsize(tfm);
  64. u8 buffer[size + alignmask];
  65. u8 *dst = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  66. tfm->__crt_alg->cra_digest.dia_final(tfm, dst);
  67. memcpy(out, dst, size);
  68. } else
  69. tfm->__crt_alg->cra_digest.dia_final(tfm, out);
  70. }
  71. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  72. {
  73. u32 flags;
  74. if (tfm->__crt_alg->cra_digest.dia_setkey == NULL)
  75. return -ENOSYS;
  76. return tfm->__crt_alg->cra_digest.dia_setkey(tfm, key, keylen, &flags);
  77. }
  78. static void digest(struct crypto_tfm *tfm,
  79. struct scatterlist *sg, unsigned int nsg, u8 *out)
  80. {
  81. init(tfm);
  82. update(tfm, sg, nsg);
  83. final(tfm, out);
  84. }
  85. int crypto_init_digest_flags(struct crypto_tfm *tfm, u32 flags)
  86. {
  87. return flags ? -EINVAL : 0;
  88. }
  89. int crypto_init_digest_ops(struct crypto_tfm *tfm)
  90. {
  91. struct digest_tfm *ops = &tfm->crt_digest;
  92. ops->dit_init = init;
  93. ops->dit_update = update;
  94. ops->dit_final = final;
  95. ops->dit_digest = digest;
  96. ops->dit_setkey = setkey;
  97. return crypto_alloc_hmac_block(tfm);
  98. }
  99. void crypto_exit_digest_ops(struct crypto_tfm *tfm)
  100. {
  101. crypto_free_hmac_block(tfm);
  102. }