| 1 | /* |
| 2 | * QEMU Crypto hmac algorithms (based on nettle) |
| 3 | * |
| 4 | * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Longpeng(Mike) <longpeng2@huawei.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 10 | * (at your option) any later version. See the COPYING file in the |
| 11 | * top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include "qemu/osdep.h" |
| 16 | #include "qapi/error.h" |
| 17 | #include "crypto/hmac.h" |
| 18 | #include "hmacpriv.h" |
| 19 | #include <nettle/hmac.h> |
| 20 | |
| 21 | typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx, |
| 22 | size_t key_length, |
| 23 | const uint8_t *key); |
| 24 | |
| 25 | typedef void (*qcrypto_nettle_hmac_update)(void *ctx, |
| 26 | size_t length, |
| 27 | const uint8_t *data); |
| 28 | |
| 29 | typedef void (*qcrypto_nettle_hmac_digest)(void *ctx, |
| 30 | size_t length, |
| 31 | uint8_t *digest); |
| 32 | |
| 33 | typedef struct QCryptoHmacNettle QCryptoHmacNettle; |
| 34 | struct QCryptoHmacNettle { |
| 35 | union qcrypto_nettle_hmac_ctx { |
| 36 | struct hmac_md5_ctx md5_ctx; |
| 37 | struct hmac_sha1_ctx sha1_ctx; |
| 38 | struct hmac_sha256_ctx sha256_ctx; /* equals hmac_sha224_ctx */ |
| 39 | struct hmac_sha512_ctx sha512_ctx; /* equals hmac_sha384_ctx */ |
| 40 | struct hmac_ripemd160_ctx ripemd160_ctx; |
| 41 | #ifdef CONFIG_CRYPTO_SM3 |
| 42 | struct hmac_sm3_ctx ctx; |
| 43 | #endif |
| 44 | } u; |
| 45 | }; |
| 46 | |
| 47 | struct qcrypto_nettle_hmac_alg { |
| 48 | qcrypto_nettle_hmac_setkey setkey; |
| 49 | qcrypto_nettle_hmac_update update; |
| 50 | qcrypto_nettle_hmac_digest digest; |
| 51 | size_t len; |
| 52 | } qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = { |
| 53 | [QCRYPTO_HASH_ALGO_MD5] = { |
| 54 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_md5_set_key, |
| 55 | .update = (qcrypto_nettle_hmac_update)hmac_md5_update, |
| 56 | .digest = (qcrypto_nettle_hmac_digest)hmac_md5_digest, |
| 57 | .len = MD5_DIGEST_SIZE, |
| 58 | }, |
| 59 | [QCRYPTO_HASH_ALGO_SHA1] = { |
| 60 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha1_set_key, |
| 61 | .update = (qcrypto_nettle_hmac_update)hmac_sha1_update, |
| 62 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha1_digest, |
| 63 | .len = SHA1_DIGEST_SIZE, |
| 64 | }, |
| 65 | [QCRYPTO_HASH_ALGO_SHA224] = { |
| 66 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha224_set_key, |
| 67 | .update = (qcrypto_nettle_hmac_update)hmac_sha224_update, |
| 68 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha224_digest, |
| 69 | .len = SHA224_DIGEST_SIZE, |
| 70 | }, |
| 71 | [QCRYPTO_HASH_ALGO_SHA256] = { |
| 72 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha256_set_key, |
| 73 | .update = (qcrypto_nettle_hmac_update)hmac_sha256_update, |
| 74 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha256_digest, |
| 75 | .len = SHA256_DIGEST_SIZE, |
| 76 | }, |
| 77 | [QCRYPTO_HASH_ALGO_SHA384] = { |
| 78 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha384_set_key, |
| 79 | .update = (qcrypto_nettle_hmac_update)hmac_sha384_update, |
| 80 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha384_digest, |
| 81 | .len = SHA384_DIGEST_SIZE, |
| 82 | }, |
| 83 | [QCRYPTO_HASH_ALGO_SHA512] = { |
| 84 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha512_set_key, |
| 85 | .update = (qcrypto_nettle_hmac_update)hmac_sha512_update, |
| 86 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha512_digest, |
| 87 | .len = SHA512_DIGEST_SIZE, |
| 88 | }, |
| 89 | [QCRYPTO_HASH_ALGO_RIPEMD160] = { |
| 90 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_ripemd160_set_key, |
| 91 | .update = (qcrypto_nettle_hmac_update)hmac_ripemd160_update, |
| 92 | .digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest, |
| 93 | .len = RIPEMD160_DIGEST_SIZE, |
| 94 | }, |
| 95 | #ifdef CONFIG_CRYPTO_SM3 |
| 96 | [QCRYPTO_HASH_ALGO_SM3] = { |
| 97 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sm3_set_key, |
| 98 | .update = (qcrypto_nettle_hmac_update)hmac_sm3_update, |
| 99 | .digest = (qcrypto_nettle_hmac_digest)hmac_sm3_digest, |
| 100 | .len = SM3_DIGEST_SIZE, |
| 101 | }, |
| 102 | #endif |
| 103 | }; |
| 104 | |
| 105 | bool qcrypto_hmac_supports(QCryptoHashAlgo alg) |
| 106 | { |
| 107 | if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) && |
| 108 | qcrypto_hmac_alg_map[alg].setkey != NULL) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | void *qcrypto_hmac_ctx_new(QCryptoHashAlgo alg, |
| 116 | const uint8_t *key, size_t nkey, |
| 117 | Error **errp) |
| 118 | { |
| 119 | QCryptoHmacNettle *ctx; |
| 120 | |
| 121 | if (!qcrypto_hmac_supports(alg)) { |
| 122 | error_setg(errp, "Unsupported hmac algorithm %s", |
| 123 | QCryptoHashAlgo_str(alg)); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | ctx = g_new0(QCryptoHmacNettle, 1); |
| 128 | |
| 129 | qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key); |
| 130 | |
| 131 | return ctx; |
| 132 | } |
| 133 | |
| 134 | static void |
| 135 | qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac) |
| 136 | { |
| 137 | QCryptoHmacNettle *ctx; |
| 138 | |
| 139 | ctx = hmac->opaque; |
| 140 | g_free(ctx); |
| 141 | } |
| 142 | |
| 143 | static int |
| 144 | qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac, |
| 145 | const struct iovec *iov, |
| 146 | size_t niov, |
| 147 | uint8_t **result, |
| 148 | size_t *resultlen, |
| 149 | Error **errp) |
| 150 | { |
| 151 | QCryptoHmacNettle *ctx; |
| 152 | size_t i; |
| 153 | |
| 154 | ctx = (QCryptoHmacNettle *)hmac->opaque; |
| 155 | |
| 156 | for (i = 0; i < niov; ++i) { |
| 157 | size_t len = iov[i].iov_len; |
| 158 | uint8_t *base = iov[i].iov_base; |
| 159 | while (len) { |
| 160 | size_t shortlen = MIN(len, UINT_MAX); |
| 161 | qcrypto_hmac_alg_map[hmac->alg].update(&ctx->u, len, base); |
| 162 | len -= shortlen; |
| 163 | base += len; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (resultlen == NULL) { |
| 168 | return 0; |
| 169 | } else if (*resultlen == 0) { |
| 170 | *resultlen = qcrypto_hmac_alg_map[hmac->alg].len; |
| 171 | *result = g_new0(uint8_t, *resultlen); |
| 172 | } else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) { |
| 173 | error_setg(errp, |
| 174 | "Result buffer size %zu is smaller than hash %zu", |
| 175 | *resultlen, qcrypto_hmac_alg_map[hmac->alg].len); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | QCryptoHmacDriver qcrypto_hmac_lib_driver = { |
| 185 | .hmac_bytesv = qcrypto_nettle_hmac_bytesv, |
| 186 | .hmac_free = qcrypto_nettle_hmac_ctx_free, |
| 187 | }; |