xref: /openbmc/qemu/include/crypto/hmac.h (revision e51d8fbb7e673e487e98327fc067700b5a3edf30)
19767b75dSLongpeng(Mike) /*
29767b75dSLongpeng(Mike)  * QEMU Crypto hmac algorithms
39767b75dSLongpeng(Mike)  *
49767b75dSLongpeng(Mike)  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
59767b75dSLongpeng(Mike)  *
69767b75dSLongpeng(Mike)  * This work is licensed under the terms of the GNU GPL, version 2 or
79767b75dSLongpeng(Mike)  * (at your option) any later version.  See the COPYING file in the
89767b75dSLongpeng(Mike)  * top-level directory.
99767b75dSLongpeng(Mike)  *
109767b75dSLongpeng(Mike)  */
119767b75dSLongpeng(Mike) 
129767b75dSLongpeng(Mike) #ifndef QCRYPTO_HMAC_H
139767b75dSLongpeng(Mike) #define QCRYPTO_HMAC_H
149767b75dSLongpeng(Mike) 
159af23989SMarkus Armbruster #include "qapi/qapi-types-crypto.h"
169767b75dSLongpeng(Mike) 
179767b75dSLongpeng(Mike) typedef struct QCryptoHmac QCryptoHmac;
189767b75dSLongpeng(Mike) struct QCryptoHmac {
19ef834aa2SMarkus Armbruster     QCryptoHashAlgo alg;
209767b75dSLongpeng(Mike)     void *opaque;
2114a5a2aeSLongpeng(Mike)     void *driver;
229767b75dSLongpeng(Mike) };
239767b75dSLongpeng(Mike) 
249767b75dSLongpeng(Mike) /**
259767b75dSLongpeng(Mike)  * qcrypto_hmac_supports:
269767b75dSLongpeng(Mike)  * @alg: the hmac algorithm
279767b75dSLongpeng(Mike)  *
289767b75dSLongpeng(Mike)  * Determine if @alg hmac algorithm is supported by
299767b75dSLongpeng(Mike)  * the current configured build
309767b75dSLongpeng(Mike)  *
319767b75dSLongpeng(Mike)  * Returns:
329767b75dSLongpeng(Mike)  *  true if the algorithm is supported, false otherwise
339767b75dSLongpeng(Mike)  */
34ef834aa2SMarkus Armbruster bool qcrypto_hmac_supports(QCryptoHashAlgo alg);
359767b75dSLongpeng(Mike) 
369767b75dSLongpeng(Mike) /**
379767b75dSLongpeng(Mike)  * qcrypto_hmac_new:
389767b75dSLongpeng(Mike)  * @alg: the hmac algorithm
399767b75dSLongpeng(Mike)  * @key: the key bytes
409767b75dSLongpeng(Mike)  * @nkey: the length of @key
419767b75dSLongpeng(Mike)  * @errp: pointer to a NULL-initialized error object
429767b75dSLongpeng(Mike)  *
439767b75dSLongpeng(Mike)  * Creates a new hmac object with the algorithm @alg
449767b75dSLongpeng(Mike)  *
459767b75dSLongpeng(Mike)  * The @key parameter provides the bytes representing
469767b75dSLongpeng(Mike)  * the secret key to use. The @nkey parameter specifies
479767b75dSLongpeng(Mike)  * the length of @key in bytes
489767b75dSLongpeng(Mike)  *
499767b75dSLongpeng(Mike)  * Note: must use qcrypto_hmac_free() to release the
509767b75dSLongpeng(Mike)  * returned hmac object when no longer required
519767b75dSLongpeng(Mike)  *
529767b75dSLongpeng(Mike)  * Returns:
539767b75dSLongpeng(Mike)  *  a new hmac object, or NULL on error
549767b75dSLongpeng(Mike)  */
55ef834aa2SMarkus Armbruster QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgo alg,
569767b75dSLongpeng(Mike)                               const uint8_t *key, size_t nkey,
579767b75dSLongpeng(Mike)                               Error **errp);
589767b75dSLongpeng(Mike) 
599767b75dSLongpeng(Mike) /**
609767b75dSLongpeng(Mike)  * qcrypto_hmac_free:
619767b75dSLongpeng(Mike)  * @hmac: the hmac object
629767b75dSLongpeng(Mike)  *
639767b75dSLongpeng(Mike)  * Release the memory associated with @hmac that was
649767b75dSLongpeng(Mike)  * previously allocated by qcrypto_hmac_new()
659767b75dSLongpeng(Mike)  */
669767b75dSLongpeng(Mike) void qcrypto_hmac_free(QCryptoHmac *hmac);
679767b75dSLongpeng(Mike) 
68133cf1e5SDaniel P. Berrangé G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoHmac, qcrypto_hmac_free)
69133cf1e5SDaniel P. Berrangé 
709767b75dSLongpeng(Mike) /**
719767b75dSLongpeng(Mike)  * qcrypto_hmac_bytesv:
729767b75dSLongpeng(Mike)  * @hmac: the hmac object
739767b75dSLongpeng(Mike)  * @iov: the array of memory regions to hmac
749767b75dSLongpeng(Mike)  * @niov: the length of @iov
759767b75dSLongpeng(Mike)  * @result: pointer to hold output hmac
769767b75dSLongpeng(Mike)  * @resultlen: pointer to hold length of @result
779767b75dSLongpeng(Mike)  * @errp: pointer to a NULL-initialized error object
789767b75dSLongpeng(Mike)  *
799767b75dSLongpeng(Mike)  * Computes the hmac across all the memory regions
80*76966095SDaniel P. Berrangé  * present in @iov.
81*76966095SDaniel P. Berrangé  *
82*76966095SDaniel P. Berrangé  * If @result_len is set to a non-zero value by the caller, then
83*76966095SDaniel P. Berrangé  * @result must hold a pointer that is @result_len in size, and
84*76966095SDaniel P. Berrangé  * @result_len match the size of the hash output. The digest will
85*76966095SDaniel P. Berrangé  * be written into @result.
86*76966095SDaniel P. Berrangé  *
87*76966095SDaniel P. Berrangé  * If @result_len is set to zero, then this function will allocate
88*76966095SDaniel P. Berrangé  * a buffer to hold the hash output digest, storing a pointer to
89*76966095SDaniel P. Berrangé  * the buffer in @result, and setting @result_len to its size.
90*76966095SDaniel P. Berrangé  * The memory referenced in @result must be released with a call
91*76966095SDaniel P. Berrangé  * to g_free() when no longer required by the caller.
929767b75dSLongpeng(Mike)  *
939767b75dSLongpeng(Mike)  * Returns:
949767b75dSLongpeng(Mike)  *  0 on success, -1 on error
959767b75dSLongpeng(Mike)  */
969767b75dSLongpeng(Mike) int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
979767b75dSLongpeng(Mike)                         const struct iovec *iov,
989767b75dSLongpeng(Mike)                         size_t niov,
999767b75dSLongpeng(Mike)                         uint8_t **result,
1009767b75dSLongpeng(Mike)                         size_t *resultlen,
1019767b75dSLongpeng(Mike)                         Error **errp);
1029767b75dSLongpeng(Mike) 
1039767b75dSLongpeng(Mike) /**
1049767b75dSLongpeng(Mike)  * qcrypto_hmac_bytes:
1059767b75dSLongpeng(Mike)  * @hmac: the hmac object
1069767b75dSLongpeng(Mike)  * @buf: the memory region to hmac
1079767b75dSLongpeng(Mike)  * @len: the length of @buf
1089767b75dSLongpeng(Mike)  * @result: pointer to hold output hmac
1099767b75dSLongpeng(Mike)  * @resultlen: pointer to hold length of @result
1109767b75dSLongpeng(Mike)  * @errp: pointer to a NULL-initialized error object
1119767b75dSLongpeng(Mike)  *
1129767b75dSLongpeng(Mike)  * Computes the hmac across all the memory region
113*76966095SDaniel P. Berrangé  * @buf of length @len.
114*76966095SDaniel P. Berrangé  *
115*76966095SDaniel P. Berrangé  * If @result_len is set to a non-zero value by the caller, then
116*76966095SDaniel P. Berrangé  * @result must hold a pointer that is @result_len in size, and
117*76966095SDaniel P. Berrangé  * @result_len match the size of the hash output. The digest will
118*76966095SDaniel P. Berrangé  * be written into @result.
119*76966095SDaniel P. Berrangé  *
120*76966095SDaniel P. Berrangé  * If @result_len is set to zero, then this function will allocate
121*76966095SDaniel P. Berrangé  * a buffer to hold the hash output digest, storing a pointer to
122*76966095SDaniel P. Berrangé  * the buffer in @result, and setting @result_len to its size.
123*76966095SDaniel P. Berrangé  * The memory referenced in @result must be released with a call
124*76966095SDaniel P. Berrangé  * to g_free() when no longer required by the caller.
1259767b75dSLongpeng(Mike)  *
1269767b75dSLongpeng(Mike)  * Returns:
1279767b75dSLongpeng(Mike)  *  0 on success, -1 on error
1289767b75dSLongpeng(Mike)  */
1299767b75dSLongpeng(Mike) int qcrypto_hmac_bytes(QCryptoHmac *hmac,
1309767b75dSLongpeng(Mike)                        const char *buf,
1319767b75dSLongpeng(Mike)                        size_t len,
1329767b75dSLongpeng(Mike)                        uint8_t **result,
1339767b75dSLongpeng(Mike)                        size_t *resultlen,
1349767b75dSLongpeng(Mike)                        Error **errp);
1359767b75dSLongpeng(Mike) 
1369767b75dSLongpeng(Mike) /**
1379767b75dSLongpeng(Mike)  * qcrypto_hmac_digestv:
1389767b75dSLongpeng(Mike)  * @hmac: the hmac object
1399767b75dSLongpeng(Mike)  * @iov: the array of memory regions to hmac
1409767b75dSLongpeng(Mike)  * @niov: the length of @iov
1419767b75dSLongpeng(Mike)  * @digest: pointer to hold output hmac
1429767b75dSLongpeng(Mike)  * @errp: pointer to a NULL-initialized error object
1439767b75dSLongpeng(Mike)  *
1449767b75dSLongpeng(Mike)  * Computes the hmac across all the memory regions
1459767b75dSLongpeng(Mike)  * present in @iov. The @digest pointer will be
1469767b75dSLongpeng(Mike)  * filled with the printable hex digest of the computed
1479767b75dSLongpeng(Mike)  * hmac, which will be terminated by '\0'. The
1489767b75dSLongpeng(Mike)  * memory pointer in @digest must be released
1499767b75dSLongpeng(Mike)  * with a call to g_free() when no longer required.
1509767b75dSLongpeng(Mike)  *
1519767b75dSLongpeng(Mike)  * Returns:
1529767b75dSLongpeng(Mike)  *  0 on success, -1 on error
1539767b75dSLongpeng(Mike)  */
1549767b75dSLongpeng(Mike) int qcrypto_hmac_digestv(QCryptoHmac *hmac,
1559767b75dSLongpeng(Mike)                          const struct iovec *iov,
1569767b75dSLongpeng(Mike)                          size_t niov,
1579767b75dSLongpeng(Mike)                          char **digest,
1589767b75dSLongpeng(Mike)                          Error **errp);
1599767b75dSLongpeng(Mike) 
1609767b75dSLongpeng(Mike) /**
1619767b75dSLongpeng(Mike)  * qcrypto_hmac_digest:
1629767b75dSLongpeng(Mike)  * @hmac: the hmac object
1639767b75dSLongpeng(Mike)  * @buf: the memory region to hmac
1649767b75dSLongpeng(Mike)  * @len: the length of @buf
1659767b75dSLongpeng(Mike)  * @digest: pointer to hold output hmac
1669767b75dSLongpeng(Mike)  * @errp: pointer to a NULL-initialized error object
1679767b75dSLongpeng(Mike)  *
1689767b75dSLongpeng(Mike)  * Computes the hmac across all the memory region
1699767b75dSLongpeng(Mike)  * @buf of length @len. The @digest pointer will be
1709767b75dSLongpeng(Mike)  * filled with the printable hex digest of the computed
1719767b75dSLongpeng(Mike)  * hmac, which will be terminated by '\0'. The
1729767b75dSLongpeng(Mike)  * memory pointer in @digest must be released
1739767b75dSLongpeng(Mike)  * with a call to g_free() when no longer required.
1749767b75dSLongpeng(Mike)  *
1759767b75dSLongpeng(Mike)  * Returns: 0 on success, -1 on error
1769767b75dSLongpeng(Mike)  */
1779767b75dSLongpeng(Mike) int qcrypto_hmac_digest(QCryptoHmac *hmac,
1789767b75dSLongpeng(Mike)                         const char *buf,
1799767b75dSLongpeng(Mike)                         size_t len,
1809767b75dSLongpeng(Mike)                         char **digest,
1819767b75dSLongpeng(Mike)                         Error **errp);
1829767b75dSLongpeng(Mike) 
1839767b75dSLongpeng(Mike) #endif
184