hmac.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * QEMU Crypto hmac algorithms
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or
  7. * (at your option) any later version. See the COPYING file in the
  8. * top-level directory.
  9. *
  10. */
  11. #ifndef QCRYPTO_HMAC_H
  12. #define QCRYPTO_HMAC_H
  13. #include "qapi/qapi-types-crypto.h"
  14. typedef struct QCryptoHmac QCryptoHmac;
  15. struct QCryptoHmac {
  16. QCryptoHashAlgo alg;
  17. void *opaque;
  18. void *driver;
  19. };
  20. /**
  21. * qcrypto_hmac_supports:
  22. * @alg: the hmac algorithm
  23. *
  24. * Determine if @alg hmac algorithm is supported by
  25. * the current configured build
  26. *
  27. * Returns:
  28. * true if the algorithm is supported, false otherwise
  29. */
  30. bool qcrypto_hmac_supports(QCryptoHashAlgo alg);
  31. /**
  32. * qcrypto_hmac_new:
  33. * @alg: the hmac algorithm
  34. * @key: the key bytes
  35. * @nkey: the length of @key
  36. * @errp: pointer to a NULL-initialized error object
  37. *
  38. * Creates a new hmac object with the algorithm @alg
  39. *
  40. * The @key parameter provides the bytes representing
  41. * the secret key to use. The @nkey parameter specifies
  42. * the length of @key in bytes
  43. *
  44. * Note: must use qcrypto_hmac_free() to release the
  45. * returned hmac object when no longer required
  46. *
  47. * Returns:
  48. * a new hmac object, or NULL on error
  49. */
  50. QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgo alg,
  51. const uint8_t *key, size_t nkey,
  52. Error **errp);
  53. /**
  54. * qcrypto_hmac_free:
  55. * @hmac: the hmac object
  56. *
  57. * Release the memory associated with @hmac that was
  58. * previously allocated by qcrypto_hmac_new()
  59. */
  60. void qcrypto_hmac_free(QCryptoHmac *hmac);
  61. G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoHmac, qcrypto_hmac_free)
  62. /**
  63. * qcrypto_hmac_bytesv:
  64. * @hmac: the hmac object
  65. * @iov: the array of memory regions to hmac
  66. * @niov: the length of @iov
  67. * @result: pointer to hold output hmac
  68. * @resultlen: pointer to hold length of @result
  69. * @errp: pointer to a NULL-initialized error object
  70. *
  71. * Computes the hmac across all the memory regions
  72. * present in @iov.
  73. *
  74. * If @result_len is set to a non-zero value by the caller, then
  75. * @result must hold a pointer that is @result_len in size, and
  76. * @result_len match the size of the hash output. The digest will
  77. * be written into @result.
  78. *
  79. * If @result_len is set to zero, then this function will allocate
  80. * a buffer to hold the hash output digest, storing a pointer to
  81. * the buffer in @result, and setting @result_len to its size.
  82. * The memory referenced in @result must be released with a call
  83. * to g_free() when no longer required by the caller.
  84. *
  85. * Returns:
  86. * 0 on success, -1 on error
  87. */
  88. int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
  89. const struct iovec *iov,
  90. size_t niov,
  91. uint8_t **result,
  92. size_t *resultlen,
  93. Error **errp);
  94. /**
  95. * qcrypto_hmac_bytes:
  96. * @hmac: the hmac object
  97. * @buf: the memory region to hmac
  98. * @len: the length of @buf
  99. * @result: pointer to hold output hmac
  100. * @resultlen: pointer to hold length of @result
  101. * @errp: pointer to a NULL-initialized error object
  102. *
  103. * Computes the hmac across all the memory region
  104. * @buf of length @len.
  105. *
  106. * If @result_len is set to a non-zero value by the caller, then
  107. * @result must hold a pointer that is @result_len in size, and
  108. * @result_len match the size of the hash output. The digest will
  109. * be written into @result.
  110. *
  111. * If @result_len is set to zero, then this function will allocate
  112. * a buffer to hold the hash output digest, storing a pointer to
  113. * the buffer in @result, and setting @result_len to its size.
  114. * The memory referenced in @result must be released with a call
  115. * to g_free() when no longer required by the caller.
  116. *
  117. * Returns:
  118. * 0 on success, -1 on error
  119. */
  120. int qcrypto_hmac_bytes(QCryptoHmac *hmac,
  121. const char *buf,
  122. size_t len,
  123. uint8_t **result,
  124. size_t *resultlen,
  125. Error **errp);
  126. /**
  127. * qcrypto_hmac_digestv:
  128. * @hmac: the hmac object
  129. * @iov: the array of memory regions to hmac
  130. * @niov: the length of @iov
  131. * @digest: pointer to hold output hmac
  132. * @errp: pointer to a NULL-initialized error object
  133. *
  134. * Computes the hmac across all the memory regions
  135. * present in @iov. The @digest pointer will be
  136. * filled with the printable hex digest of the computed
  137. * hmac, which will be terminated by '\0'. The
  138. * memory pointer in @digest must be released
  139. * with a call to g_free() when no longer required.
  140. *
  141. * Returns:
  142. * 0 on success, -1 on error
  143. */
  144. int qcrypto_hmac_digestv(QCryptoHmac *hmac,
  145. const struct iovec *iov,
  146. size_t niov,
  147. char **digest,
  148. Error **errp);
  149. /**
  150. * qcrypto_hmac_digest:
  151. * @hmac: the hmac object
  152. * @buf: the memory region to hmac
  153. * @len: the length of @buf
  154. * @digest: pointer to hold output hmac
  155. * @errp: pointer to a NULL-initialized error object
  156. *
  157. * Computes the hmac across all the memory region
  158. * @buf of length @len. The @digest pointer will be
  159. * filled with the printable hex digest of the computed
  160. * hmac, which will be terminated by '\0'. The
  161. * memory pointer in @digest must be released
  162. * with a call to g_free() when no longer required.
  163. *
  164. * Returns: 0 on success, -1 on error
  165. */
  166. int qcrypto_hmac_digest(QCryptoHmac *hmac,
  167. const char *buf,
  168. size_t len,
  169. char **digest,
  170. Error **errp);
  171. #endif