/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* ECDSA signature implementation. This class currently supports the
* following algorithm names:
*
* . "NONEwithECDSA"
* . "SHA1withECDSA"
* . "SHA256withECDSA"
* . "SHA384withECDSA"
* . "SHA512withECDSA"
*
* @since 1.7
*/
// message digest implementation we use
// supplied entropy
// flag indicating whether the digest has been reset
private boolean needsReset;
// private key, if initialized for signing
// public key, if initialized for verifying
/**
* Constructs a new ECDSASignature. Used by Raw subclass.
*
* @exception ProviderException if the native ECC library is unavailable.
*/
ECDSASignature() {
}
/**
* Constructs a new ECDSASignature. Used by subclasses.
*/
try {
} catch (NoSuchAlgorithmException e) {
throw new ProviderException(e);
}
needsReset = false;
}
// Nested class for NONEwithECDSA signatures
// the longest supported digest is 512 bits (SHA-512)
private final byte[] precomputedDigest;
public Raw() {
precomputedDigest = new byte[RAW_ECDSA_MAX];
}
// Stores the precomputed message digest value.
return;
}
precomputedDigest[offset++] = b;
}
// Stores the precomputed message digest value.
throws SignatureException {
return;
}
}
// Stores the precomputed message digest value.
if (len <= 0) {
return;
}
return;
}
}
protected void resetDigest(){
offset = 0;
}
// Returns the precomputed message digest value.
if (offset > RAW_ECDSA_MAX) {
throw new SignatureException("Message digest is too long");
}
offset = 0;
return result;
}
}
// Nested class for SHA1withECDSA signatures
public SHA1() {
super("SHA1");
}
}
// Nested class for SHA256withECDSA signatures
public SHA256() {
super("SHA-256");
}
}
// Nested class for SHA384withECDSA signatures
public SHA384() {
super("SHA-384");
}
}
// Nested class for SHA512withECDSA signatures
public SHA512() {
super("SHA-512");
}
}
// initialize for verification. See JCA doc
throws InvalidKeyException {
// Should check that the supplied key is appropriate for signature
// algorithm (e.g. P-256 for SHA256withECDSA)
this.privateKey = null;
resetDigest();
}
// initialize for signing. See JCA doc
throws InvalidKeyException {
}
// initialize for signing. See JCA doc
throws InvalidKeyException {
// Should check that the supplied key is appropriate for signature
// algorithm (e.g. P-256 for SHA256withECDSA)
resetDigest();
}
/**
* Resets the message digest if needed.
*/
protected void resetDigest() {
if (needsReset) {
if (messageDigest != null) {
}
needsReset = false;
}
}
/**
* Returns the message digest value.
*/
needsReset = false;
return messageDigest.digest();
}
// update the signature with the plaintext data. See JCA doc
messageDigest.update(b);
needsReset = true;
}
// update the signature with the plaintext data. See JCA doc
throws SignatureException {
needsReset = true;
}
// update the signature with the plaintext data. See JCA doc
if (len <= 0) {
return;
}
needsReset = true;
}
// sign the data and return the signature. See JCA doc
// seed is twice the key size (in bytes) plus 1
}
try {
return encodeSignature(
} catch (GeneralSecurityException e) {
throw new SignatureException("Could not sign data", e);
}
}
// verify the data and return the result. See JCA doc
byte[] w;
if (publicKey instanceof ECPublicKeyImpl) {
} else { // instanceof ECPublicKey
}
try {
return verifySignedDigest(
} catch (GeneralSecurityException e) {
throw new SignatureException("Could not verify signature", e);
}
}
// set parameter, not supported. See JCA doc
throws InvalidParameterException {
throw new UnsupportedOperationException("setParameter() not supported");
}
// get parameter, not supported. See JCA doc
throws InvalidParameterException {
throw new UnsupportedOperationException("getParameter() not supported");
}
// Convert the concatenation of R and S into their DER encoding
try {
byte[] bytes = new byte[n];
out.putInteger(r);
out.putInteger(s);
return result.toByteArray();
} catch (Exception e) {
throw new SignatureException("Could not encode signature", e);
}
}
// Convert the DER encoding of R and S into a concatenation of R and S
try {
// trim leading zeroes
// r and s each occupy half the array
byte[] result = new byte[k << 1];
return result;
} catch (Exception e) {
throw new SignatureException("Could not decode signature", e);
}
}
// trim leading (most significant) zeroes from the result
private static byte[] trimZeroes(byte[] b) {
int i = 0;
i++;
}
if (i == 0) {
return b;
}
byte[] t = new byte[b.length - i];
return t;
}
/**
* Signs the digest using the private key.
*
* @param digest the digest to be signed.
* @param s the private key's S value.
* @param encodedParams the curve's DER encoded object identifier.
* @param seed the random seed.
*
* @return byte[] the signature.
*/
/**
* Verifies the signed digest using the public key.
*
* @param signedDigest the signature to be verified. It is encoded
* as a concatenation of the key's R and S values.
* @param digest the digest to be used.
* @param w the public key's W point (in uncompressed form).
* @param encodedParams the curve's DER encoded object identifier.
*
* @return boolean true if the signature is successfully verified.
*/
byte[] digest, byte[] w, byte[] encodedParams)
throws GeneralSecurityException;
}