adds DH with fips wrappers and constructor tests;

pull/2/merge
Moisés Guimarães 2015-04-05 18:47:21 -03:00
parent 34754a6bc0
commit 2deb74720c
11 changed files with 472 additions and 8 deletions

View File

@ -87,6 +87,7 @@
<class name="com.wolfssl.wolfcrypt.Hmac" />
<class name="com.wolfssl.wolfcrypt.Rng" />
<class name="com.wolfssl.wolfcrypt.Rsa" />
<class name="com.wolfssl.wolfcrypt.Dh" />
<class name="com.wolfssl.wolfcrypt.Asn" />
</javah>
</target>

View File

@ -26,6 +26,7 @@ LOCAL_SRC_FILES := jni_fips.c \
jni_hmac.c \
jni_rng.c \
jni_rsa.c \
jni_dh.c \
jni_asn.c
LOCAL_CFLAGS := -DHAVE_CONFIG_H -Wall -Wno-unused

View File

@ -0,0 +1,23 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_wolfssl_wolfcrypt_Dh */
#ifndef _Included_com_wolfssl_wolfcrypt_Dh
#define _Included_com_wolfssl_wolfcrypt_Dh
#ifdef __cplusplus
extern "C" {
#endif
#undef com_wolfssl_wolfcrypt_Dh_NULL
#define com_wolfssl_wolfcrypt_Dh_NULL 0LL
/*
* Class: com_wolfssl_wolfcrypt_Dh
* Method: mallocNativeStruct
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Dh_mallocNativeStruct
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -329,28 +329,84 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_RsaPrivateDecrypt_1fips
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: InitMd5_fips
* Method: InitMd5
* Signature: (Lcom/wolfssl/wolfcrypt/Md5;)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_InitMd5_1fips
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_InitMd5
(JNIEnv *, jclass, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: Md5Update_fips
* Method: Md5Update
* Signature: (Lcom/wolfssl/wolfcrypt/Md5;Ljava/nio/ByteBuffer;J)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Update_1fips
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Update
(JNIEnv *, jclass, jobject, jobject, jlong);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: Md5Final_fips
* Method: Md5Final
* Signature: (Lcom/wolfssl/wolfcrypt/Md5;Ljava/nio/ByteBuffer;)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Final_1fips
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Final
(JNIEnv *, jclass, jobject, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: InitDhKey
* Signature: (Lcom/wolfssl/wolfcrypt/Dh;)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Fips_InitDhKey
(JNIEnv *, jclass, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: FreeDhKey
* Signature: (Lcom/wolfssl/wolfcrypt/Dh;)V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Fips_FreeDhKey
(JNIEnv *, jclass, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: DhGenerateKeyPair
* Signature: (Lcom/wolfssl/wolfcrypt/Dh;Lcom/wolfssl/wolfcrypt/Rng;Ljava/nio/ByteBuffer;[JLjava/nio/ByteBuffer;[J)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhGenerateKeyPair
(JNIEnv *, jclass, jobject, jobject, jobject, jlongArray, jobject, jlongArray);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: DhAgree
* Signature: (Lcom/wolfssl/wolfcrypt/Dh;Ljava/nio/ByteBuffer;[JLjava/nio/ByteBuffer;JLjava/nio/ByteBuffer;J)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhAgree
(JNIEnv *, jclass, jobject, jobject, jlongArray, jobject, jlong, jobject, jlong);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: DhKeyDecode
* Signature: (Ljava/nio/ByteBuffer;[JLcom/wolfssl/wolfcrypt/Dh;J)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhKeyDecode
(JNIEnv *, jclass, jobject, jlongArray, jobject, jlong);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: DhSetKey
* Signature: (Lcom/wolfssl/wolfcrypt/Dh;Ljava/nio/ByteBuffer;JLjava/nio/ByteBuffer;J)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhSetKey
(JNIEnv *, jclass, jobject, jobject, jlong, jobject, jlong);
/*
* Class: com_wolfssl_wolfcrypt_Fips
* Method: DhParamsLoad
* Signature: (Ljava/nio/ByteBuffer;JLjava/nio/ByteBuffer;[JLjava/nio/ByteBuffer;[J)I
*/
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhParamsLoad
(JNIEnv *, jclass, jobject, jlong, jobject, jlongArray, jobject, jlongArray);
#ifdef __cplusplus
}
#endif

29
jni/jni_dh.c 100644
View File

