boolean verifySignature(byte[] sigData, int sigOffset, int sigLength, int updateOffset, int updateLength) throws InvalidKeyException, SignatureException {
signature.initVerify(pubkey);
signature.update(cleartext, updateOffset, updateLength); return signature.verify(sigData, sigOffset, sigLength);
}
static Offsets init(String provider, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException,
InvalidKeyException, SignatureException { // fill the cleartext data with random bytes byte[] cleartext = newbyte[100];
RandomFactory.getRandom().nextBytes(cleartext);
// NONEwith requires input to be of 20 bytes int size = algorithm.contains("NONEwith") ? 20 : 100;
boolean result = true; try {
Offsets test = init(args[0], args[1]);
// We are trying 3 different offsets, data size has nothing to do // with signature length for (int chunk = 3; chunk > 0; chunk--) { int signOffset = test.getDataSize() / chunk;
System.out.println("Running test with offset " + signOffset); byte[] signData = test.shiftSignData(signOffset);
if (success) {
System.out.println("Successfully verified with offset "
+ signOffset);
} else {
System.out.println("Verification failed with offset "
+ signOffset);
result = false;
}
}
// save signature to offset 0 byte[] signData = test.shiftSignData(0);
// Negative tests
// Test signature offset 0. // Wrong test data will be passed to update, // so signature verification should fail. for (int chunk = 3; chunk > 0; chunk--) { int dataOffset = (test.getDataSize() - 1) / chunk; boolean success; try {
success = test.verifySignature(signData, 0,
test.getSignatureLength(), dataOffset,
(test.getDataSize() - dataOffset));
} catch (SignatureException e) { // Since we are trying different data size, it can throw // SignatureException
success = false;
}
if (!success) {
System.out.println("Signature verification failed "
+ "as expected, with data offset " + dataOffset
+ " and length "
+ (test.getDataSize() - dataOffset));
} else {
System.out.println("Signature verification "
+ "should not succeed, with data offset "
+ dataOffset + " and length "
+ (test.getDataSize() - dataOffset));
result = false;
}
}
// Tests with manipulating offset and length
result &= Offsets.checkFailure(test, signData, -1,
test.getSignatureLength());
result &= Offsets.checkFailure(test, signData, 0,
test.getSignatureLength() - 1);
result &= Offsets.checkFailure(test, signData,
test.getSignatureLength() + 1, test.getSignatureLength());
result &= Offsets.checkFailure(test, signData, 0,
test.getSignatureLength() + 1);
result &= Offsets.checkFailure(test, signData, 0, 0);
result &= Offsets.checkFailure(test, signData, 0, -1);
result &= Offsets.checkFailure(test, signData, 2147483646, test.getSignatureLength());
result &= Offsets.checkFailure(test, null, 0,
test.getSignatureLength());
} catch (NoSuchProviderException nspe) {
System.out.println("No such provider: " + nspe);
}
if (!result) { thrownew RuntimeException("Some test cases failed");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.