Add support for ML-DSA signature for EVM and IMA
Subsystems (4)
v3 cover Add support for ML-DSA signature for EVM and IMA
Based on IMA sigv3 type of signatures, add support for ML-DSA signature for EVM and IMA. Use the existing ML-DSA hashless signing mode (pure mode).
Stefan
v3:
- new patches 1/4 and 2/4
- addressed Mimi's comments on v2
v2:
- Dropped 1/3
- Using "none" as hash_algo in 2/2
Stefan Berger (4):
integrity: Check for NULL returned by asymmetric_key_public_key
integrity: Check that algo parameter is within valid range
integrity: Refactor asymmetric_verify for reusability
integrity: Add support for sigv3 verification using ML-DSA keys
security/integrity/digsig_asymmetric.c | 152 +++++++++++++++++++++---- 1 file changed, 131 insertions(+), 21 deletions(-)
base-commit: 82bbd447199ff1441031d2eaf9afe041550cf525
Signature · 1 line
2.53.0
PATCH v3 1/4 integrity: Check for NULL returned by asymmetric_key_public_key
Check for a NULL pointer returned by asymmetric_key_public_key and return -ENOKEY in this case.
--- security/integrity/digsig_asymmetric.c | 4 ++++ 1 file changed, 4 insertions(+)
security/integrity/digsig_asymmetric.c +4 -0
@@ -108,6 +108,10 @@ int asymmetric_verify(struct key *keyring, const char *sig, pks.hash_algo = hash_algo_name[hdr->hash_algo]; pk =asymmetric_key_public_key (key); +if (!pk) { + ret = -ENOKEY ; + goto out; + } pks.pkey_algo = pk->pkey_algo;if (!strcmp (pk->pkey_algo,"rsa" )) { pks.encoding ="pkcs1" ;
Signature · 1 line
2.53.0
PATCH v3 2/4 integrity: Check that algo parameter is within valid range
In reply to Stefan BergerCheck that the algo parameter passed to calc_file_id_hash is within valid range. Do this in asymmetric_verify_v3 since this value will also be passed to a hashless signature verification function from here.
--- security/integrity/digsig_asymmetric.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
security/integrity/digsig_asymmetric.c +4 -1
@@ -139,7 +139,7 @@ int asymmetric_verify(struct key *keyring, const char *sig,/* * calc_file_id_hash - calculate the hash of the ima_file_id struct data * @type: xattr type [enum evm_ima_xattr_type] -* @algo: hash algorithm [enum hash_algo] +* @algo: hash algorithm [enum hash_algo]; caller must ensure valid value * @digest: pointer to the digest to be hashed * @hash: (out) pointer to the hash *
@@ -187,6 +187,9 @@ int asymmetric_verify_v3(struct key *keyring, const char *sig, int siglen,struct ima_max_digest_data hash;int rc; +if (algo >=HASH_ALGO__LAST ) +return -ENOPKG ; + rc =calc_file_id_hash (hdr->type, algo, data, &hash);if (rc)return -EINVAL ;
Signature · 1 line
2.53.0
PATCH v3 3/4 integrity: Refactor asymmetric_verify for reusability
In reply to Stefan BergerRefactor asymmetric_verify for reusability. Have it call asymmetric_verify_common with the signature verification key and the public_key structure as parameters. sigv3 support for ML-DSA will need to check the public key type first to decide how to do the signature verification and therefore will have these parameters available for calling asymmetric_verify_common.
---
v3:
- added kernel-doc
- NULL pointer check on asymmetric_key_public_key return value
--- security/integrity/digsig_asymmetric.c | 62 ++++++++++++++++++-------- 1 file changed, 43 insertions(+), 19 deletions(-)
security/integrity/digsig_asymmetric.c +43 -19
@@ -79,18 +79,25 @@ static struct key *request_asymmetric_key(struct key *keyring, uint32_t keyid)return key; } -int asymmetric_verify (struct key *keyring,const char *sig, -int siglen,const char *data,int datalen) +/** +* asymmetric_verify_common -- sigv2 and sigv3 common verify function +* @key: The key to use for signature verification; caller must free it +* @pk: The associated public key; must not be NULL +* @sig: The xattr signature +* @siglen: The length of the xattr signature; must be at least +* sizeof(struct signature_v2_hdr) +* @data: The data to verify the signature on +* @datalen: Length of @data +*/ +static int asymmetric_verify_common (const struct key *key, +const struct public_key *pk, +const char *sig,int siglen, +const char *data,int datalen) { -struct public_key_signature pks;struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig; -const struct public_key *pk; -struct key *key; +struct public_key_signature pks;int ret; -if (siglen <=sizeof (*hdr)) -return -EBADMSG ; - siglen -=sizeof (*hdr);if (siglen !=be16_to_cpu (hdr->sig_size))
@@ -99,19 +106,9 @@ int asymmetric_verify(struct key *keyring, const char *sig,if (hdr->hash_algo >=HASH_ALGO__LAST )return -ENOPKG ; - key =request_asymmetric_key (keyring,be32_to_cpu (hdr->keyid)); -if (IS_ERR (key)) -return PTR_ERR (key); -memset (&pks,0 ,sizeof (pks)); pks.hash_algo = hash_algo_name[hdr->hash_algo]; - - pk =asymmetric_key_public_key (key); -if (!pk) { - ret = -ENOKEY ; - goto out; - } pks.pkey_algo = pk->pkey_algo;if (!strcmp (pk->pkey_algo,"rsa" )) { pks.encoding ="pkcs1" ;
@@ -131,11 +128,38 @@ int asymmetric_verify(struct key *keyring, const char *sig, pks.s_size = siglen; ret =verify_signature (key, &pks); out: -key_put (key);pr_debug ("%s() = %d\n" , __func__, ret);return ret; } +int asymmetric_verify (struct key *keyring,const char *sig, +int siglen,const char *data,int datalen) +{ +struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig; +const struct public_key *pk; +struct key *key; +int ret; + +if (siglen <=sizeof (*hdr)) +return -EBADMSG ; + + key =request_asymmetric_key (keyring,be32_to_cpu (hdr->keyid)); +if (IS_ERR (key)) +return PTR_ERR (key); + pk =asymmetric_key_public_key (key); +if (!pk) { + ret = -ENOKEY ; + goto out; + } + + ret =asymmetric_verify_common (key, pk, sig, siglen, data, datalen); + +out: +key_put (key); + +return ret; +} +/* * calc_file_id_hash - calculate the hash of the ima_file_id struct data * @type: xattr type [enum evm_ima_xattr_type]
Signature · 1 line
2.53.0
PATCH v3 4/4 integrity: Add support for sigv3 verification using ML-DSA keys
In reply to Stefan BergerAdd support for sigv3 signature verification using ML-DSA in pure mode. When a sigv3 signature is verified, first check whether the key to use for verification is an ML-DSA key and therefore uses a hashless signature verification scheme. The hashless signature verification method uses the ima_file_id structure directly for signature verification rather than its digest.
---
v3:
- Renamed err_exit label to 'out'
- Updated kernel-doc for new function
- Relying on algo verified by caller of asymmetric_verify_v3_hashless
- NULL pointer check on asymmetric_key_public_key return value
v2: Set hash_algo in public_key_signature to "none"
--- security/integrity/digsig_asymmetric.c | 89 ++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 5 deletions(-)
security/integrity/digsig_asymmetric.c +84 -5
@@ -204,20 +204,99 @@ static int calc_file_id_hash(enum evm_ima_xattr_type type,return rc; } +/** +* asymmetric_verify_v3_hashless - Use hashless signature verification on sigv3 +* @key: The key to use for signature verification; caller must free it +* @pk: The associated public key; must not be NULL +* @encoding: The encoding the key type uses +* @sig: The xattr signature +* @siglen: The length of the xattr signature; must be at least +* sizeof(struct signature_v2_hdr) +* @algo: hash algorithm [enum hash_algo]; caller must ensure valid value +* @digest: The file digest +* +* Create an ima_file_id structure and use it for signature verification +* directly. This can be used for ML-DSA in pure mode for example. +*/ +static int asymmetric_verify_v3_hashless (struct key *key, +const struct public_key *pk, +const char *encoding, +const char *sig,int siglen, +u8 algo, +const u8 *digest) +{ +struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig; +struct ima_file_id file_id = { + .hash_type = hdr->type, + .hash_algorithm = algo, + }; +size_t digest_size = hash_digest_size[algo]; +struct public_key_signature pks = { + .m = (u8 *)&file_id, + .m_size =sizeof (file_id) - (HASH_MAX_DIGESTSIZE - digest_size), + .s = hdr->sig, + .s_size = siglen -sizeof (*hdr), + .pkey_algo = pk->pkey_algo, + .hash_algo ="none" , + .encoding = encoding, + }; +int ret; + +if (hdr->type !=IMA_VERITY_DIGSIG && + hdr->type !=EVM_IMA_XATTR_DIGSIG && + hdr->type !=EVM_XATTR_PORTABLE_DIGSIG ) +return -EINVAL ; + +if (pks.s_size !=be16_to_cpu (hdr->sig_size)) +return -EBADMSG ; + +memcpy (file_id.hash, digest, digest_size); + + ret =verify_signature (key, &pks); +pr_debug ("%s() = %d\n" , __func__, ret); +return ret; +} +int asymmetric_verify_v3 (struct key *keyring,const char *sig,int siglen,const char *data,int datalen,u8 algo) {struct signature_v2_hdr *hdr = (struct signature_v2_hdr *)sig;struct ima_max_digest_data hash; +const struct public_key *pk; +struct key *key;int rc;if (algo >=HASH_ALGO__LAST )return -ENOPKG ; - rc =calc_file_id_hash (hdr->type, algo, data, &hash); -if (rc) -return -EINVAL ; +if (siglen <=sizeof (*hdr)) +return -EBADMSG ; -return asymmetric_verify (keyring, sig, siglen, hash.digest, - hash.hdr.length); + key =request_asymmetric_key (keyring,be32_to_cpu (hdr->keyid)); +if (IS_ERR (key)) +return PTR_ERR (key); + + pk =asymmetric_key_public_key (key); +if (!pk) { + rc = -ENOKEY ; + goto out; + } +if (!strncmp (pk->pkey_algo,"mldsa" ,5 )) { + rc =asymmetric_verify_v3_hashless (key, pk,"raw" , + sig, siglen, algo, data); + }else { + rc =calc_file_id_hash (hdr->type, algo, data, &hash); +if (rc) { + rc = -EINVAL ; + goto out; + } + + rc =asymmetric_verify_common (key, pk, sig, siglen, hash.digest, + hash.hdr.length); + } + +out: +key_put (key); + +return rc; }
Signature · 1 line
2.53.0
Quoted 19 lines · Stefan Berger
Based on IMA sigv3 type of signatures, add support for ML-DSA signature for EVM and IMA. Use the existing ML-DSA hashless signing mode (pure mode).
Stefan
v3:
- new patches 1/4 and 2/4
- addressed Mimi's comments on v2v2:
- Dropped 1/3
- Using "none" ashash_algoin 2/2Stefan Berger (4):
integrity: Check for NULL returned byasymmetric_key_public_key
integrity: Check that algo parameter is within valid range
integrity: Refactorasymmetric_verifyfor reusability
integrity: Add support for sigv3 verification using ML-DSA keys
Quoted 8 lines
security/integrity/digsig_asymmetric.c | 152 +++++++++++++++++++++---- 1 file changed, 131 insertions(+), 21 deletions(-)base-commit:
82bbd447199ff1441031d2eaf9afe041550cf525Signature · 1 line
2.53.0
Hi Stefan,
I have tested this patch series on x86_64 and IMA signature v3 appraisal works correctly with ML-DSA keys and appraise_type=sigv3. Signature v3 works with appraise_type=imasig also and I think it is intentional for backward compatibility of ima policies.