Search in sources :

Example 1 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest in project web3sdk by FISCO-BCOS.

the class EncryptType method setEncryptInterface.

private static void setEncryptInterface(int encryptType) {
    SignInterface signInterface;
    HashInterface hashInterface;
    if (encryptType == SM2_TYPE) {
        signInterface = new SM2Sign();
        hashInterface = new SM3Digest();
    } else {
        signInterface = new ECDSASign();
        hashInterface = new SHA3Digest();
    }
    Sign.setSignInterface(signInterface);
    Hash.setHashInterface(hashInterface);
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest) SM2Sign(org.fisco.bcos.web3j.crypto.gm.sm2.SM2Sign)

Example 2 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest in project web3sdk by FISCO-BCOS.

the class SM2Algorithm method verify.

/**
 * SM2公钥验签
 *
 * @param msg
 * @param signData
 * @param biX
 * @param biY
 * @return
 * @author fisco-bcos
 */
private static boolean verify(byte[] msg, byte[] signData, BigInteger biX, BigInteger biY) {
    ECPoint userKey = curve256.createPoint(biX, biY);
    byte[] btRS = signData;
    byte[] btR = ByteUtils.subByteArray(btRS, 0, btRS.length / 2);
    byte[] btS = ByteUtils.subByteArray(btRS, btR.length, btRS.length - btR.length);
    BigInteger r = new BigInteger(1, btR);
    // 检验 r ′ ∈[1, n-1]是否成立,若不成立则验证不通过;
    if (!checkValidateK(r))
        return false;
    BigInteger s = new BigInteger(1, btS);
    // 检验 s ′ ∈[1, n-1]是否成立,若不成立则验证不通过;
    if (!checkValidateK(s))
        return false;
    SM3Digest sm3 = new SM3Digest();
    byte[] z = sm2GetZ(USER_ID, userKey);
    sm3.update(z, 0, z.length);
    byte[] p = msg;
    sm3.update(p, 0, p.length);
    byte[] hashData = new byte[32];
    sm3.doFinal(hashData, 0);
    BigInteger e = new BigInteger(1, hashData);
    BigInteger t = r.add(s).mod(n);
    if (t.equals(BigInteger.ZERO))
        return false;
    ECPoint x1y1 = g256.multiply(s);
    x1y1 = x1y1.add(userKey.multiply(t));
    BigInteger R = e.add(x1y1.normalize().getAffineXCoord().toBigInteger()).mod(n);
    return r.equals(R);
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest) BigInteger(java.math.BigInteger) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 3 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest in project web3sdk by FISCO-BCOS.

the class SM2KeyGenerator method generatePrivateKey.

/*
     * 根据数据生成SM2私钥
     *
     * @param imei 手机序列号
     *
     * @param pin pin码
     *
     * @param rand 随机数
     *
     * @return String 私钥(16进制字符串,256bits)
     */
private SM2PrivateKey generatePrivateKey() {
    SecureRandom random = new SecureRandom();
    byte[] r = new byte[32];
    BigInteger k;
    do {
        random.nextBytes(r);
        k = new BigInteger(1, r);
    } while (!checkValidateK(k));
    byte[] in = k.toByteArray();
    SM3Digest digest = new SM3Digest();
    byte[] out = new byte[32];
    digest.update(in, 0, in.length);
    digest.doFinal(out, 0);
    String value = KeyUtils.bcdhex_to_aschex(out);
    return new SM2PrivateKey(new BigInteger(value, 16));
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest) SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger)

Example 4 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest in project web3sdk by FISCO-BCOS.

the class SM2Algorithm method kdf.

/*
     * 第5步:计算 t = KDF(x2, y2, keyLen)
     */
private static byte[] kdf(BigInteger x2, BigInteger y2, int keyLen) {
    byte[] t = new byte[keyLen];
    SM3Digest sm3 = new SM3Digest();
    byte[] sm3Ret = new byte[32];
    int ct = 1;
    int value = keyLen / 32;
    int remainder = keyLen % 32;
    byte[] x2Buf = padding(x2.toByteArray());
    byte[] y2Buf = padding(y2.toByteArray());
    int offset = 0;
    for (int i = 0; i < value; i++) {
        sm3.update(x2Buf, 0, x2Buf.length);
        sm3.update(y2Buf, 0, y2Buf.length);
        sm3.update((byte) (ct >> 24 & 0x00ff));
        sm3.update((byte) (ct >> 16 & 0x00ff));
        sm3.update((byte) (ct >> 8 & 0x00ff));
        sm3.update((byte) (ct & 0x00ff));
        sm3.doFinal(t, offset);
        offset += 32;
        ct++;
    }
    if (remainder != 0) {
        sm3.update(x2Buf, 0, x2Buf.length);
        sm3.update(y2Buf, 0, y2Buf.length);
        sm3.update((byte) (ct >> 24 & 0x00ff));
        sm3.update((byte) (ct >> 16 & 0x00ff));
        sm3.update((byte) (ct >> 8 & 0x00ff));
        sm3.update((byte) (ct & 0x00ff));
        sm3.doFinal(sm3Ret, 0);
    }
    System.arraycopy(sm3Ret, 0, t, offset, remainder);
    return t;
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 5 with SM3Digest

use of org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest in project web3sdk by FISCO-BCOS.

the class SM2Algorithm method Sign.

/**
 * SM2私钥签名
 *
 * @param md 待签名数据
 * @param privateKeyS
 * @return
 * @author fisco-bcos
 */
private static BigInteger[] Sign(byte[] md, BigInteger privateKeyS) {
    SM3Digest sm3 = new SM3Digest();
    byte[] z = sm2GetZ(USER_ID, g256.multiply(privateKeyS));
    sm3.update(z, 0, z.length);
    byte[] p = md;
    sm3.update(p, 0, p.length);
    byte[] hashData = new byte[32];
    sm3.doFinal(hashData, 0);
    return SignSm3(hashData, privateKeyS);
}
Also used : SM3Digest(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest)

Aggregations

SM3Digest (org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest)6 ECPoint (org.bouncycastle.math.ec.ECPoint)3 BigInteger (java.math.BigInteger)2 SecureRandom (java.security.SecureRandom)1 SM2Sign (org.fisco.bcos.web3j.crypto.gm.sm2.SM2Sign)1 SM3Digest (org.fisco.bcos.web3j.crypto.gm.sm3.SM3Digest)1