hash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * QEMU Crypto hash algorithms
  3. *
  4. * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
  5. * Copyright (c) 2015 Red Hat, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #ifndef QCRYPTO_HASH_H
  22. #define QCRYPTO_HASH_H
  23. #include "qapi/qapi-types-crypto.h"
  24. #define QCRYPTO_HASH_DIGEST_LEN_MD5 16
  25. #define QCRYPTO_HASH_DIGEST_LEN_SHA1 20
  26. #define QCRYPTO_HASH_DIGEST_LEN_SHA224 28
  27. #define QCRYPTO_HASH_DIGEST_LEN_SHA256 32
  28. #define QCRYPTO_HASH_DIGEST_LEN_SHA384 48
  29. #define QCRYPTO_HASH_DIGEST_LEN_SHA512 64
  30. #define QCRYPTO_HASH_DIGEST_LEN_RIPEMD160 20
  31. #define QCRYPTO_HASH_DIGEST_LEN_SM3 32
  32. /* See also "QCryptoHashAlgo" defined in qapi/crypto.json */
  33. typedef struct QCryptoHash QCryptoHash;
  34. struct QCryptoHash {
  35. QCryptoHashAlgo alg;
  36. void *opaque;
  37. void *driver;
  38. };
  39. /**
  40. * qcrypto_hash_supports:
  41. * @alg: the hash algorithm
  42. *
  43. * Determine if @alg hash algorithm is supported by the
  44. * current configured build.
  45. *
  46. * Returns: true if the algorithm is supported, false otherwise
  47. */
  48. gboolean qcrypto_hash_supports(QCryptoHashAlgo alg);
  49. /**
  50. * qcrypto_hash_digest_len:
  51. * @alg: the hash algorithm
  52. *
  53. * Determine the size of the hash digest in bytes
  54. *
  55. * Returns: the digest length in bytes
  56. */
  57. size_t qcrypto_hash_digest_len(QCryptoHashAlgo alg);
  58. /**
  59. * qcrypto_hash_bytesv:
  60. * @alg: the hash algorithm
  61. * @iov: the array of memory regions to hash
  62. * @niov: the length of @iov
  63. * @result: pointer to hold output hash
  64. * @resultlen: pointer to hold length of @result
  65. * @errp: pointer to a NULL-initialized error object
  66. *
  67. * Computes the hash across all the memory regions
  68. * present in @iov.
  69. *
  70. * If @result_len is set to a non-zero value by the caller, then
  71. * @result must hold a pointer that is @result_len in size, and
  72. * @result_len match the size of the hash output. The digest will
  73. * be written into @result.
  74. *
  75. * If @result_len is set to zero, then this function will allocate
  76. * a buffer to hold the hash output digest, storing a pointer to
  77. * the buffer in @result, and setting @result_len to its size.
  78. * The memory referenced in @result must be released with a call
  79. * to g_free() when no longer required by the caller.
  80. *
  81. * Returns: 0 on success, -1 on error
  82. */
  83. int qcrypto_hash_bytesv(QCryptoHashAlgo alg,
  84. const struct iovec *iov,
  85. size_t niov,
  86. uint8_t **result,
  87. size_t *resultlen,
  88. Error **errp);
  89. /**
  90. * qcrypto_hash_bytes:
  91. * @alg: the hash algorithm
  92. * @buf: the memory region to hash
  93. * @len: the length of @buf
  94. * @result: pointer to hold output hash
  95. * @resultlen: pointer to hold length of @result
  96. * @errp: pointer to a NULL-initialized error object
  97. *
  98. * Computes the hash across all the memory region
  99. * @buf of length @len.
  100. *
  101. * If @result_len is set to a non-zero value by the caller, then
  102. * @result must hold a pointer that is @result_len in size, and
  103. * @result_len match the size of the hash output. The digest will
  104. * be written into @result.
  105. *
  106. * If @result_len is set to zero, then this function will allocate
  107. * a buffer to hold the hash output digest, storing a pointer to
  108. * the buffer in @result, and setting @result_len to its size.
  109. * The memory referenced in @result must be released with a call
  110. * to g_free() when no longer required by the caller.
  111. *
  112. * Returns: 0 on success, -1 on error
  113. */
  114. int qcrypto_hash_bytes(QCryptoHashAlgo alg,
  115. const char *buf,
  116. size_t len,
  117. uint8_t **result,
  118. size_t *resultlen,
  119. Error **errp);
  120. /**
  121. * qcrypto_hash_digestv:
  122. * @alg: the hash algorithm
  123. * @iov: the array of memory regions to hash
  124. * @niov: the length of @iov
  125. * @digest: pointer to hold output hash
  126. * @errp: pointer to a NULL-initialized error object
  127. *
  128. * Computes the hash across all the memory regions
  129. * present in @iov. The @digest pointer will be
  130. * filled with the printable hex digest of the computed
  131. * hash, which will be terminated by '\0'. The
  132. * memory pointer in @digest must be released
  133. * with a call to g_free() when no longer required.
  134. *
  135. * Returns: 0 on success, -1 on error
  136. */
  137. int qcrypto_hash_digestv(QCryptoHashAlgo alg,
  138. const struct iovec *iov,
  139. size_t niov,
  140. char **digest,
  141. Error **errp);
  142. /**
  143. * qcrypto_hash_updatev:
  144. * @hash: hash object from qcrypto_hash_new
  145. * @iov: the array of memory regions to hash
  146. * @niov: the length of @iov
  147. * @errp: pointer to a NULL-initialized error object
  148. *
  149. * Updates the given hash object with all the memory regions
  150. * present in @iov.
  151. *
  152. * Returns: 0 on success, -1 on error
  153. */
  154. int qcrypto_hash_updatev(QCryptoHash *hash,
  155. const struct iovec *iov,
  156. size_t niov,
  157. Error **errp);
  158. /**
  159. * qcrypto_hash_update:
  160. * @hash: hash object from qcrypto_hash_new
  161. * @buf: the memory region to hash
  162. * @len: the length of @buf
  163. * @errp: pointer to a NULL-initialized error object
  164. *
  165. * Updates the given hash object with the data from
  166. * the given buffer.
  167. *
  168. * Returns: 0 on success, -1 on error
  169. */
  170. int qcrypto_hash_update(QCryptoHash *hash,
  171. const char *buf,
  172. size_t len,
  173. Error **errp);
  174. /**
  175. * qcrypto_hash_finalize_digest:
  176. * @hash: the hash object to finalize
  177. * @digest: pointer to hold output hash
  178. * @errp: pointer to a NULL-initialized error object
  179. *
  180. * Computes the hash from the given hash object. Hash object
  181. * is expected to have its data updated from the qcrypto_hash_update function.
  182. * The @digest pointer will be filled with the printable hex digest of the
  183. * computed hash, which will be terminated by '\0'. The memory pointer
  184. * in @digest must be released with a call to g_free() when
  185. * no longer required.
  186. *
  187. * Returns: 0 on success, -1 on error
  188. */
  189. int qcrypto_hash_finalize_digest(QCryptoHash *hash,
  190. char **digest,
  191. Error **errp);
  192. /**
  193. * qcrypto_hash_finalize_base64:
  194. * @hash_ctx: hash object to finalize
  195. * @base64: pointer to store the hash result in
  196. * @errp: pointer to a NULL-initialized error object
  197. *
  198. * Computes the hash from the given hash object. Hash object
  199. * is expected to have it's data updated from the qcrypto_hash_update function.
  200. * The @base64 pointer will be filled with the base64 encoding of the computed
  201. * hash, which will be terminated by '\0'. The memory pointer in @base64
  202. * must be released with a call to g_free() when no longer required.
  203. *
  204. * Returns: 0 on success, -1 on error
  205. */
  206. int qcrypto_hash_finalize_base64(QCryptoHash *hash,
  207. char **base64,
  208. Error **errp);
  209. /**
  210. * qcrypto_hash_finalize_bytes:
  211. * @hash_ctx: hash object to finalize
  212. * @result: pointer to store the hash result in
  213. * @result_len: Pointer to store the length of the result in
  214. * @errp: pointer to a NULL-initialized error object
  215. *
  216. * Computes the hash from the given hash object. Hash object
  217. * is expected to have it's data updated from the qcrypto_hash_update function.
  218. *
  219. * If @result_len is set to a non-zero value by the caller, then
  220. * @result must hold a pointer that is @result_len in size, and
  221. * @result_len match the size of the hash output. The digest will
  222. * be written into @result.
  223. *
  224. * If @result_len is set to zero, then this function will allocate
  225. * a buffer to hold the hash output digest, storing a pointer to
  226. * the buffer in @result, and setting @result_len to its size.
  227. * The memory referenced in @result must be released with a call
  228. * to g_free() when no longer required by the caller.
  229. *
  230. * Returns: 0 on success, -1 on error
  231. */
  232. int qcrypto_hash_finalize_bytes(QCryptoHash *hash,
  233. uint8_t **result,
  234. size_t *result_len,
  235. Error **errp);
  236. /**
  237. * qcrypto_hash_new:
  238. * @alg: the hash algorithm
  239. * @errp: pointer to a NULL-initialized error object
  240. *
  241. * Creates a new hashing context for the chosen algorithm for
  242. * usage with qcrypto_hash_update.
  243. *
  244. * Returns: New hash object with the given algorithm, or NULL on error.
  245. */
  246. QCryptoHash *qcrypto_hash_new(QCryptoHashAlgo alg, Error **errp);
  247. /**
  248. * qcrypto_hash_free:
  249. * @hash: hash object to free
  250. *
  251. * Frees a hashing context for the chosen algorithm.
  252. */
  253. void qcrypto_hash_free(QCryptoHash *hash);
  254. G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoHash, qcrypto_hash_free)
  255. /**
  256. * qcrypto_hash_digest:
  257. * @alg: the hash algorithm
  258. * @buf: the memory region to hash
  259. * @len: the length of @buf
  260. * @digest: pointer to hold output hash
  261. * @errp: pointer to a NULL-initialized error object
  262. *
  263. * Computes the hash across all the memory region
  264. * @buf of length @len. The @digest pointer will be
  265. * filled with the printable hex digest of the computed
  266. * hash, which will be terminated by '\0'. The
  267. * memory pointer in @digest must be released
  268. * with a call to g_free() when no longer required.
  269. *
  270. * Returns: 0 on success, -1 on error
  271. */
  272. int qcrypto_hash_digest(QCryptoHashAlgo alg,
  273. const char *buf,
  274. size_t len,
  275. char **digest,
  276. Error **errp);
  277. /**
  278. * qcrypto_hash_base64v:
  279. * @alg: the hash algorithm
  280. * @iov: the array of memory regions to hash
  281. * @niov: the length of @iov
  282. * @base64: pointer to hold output hash
  283. * @errp: pointer to a NULL-initialized error object
  284. *
  285. * Computes the hash across all the memory regions
  286. * present in @iov. The @base64 pointer will be
  287. * filled with the base64 encoding of the computed
  288. * hash, which will be terminated by '\0'. The
  289. * memory pointer in @base64 must be released
  290. * with a call to g_free() when no longer required.
  291. *
  292. * Returns: 0 on success, -1 on error
  293. */
  294. int qcrypto_hash_base64v(QCryptoHashAlgo alg,
  295. const struct iovec *iov,
  296. size_t niov,
  297. char **base64,
  298. Error **errp);
  299. /**
  300. * qcrypto_hash_base64:
  301. * @alg: the hash algorithm
  302. * @buf: the memory region to hash
  303. * @len: the length of @buf
  304. * @base64: pointer to hold output hash
  305. * @errp: pointer to a NULL-initialized error object
  306. *
  307. * Computes the hash across all the memory region
  308. * @buf of length @len. The @base64 pointer will be
  309. * filled with the base64 encoding of the computed
  310. * hash, which will be terminated by '\0'. The
  311. * memory pointer in @base64 must be released
  312. * with a call to g_free() when no longer required.
  313. *
  314. * Returns: 0 on success, -1 on error
  315. */
  316. int qcrypto_hash_base64(QCryptoHashAlgo alg,
  317. const char *buf,
  318. size_t len,
  319. char **base64,
  320. Error **errp);
  321. #endif /* QCRYPTO_HASH_H */