hash.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Hash: Hash algorithms under the crypto API
  3. *
  4. * Copyright (c) 2008 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. */
  12. #ifndef _CRYPTO_HASH_H
  13. #define _CRYPTO_HASH_H
  14. #include <linux/crypto.h>
  15. struct crypto_ahash {
  16. struct crypto_tfm base;
  17. };
  18. static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
  19. {
  20. return (struct crypto_ahash *)tfm;
  21. }
  22. static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
  23. u32 type, u32 mask)
  24. {
  25. type &= ~CRYPTO_ALG_TYPE_MASK;
  26. mask &= ~CRYPTO_ALG_TYPE_MASK;
  27. type |= CRYPTO_ALG_TYPE_AHASH;
  28. mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
  29. return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
  30. }
  31. static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
  32. {
  33. return &tfm->base;
  34. }
  35. static inline void crypto_free_ahash(struct crypto_ahash *tfm)
  36. {
  37. crypto_free_tfm(crypto_ahash_tfm(tfm));
  38. }
  39. static inline unsigned int crypto_ahash_alignmask(
  40. struct crypto_ahash *tfm)
  41. {
  42. return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
  43. }
  44. static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
  45. {
  46. return &crypto_ahash_tfm(tfm)->crt_ahash;
  47. }
  48. static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
  49. {
  50. return crypto_ahash_crt(tfm)->digestsize;
  51. }
  52. static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
  53. {
  54. return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
  55. }
  56. static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
  57. {
  58. crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
  59. }
  60. static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
  61. {
  62. crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
  63. }
  64. static inline struct crypto_ahash *crypto_ahash_reqtfm(
  65. struct ahash_request *req)
  66. {
  67. return __crypto_ahash_cast(req->base.tfm);
  68. }
  69. static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
  70. {
  71. return crypto_ahash_crt(tfm)->reqsize;
  72. }
  73. static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
  74. const u8 *key, unsigned int keylen)
  75. {
  76. struct ahash_tfm *crt = crypto_ahash_crt(tfm);
  77. return crt->setkey(tfm, key, keylen);
  78. }
  79. static inline int crypto_ahash_digest(struct ahash_request *req)
  80. {
  81. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  82. return crt->digest(req);
  83. }
  84. static inline int crypto_ahash_init(struct ahash_request *req)
  85. {
  86. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  87. return crt->init(req);
  88. }
  89. static inline int crypto_ahash_update(struct ahash_request *req)
  90. {
  91. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  92. return crt->update(req);
  93. }
  94. static inline int crypto_ahash_final(struct ahash_request *req)
  95. {
  96. struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
  97. return crt->final(req);
  98. }
  99. static inline void ahash_request_set_tfm(struct ahash_request *req,
  100. struct crypto_ahash *tfm)
  101. {
  102. req->base.tfm = crypto_ahash_tfm(tfm);
  103. }
  104. static inline struct ahash_request *ahash_request_alloc(
  105. struct crypto_ahash *tfm, gfp_t gfp)
  106. {
  107. struct ahash_request *req;
  108. req = kmalloc(sizeof(struct ahash_request) +
  109. crypto_ahash_reqsize(tfm), gfp);
  110. if (likely(req))
  111. ahash_request_set_tfm(req, tfm);
  112. return req;
  113. }
  114. static inline void ahash_request_free(struct ahash_request *req)
  115. {
  116. kfree(req);
  117. }
  118. static inline struct ahash_request *ahash_request_cast(
  119. struct crypto_async_request *req)
  120. {
  121. return container_of(req, struct ahash_request, base);
  122. }
  123. static inline void ahash_request_set_callback(struct ahash_request *req,
  124. u32 flags,
  125. crypto_completion_t complete,
  126. void *data)
  127. {
  128. req->base.complete = complete;
  129. req->base.data = data;
  130. req->base.flags = flags;
  131. }
  132. static inline void ahash_request_set_crypt(struct ahash_request *req,
  133. struct scatterlist *src, u8 *result,
  134. unsigned int nbytes)
  135. {
  136. req->src = src;
  137. req->nbytes = nbytes;
  138. req->result = result;
  139. }
  140. #endif /* _CRYPTO_HASH_H */