ECDSAUtils.java 6.9 KB
Newer Older
H
huanghaiquan 已提交
1
package utils.crypto.classic;
H
huanghaiquan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

import java.math.BigInteger;
import java.security.SecureRandom;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECKeyGenerationParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.signers.ECDSASigner;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;

/**
 * @author zhanglin33
 * @title: ECDSAUtils
 * @description: ECDSA signature algorithm based on Curve secp256k1 with SHA256
 * @date 2019-03-25, 17:21
 */
public class ECDSAUtils {

	public static final int R_SIZE = 32;
	public static final int S_SIZE = 32;

	// p = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - 1
	// the curve equation is y^2 = x^3 + 7.
I
imuge 已提交
33
	private static final ECNamedCurveParameterSpec PARAMS = ECNamedCurveTable.getParameterSpec("secp256r1");
H
huanghaiquan 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	private static final ECCurve CURVE = PARAMS.getCurve();
	public static final ECDomainParameters DOMAIN_PARAMS = new ECDomainParameters(CURVE, PARAMS.getG(), PARAMS.getN(),
			PARAMS.getH());

	// -----------------Key Generation Algorithm-----------------

	/**
	 * key generation
	 *
	 * @return key pair
	 */
	public static AsymmetricCipherKeyPair generateKeyPair() {
		SecureRandom random = new SecureRandom();
		return generateKeyPair(random);
	}

	public static AsymmetricCipherKeyPair generateKeyPair(SecureRandom random) {

		ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(DOMAIN_PARAMS, random);
		ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator();

		// To generate the key pair
		keyPairGenerator.init(keyGenerationParams);
		return keyPairGenerator.generateKeyPair();
	}

	/**
	 * public key retrieval
	 *
	 * @param privateKey private key
	 * @return publicKey
	 */
	public static byte[] retrievePublicKey(byte[] privateKey) {
		ECPoint publicKeyPoint = DOMAIN_PARAMS.getG().multiply(new BigInteger(1, privateKey)).normalize();
		return publicKeyPoint.getEncoded(false);
	}

	// -----------------Digital Signature Algorithm-----------------

	/**
	 * signature generation
	 *
	 * @param data       data to be signed
	 * @param privateKey private key
	 * @return signature
	 */
	public static byte[] sign(byte[] data, byte[] privateKey) {
		return sign(data, 0, data.length, privateKey);
	}

	/**
	 * signature generation
	 *
	 * @param data       data to be signed
	 * @param privateKey private key
	 * @return signature
	 */
	public static byte[] sign(byte[] data, int offset, int length, byte[] privateKey) {
		SecureRandom random = new SecureRandom();
		ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(new BigInteger(1, privateKey), DOMAIN_PARAMS);
		CipherParameters params = new ParametersWithRandom(privKey, random);

		return sign(data, offset, length, params);
	}

	public static byte[] sign(byte[] data, byte[] privateKey, SecureRandom random) {

		ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(new BigInteger(1, privateKey), DOMAIN_PARAMS);
		CipherParameters params = new ParametersWithRandom(privKey, random);

		return sign(data, 0, data.length, params);
	}

	public static byte[] sign(byte[] data, CipherParameters params) {
		return sign(data, 0, data.length, params);
	}

	public static byte[] sign(byte[] data, int offset, int length, CipherParameters params) {
		byte[] hashedMsg = SHA256Utils.hash(data, offset, length);
		return sign(params, hashedMsg);
	}

	public static byte[] sign(CipherParameters params, byte[] hashedMsg) {
		ECDSASigner signer = new ECDSASigner();
		signer.init(true, params);
		BigInteger[] signature = signer.generateSignature(hashedMsg);

		byte[] rBytes = trimBigIntegerTo32Bytes(signature[0]);
		byte[] sBytes = trimBigIntegerTo32Bytes(signature[1]);

		byte[] result = new byte[R_SIZE + S_SIZE];
		System.arraycopy(rBytes, 0, result, 0, R_SIZE);
		System.arraycopy(sBytes, 0, result, R_SIZE, S_SIZE);

		return result;
	}
	
	
	/**
	 * verification
	 *
	 * @param data      data to be signed
	 * @param publicKey public key
	 * @param signature signature to be verified
	 * @return true or false
	 */
	public static boolean verify(byte[] data, byte[] publicKey, byte[] signature) {
		return verify(data, 0, data.length, publicKey, signature);
	}

	/**
	 * verification
	 *
	 * @param data      data to be signed
	 * @param publicKey public key
	 * @param signature signature to be verified
	 * @return true or false
	 */
	public static boolean verify(byte[] data, int offset, int length, byte[] publicKey, byte[] signature) {

		ECPoint pubKeyPoint = resolvePubKeyBytes(publicKey);
		ECPublicKeyParameters pubKey = new ECPublicKeyParameters(pubKeyPoint, DOMAIN_PARAMS);

		return verify(data, offset, length, pubKey, signature);
	}

	public static boolean verify(byte[] data, CipherParameters params, byte[] signature) {
		return verify(data, 0, data.length, params, signature);
	}

	public static boolean verify(byte[] data, int offset, int length, CipherParameters params, byte[] signature) {
		
		byte[] hashedMsg = SHA256Utils.hash(data, offset, length);
		return verify(params, signature, hashedMsg);
	}

	public static boolean verify(CipherParameters params, byte[] signature, byte[] hashedMsg) {

		byte[] rBytes = new byte[R_SIZE];
		byte[] sBytes = new byte[S_SIZE];
		System.arraycopy(signature, 0, rBytes, 0, R_SIZE);
		System.arraycopy(signature, R_SIZE, sBytes, 0, S_SIZE);

		BigInteger r = new BigInteger(1, rBytes);
		BigInteger s = new BigInteger(1, sBytes);

		ECDSASigner verifier = new ECDSASigner();
		verifier.init(false, params);
		return verifier.verifySignature(hashedMsg, r, s);
	}

	// To convert BigInteger to byte[] whose length is 32
	public static byte[] trimBigIntegerTo32Bytes(BigInteger b) {
		byte[] tmp = b.toByteArray();
		byte[] result = new byte[32];
		if (tmp.length > result.length) {
			System.arraycopy(tmp, tmp.length - result.length, result, 0, result.length);
		} else {
			System.arraycopy(tmp, 0, result, result.length - tmp.length, tmp.length);
		}
		return result;
	}

	// To retrieve the public key point from publicKey in byte array mode
	private static ECPoint resolvePubKeyBytes(byte[] publicKey) {
		return CURVE.decodePoint(publicKey);
	}

	public static ECCurve getCurve() {
		return CURVE;
	}

	public static ECDomainParameters getDomainParams() {
		return DOMAIN_PARAMS;
	}

	public static byte[] privKey2Bytes_RawKey(ECPrivateKeyParameters privKey) {
		byte[] privKeyBytes = ECDSAUtils.trimBigIntegerTo32Bytes(privKey.getD());
		return privKeyBytes;
	}

	public static byte[] pubKey2Bytes_RawKey(ECPublicKeyParameters pubKey) {
		byte[] pubKeyBytes = pubKey.getQ().getEncoded(false);
		return pubKeyBytes;
	}
}