@ -0,0 +1,29 @@
#include <com_wolfssl_wolfcrypt_Sha.h>
#include <wolfcrypt_jni_error.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/dh.h>
/* #define WOLFCRYPT_JNI_DEBUG_ON */
#include <wolfcrypt_jni_debug.h>
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_Dh_mallocNativeStruct(
JNIEnv* env, jobject this)
{
jlong ret = 0;
#ifdef NO_DH
throwNotCompiledInException(env);
#else
ret = (jlong) XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (!ret)
throwOutOfMemoryException(env, "Failed to allocate Dh object");
LogStr("new Dh() = %p\n", ret);
#endif
return ret;
}

View File

@ -13,6 +13,7 @@
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <cyassl/ctaocrypt/dh.h>
/* #define WOLFCRYPT_JNI_DEBUG_ON */
#include <wolfcrypt_jni_debug.h>
@ -1179,3 +1180,205 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_Md5Final_1fips(
return ret;
}
/*
* wolfCrypt FIPS API - Key agreement Service
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Fips_InitDhKey(
JNIEnv* env, jclass class, jobject key_object)
{
#if defined(HAVE_FIPS) && !defined(NO_DH)
DhKey* key = (DhKey*) getNativeStruct(env, key_object);
if (key)
InitDhKey(key);
#endif
}
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Fips_FreeDhKey(
JNIEnv* env, jclass class, jobject key_object)
{
#if defined(HAVE_FIPS) && !defined(NO_DH)
DhKey* key = (DhKey*) getNativeStruct(env, key_object);
if (key)
FreeDhKey(key);
#endif
}
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhGenerateKeyPair(
JNIEnv* env, jclass class, jobject key_object, jobject rng_object,
jobject priv_buffer, jlongArray privSz, jobject pub_buffer,
jlongArray pubSz)
{
jint ret = NOT_COMPILED_IN;
#if defined(HAVE_FIPS) && !defined(NO_DH)
DhKey* key = (DhKey*) getNativeStruct(env, key_object);
RNG* rng = (RNG*) getNativeStruct(env, rng_object);
byte* priv = getDirectBufferAddress(env, priv_buffer);
byte* pub = getDirectBufferAddress(env, pub_buffer);
word32 tmpPrivSz, tmpPubSz;
if (!key || !rng || !priv || !pub)
return BAD_FUNC_ARG;
(*env)->GetLongArrayRegion(env, privSz, 0, 1, (jlong*) &tmpPrivSz);
(*env)->GetLongArrayRegion(env, pubSz, 0, 1, (jlong*) &tmpPubSz);
ret = DhGenerateKeyPair(key, rng, priv, &tmpPrivSz, pub, &tmpPubSz);
(*env)->SetLongArrayRegion(env, privSz, 0, 1, (jlong*) &tmpPrivSz);
(*env)->SetLongArrayRegion(env, pubSz, 0, 1, (jlong*) &tmpPubSz);
LogStr("DhGenerateKeyPair(key=%p, rng=%p, priv, privSz, pub, pubSz) = %d\n",
key, rng, ret);
LogStr("priv:\n");
LogHex(priv, tmpPrivSz);
LogStr("pub:\n");
LogHex(pub, tmpPubSz);
#endif
return ret;
}
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_1DhAgree(
JNIEnv* env, jclass class, jobject key_object, jobject agree_buffer,
jlongArray agreeSz, jobject priv_buffer, jlong privSz, jobject pub_buffer,
jlong pubSz)
{
jint ret = NOT_COMPILED_IN;
#if defined(HAVE_FIPS) && !defined(NO_DH)
DhKey* key = (DhKey*) getNativeStruct(env, key_object);
byte* agree = getDirectBufferAddress(env, agree_buffer);
byte* priv = getDirectBufferAddress(env, priv_buffer);
byte* pub = getDirectBufferAddress(env, pub_buffer);
word32 tmpAgreeSz;
if (!key || !agree || !priv || !pub)
return BAD_FUNC_ARG;
(*env)->GetLongArrayRegion(env, agreeSz, 0, 1, (jlong*) &tmpAgreeSz);
ret = DhAgree(key, agree, &tmpAgreeSz, priv, privSz, pub, pubSz);
(*env)->SetLongArrayRegion(env, agreeSz, 0, 1, (jlong*) &tmpAgreeSz);
LogStr("DhAgree(key=%p, agree, agreeSz, priv, privSz, pub, pubSz) = %d\n",
key, ret);
LogStr("agree:\n");
LogHex(agree, tmpAgreeSz);
LogStr("priv:\n");
LogHex(priv, privSz);
LogStr("pub:\n");
LogHex(pub, pubSz);
#endif
return ret;
}
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhKeyDecode(
JNIEnv* env, jclass class, jobject input_buffer, jlongArray inOutIdx,
jobject key_object, jlong inSz)
{
jint ret = NOT_COMPILED_IN;
#if defined(HAVE_FIPS) && !defined(NO_DH)
DhKey* key = (DhKey*) getNativeStruct(env, key_object);
byte* input = getDirectBufferAddress(env, input_buffer);
word32 tmpInOutIdx;
if (!key || !input)
return BAD_FUNC_ARG;
(*env)->GetLongArrayRegion(env, inOutIdx, 0, 1, (jlong*) &tmpInOutIdx);
ret = DhKeyDecode(input, &tmpInOutIdx, key, inSz);
(*env)->SetLongArrayRegion(env, inOutIdx, 0, 1, (jlong*) &tmpInOutIdx);
LogStr("DhKeyDecode(input, &inOutIdx, key=%p, inSz) = %d\n", key, ret);
LogStr("input:\n");
LogHex(input, inSz);
#endif
return ret;
}
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhSetKey(
JNIEnv* env, jclass class, jobject key_object, jobject p_buffer, jlong pSz,
jobject g_buffer, jlong gSz)
{
jint ret = NOT_COMPILED_IN;
#if defined(HAVE_FIPS) && !defined(NO_DH)
DhKey* key = (DhKey*) getNativeStruct(env, key_object);
byte* p = getDirectBufferAddress(env, p_buffer);
byte* g = getDirectBufferAddress(env, g_buffer);
if (!key || !p || !g)
return BAD_FUNC_ARG;
ret = DhSetKey(key, p, pSz, g, gSz);
LogStr("DhSetKey(key=%p, p, pSz, g, gSz) = %d\n", key, ret);
LogStr("p:\n");
LogHex(p, pSz);
LogStr("g:\n");
LogHex(g, gSz);
#endif
return ret;
}
JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_Fips_DhParamsLoad(
JNIEnv* env, jclass class, jobject input_buffer, jlong inSz,
jobject p_buffer, jlongArray pInOutSz, jobject g_buffer,
jlongArray gInOutSz)
{
jint ret = NOT_COMPILED_IN;
#if defined(HAVE_FIPS) && !defined(NO_DH)
byte* input = getDirectBufferAddress(env, p_buffer);
byte* p = getDirectBufferAddress(env, p_buffer);
byte* g = getDirectBufferAddress(env, g_buffer);
word32 tmpPInOutSz, tmpGInOutSz;
if (!input || !p || !g)
return BAD_FUNC_ARG;
(*env)->GetLongArrayRegion(env, pInOutSz, 0, 1, (jlong*) &tmpPInOutSz);
(*env)->GetLongArrayRegion(env, gInOutSz, 0, 1, (jlong*) &tmpGInOutSz);
ret = DhParamsLoad(input, inSz, p, &tmpPInOutSz, g, &tmpGInOutSz);
(*env)->SetLongArrayRegion(env, pInOutSz, 0, 1, (jlong*) &tmpPInOutSz);
(*env)->SetLongArrayRegion(env, gInOutSz, 0, 1, (jlong*) &tmpGInOutSz);
LogStr("DhParamsLoad(input, inSz, p, &pInOutSz, g, &gInOutSz) = %d\n", ret);
LogStr("input:\n");
LogHex(input, inSz);
LogStr("p:\n");
LogHex(p, tmpPInOutSz);
LogStr("g:\n");
LogHex(g, tmpGInOutSz);
#endif
return ret;
}

View File

@ -3,7 +3,7 @@ SRC_PATH = jni
INC_PATH = $(SRC_PATH)/include
OBJ_LIST = jni_fips.o jni_native_struct.o jni_aes.o jni_des3.o jni_md5.o \
jni_sha.o jni_hmac.o jni_rng.o jni_rsa.o jni_asn.o
jni_sha.o jni_hmac.o jni_rng.o jni_rsa.o jni_dh.o jni_asn.o
OBJS = $(patsubst %,$(OUT_PATH)/%,$(OBJ_LIST))
TARGET = $(OUT_PATH)/libwolfcrypt-jni.jnilib

View File

@ -0,0 +1,12 @@
package com.wolfssl.wolfcrypt;
/**
* Wrapper for the native WolfCrypt DH implementation.
*
* @author Moisés Guimarães
* @version 1.0, April 2015
*/
public class Dh extends NativeStruct {
protected native long mallocNativeStruct() throws OutOfMemoryError;
}

