提交 21b1fcb4 编写于 作者: V valeriep

8071726: Better RSA optimizations

Summary: Added a check when RSA signature is generated with a RSAPrivateCRTKey object.
Reviewed-by: mullan
上级 8f35916e
/* /*
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -349,7 +349,7 @@ public final class RSACipher extends CipherSpi { ...@@ -349,7 +349,7 @@ public final class RSACipher extends CipherSpi {
switch (mode) { switch (mode) {
case MODE_SIGN: case MODE_SIGN:
data = padding.pad(buffer, 0, bufOfs); data = padding.pad(buffer, 0, bufOfs);
return RSACore.rsa(data, privateKey); return RSACore.rsa(data, privateKey, true);
case MODE_VERIFY: case MODE_VERIFY:
byte[] verifyBuffer = RSACore.convert(buffer, 0, bufOfs); byte[] verifyBuffer = RSACore.convert(buffer, 0, bufOfs);
data = RSACore.rsa(verifyBuffer, publicKey); data = RSACore.rsa(verifyBuffer, publicKey);
...@@ -359,7 +359,7 @@ public final class RSACipher extends CipherSpi { ...@@ -359,7 +359,7 @@ public final class RSACipher extends CipherSpi {
return RSACore.rsa(data, publicKey); return RSACore.rsa(data, publicKey);
case MODE_DECRYPT: case MODE_DECRYPT:
byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs); byte[] decryptBuffer = RSACore.convert(buffer, 0, bufOfs);
data = RSACore.rsa(decryptBuffer, privateKey); data = RSACore.rsa(decryptBuffer, privateKey, false);
return padding.unpad(data); return padding.unpad(data);
default: default:
throw new AssertionError("Internal error"); throw new AssertionError("Internal error");
......
/* /*
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -102,12 +102,24 @@ public final class RSACore { ...@@ -102,12 +102,24 @@ public final class RSACore {
/** /**
* Perform an RSA private key operation. Uses CRT if the key is a * Perform an RSA private key operation. Uses CRT if the key is a
* CRT key. * CRT key with additional verification check after the signature
* is computed.
*/ */
@Deprecated
public static byte[] rsa(byte[] msg, RSAPrivateKey key) public static byte[] rsa(byte[] msg, RSAPrivateKey key)
throws BadPaddingException { throws BadPaddingException {
return rsa(msg, key, true);
}
/**
* Perform an RSA private key operation. Uses CRT if the key is a
* CRT key. Set 'verify' to true if this function is used for
* generating a signature.
*/
public static byte[] rsa(byte[] msg, RSAPrivateKey key, boolean verify)
throws BadPaddingException {
if (key instanceof RSAPrivateCrtKey) { if (key instanceof RSAPrivateCrtKey) {
return crtCrypt(msg, (RSAPrivateCrtKey)key); return crtCrypt(msg, (RSAPrivateCrtKey)key, verify);
} else { } else {
return priCrypt(msg, key.getModulus(), key.getPrivateExponent()); return priCrypt(msg, key.getModulus(), key.getPrivateExponent());
} }
...@@ -148,10 +160,11 @@ public final class RSACore { ...@@ -148,10 +160,11 @@ public final class RSACore {
* RSA private key operations with CRT. Algorithm and variable naming * RSA private key operations with CRT. Algorithm and variable naming
* are taken from PKCS#1 v2.1, section 5.1.2. * are taken from PKCS#1 v2.1, section 5.1.2.
*/ */
private static byte[] crtCrypt(byte[] msg, RSAPrivateCrtKey key) private static byte[] crtCrypt(byte[] msg, RSAPrivateCrtKey key,
throws BadPaddingException { boolean verify) throws BadPaddingException {
BigInteger n = key.getModulus(); BigInteger n = key.getModulus();
BigInteger c = parseMsg(msg, n); BigInteger c0 = parseMsg(msg, n);
BigInteger c = c0;
BigInteger p = key.getPrimeP(); BigInteger p = key.getPrimeP();
BigInteger q = key.getPrimeQ(); BigInteger q = key.getPrimeQ();
BigInteger dP = key.getPrimeExponentP(); BigInteger dP = key.getPrimeExponentP();
...@@ -184,6 +197,9 @@ public final class RSACore { ...@@ -184,6 +197,9 @@ public final class RSACore {
if (ENABLE_BLINDING) { if (ENABLE_BLINDING) {
m = m.multiply(brp.v).mod(n); m = m.multiply(brp.v).mod(n);
} }
if (verify && !c0.equals(m.modPow(e, n))) {
throw new BadPaddingException("RSA private key operation failed");
}
return toByteArray(m, getByteLength(n)); return toByteArray(m, getByteLength(n));
} }
......
/* /*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -173,7 +173,7 @@ public abstract class RSASignature extends SignatureSpi { ...@@ -173,7 +173,7 @@ public abstract class RSASignature extends SignatureSpi {
try { try {
byte[] encoded = encodeSignature(digestOID, digest); byte[] encoded = encodeSignature(digestOID, digest);
byte[] padded = padding.pad(encoded); byte[] padded = padding.pad(encoded);
byte[] encrypted = RSACore.rsa(padded, privateKey); byte[] encrypted = RSACore.rsa(padded, privateKey, true);
return encrypted; return encrypted;
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
throw new SignatureException("Could not sign data", e); throw new SignatureException("Could not sign data", e);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册