zoukankan      html  css  js  c++  java
  • 制作SM2证书

    前段时间将系统的RSA算法全部升级为SM2国密算法,密码机和UKey硬件设备大都同时支持RSA和SM2算法,只是应用系统的加解密签名验证需要修改,这个更改底层调用的加密动态库来,原来RSA用的对称加密算法DES(AES)和摘要MD5(SHA1)也相应改变,分别对应SM1、SM3算法,SM1算法基于硬件实现,SM2、SM3算法已公开。

    SM2签名验证算法

    SM2签名同样也是需要先摘要原文数据,即先使用SM3密码杂凑算法计算出32byte摘要。SM3需要摘要签名方ID(默认1234567812345678)、曲线参数a,b,Gx,Gy、共钥坐标(x,y)计算出Z值,然后再杂凑原文得出摘要数据。这个地方要注意曲线参数和坐标点都是32byte,在转换为BigInteger大数计算转成字节流时要去掉空补位,否则可能会出现摘要计算不正确的问题。SM2签名实现如下:

    [java] view plaincopy
     
    1. public static BigInteger[] Sm2Sign(byte[] md, AsymmetricCipherKeyPair keypair)  
    2. {  
    3.             SM3Digest sm3 = new SM3Digest();  
    4.   
    5.             ECPublicKeyParameters ecpub = (ECPublicKeyParameters)keypair.Public;  
    6.   
    7.             byte[] z = SM2CryptoServiceProvider.Sm2GetZ(Encoding.Default.GetBytes(SM2CryptoServiceProvider.userId), ecpub.Q);  
    8.             sm3.BlockUpdate(z, 0, z.Length);  
    9.   
    10.             byte[] p = md;  
    11.             sm3.BlockUpdate(p, 0, p.Length);  
    12.   
    13.             byte[] hashData = new byte[32];  
    14.             sm3.DoFinal(hashData, 0);  
    15.   
    16.             // e  
    17.             BigInteger e = new BigInteger(1, hashData);  
    18.             // k  
    19.             BigInteger k = null;  
    20.             ECPoint kp = null;  
    21.             BigInteger r = null;  
    22.             BigInteger s = null;  
    23.             BigInteger userD = null;  
    24.   
    25.             do  
    26.             {  
    27.                 do  
    28.                 {  
    29.   
    30.                     ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)keypair.Private;  
    31.                     k = ecpriv.D;  
    32.                     kp = ecpub.Q;  
    33.   
    34.                     userD = ecpriv.D;  
    35.   
    36.                     // r  
    37.                     r = e.Add(kp.X.ToBigInteger());  
    38.                     r = r.Mod(ecc_n);  
    39.                 }  
    40.                 while (r.Equals(BigInteger.Zero) || r.Add(k).Equals(ecc_n));  
    41.   
    42.                 // (1 + dA)~-1  
    43.                 BigInteger da_1 = userD.Add(BigInteger.One);  
    44.                 da_1 = da_1.ModInverse(ecc_n);  
    45.                 // s  
    46.                 s = r.Multiply(userD);  
    47.                 s = k.Subtract(s).Mod(ecc_n);  
    48.                 s = da_1.Multiply(s).Mod(ecc_n);  
    49.             }  
    50.             while (s.Equals(BigInteger.Zero));  
    51.   
    52.             byte[] btRS = new byte[64];  
    53.             byte[] btR = r.ToByteArray();  
    54.             byte[] btS = s.ToByteArray();  
    55.             Array.Copy(btR, btR.Length - 32, btRS, 0, 32);  
    56.             Array.Copy(btS, btS.Length - 32, btRS, 32, 32);  
    57.   
    58.             return new BigInteger[] { r, s };  
    59. }  

    SM2算法是基于ECC算法的,签名同样返回2个大数,共64byte。由于原来RSA算法已很普遍支持,要实现RSA的签名验签都有标准库的实现,而SM2是国密算法在国际上还没有标准通用,算法Oid标识在X509标准中是没定义的。在.Net或Java中可以基于使用BouncyCastle加密库实现,开源的也比较好学习扩展。SM2算法验签可以使用软验签,即可以不需要使用硬件设备,同样使用原始数据、签名、证书(公钥)来实现对签名方验证,保证数据完整性未被篡改。验证过程同样需先摘要原文数据,公钥在证书中是以一个66byte的BitString,去掉前面标记位即64byte为共钥坐标(x,y),中间分割截取再以Hex方式转成BigInteger大数计算,验签代码如下:

    [java] view plaincopy
     
    1. public static bool Verify(byte[] msg, byte[] signData, byte[] certData)  
    2. {  
    3.   
    4.             X509Certificate2 x5092 = new X509Certificate2(certData);  
    5.             byte[] certPK = x5092.GetPublicKey();  
    6.   
    7.             certPK = SubByte(certPK, 1, 64);  
    8.   
    9.             byte[] certPKX = SubByte(certPK, certPK.Length - 32 - 32, 32);  
    10.             byte[] certPKY = SubByte(certPK, certPK.Length - 32, 32);  
    11.   
    12.   
    13.             System.String strcertPKX = ByteToHexStr(certPKX);  
    14.             System.String strcertPKY = ByteToHexStr(certPKY);  
    15.             BigInteger biX = new BigInteger(strcertPKX, 16);  
    16.             BigInteger biY = new BigInteger(strcertPKY, 16);  
    17.   
    18.   
    19.             ECFieldElement x = new FpFieldElement(ecc_p, biX);  
    20.             ECFieldElement y = new FpFieldElement(ecc_p, biY);  
    21.   
    22.             ECPoint userKey = new FpPoint(ecc_curve, x, y);  
    23.   
    24.   
    25.             SM3Digest sm3 = new SM3Digest();  
    26.             byte[] z = Sm2GetZ(Encoding.Default.GetBytes(userId), userKey);  
    27.             sm3.BlockUpdate(z, 0, z.Length);  
    28.   
    29.   
    30.             byte[] p = msg;  
    31.             sm3.BlockUpdate(p, 0, p.Length);  
    32.   
    33.             byte[] md = new byte[32];  
    34.             sm3.DoFinal(md, 0);  
    35.   
    36.   
    37.             byte[] btR = SubByte(signData, 0, 32);  
    38.             byte[] btS = SubByte(signData, 32, 32);  
    39.   
    40.   
    41.             System.String strR = ByteToHexStr(btR);  
    42.             System.String strS = ByteToHexStr(btS);  
    43.             BigInteger r = new BigInteger(strR, 16);  
    44.             BigInteger s = new BigInteger(strS, 16);  
    45.   
    46.             // e_  
    47.             BigInteger e = new BigInteger(1, md);  
    48.             // t  
    49.             BigInteger t = r.Add(s).Mod(ecc_n);  
    50.   
    51.             if (t.Equals(BigInteger.Zero))  
    52.                 return false;  
    53.   
    54.             // x1y1  
    55.             ECPoint x1y1 = ecc_point_g.Multiply(s);  
    56.             x1y1 = x1y1.Add(userKey.Multiply(t));  
    57.   
    58.             // R  
    59.             BigInteger R = e.Add(x1y1.X.ToBigInteger()).Mod(ecc_n);  
    60.   
    61.             return r.Equals(R);  
    62. }  

    制作SM2证书

    基于BouncyCastle开源库,可以轻松制作X509证书、CRL、pkcs10、pkcs12,支持国际通用的RSA、ECC算法。制作SM2证书可以通过扩展BouncyCastle库来实现,需加入SM2签名算法DerObjectIdentifier标识1.2.156.10197.1.501(基于SM3的SM2算法签名),密钥对的生成使用国密推荐曲线参数,然后如上所示自行实现SM2签名验证算法。X509证书由证书主体、证书签名算法标识、签名组成,和RSA证书主要不同的是SM2证书的签名算法标识和签名,及证书公钥使用ECKeyParameters。生成自签名SM2证书代码如下:

    SM2证书生成
    [java] view plaincopy
     
    1. public static Org.BouncyCastle.X509.X509Certificate MakeRootCert(string filePath, IDictionary subjectNames)  
    2. {  
    3.         AsymmetricCipherKeyPair keypair = SM2CryptoServiceProvider.SM2KeyPairGenerator.GenerateKeyPair();  
    4.         ECPublicKeyParameters pubKey = (ECPublicKeyParameters)keypair.Public; //CA公钥     
    5.         ECPrivateKeyParameters priKey = (ECPrivateKeyParameters)keypair.Private;    //CA私钥     
    6.   
    7.   
    8.   
    9.         X509Name issuerDN = new X509Name(GetDictionaryKeys(subjectNames), subjectNames);  
    10.         X509Name subjectDN = issuerDN;  //自签证书,两者一样  
    11.   
    12.         SM2X509V3CertificateGenerator sm2CertGen = new SM2X509V3CertificateGenerator();  
    13.         //X509V3CertificateGenerator sm2CertGen = new X509V3CertificateGenerator();  
    14.         sm2CertGen.SetSerialNumber(new BigInteger(128, new Random()));   //128位     
    15.         sm2CertGen.SetIssuerDN(issuerDN);  
    16.         sm2CertGen.SetNotBefore(DateTime.UtcNow.AddDays(-1));  
    17.         sm2CertGen.SetNotAfter(DateTime.UtcNow.AddDays(365 * 10));  
    18.         sm2CertGen.SetSubjectDN(subjectDN);  
    19.         sm2CertGen.SetPublicKey(pubKey); //公钥  
    20.   
    21.   
    22.         sm2CertGen.SetSignatureAlgorithm("SM3WITHSM2");  
    23.   
    24.         sm2CertGen.AddExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(true));  
    25.         sm2CertGen.AddExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure(pubKey));  
    26.         sm2CertGen.AddExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(pubKey));  
    27.         sm2CertGen.AddExtension(X509Extensions.KeyUsage, true, new KeyUsage(6));  
    28.   
    29.   
    30.         Org.BouncyCastle.X509.X509Certificate sm2Cert = sm2CertGen.Generate(keypair);  
    31.   
    32.         sm2Cert.CheckValidity();  
    33.         sm2Cert.Verify(pubKey);  
    34.   
    35.         return sm2Cert;  
    36. }  


    X509证书使用ASN1语法进行编码,是用类型标识、长度和值序列来描述数据结构的。SM2证书在制作设置公钥时,默认会带ECKeyParameters参数,并没有SM2的公钥参数1.2.156.10197.1.301,因此需要自己写个SM2椭圆曲线密码算法标识对象,这样在生成的证书中就可以看到公钥参数字段,如下所示:

    SM2证书公钥标识
    [java] view plaincopy
     
    1. using System;  
    2.   
    3. using Org.BouncyCastle.Asn1.X509;  
    4. using Org.BouncyCastle.Asn1;  
    5.   
    6.   
    7. namespace Common.Security  
    8. {  
    9.     public class SM2AlgorithmIdentifier  
    10.         : AlgorithmIdentifier  
    11.     {  
    12.   
    13.         private readonly bool parametersDefined;  
    14.   
    15.     public SM2AlgorithmIdentifier(  
    16.             DerObjectIdentifier objectID):base(objectID)  
    17.         {  
    18.   
    19.         }  
    20.   
    21.   
    22.     public SM2AlgorithmIdentifier(  
    23.             DerObjectIdentifier    objectID,  
    24.             Asn1Encodable parameters)  
    25.             : base(objectID, parameters)  
    26.         {  
    27.             this.parametersDefined = true;  
    28.         }  
    29.   
    30.     /** 
    31.          * Produce an object suitable for an Asn1OutputStream. 
    32.          *          *      AlgorithmIdentifier ::= Sequence { 
    33.          *                            algorithm OBJECT IDENTIFIER, 
    34.          *                            parameters ANY DEFINED BY algorithm OPTIONAL } 
    35.          *  
    36.     */  
    37.           public override Asn1Object ToAsn1Object()  
    38.           {  
    39.              DerObjectIdentifier sm2Identifier = new DerObjectIdentifier("1.2.156.10197.1.301");  
    40.              Asn1EncodableVector v = new Asn1EncodableVector(base.ObjectID, sm2Identifier);  
    41.              return new DerSequence(v);            
    42.           }       
    43.   
    44.       }   
    45. }  


    SM2算法是国密局公布的公钥密码算法,在相当强度下密钥比RSA短,在使用智能卡有限空间存储时非常可贵。目前国内很多CA大都升级支持SM2算法证书,相信以后会慢慢地推广更多应用,也期望之后能与国际标准接轨。

    附:

    国密推荐256位曲线参数

    • p=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 FFFFFFFF FFFFFFFF
    • a=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 00000000 FFFFFFFF FFFFFFFC
    • b=28E9FA9E 9D9F5E34 4D5A9E4B CF6509A7 F39789F5 15AB8F92 DDBCBD41 4D940E93
    • n=FFFFFFFE FFFFFFFF FFFFFFFF FFFFFFFF 7203DF6B 21C6052B 53BBF409 39D54123
    • Gx=32C4AE2C 1F198119 5F990446 6A39C994 8FE30BBF F2660BE1 715A4589 334C74C7
    • Gy=BC3736A2 F4F6779C 59BDCEE3 6B692153 D0A9877C C62A4740 02DF32E5 2139F0A0

    SM2国密算法测试示例

  • 相关阅读:
    SQLSERVER 根据传入的参数拼接sql语句字符串,反馈结果集
    SQLSERVER 时间函数汇总
    在网页中加入百度地图
    关于收到谷歌邮件 Googlebot can't access your site 的解决方法
    phoneGap 3.5 eclipise 模拟器调试
    将MongoDB设为Windows服务
    apply 判定变量类型
    angularjs 手动启动
    Angular js ie 7,8 兼容性
    jQuery 之正则表达式篇
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/4597840.html
Copyright © 2011-2022 走看看