Skip to content

Fixed BUG、CMS: Add SM3WithSM2、Improve ZUC128 and add ZUC256 #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crypto/src/cms/CMSSignedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
Expand Down Expand Up @@ -57,6 +58,7 @@ public abstract class CmsSignedGenerator
public static readonly string DigestSha512 = NistObjectIdentifiers.IdSha512.Id;
public static readonly string DigestSha512_224 = NistObjectIdentifiers.IdSha512_224.Id;
public static readonly string DigestSha512_256 = NistObjectIdentifiers.IdSha512_256.Id;
public static readonly string DigestSM3 = GMObjectIdentifiers.sm3.Id;

public static readonly string DigestSha3_224 = NistObjectIdentifiers.IdSha3_224.Id;
public static readonly string DigestSha3_256 = NistObjectIdentifiers.IdSha3_256.Id;
Expand All @@ -77,6 +79,7 @@ public abstract class CmsSignedGenerator
public static readonly string EncryptionECGost3410 = CryptoProObjectIdentifiers.GostR3410x2001.Id;
public static readonly string EncryptionECGost3410_2012_256 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256.Id;
public static readonly string EncryptionECGost3410_2012_512 = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512.Id;
public static readonly string EncryptionSM2 = GMObjectIdentifiers.sm2encrypt.Id;

internal List<Asn1Encodable> _certs = new List<Asn1Encodable>();
internal List<Asn1Encodable> _crls = new List<Asn1Encodable>();
Expand Down
7 changes: 7 additions & 0 deletions crypto/src/cms/CMSSignedHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Eac;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
Expand Down Expand Up @@ -109,6 +110,8 @@ static CmsSignedHelper()
AddEntries(RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_256, "GOST3411-2012-256", "ECGOST3410");
AddEntries(RosstandartObjectIdentifiers.id_tc26_signwithdigest_gost_3410_12_512, "GOST3411-2012-512", "ECGOST3410");

AddEntries(GMObjectIdentifiers.sm2sign_with_sm3, "SM3", "SM2");

m_encryptionAlgs.Add(X9ObjectIdentifiers.IdDsa, "DSA");
m_encryptionAlgs.Add(PkcsObjectIdentifiers.RsaEncryption, "RSA");
m_encryptionAlgs.Add(TeleTrusTObjectIdentifiers.TeleTrusTRsaSignatureAlgorithm, "RSA");
Expand All @@ -121,6 +124,7 @@ static CmsSignedHelper()
m_encryptionAlgs.Add(new DerObjectIdentifier("1.3.6.1.4.1.5849.1.6.2"), "ECGOST3410");
m_encryptionAlgs.Add(new DerObjectIdentifier("1.3.6.1.4.1.5849.1.1.5"), "GOST3410");
m_encryptionAlgs.Add(X9ObjectIdentifiers.IdECPublicKey, "ECDSA");
m_encryptionAlgs.Add(GMObjectIdentifiers.sm2p256v1, "SM2");

m_digestAlgs.Add(PkcsObjectIdentifiers.MD2, "MD2");
m_digestAlgs.Add(PkcsObjectIdentifiers.MD4, "MD4");
Expand All @@ -143,6 +147,7 @@ static CmsSignedHelper()
m_digestAlgs.Add(new DerObjectIdentifier("1.3.6.1.4.1.5849.1.2.1"), "GOST3411");
m_digestAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256, "GOST3411-2012-256");
m_digestAlgs.Add(RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512, "GOST3411-2012-512");
m_digestAlgs.Add(GMObjectIdentifiers.sm3, "SM3");

m_digestAliases.Add("SHA1", new string[]{ "SHA-1" });
m_digestAliases.Add("SHA224", new string[]{ "SHA-224" });
Expand All @@ -160,6 +165,7 @@ static CmsSignedHelper()
m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha3_256, NistObjectIdentifiers.IdEcdsaWithSha3_256);
m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha3_384, NistObjectIdentifiers.IdEcdsaWithSha3_384);
m_ecAlgorithms.Add(CmsSignedGenerator.DigestSha3_512, NistObjectIdentifiers.IdEcdsaWithSha3_512);
m_ecAlgorithms.Add(CmsSignedGenerator.DigestSM3, GMObjectIdentifiers.sm2sign_with_sm3);

m_noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
m_noParams.Add(OiwObjectIdentifiers.DsaWithSha1);
Expand Down Expand Up @@ -194,6 +200,7 @@ static CmsSignedHelper()
m_noParams.Add(BsiObjectIdentifiers.ecdsa_plain_SHA3_256);
m_noParams.Add(BsiObjectIdentifiers.ecdsa_plain_SHA3_384);
m_noParams.Add(BsiObjectIdentifiers.ecdsa_plain_SHA3_512);
m_noParams.Add(GMObjectIdentifiers.sm2sign_with_sm3);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions crypto/src/cms/SignerInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Cms;
using Org.BouncyCastle.Asn1.EdEC;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
Expand Down Expand Up @@ -320,6 +321,25 @@ private bool DoVerify(AsymmetricKeyParameter publicKey)
}

sig = SignerUtilities.GetSigner(sigAlgOid);
}
else if (GMObjectIdentifiers.sm2p256v1.Equals(sigAlgOid))
{
if (sigAlgParams != null && sigAlgParams != DerNull.Instance)
throw new CmsException("SM2 signature cannot specify algorithm parameters");

if (signedAttributeSet == null)
{
digestName = null;
}
else
{
if (!GMObjectIdentifiers.sm3.Equals(digAlgOid) || (digAlgParams != null && digAlgParams != DerNull.Instance))
throw new CmsException("SM2 signature used with unsupported digest algorithm");

digestName = CmsSignedHelper.GetDigestAlgName(digAlgOid);
}
string signatureName = digestName + "with" + CmsSignedHelper.GetEncryptionAlgName(sigAlgOid);
sig = SignerUtilities.GetSigner(signatureName);
}
//else if (EdECObjectIdentifiers.id_Ed448.Equals(sigAlgOid))
//{
Expand Down
Loading