crypto: Introduce SM3 hash hmac pbkdf algorithm

Introduce the SM3 cryptographic hash algorithm (GB/T 32905-2016).

SM3 (GB/T 32905-2016) is a cryptographic standard issued by the
Organization of State Commercial Cryptography Administration (OSCCA)
as an authorized cryptographic algorithm for use within China.

Detect the SM3 cryptographic hash algorithm and enable the feature silently
if it is available.

Signed-off-by: cheliequan <cheliequan@inspur.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
liequan che 2024-10-30 08:51:46 +00:00 committed by Daniel P. Berrangé
parent 62eb377e0a
commit d078da86d6
13 changed files with 135 additions and 1 deletions

View file

@ -34,6 +34,9 @@ static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
[QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
[QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
[QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
#ifdef CONFIG_CRYPTO_SM3
[QCRYPTO_HASH_ALGO_SM3] = GCRY_MD_SM3,
#endif
};
gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)

View file

@ -26,6 +26,9 @@
#include <nettle/md5.h>
#include <nettle/sha.h>
#include <nettle/ripemd160.h>
#ifdef CONFIG_CRYPTO_SM3
#include <nettle/sm3.h>
#endif
typedef void (*qcrypto_nettle_init)(void *ctx);
typedef void (*qcrypto_nettle_write)(void *ctx,
@ -43,6 +46,9 @@ union qcrypto_hash_ctx {
struct sha384_ctx sha384;
struct sha512_ctx sha512;
struct ripemd160_ctx ripemd160;
#ifdef CONFIG_CRYPTO_SM3
struct sm3_ctx sm3;
#endif
};
struct qcrypto_hash_alg {
@ -93,6 +99,14 @@ struct qcrypto_hash_alg {
.result = (qcrypto_nettle_result)ripemd160_digest,
.len = RIPEMD160_DIGEST_SIZE,
},
#ifdef CONFIG_CRYPTO_SM3
[QCRYPTO_HASH_ALGO_SM3] = {
.init = (qcrypto_nettle_init)sm3_init,
.write = (qcrypto_nettle_write)sm3_update,
.result = (qcrypto_nettle_result)sm3_digest,
.len = SM3_DIGEST_SIZE,
},
#endif
};
gboolean qcrypto_hash_supports(QCryptoHashAlgo alg)

View file

@ -33,6 +33,9 @@ static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALGO__MAX] = {
[QCRYPTO_HASH_ALGO_SHA384] = QCRYPTO_HASH_DIGEST_LEN_SHA384,
[QCRYPTO_HASH_ALGO_SHA512] = QCRYPTO_HASH_DIGEST_LEN_SHA512,
[QCRYPTO_HASH_ALGO_RIPEMD160] = QCRYPTO_HASH_DIGEST_LEN_RIPEMD160,
#ifdef CONFIG_CRYPTO_SM3
[QCRYPTO_HASH_ALGO_SM3] = QCRYPTO_HASH_DIGEST_LEN_SM3,
#endif
};
size_t qcrypto_hash_digest_len(QCryptoHashAlgo alg)

View file

@ -26,6 +26,9 @@ static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = {
[QCRYPTO_HASH_ALGO_SHA384] = GCRY_MAC_HMAC_SHA384,
[QCRYPTO_HASH_ALGO_SHA512] = GCRY_MAC_HMAC_SHA512,
[QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MAC_HMAC_RMD160,
#ifdef CONFIG_CRYPTO_SM3
[QCRYPTO_HASH_ALGO_SM3] = GCRY_MAC_HMAC_SM3,
#endif
};
typedef struct QCryptoHmacGcrypt QCryptoHmacGcrypt;

View file

@ -38,6 +38,9 @@ struct QCryptoHmacNettle {
struct hmac_sha256_ctx sha256_ctx; /* equals hmac_sha224_ctx */
struct hmac_sha512_ctx sha512_ctx; /* equals hmac_sha384_ctx */
struct hmac_ripemd160_ctx ripemd160_ctx;
#ifdef CONFIG_CRYPTO_SM3
struct hmac_sm3_ctx ctx;
#endif
} u;
};
@ -89,6 +92,14 @@ struct qcrypto_nettle_hmac_alg {
.digest = (qcrypto_nettle_hmac_digest)hmac_ripemd160_digest,
.len = RIPEMD160_DIGEST_SIZE,
},
#ifdef CONFIG_CRYPTO_SM3
[QCRYPTO_HASH_ALGO_SM3] = {
.setkey = (qcrypto_nettle_hmac_setkey)hmac_sm3_set_key,
.update = (qcrypto_nettle_hmac_update)hmac_sm3_update,
.digest = (qcrypto_nettle_hmac_digest)hmac_sm3_digest,
.len = SM3_DIGEST_SIZE,
},
#endif
};
bool qcrypto_hmac_supports(QCryptoHashAlgo alg)

View file

@ -33,6 +33,9 @@ bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)
case QCRYPTO_HASH_ALGO_SHA384:
case QCRYPTO_HASH_ALGO_SHA512:
case QCRYPTO_HASH_ALGO_RIPEMD160:
#ifdef CONFIG_CRYPTO_SM3
case QCRYPTO_HASH_ALGO_SM3:
#endif
return qcrypto_hash_supports(hash);
default:
return false;
@ -54,6 +57,9 @@ int qcrypto_pbkdf2(QCryptoHashAlgo hash,
[QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
[QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
[QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
#ifdef CONFIG_CRYPTO_SM3
[QCRYPTO_HASH_ALGO_SM3] = GCRY_MD_SM3,
#endif
};
int ret;

View file

@ -34,6 +34,9 @@ bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)
case QCRYPTO_HASH_ALGO_SHA384:
case QCRYPTO_HASH_ALGO_SHA512:
case QCRYPTO_HASH_ALGO_RIPEMD160:
#ifdef CONFIG_CRYPTO_SM3
case QCRYPTO_HASH_ALGO_SM3:
#endif
return true;
default:
return false;
@ -55,6 +58,9 @@ int qcrypto_pbkdf2(QCryptoHashAlgo hash,
struct hmac_sha384_ctx sha384;
struct hmac_sha512_ctx sha512;
struct hmac_ripemd160_ctx ripemd160;
#ifdef CONFIG_CRYPTO_SM3
struct hmac_sm3_ctx sm3;
#endif
} ctx;
if (iterations > UINT_MAX) {
@ -106,6 +112,13 @@ int qcrypto_pbkdf2(QCryptoHashAlgo hash,
PBKDF2(&ctx.ripemd160, hmac_ripemd160_update, hmac_ripemd160_digest,
RIPEMD160_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
break;
#ifdef CONFIG_CRYPTO_SM3
case QCRYPTO_HASH_ALGO_SM3:
hmac_sm3_set_key(&ctx.sm3, nkey, key);
PBKDF2(&ctx.sm3, hmac_sm3_update, hmac_sm3_digest,
SM3_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
break;
#endif
default:
error_setg_errno(errp, ENOSYS,