hsh.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * \brief Cryptographic Hashing
  19. * \defgroup hsh Hash
  20. * @{
  21. */
  22. #pragma once
  23. #include <jose/cfg.h>
  24. #include <jose/io.h>
  25. #include <jansson.h>
  26. #include <stdint.h>
  27. /**
  28. * Hashes data with the specified algorithm.
  29. *
  30. * This function hashes the first \p dlen bytes of \p data using the \p alg
  31. * specified and returns the output as a URL-safe Base64 encoded JSON string.
  32. *
  33. * \param cfg The configuration context (optional).
  34. * \param alg The hashing algorithm.
  35. * \param data The input data buffer.
  36. * \param dlen The length of the data in the input buffer.
  37. * \return The hash as a URL-safe Base64 encoded JSON string.
  38. */
  39. json_t *
  40. hsh(jose_cfg_t *cfg, const char *alg, const void *data, size_t dlen);
  41. /**
  42. * Hashes data with the specified algorithm using IO chaining.
  43. *
  44. * This function creates an IO chain filter that takes the data to be hashed
  45. * as input and outputs a hash of the input data.
  46. *
  47. * \param cfg The configuration context (optional).
  48. * \param alg The hashing algorithm.
  49. * \param next The size of the output hash buffer.
  50. * \return The number of bytes written to the hash buffer or SIZE_MAX on error.
  51. */
  52. jose_io_t *
  53. hsh_io(jose_cfg_t *cfg, const char *alg, jose_io_t *next);
  54. /**
  55. * Hashes data with the specified algorithm into a buffer.
  56. *
  57. * This function hashes the first \p dlen bytes of \p data using the \p alg
  58. * specified and stores the output in \p hash (a buffer of size \p hlen).
  59. *
  60. * If \p hash is NULL, the required size of the output buffer is returned.
  61. *
  62. * \param cfg The configuration context (optional).
  63. * \param alg The hashing algorithm.
  64. * \param data The input data buffer.
  65. * \param dlen The length of the data in the input buffer.
  66. * \param hash The output hash buffer.
  67. * \param hlen The size of the output hash buffer.
  68. * \return The number of bytes written to the hash buffer or SIZE_MAX on error.
  69. */
  70. size_t
  71. hsh_buf(jose_cfg_t *cfg, const char *alg,
  72. const void *data, size_t dlen, void *hash, size_t hlen);
  73. /** @} */