forked from nayuki/Native-hashes-for-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd4-jni.c
More file actions
52 lines (44 loc) · 1.45 KB
/
md4-jni.c
File metadata and controls
52 lines (44 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Native hash functions for Java
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/native-hash-functions-for-java
*/
#include <stdint.h>
#include <jni.h>
#define STATE_LEN 4
extern void md4_compress_block(const jbyte *block, uint32_t state[STATE_LEN]);
/*
* Class: nayuki_nativehash_Md4
* Method: compress
* Signature: ([I[BII)Z
*/
JNIEXPORT jboolean JNICALL Java_nayuki_nativehash_Md4_compress(JNIEnv *env, jclass thisClass, jintArray stateArray, jbyteArray msg, jint off, jint len) {
jboolean status = JNI_FALSE;
if (len < 0 || (len & 63) != 0) // Block size is 64 bytes
goto cleanup0;
JNIEnv theEnv = *env;
(void)thisClass;
// Get state array and convert to uint32_t
jint *stateJava = theEnv->GetIntArrayElements(env, stateArray, NULL);
if (stateJava == NULL)
goto cleanup0;
uint32_t state[STATE_LEN];
for (int i = 0; i < STATE_LEN; i++)
state[i] = (uint32_t)stateJava[i];
// Iterate over each block in msg
jbyte *block = theEnv->GetPrimitiveArrayCritical(env, msg, NULL);
if (block == NULL)
goto cleanup1;
for (jint end = off + len; off < end; off += 64)
md4_compress_block(&block[off], state);
theEnv->ReleasePrimitiveArrayCritical(env, msg, block, JNI_ABORT);
// Convert state array to jint and clean up
for (int i = 0; i < STATE_LEN; i++)
stateJava[i] = (jint)state[i];
status = JNI_TRUE;
cleanup1:
theEnv->ReleaseIntArrayElements(env, stateArray, stateJava, 0);
cleanup0:
return status;
}