xref: /qemu/crypto/hmac.c (revision e688df6b)
112a4f216SLongpeng(Mike) /*
212a4f216SLongpeng(Mike)  * QEMU Crypto hmac algorithms
312a4f216SLongpeng(Mike)  *
412a4f216SLongpeng(Mike)  * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
512a4f216SLongpeng(Mike)  *
612a4f216SLongpeng(Mike)  * This work is licensed under the terms of the GNU GPL, version 2 or
712a4f216SLongpeng(Mike)  * (at your option) any later version.  See the COPYING file in the
812a4f216SLongpeng(Mike)  * top-level directory.
912a4f216SLongpeng(Mike)  *
1012a4f216SLongpeng(Mike)  */
1112a4f216SLongpeng(Mike) 
1212a4f216SLongpeng(Mike) #include "qemu/osdep.h"
1312a4f216SLongpeng(Mike) #include "crypto/hmac.h"
1414a5a2aeSLongpeng(Mike) #include "hmacpriv.h"
1512a4f216SLongpeng(Mike) 
1612a4f216SLongpeng(Mike) static const char hex[] = "0123456789abcdef";
1712a4f216SLongpeng(Mike) 
qcrypto_hmac_bytesv(QCryptoHmac * hmac,const struct iovec * iov,size_t niov,uint8_t ** result,size_t * resultlen,Error ** errp)1814a5a2aeSLongpeng(Mike) int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
1914a5a2aeSLongpeng(Mike)                         const struct iovec *iov,
2014a5a2aeSLongpeng(Mike)                         size_t niov,
2114a5a2aeSLongpeng(Mike)                         uint8_t **result,
2214a5a2aeSLongpeng(Mike)                         size_t *resultlen,
2314a5a2aeSLongpeng(Mike)                         Error **errp)
2414a5a2aeSLongpeng(Mike) {
2514a5a2aeSLongpeng(Mike)     QCryptoHmacDriver *drv = hmac->driver;
2614a5a2aeSLongpeng(Mike) 
2714a5a2aeSLongpeng(Mike)     return drv->hmac_bytesv(hmac, iov, niov, result, resultlen, errp);
2814a5a2aeSLongpeng(Mike) }
2914a5a2aeSLongpeng(Mike) 
qcrypto_hmac_bytes(QCryptoHmac * hmac,const char * buf,size_t len,uint8_t ** result,size_t * resultlen,Error ** errp)3012a4f216SLongpeng(Mike) int qcrypto_hmac_bytes(QCryptoHmac *hmac,
3112a4f216SLongpeng(Mike)                        const char *buf,
3212a4f216SLongpeng(Mike)                        size_t len,
3312a4f216SLongpeng(Mike)                        uint8_t **result,
3412a4f216SLongpeng(Mike)                        size_t *resultlen,
3512a4f216SLongpeng(Mike)                        Error **errp)
3612a4f216SLongpeng(Mike) {
3712a4f216SLongpeng(Mike)     struct iovec iov = {
3812a4f216SLongpeng(Mike)             .iov_base = (char *)buf,
3912a4f216SLongpeng(Mike)             .iov_len = len
4012a4f216SLongpeng(Mike)     };
4112a4f216SLongpeng(Mike) 
4212a4f216SLongpeng(Mike)     return qcrypto_hmac_bytesv(hmac, &iov, 1, result, resultlen, errp);
4312a4f216SLongpeng(Mike) }
4412a4f216SLongpeng(Mike) 
qcrypto_hmac_digestv(QCryptoHmac * hmac,const struct iovec * iov,size_t niov,char ** digest,Error ** errp)4512a4f216SLongpeng(Mike) int qcrypto_hmac_digestv(QCryptoHmac *hmac,
4612a4f216SLongpeng(Mike)                          const struct iovec *iov,
4712a4f216SLongpeng(Mike)                          size_t niov,
4812a4f216SLongpeng(Mike)                          char **digest,
4912a4f216SLongpeng(Mike)                          Error **errp)
5012a4f216SLongpeng(Mike) {
5112a4f216SLongpeng(Mike)     uint8_t *result = NULL;
5212a4f216SLongpeng(Mike)     size_t resultlen = 0;
5312a4f216SLongpeng(Mike)     size_t i;
5412a4f216SLongpeng(Mike) 
5512a4f216SLongpeng(Mike)     if (qcrypto_hmac_bytesv(hmac, iov, niov, &result, &resultlen, errp) < 0) {
5612a4f216SLongpeng(Mike)         return -1;
5712a4f216SLongpeng(Mike)     }
5812a4f216SLongpeng(Mike) 
5912a4f216SLongpeng(Mike)     *digest = g_new0(char, (resultlen * 2) + 1);
6012a4f216SLongpeng(Mike) 
6112a4f216SLongpeng(Mike)     for (i = 0 ; i < resultlen ; i++) {
6212a4f216SLongpeng(Mike)         (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
6312a4f216SLongpeng(Mike)         (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
6412a4f216SLongpeng(Mike)     }
6512a4f216SLongpeng(Mike) 
6612a4f216SLongpeng(Mike)     (*digest)[resultlen * 2] = '\0';
6712a4f216SLongpeng(Mike) 
6812a4f216SLongpeng(Mike)     g_free(result);
6912a4f216SLongpeng(Mike)     return 0;
7012a4f216SLongpeng(Mike) }
7112a4f216SLongpeng(Mike) 
qcrypto_hmac_digest(QCryptoHmac * hmac,const char * buf,size_t len,char ** digest,Error ** errp)7212a4f216SLongpeng(Mike) int qcrypto_hmac_digest(QCryptoHmac *hmac,
7312a4f216SLongpeng(Mike)                         const char *buf,
7412a4f216SLongpeng(Mike)                         size_t len,
7512a4f216SLongpeng(Mike)                         char **digest,
7612a4f216SLongpeng(Mike)                         Error **errp)
7712a4f216SLongpeng(Mike) {
7812a4f216SLongpeng(Mike)     struct iovec iov = {
7912a4f216SLongpeng(Mike)             .iov_base = (char *)buf,
8012a4f216SLongpeng(Mike)             .iov_len = len
8112a4f216SLongpeng(Mike)     };
8212a4f216SLongpeng(Mike) 
8312a4f216SLongpeng(Mike)     return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp);
8412a4f216SLongpeng(Mike) }
8514a5a2aeSLongpeng(Mike) 
qcrypto_hmac_new(QCryptoHashAlgorithm alg,const uint8_t * key,size_t nkey,Error ** errp)8614a5a2aeSLongpeng(Mike) QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
8714a5a2aeSLongpeng(Mike)                               const uint8_t *key, size_t nkey,
8814a5a2aeSLongpeng(Mike)                               Error **errp)
8914a5a2aeSLongpeng(Mike) {
9014a5a2aeSLongpeng(Mike)     QCryptoHmac *hmac;
9142e7e15fSLongpeng(Mike)     void *ctx = NULL;
9242e7e15fSLongpeng(Mike)     QCryptoHmacDriver *drv = NULL;
9314a5a2aeSLongpeng(Mike) 
9442e7e15fSLongpeng(Mike) #ifdef CONFIG_AF_ALG
95*f1710638SLongpeng     ctx = qcrypto_afalg_hmac_ctx_new(alg, key, nkey, NULL);
9642e7e15fSLongpeng(Mike)     if (ctx) {
9742e7e15fSLongpeng(Mike)         drv = &qcrypto_hmac_afalg_driver;
9842e7e15fSLongpeng(Mike)     }
9942e7e15fSLongpeng(Mike) #endif
10042e7e15fSLongpeng(Mike) 
10142e7e15fSLongpeng(Mike)     if (!ctx) {
10214a5a2aeSLongpeng(Mike)         ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
10314a5a2aeSLongpeng(Mike)         if (!ctx) {
10414a5a2aeSLongpeng(Mike)             return NULL;
10514a5a2aeSLongpeng(Mike)         }
10614a5a2aeSLongpeng(Mike) 
10742e7e15fSLongpeng(Mike)         drv = &qcrypto_hmac_lib_driver;
10842e7e15fSLongpeng(Mike)     }
10942e7e15fSLongpeng(Mike) 
11014a5a2aeSLongpeng(Mike)     hmac = g_new0(QCryptoHmac, 1);
11114a5a2aeSLongpeng(Mike)     hmac->alg = alg;
11214a5a2aeSLongpeng(Mike)     hmac->opaque = ctx;
11342e7e15fSLongpeng(Mike)     hmac->driver = (void *)drv;
11414a5a2aeSLongpeng(Mike) 
11514a5a2aeSLongpeng(Mike)     return hmac;
11614a5a2aeSLongpeng(Mike) }
11714a5a2aeSLongpeng(Mike) 
qcrypto_hmac_free(QCryptoHmac * hmac)11814a5a2aeSLongpeng(Mike) void qcrypto_hmac_free(QCryptoHmac *hmac)
11914a5a2aeSLongpeng(Mike) {
12014a5a2aeSLongpeng(Mike)     QCryptoHmacDriver *drv;
12114a5a2aeSLongpeng(Mike) 
12214a5a2aeSLongpeng(Mike)     if (hmac) {
12314a5a2aeSLongpeng(Mike)         drv = hmac->driver;
12414a5a2aeSLongpeng(Mike)         drv->hmac_free(hmac);
12514a5a2aeSLongpeng(Mike)         g_free(hmac);
12614a5a2aeSLongpeng(Mike)     }
12714a5a2aeSLongpeng(Mike) }
128