|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.security.ec; |
|
|
|
import java.nio.ByteBuffer; |
|
|
|
import java.security.*; |
|
import java.security.interfaces.*; |
|
import java.security.spec.*; |
|
import java.util.Optional; |
|
|
|
import sun.security.jca.JCAUtil; |
|
import sun.security.util.*; |
|
import static sun.security.ec.ECOperations.IntermediateValueException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
abstract class ECDSASignature extends SignatureSpi { |
|
|
|
|
|
private final MessageDigest messageDigest; |
|
|
|
|
|
private SecureRandom random; |
|
|
|
|
|
private boolean needsReset; |
|
|
|
|
|
private ECPrivateKey privateKey; |
|
|
|
|
|
private ECPublicKey publicKey; |
|
|
|
|
|
private ECParameterSpec sigParams = null; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
ECDSASignature() { |
|
messageDigest = null; |
|
} |
|
|
|
|
|
|
|
*/ |
|
ECDSASignature(String digestName) { |
|
try { |
|
messageDigest = MessageDigest.getInstance(digestName); |
|
} catch (NoSuchAlgorithmException e) { |
|
throw new ProviderException(e); |
|
} |
|
needsReset = false; |
|
} |
|
|
|
|
|
public static final class Raw extends ECDSASignature { |
|
|
|
|
|
private static final int RAW_ECDSA_MAX = 64; |
|
|
|
private final byte[] precomputedDigest; |
|
private int offset = 0; |
|
|
|
public Raw() { |
|
precomputedDigest = new byte[RAW_ECDSA_MAX]; |
|
} |
|
|
|
|
|
@Override |
|
protected void engineUpdate(byte b) throws SignatureException { |
|
if (offset >= precomputedDigest.length) { |
|
offset = RAW_ECDSA_MAX + 1; |
|
return; |
|
} |
|
precomputedDigest[offset++] = b; |
|
} |
|
|
|
|
|
@Override |
|
protected void engineUpdate(byte[] b, int off, int len) |
|
throws SignatureException { |
|
if (offset >= precomputedDigest.length) { |
|
offset = RAW_ECDSA_MAX + 1; |
|
return; |
|
} |
|
System.arraycopy(b, off, precomputedDigest, offset, len); |
|
offset += len; |
|
} |
|
|
|
|
|
@Override |
|
protected void engineUpdate(ByteBuffer byteBuffer) { |
|
int len = byteBuffer.remaining(); |
|
if (len <= 0) { |
|
return; |
|
} |
|
if (offset + len >= precomputedDigest.length) { |
|
offset = RAW_ECDSA_MAX + 1; |
|
return; |
|
} |
|
byteBuffer.get(precomputedDigest, offset, len); |
|
offset += len; |
|
} |
|
|
|
@Override |
|
protected void resetDigest() { |
|
offset = 0; |
|
} |
|
|
|
|
|
@Override |
|
protected byte[] getDigestValue() throws SignatureException { |
|
if (offset > RAW_ECDSA_MAX) { |
|
throw new SignatureException("Message digest is too long"); |
|
|
|
} |
|
byte[] result = new byte[offset]; |
|
System.arraycopy(precomputedDigest, 0, result, 0, offset); |
|
offset = 0; |
|
|
|
return result; |
|
} |
|
} |
|
|
|
|
|
public static final class SHA1 extends ECDSASignature { |
|
public SHA1() { |
|
super("SHA1"); |
|
} |
|
} |
|
|
|
|
|
public static final class SHA224 extends ECDSASignature { |
|
public SHA224() { |
|
super("SHA-224"); |
|
} |
|
} |
|
|
|
|
|
public static final class SHA256 extends ECDSASignature { |
|
public SHA256() { |
|
super("SHA-256"); |
|
} |
|
} |
|
|
|
|
|
public static final class SHA384 extends ECDSASignature { |
|
public SHA384() { |
|
super("SHA-384"); |
|
} |
|
} |
|
|
|
|
|
public static final class SHA512 extends ECDSASignature { |
|
public SHA512() { |
|
super("SHA-512"); |
|
} |
|
} |
|
|
|
|
|
@Override |
|
protected void engineInitVerify(PublicKey publicKey) |
|
throws InvalidKeyException { |
|
ECPublicKey key = (ECPublicKey) ECKeyFactory.toECKey(publicKey); |
|
if (!isCompatible(this.sigParams, key.getParams())) { |
|
throw new InvalidKeyException("Key params does not match signature params"); |
|
} |
|
|
|
// Should check that the supplied key is appropriate for signature |
|
|
|
this.publicKey = key; |
|
this.privateKey = null; |
|
resetDigest(); |
|
} |
|
|
|
|
|
@Override |
|
protected void engineInitSign(PrivateKey privateKey) |
|
throws InvalidKeyException { |
|
engineInitSign(privateKey, null); |
|
} |
|
|
|
|
|
@Override |
|
protected void engineInitSign(PrivateKey privateKey, SecureRandom random) |
|
throws InvalidKeyException { |
|
ECPrivateKey key = (ECPrivateKey) ECKeyFactory.toECKey(privateKey); |
|
if (!isCompatible(this.sigParams, key.getParams())) { |
|
throw new InvalidKeyException("Key params does not match signature params"); |
|
} |
|
|
|
// Should check that the supplied key is appropriate for signature |
|
|
|
this.privateKey = key; |
|
this.publicKey = null; |
|
this.random = random; |
|
resetDigest(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
protected void resetDigest() { |
|
if (needsReset) { |
|
if (messageDigest != null) { |
|
messageDigest.reset(); |
|
} |
|
needsReset = false; |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
protected byte[] getDigestValue() throws SignatureException { |
|
needsReset = false; |
|
return messageDigest.digest(); |
|
} |
|
|
|
|
|
@Override |
|
protected void engineUpdate(byte b) throws SignatureException { |
|
messageDigest.update(b); |
|
needsReset = true; |
|
} |
|
|
|
|
|
@Override |
|
protected void engineUpdate(byte[] b, int off, int len) |
|
throws SignatureException { |
|
messageDigest.update(b, off, len); |
|
needsReset = true; |
|
} |
|
|
|
|
|
@Override |
|
protected void engineUpdate(ByteBuffer byteBuffer) { |
|
int len = byteBuffer.remaining(); |
|
if (len <= 0) { |
|
return; |
|
} |
|
|
|
messageDigest.update(byteBuffer); |
|
needsReset = true; |
|
} |
|
|
|
private static boolean isCompatible(ECParameterSpec sigParams, |
|
ECParameterSpec keyParams) { |
|
if (sigParams == null) { |
|
|
|
return true; |
|
} |
|
return ECUtil.equals(sigParams, keyParams); |
|
} |
|
|
|
|
|
private byte[] signDigestImpl(ECDSAOperations ops, int seedBits, |
|
byte[] digest, ECPrivateKeyImpl privImpl, SecureRandom random) |
|
throws SignatureException { |
|
|
|
byte[] seedBytes = new byte[(seedBits + 7) / 8]; |
|
byte[] s = privImpl.getArrayS(); |
|
|
|
// Attempt to create the signature in a loop that uses new random input |
|
// each time. The chance of failure is very small assuming the |
|
|
|
int numAttempts = 128; |
|
for (int i = 0; i < numAttempts; i++) { |
|
random.nextBytes(seedBytes); |
|
ECDSAOperations.Seed seed = new ECDSAOperations.Seed(seedBytes); |
|
try { |
|
return ops.signDigest(s, digest, seed); |
|
} catch (IntermediateValueException ex) { |
|
// try again in the next iteration |
|
} |
|
} |
|
|
|
throw new SignatureException("Unable to produce signature after " |
|
+ numAttempts + " attempts"); |
|
} |
|
|
|
|
|
private Optional<byte[]> signDigestImpl(ECPrivateKey privateKey, |
|
byte[] digest, SecureRandom random) throws SignatureException { |
|
|
|
if (! (privateKey instanceof ECPrivateKeyImpl)) { |
|
return Optional.empty(); |
|
} |
|
ECPrivateKeyImpl privImpl = (ECPrivateKeyImpl) privateKey; |
|
ECParameterSpec params = privateKey.getParams(); |
|
|
|
|
|
int seedBits = params.getOrder().bitLength() + 64; |
|
Optional<ECDSAOperations> opsOpt = |
|
ECDSAOperations.forParameters(params); |
|
if (!opsOpt.isPresent()) { |
|
return Optional.empty(); |
|
} else { |
|
byte[] sig = signDigestImpl(opsOpt.get(), seedBits, digest, |
|
privImpl, random); |
|
return Optional.of(sig); |
|
} |
|
} |
|
|
|
private byte[] signDigestNative(ECPrivateKey privateKey, byte[] digest, |
|
SecureRandom random) throws SignatureException { |
|
|
|
byte[] s = privateKey.getS().toByteArray(); |
|
ECParameterSpec params = privateKey.getParams(); |
|
|
|
|
|
byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params); |
|
int orderLength = params.getOrder().bitLength(); |
|
|
|
|
|
byte[] seed = new byte[(((orderLength + 7) >> 3) + 1) * 2]; |
|
|
|
random.nextBytes(seed); |
|
|
|
|
|
int timingArgument = random.nextInt(); |
|
|
|
timingArgument |= 1; |
|
|
|
try { |
|
return signDigest(digest, s, encodedParams, seed, |
|
timingArgument); |
|
} catch (GeneralSecurityException e) { |
|
throw new SignatureException("Could not sign data", e); |
|
} |
|
} |
|
|
|
|
|
@Override |
|
protected byte[] engineSign() throws SignatureException { |
|
|
|
if (random == null) { |
|
random = JCAUtil.getSecureRandom(); |
|
} |
|
|
|
byte[] digest = getDigestValue(); |
|
Optional<byte[]> sigOpt = signDigestImpl(privateKey, digest, random); |
|
byte[] sig; |
|
if (sigOpt.isPresent()) { |
|
sig = sigOpt.get(); |
|
} else { |
|
sig = signDigestNative(privateKey, digest, random); |
|
} |
|
|
|
return ECUtil.encodeSignature(sig); |
|
} |
|
|
|
|
|
@Override |
|
protected boolean engineVerify(byte[] signature) throws SignatureException { |
|
|
|
byte[] w; |
|
ECParameterSpec params = publicKey.getParams(); |
|
|
|
byte[] encodedParams = ECUtil.encodeECParameterSpec(null, params); |
|
|
|
if (publicKey instanceof ECPublicKeyImpl) { |
|
w = ((ECPublicKeyImpl) publicKey).getEncodedPublicValue(); |
|
} else { |
|
w = ECUtil.encodePoint(publicKey.getW(), params.getCurve()); |
|
} |
|
|
|
try { |
|
|
|
return verifySignedDigest( |
|
ECUtil.decodeSignature(signature), getDigestValue(), |
|
w, encodedParams); |
|
|
|
} catch (GeneralSecurityException e) { |
|
throw new SignatureException("Could not verify signature", e); |
|
} |
|
} |
|
|
|
|
|
@Override |
|
@Deprecated |
|
protected void engineSetParameter(String param, Object value) |
|
throws InvalidParameterException { |
|
throw new UnsupportedOperationException("setParameter() not supported"); |
|
} |
|
|
|
@Override |
|
protected void engineSetParameter(AlgorithmParameterSpec params) |
|
throws InvalidAlgorithmParameterException { |
|
if (params != null && !(params instanceof ECParameterSpec)) { |
|
throw new InvalidAlgorithmParameterException("No parameter accepted"); |
|
} |
|
ECKey key = (this.privateKey == null? this.publicKey : this.privateKey); |
|
if ((key != null) && !isCompatible((ECParameterSpec)params, key.getParams())) { |
|
throw new InvalidAlgorithmParameterException |
|
("Signature params does not match key params"); |
|
} |
|
|
|
sigParams = (ECParameterSpec) params; |
|
} |
|
|
|
|
|
@Override |
|
@Deprecated |
|
protected Object engineGetParameter(String param) |
|
throws InvalidParameterException { |
|
throw new UnsupportedOperationException("getParameter() not supported"); |
|
} |
|
|
|
@Override |
|
protected AlgorithmParameters engineGetParameters() { |
|
if (sigParams == null) { |
|
return null; |
|
} |
|
try { |
|
AlgorithmParameters ap = AlgorithmParameters.getInstance("EC"); |
|
ap.init(sigParams); |
|
return ap; |
|
} catch (Exception e) { |
|
|
|
throw new ProviderException("Error retrieving EC parameters", e); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static native byte[] signDigest(byte[] digest, byte[] s, |
|
byte[] encodedParams, byte[] seed, int timing) |
|
throws GeneralSecurityException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static native boolean verifySignedDigest(byte[] signature, |
|
byte[] digest, byte[] w, byte[] encodedParams) |
|
throws GeneralSecurityException; |
|
} |