Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Message Digests wrapper, tests
- Loading branch information
Showing
5 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "com_canononical_openssl_OpenSSLMDSpi.h" | ||
#include "jssl.h" | ||
#include "md.h" | ||
#include "jni_utils.h" | ||
|
||
extern OSSL_LIB_CTX *global_libctx; | ||
|
||
/* | ||
* Class: OpenSSLMDSpi | ||
* Method: doInit0 | ||
* Signature: (Ljava/lang/String;)J | ||
*/ | ||
JNIEXPORT jlong JNICALL Java_OpenSSLMDSpi_doInit0 | ||
(JNIEnv *env, jobject this, jstring algorithm) { | ||
return (jlong) md_init(global_libctx, (const char*)jstring_to_char_array(env, algorithm)); | ||
} | ||
|
||
/* | ||
* Class: OpenSSLMDSpi | ||
* Method: doUpdate0 | ||
* Signature: ([B)V | ||
*/ | ||
JNIEXPORT void JNICALL Java_OpenSSLMDSpi_doUpdate0 | ||
(JNIEnv *env, jobject this, jbyteArray data) { | ||
md_context *ctx = (md_context*) get_long_field(env, this, "nativeHandle"); | ||
byte *data_array = jbyteArray_to_byte_array(env, data); | ||
int length = array_length(env, data); | ||
md_update(ctx, data_array, length); | ||
} | ||
|
||
/* | ||
* Class: OpenSSLMDSpi | ||
* Method: doFinal0 | ||
* Signature: ()[B | ||
*/ | ||
JNIEXPORT jbyteArray JNICALL Java_OpenSSLMDSpi_doFinal0 | ||
(JNIEnv *env, jobject this) { | ||
byte digest[1024]; | ||
int digest_length = 0; | ||
md_context *ctx = (md_context*) get_long_field(env, this, "nativeHandle"); | ||
md_digest(ctx, digest, &digest_length); | ||
return byte_array_to_jbyteArray(env, digest, digest_length); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import java.nio.ByteBuffer; | ||
import java.security.DigestException; | ||
import java.security.MessageDigestSpi; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
|
||
public class OpenSSLMDSpi extends MessageDigestSpi { | ||
|
||
static { | ||
System.loadLibrary("jssl"); | ||
} | ||
|
||
private String mdName; | ||
private long nativeHandle; | ||
|
||
private enum MDAlgorithms { | ||
SHA1 (Set.of("SHA", "SHA1", "SHA-1"), 20), | ||
SHA224 (Set.of("SHA224", "SHA-224", "SHA2-224"), 28), | ||
SHA256 (Set.of("SHA256", "SHA-256", "SHA2-256"), 32), | ||
SHA384 (Set.of("SHA384", "SHA-384", "SHA2-384"), 48), | ||
SHA512 (Set.of("SHA512", "SHA-512", "SHA2-512"), 64), | ||
SHA3_224 (Set.of("SHA3-224"), 28), | ||
SHA3_256 (Set.of("SHA3-256"), 32), | ||
SHA3_384 (Set.of("SHA3-384"), 48), | ||
SHA3_512 (Set.of("SHA3-512"), 64), | ||
KECCAK_KEMAK_128 (Set.of("KECCAK-KEMAK-128", "KECCAK-KEMAK128"), 16), | ||
KECCAK_KEMAK_256 (Set.of("KECCAK-KEMAK-256", "KECCAK-KEMAK256"), 32); | ||
|
||
final Set<String> names; | ||
final int hashSize; | ||
|
||
MDAlgorithms (Set<String> names, int hashSize) { | ||
this.names = names; | ||
this.hashSize = hashSize; | ||
} | ||
|
||
Set<String> getNames() { | ||
return this.names; | ||
} | ||
|
||
int getHashSize() { | ||
return hashSize; | ||
} | ||
} | ||
|
||
private boolean initialized = false; | ||
|
||
OpenSSLMDSpi(String algorithm) throws NoSuchAlgorithmException { | ||
boolean found = false; | ||
for (var algo : MDAlgorithms.values()) { | ||
if (algo.getNames().contains(algorithm)) { | ||
found = true; | ||
} | ||
} | ||
|
||
if (!found) { | ||
throw new NoSuchAlgorithmException(algorithm); | ||
} | ||
|
||
this.mdName = algorithm; | ||
} | ||
|
||
@Override | ||
protected byte[] engineDigest() { | ||
return doFinal0(); | ||
} | ||
|
||
@Override | ||
protected int engineDigest(byte[] buf, int offset, int len) throws DigestException { | ||
byte[] digest = engineDigest(); | ||
if (len < digest.length) { | ||
throw new DigestException("Digest length = " + digest.length + " is greater than len = " + len); | ||
} | ||
System.arraycopy(digest, 0, buf, offset, len); | ||
return len; | ||
} | ||
|
||
@Override | ||
protected int engineGetDigestLength() { | ||
for (var algo : MDAlgorithms.values()) { | ||
if (algo.getNames().contains(mdName)) { | ||
return algo.getHashSize(); | ||
} | ||
} | ||
// we should never come here | ||
System.out.println("[WARNING] Algo not supported"); | ||
return 0; | ||
} | ||
|
||
@Override | ||
protected void engineReset() { | ||
// TODO | ||
} | ||
|
||
@Override | ||
protected void engineUpdate(byte input) { | ||
engineUpdate(new byte[] { input }); | ||
} | ||
|
||
@Override | ||
protected void engineUpdate(byte []input, int offset, int len) { | ||
engineUpdate(Arrays.copyOfRange(input, offset, len)); | ||
} | ||
|
||
@Override | ||
protected void engineUpdate(ByteBuffer data) { | ||
engineUpdate(data.array()); | ||
} | ||
|
||
private void engineUpdate(byte[] data) { | ||
synchronized(this) { | ||
if (!this.initialized) { | ||
nativeHandle = doInit0(mdName); | ||
this.initialized = true; | ||
} | ||
} | ||
doUpdate0(data); | ||
} | ||
|
||
private native long doInit0(String name); | ||
private native void doUpdate0(byte[] data); | ||
private native byte[] doFinal0(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.Arrays; | ||
import java.util.function.*; | ||
import java.util.List; | ||
|
||
public class MDTest { | ||
|
||
private static byte[] input = """ | ||
From that time on, the world was hers for the reading. | ||
She would never be lonely again, never miss the lack of intimate friends. | ||
Books became her friends and there was one for every mood.""".getBytes(); | ||
|
||
private static byte[] input1 = """ | ||
From that time on, the world was hers for the reading. | ||
She would never be lonely again, never miss the lack of intimate friends. | ||
Books became her friends and there was one for every mood""".getBytes(); | ||
|
||
private static BiFunction<OpenSSLMDSpi, byte[], byte[]> macCompute = (md, input) -> { | ||
md.engineUpdate(input, 0, input.length); | ||
return md.engineDigest(); | ||
}; | ||
|
||
private static void runTest(String name) throws Exception { | ||
System.out.print("Testing " + name + ": "); | ||
byte[] output1 = macCompute.apply(new OpenSSLMDSpi(name), input); | ||
byte[] output2 = macCompute.apply(new OpenSSLMDSpi(name), input); | ||
byte[] output3 = macCompute.apply(new OpenSSLMDSpi(name), input1); | ||
if (Arrays.equals(output1, output2) && !Arrays.equals(output2, output3)) { | ||
System.out.println("PASSED"); | ||
} else { | ||
System.out.println("FAILED"); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
List<String> tests = List.of("SHA1", "SHA224", "SHA256", "SHA384", "SHA512", | ||
"SHA2-224", "SHA2-256", "SHA2-384", "SHA2-512", | ||
"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512"); | ||
for (var test: tests) { | ||
runTest(test); | ||
} | ||
} | ||
} |