| 1 | /* |
| 2 | * QEMU Crypto hash algorithms |
| 3 | * |
| 4 | * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates |
| 5 | * Copyright (c) 2016 Red Hat, Inc. |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "qemu/osdep.h" |
| 23 | #include "qapi/error.h" |
| 24 | #include "crypto/hash.h" |
| 25 | #include "hashpriv.h" |
| 26 | #include <nettle/md5.h> |
| 27 | #include <nettle/sha1.h> |
| 28 | #include <nettle/sha2.h> |
| 29 | #include <nettle/ripemd160.h> |
| 30 | #ifdef CONFIG_CRYPTO_SM3 |
| 31 | #include <nettle/sm3.h> |
| 32 | #endif |
| 33 | |
| 34 | typedef void (*qcrypto_nettle_init)(void *ctx); |
| 35 | typedef void (*qcrypto_nettle_write)(void *ctx, |
| 36 | size_t len, |
| 37 | const uint8_t *buf); |
| 38 | typedef void (*qcrypto_nettle_result)(void *ctx, |
| 39 | size_t len, |
| 40 | uint8_t *buf); |
| 41 | |
| 42 | union qcrypto_hash_ctx { |
| 43 | struct md5_ctx md5; |
| 44 | struct sha1_ctx sha1; |
| 45 | struct sha224_ctx sha224; |
| 46 | struct sha256_ctx sha256; |
| 47 | struct sha384_ctx sha384; |
| 48 | struct sha512_ctx sha512; |
| 49 | struct ripemd160_ctx ripemd160; |
| 50 | #ifdef CONFIG_CRYPTO_SM3 |
| 51 | struct sm3_ctx sm3; |
| 52 | #endif |
| 53 | }; |
| 54 | |
| 55 | struct qcrypto_hash_alg { |
| 56 | qcrypto_nettle_init init; |
| 57 | qcrypto_nettle_write write; |
| 58 | qcrypto_nettle_result result; |
| 59 | size_t len; |
| 60 | } qcrypto_hash_alg_map[] = { |
| 61 | [QCRYPTO_HASH_ALGO_MD5] = { |
| 62 | .init = (qcrypto_nettle_init)md5_init, |
| 63 | .write = (qcrypto_nettle_write)md5_update, |
| 64 | .result = (qcrypto_nettle_result)md5_digest, |
| 65 | .len = MD5_DIGEST_SIZE, |
| 66 | }, |
| 67 | [QCRYPTO_HASH_ALGO_SHA1] = { |
| 68 | .init = (qcrypto_nettle_init)sha1_init, |
| 69 | .write = (qcrypto_nettle_write)sha1_update, |
| 70 | .result = (qcrypto_nettle_result)sha1_digest, |
| 71 | .len = SHA1_DIGEST_SIZE, |
| 72 | }, |
| 73 | [QCRYPTO_HASH_ALGO_SHA224] = { |
| 74 | .init = (qcrypto_nettle_init)sha224_init, |
| 75 | .write = (qcrypto_nettle_write)sha224_update, |
| 76 | .result = (qcrypto_nettle_result)sha224_digest, |
| 77 | .len = SHA224_DIGEST_SIZE, |
| 78 | }, |
| 79 | [QCRYPTO_HASH_ALGO_SHA256] = { |
| 80 | .init = (qcrypto_nettle_init)sha256_init, |
| 81 | .write = (qcrypto_nettle_write)sha256_update, |
| 82 | .result = (qcrypto_nettle_result)sha256_digest, |
| 83 | .len = SHA256_DIGEST_SIZE, |
| 84 | }, |
| 85 | [QCRYPTO_HASH_ALGO_SHA384] = { |
| 86 | .init = (qcrypto_nettle_init)sha384_init, |
| 87 | .write = (qcrypto_nettle_write)sha384_update, |
| 88 | .result = (qcrypto_nettle_result)sha384_digest, |
| 89 | .len = SHA384_DIGEST_SIZE, |
| 90 | }, |
| 91 | [QCRYPTO_HASH_ALGO_SHA512] = { |
| 92 | .init = (qcrypto_nettle_init)sha512_init, |
| 93 | .write = (qcrypto_nettle_write)sha512_update, |
| 94 | .result = (qcrypto_nettle_result)sha512_digest, |
| 95 | .len = SHA512_DIGEST_SIZE, |
| 96 | }, |
| 97 | [QCRYPTO_HASH_ALGO_RIPEMD160] = { |
| 98 | .init = (qcrypto_nettle_init)ripemd160_init, |
| 99 | .write = (qcrypto_nettle_write)ripemd160_update, |
| 100 | .result = (qcrypto_nettle_result)ripemd160_digest, |
| 101 | .len = RIPEMD160_DIGEST_SIZE, |
| 102 | }, |
| 103 | #ifdef CONFIG_CRYPTO_SM3 |
| 104 | [QCRYPTO_HASH_ALGO_SM3] = { |
| 105 | .init = (qcrypto_nettle_init)sm3_init, |
| 106 | .write = (qcrypto_nettle_write)sm3_update, |
| 107 | .result = (qcrypto_nettle_result)sm3_digest, |
| 108 | .len = SM3_DIGEST_SIZE, |
| 109 | }, |
| 110 | #endif |
| 111 | }; |
| 112 | |
| 113 | gboolean qcrypto_hash_supports(QCryptoHashAlgo alg) |
| 114 | { |
| 115 | if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) && |
| 116 | qcrypto_hash_alg_map[alg].init != NULL) { |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | static |
| 123 | QCryptoHash *qcrypto_nettle_hash_new(QCryptoHashAlgo alg, Error **errp) |
| 124 | { |
| 125 | QCryptoHash *hash; |
| 126 | |
| 127 | hash = g_new(QCryptoHash, 1); |
| 128 | hash->alg = alg; |
| 129 | hash->opaque = g_new(union qcrypto_hash_ctx, 1); |
| 130 | |
| 131 | qcrypto_hash_alg_map[alg].init(hash->opaque); |
| 132 | return hash; |
| 133 | } |
| 134 | |
| 135 | static |
| 136 | void qcrypto_nettle_hash_free(QCryptoHash *hash) |
| 137 | { |
| 138 | union qcrypto_hash_ctx *ctx = hash->opaque; |
| 139 | |
| 140 | g_free(ctx); |
| 141 | g_free(hash); |
| 142 | } |
| 143 | |
| 144 | static |
| 145 | int qcrypto_nettle_hash_update(QCryptoHash *hash, |
| 146 | const struct iovec *iov, |
| 147 | size_t niov, |
| 148 | Error **errp) |
| 149 | { |
| 150 | union qcrypto_hash_ctx *ctx = hash->opaque; |
| 151 | |
| 152 | for (int i = 0; i < niov; i++) { |
| 153 | qcrypto_hash_alg_map[hash->alg].write(ctx, |
| 154 | iov[i].iov_len, |
| 155 | iov[i].iov_base); |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static |
| 162 | int qcrypto_nettle_hash_finalize(QCryptoHash *hash, |
| 163 | uint8_t **result, |
| 164 | size_t *result_len, |
| 165 | Error **errp) |
| 166 | { |
| 167 | union qcrypto_hash_ctx *ctx = hash->opaque; |
| 168 | int ret = qcrypto_hash_alg_map[hash->alg].len; |
| 169 | |
| 170 | if (*result_len == 0) { |
| 171 | *result_len = ret; |
| 172 | *result = g_new(uint8_t, *result_len); |
| 173 | } else if (*result_len != ret) { |
| 174 | error_setg(errp, |
| 175 | "Result buffer size %zu is smaller than hash %d", |
| 176 | *result_len, ret); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | qcrypto_hash_alg_map[hash->alg].result(ctx, *result_len, *result); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | QCryptoHashDriver qcrypto_hash_lib_driver = { |
| 186 | .hash_new = qcrypto_nettle_hash_new, |
| 187 | .hash_update = qcrypto_nettle_hash_update, |
| 188 | .hash_finalize = qcrypto_nettle_hash_finalize, |
| 189 | .hash_free = qcrypto_nettle_hash_free, |
| 190 | }; |