View File

@ -729,4 +729,127 @@ public class Fips extends WolfObject {
* wolfCrypt FIPS API - Key agreement Service
*/
/**
* Initializes Dh object for use. FreeDhKey_fips must be called for
* resources deallocation.
*
* @param key
* the Dh object.
*/
public static native void InitDhKey(Dh key);
/**
* Releases Dh object's resources.
*
* @param key
* the Dh object.
*/
public static native void FreeDhKey(Dh key);
/**
* Generates the public part pub of size pubSz, private part priv of size
* privSz using rng for Dh key.
*
* @param key
* the key.
* @param rng
* the random source.
* @param priv
* the private part buffer.
* @param privSz
* the private part length.
* @param pub
* the public part buffer.
* @param pubSz
* the the public part length.
*
* @return 0 on success, {@literal <} 0 on error.
*/
public static native int DhGenerateKeyPair(Dh key, Rng rng,
ByteBuffer priv, long[] privSz, ByteBuffer pub, long[] pubSz);
/**
* Creates the agreement agree of size agreeSz using Dh key private priv of
* size privSz and peers public key otherPub of size pubSz.
*
* @param key
* the key.
* @param agree
* the agree buffer.
* @param agreeSz
* the agree length.
* @param priv
* the private part buffer.
* @param privSz
* the private part length.
* @param otherPub
* the peer's public part buffer.
* @param pubSz
* the the public part length.
*
* @return 0 on success, {@literal <} 0 on error.
*/
public static native int DhAgree(Dh key, ByteBuffer agree,
long[] agreeSz, ByteBuffer priv, long privSz, ByteBuffer otherPub,
long pubSz);
/**
* Decodes the DER group parameters from buffer input starting at index
* inOutIdx of size inSz into Dh key.
*
* @param input
* the parameters buffer.
* @param inOutIdx
* the parameters' starting index.
* @param key
* the Dh key.
* @param inSz
* the parameters buffer length. (not from inOutIdx)
*
* @return 0 on success, {@literal <} 0 on error.
*/
public static native int DhKeyDecode(ByteBuffer input, long[] inOutIdx,
Dh key, long inSz);
/**
* Sets the group parameters for the Dh key from the unsigned binary inputs
* p of size pSz and g of size gSz.
*
* @param key
* the Dh key.
* @param p
* the prime buffer.
* @param pSz
* the prime length.
* @param g
* the primitive root molulo p buffer.
* @param gSz
* the primitive root modulo p length.
*
* @return 0 on success, {@literal <} 0 on error.
*/
public static native int DhSetKey(Dh key, ByteBuffer p, long pSz,
ByteBuffer g, long gSz);
/**
* Loads the Dh group parameters.
*
* @param input
* the parameters buffer.
* @param inSz
* the parameters size.
* @param p
* the prime buffer.
* @param pInOutSz
* the prime length.
* @param g
* the primitive root molulo p buffer.
* @param gInOutSz
* the primitive root modulo p length.
*
* @return 0 on success, {@literal <} 0 on error.
*/
public static native int DhParamsLoad(ByteBuffer input, long inSz,
ByteBuffer p, long[] pInOutSz, ByteBuffer g, long[] gInOutSz);
}

View File

@ -0,0 +1,16 @@
package com.wolfssl.wolfcrypt;
import static org.junit.Assert.*;
import org.junit.Test;
import com.wolfssl.wolfcrypt.Dh;
public class DhTest {
@Test
public void constructorShouldInitializeNativeStruct() {
assertNotEquals(NativeStruct.NULL, new Dh().getNativeStruct());
}
}

View File

@ -7,7 +7,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ AesTest.class, Des3Test.class, Md5Test.class, ShaTest.class,
Sha256Test.class, Sha384Test.class, Sha512Test.class, HmacTest.class,
RngTest.class, RsaTest.class })
RngTest.class, RsaTest.class, DhTest.class })
public class WolfCryptTestSuite {
}