1 /* 2 * QEMU Crypto hash algorithms 3 * 4 * Copyright (c) 2015 Red Hat, Inc. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18 * 19 */ 20 21 #ifndef QCRYPTO_HASH_H__ 22 #define QCRYPTO_HASH_H__ 23 24 #include "qemu-common.h" 25 #include "qapi/error.h" 26 27 typedef enum { 28 QCRYPTO_HASH_ALG_MD5, 29 QCRYPTO_HASH_ALG_SHA1, 30 QCRYPTO_HASH_ALG_SHA256, 31 32 QCRYPTO_HASH_ALG_LAST 33 } QCryptoHashAlgorithm; 34 35 36 /** 37 * qcrypto_hash_supports: 38 * @alg: the hash algorithm 39 * 40 * Determine if @alg hash algorithm is supported by the 41 * current configured build. 42 * 43 * Returns: true if the algorithm is supported, false otherwise 44 */ 45 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg); 46 47 /** 48 * qcrypto_hash_bytesv: 49 * @alg: the hash algorithm 50 * @iov: the array of memory regions to hash 51 * @niov: the length of @iov 52 * @result: pointer to hold output hash 53 * @resultlen: pointer to hold length of @result 54 * @errp: pointer to uninitialized error object 55 * 56 * Computes the hash across all the memory regions 57 * present in @iov. The @result pointer will be 58 * filled with raw bytes representing the computed 59 * hash, which will have length @resultlen. The 60 * memory pointer in @result must be released 61 * with a call to g_free() when no longer required. 62 * 63 * Returns: 0 on success, -1 on error 64 */ 65 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg, 66 const struct iovec *iov, 67 size_t niov, 68 uint8_t **result, 69 size_t *resultlen, 70 Error **errp); 71 72 /** 73 * qcrypto_hash_bytes: 74 * @alg: the hash algorithm 75 * @buf: the memory region to hash 76 * @len: the length of @buf 77 * @result: pointer to hold output hash 78 * @resultlen: pointer to hold length of @result 79 * @errp: pointer to uninitialized error object 80 * 81 * Computes the hash across all the memory region 82 * @buf of length @len. The @result pointer will be 83 * filled with raw bytes representing the computed 84 * hash, which will have length @resultlen. The 85 * memory pointer in @result must be released 86 * with a call to g_free() when no longer required. 87 * 88 * Returns: 0 on success, -1 on error 89 */ 90 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg, 91 const char *buf, 92 size_t len, 93 uint8_t **result, 94 size_t *resultlen, 95 Error **errp); 96 97 /** 98 * qcrypto_hash_digestv: 99 * @alg: the hash algorithm 100 * @iov: the array of memory regions to hash 101 * @niov: the length of @iov 102 * @digest: pointer to hold output hash 103 * @errp: pointer to uninitialized error object 104 * 105 * Computes the hash across all the memory regions 106 * present in @iov. The @digest pointer will be 107 * filled with the printable hex digest of the computed 108 * hash, which will be terminated by '\0'. The 109 * memory pointer in @digest must be released 110 * with a call to g_free() when no longer required. 111 * 112 * Returns: 0 on success, -1 on error 113 */ 114 int qcrypto_hash_digestv(QCryptoHashAlgorithm alg, 115 const struct iovec *iov, 116 size_t niov, 117 char **digest, 118 Error **errp); 119 120 /** 121 * qcrypto_hash_digest: 122 * @alg: the hash algorithm 123 * @buf: the memory region to hash 124 * @len: the length of @buf 125 * @digest: pointer to hold output hash 126 * @errp: pointer to uninitialized error object 127 * 128 * Computes the hash across all the memory region 129 * @buf of length @len. 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_digest(QCryptoHashAlgorithm alg, 138 const char *buf, 139 size_t len, 140 char **digest, 141 Error **errp); 142 143 /** 144 * qcrypto_hash_base64v: 145 * @alg: the hash algorithm 146 * @iov: the array of memory regions to hash 147 * @niov: the length of @iov 148 * @base64: pointer to hold output hash 149 * @errp: pointer to uninitialized error object 150 * 151 * Computes the hash across all the memory regions 152 * present in @iov. The @base64 pointer will be 153 * filled with the base64 encoding of the computed 154 * hash, which will be terminated by '\0'. The 155 * memory pointer in @base64 must be released 156 * with a call to g_free() when no longer required. 157 * 158 * Returns: 0 on success, -1 on error 159 */ 160 int qcrypto_hash_base64v(QCryptoHashAlgorithm alg, 161 const struct iovec *iov, 162 size_t niov, 163 char **base64, 164 Error **errp); 165 166 /** 167 * qcrypto_hash_base64: 168 * @alg: the hash algorithm 169 * @buf: the memory region to hash 170 * @len: the length of @buf 171 * @base64: pointer to hold output hash 172 * @errp: pointer to uninitialized error object 173 * 174 * Computes the hash across all the memory region 175 * @buf of length @len. The @base64 pointer will be 176 * filled with the base64 encoding of the computed 177 * hash, which will be terminated by '\0'. The 178 * memory pointer in @base64 must be released 179 * with a call to g_free() when no longer required. 180 * 181 * Returns: 0 on success, -1 on error 182 */ 183 int qcrypto_hash_base64(QCryptoHashAlgorithm alg, 184 const char *buf, 185 size_t len, 186 char **base64, 187 Error **errp); 188 189 #endif /* QCRYPTO_HASH_H__ */ 190