Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 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" |
Longpeng(Mike) | 14a5a2a | 2017-07-14 14:04:04 -0400 | [diff] [blame] | 18 | #include "hmacpriv.h" |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 19 | #include <nettle/hmac.h> |
| 20 | |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 21 | typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx, |
| 22 | size_t key_length, const uint8_t *key); |
| 23 | |
| 24 | typedef void (*qcrypto_nettle_hmac_update)(void *ctx, |
| 25 | size_t length, const uint8_t *data); |
| 26 | |
| 27 | typedef void (*qcrypto_nettle_hmac_digest)(void *ctx, |
| 28 | size_t length, uint8_t *digest); |
| 29 | |
| 30 | typedef struct QCryptoHmacNettle QCryptoHmacNettle; |
| 31 | struct QCryptoHmacNettle { |
| 32 | union qcrypto_nettle_hmac_ctx { |
| 33 | struct hmac_md5_ctx md5_ctx; |
| 34 | struct hmac_sha1_ctx sha1_ctx; |
| 35 | struct hmac_sha256_ctx sha256_ctx; /* equals hmac_sha224_ctx */ |
| 36 | struct hmac_sha512_ctx sha512_ctx; /* equals hmac_sha384_ctx */ |
| 37 | struct hmac_ripemd160_ctx ripemd160_ctx; |
| 38 | } u; |
| 39 | }; |
| 40 | |
| 41 | struct qcrypto_nettle_hmac_alg { |
| 42 | qcrypto_nettle_hmac_setkey setkey; |
| 43 | qcrypto_nettle_hmac_update update; |
| 44 | qcrypto_nettle_hmac_digest digest; |
| 45 | size_t len; |
| 46 | } qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = { |
| 47 | [QCRYPTO_HASH_ALG_MD5] = { |
| 48 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_md5_set_key, |
| 49 | .update = (qcrypto_nettle_hmac_update)hmac_md5_update, |
| 50 | .digest = (qcrypto_nettle_hmac_digest)hmac_md5_digest, |
| 51 | .len = MD5_DIGEST_SIZE, |
| 52 | }, |
| 53 | [QCRYPTO_HASH_ALG_SHA1] = { |
| 54 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha1_set_key, |
| 55 | .update = (qcrypto_nettle_hmac_update)hmac_sha1_update, |
| 56 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha1_digest, |
| 57 | .len = SHA1_DIGEST_SIZE, |
| 58 | }, |
| 59 | [QCRYPTO_HASH_ALG_SHA224] = { |
| 60 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha224_set_key, |
| 61 | .update = (qcrypto_nettle_hmac_update)hmac_sha224_update, |
| 62 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha224_digest, |
| 63 | .len = SHA224_DIGEST_SIZE, |
| 64 | }, |
| 65 | [QCRYPTO_HASH_ALG_SHA256] = { |
| 66 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha256_set_key, |
| 67 | .update = (qcrypto_nettle_hmac_update)hmac_sha256_update, |
| 68 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha256_digest, |
| 69 | .len = SHA256_DIGEST_SIZE, |
| 70 | }, |
| 71 | [QCRYPTO_HASH_ALG_SHA384] = { |
| 72 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha384_set_key, |
| 73 | .update = (qcrypto_nettle_hmac_update)hmac_sha384_update, |
| 74 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha384_digest, |
| 75 | .len = SHA384_DIGEST_SIZE, |
| 76 | }, |
| 77 | [QCRYPTO_HASH_ALG_SHA512] = { |
| 78 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_sha512_set_key, |
| 79 | .update = (qcrypto_nettle_hmac_update)hmac_sha512_update, |
| 80 | .digest = (qcrypto_nettle_hmac_digest)hmac_sha512_digest, |
| 81 | .len = SHA512_DIGEST_SIZE, |
| 82 | }, |
| 83 | [QCRYPTO_HASH_ALG_RIPEMD160] = { |
| 84 | .setkey = (qcrypto_nettle_hmac_setkey)hmac_ripemd160_set_key, |
| 85 | .update = (qcrypto_nettle_hmac_update)hmac_ripemd160_update, |
| 86 | .digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest, |
| 87 | .len = RIPEMD160_DIGEST_SIZE, |
| 88 | }, |
| 89 | }; |
| 90 | |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 91 | bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) |
| 92 | { |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 93 | if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) && |
| 94 | qcrypto_hmac_alg_map[alg].setkey != NULL) { |
| 95 | return true; |
| 96 | } |
| 97 | |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
Longpeng(Mike) | 14a5a2a | 2017-07-14 14:04:04 -0400 | [diff] [blame] | 101 | void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, |
| 102 | const uint8_t *key, size_t nkey, |
| 103 | Error **errp) |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 104 | { |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 105 | QCryptoHmacNettle *ctx; |
| 106 | |
| 107 | if (!qcrypto_hmac_supports(alg)) { |
| 108 | error_setg(errp, "Unsupported hmac algorithm %s", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 109 | QCryptoHashAlgorithm_str(alg)); |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 113 | ctx = g_new0(QCryptoHmacNettle, 1); |
| 114 | |
| 115 | qcrypto_hmac_alg_map[alg].setkey(&ctx->u, nkey, key); |
| 116 | |
Longpeng(Mike) | 8c2776d | 2017-07-14 14:04:02 -0400 | [diff] [blame] | 117 | return ctx; |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Longpeng(Mike) | 14a5a2a | 2017-07-14 14:04:04 -0400 | [diff] [blame] | 120 | static void |
| 121 | qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac) |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 122 | { |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 123 | QCryptoHmacNettle *ctx; |
| 124 | |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 125 | ctx = hmac->opaque; |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 126 | g_free(ctx); |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Longpeng(Mike) | 14a5a2a | 2017-07-14 14:04:04 -0400 | [diff] [blame] | 129 | static int |
| 130 | qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac, |
| 131 | const struct iovec *iov, |
| 132 | size_t niov, |
| 133 | uint8_t **result, |
| 134 | size_t *resultlen, |
| 135 | Error **errp) |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 136 | { |
Longpeng(Mike) | f4d7674 | 2016-12-13 18:42:59 +0800 | [diff] [blame] | 137 | QCryptoHmacNettle *ctx; |
| 138 | int i; |
| 139 | |
| 140 | ctx = (QCryptoHmacNettle *)hmac->opaque; |
| 141 | |
| 142 | for (i = 0; i < niov; ++i) { |
| 143 | size_t len = iov[i].iov_len; |
| 144 | uint8_t *base = iov[i].iov_base; |
| 145 | while (len) { |
| 146 | size_t shortlen = MIN(len, UINT_MAX); |
| 147 | qcrypto_hmac_alg_map[hmac->alg].update(&ctx->u, len, base); |
| 148 | len -= shortlen; |
| 149 | base += len; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (*resultlen == 0) { |
| 154 | *resultlen = qcrypto_hmac_alg_map[hmac->alg].len; |
| 155 | *result = g_new0(uint8_t, *resultlen); |
| 156 | } else if (*resultlen != qcrypto_hmac_alg_map[hmac->alg].len) { |
| 157 | error_setg(errp, |
| 158 | "Result buffer size %zu is smaller than hash %zu", |
| 159 | *resultlen, qcrypto_hmac_alg_map[hmac->alg].len); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | qcrypto_hmac_alg_map[hmac->alg].digest(&ctx->u, *resultlen, *result); |
| 164 | |
| 165 | return 0; |
Longpeng(Mike) | 12a4f21 | 2016-12-13 18:42:56 +0800 | [diff] [blame] | 166 | } |
Longpeng(Mike) | 8c2776d | 2017-07-14 14:04:02 -0400 | [diff] [blame] | 167 | |
Longpeng(Mike) | 14a5a2a | 2017-07-14 14:04:04 -0400 | [diff] [blame] | 168 | QCryptoHmacDriver qcrypto_hmac_lib_driver = { |
| 169 | .hmac_bytesv = qcrypto_nettle_hmac_bytesv, |
| 170 | .hmac_free = qcrypto_nettle_hmac_ctx_free, |
| 171 | }; |