You are viewing a plain text version of this content. The canonical link for it is here.
Posted to juice-svn@xml.apache.org by ra...@apache.org on 2005/01/05 21:20:56 UTC
svn commit: r124268 - in incubator/juice: . java/src/org/apache/security/jce java/tests/org/apache/security/jce native/src
Author: raul
Date: Wed Jan 5 12:20:54 2005
New Revision: 124268
URL: http://svn.apache.org/viewcvs?view=rev&rev=124268
Log:
Added SecureRandom,
Update the SHA digest in chunks instead of in storing everything in a ByteArray
and digesting at the end.
Added:
incubator/juice/java/src/org/apache/security/jce/SecureRandom.java
incubator/juice/native/src/random.c
Modified:
incubator/juice/build.xml
incubator/juice/java/src/org/apache/security/jce/Provider.java
incubator/juice/java/src/org/apache/security/jce/SHA.java
incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java
incubator/juice/native/src/Makefile.am
incubator/juice/native/src/digest.c
incubator/juice/native/src/digest.h
incubator/juice/native/src/juice.h
incubator/juice/native/src/sha-1.c
Modified: incubator/juice/build.xml
Url: http://svn.apache.org/viewcvs/incubator/juice/build.xml?view=diff&rev=124268&p1=incubator/juice/build.xml&r1=124267&p2=incubator/juice/build.xml&r2=124268
==============================================================================
--- incubator/juice/build.xml (original)
+++ incubator/juice/build.xml Wed Jan 5 12:20:54 2005
@@ -54,6 +54,7 @@
<class name="org.apache.security.jce.MD5"/>
<class name="org.apache.security.jce.SHA"/>
<class name="org.apache.security.jce.RSA"/>
+ <class name="org.apache.security.jce.SecureRandom"/>
<classpath refid="build.path"/>
</javah>
</target>
Modified: incubator/juice/java/src/org/apache/security/jce/Provider.java
Url: http://svn.apache.org/viewcvs/incubator/juice/java/src/org/apache/security/jce/Provider.java?view=diff&rev=124268&p1=incubator/juice/java/src/org/apache/security/jce/Provider.java&r1=124267&p2=incubator/juice/java/src/org/apache/security/jce/Provider.java&r2=124268
==============================================================================
--- incubator/juice/java/src/org/apache/security/jce/Provider.java (original)
+++ incubator/juice/java/src/org/apache/security/jce/Provider.java Wed Jan 5 12:20:54 2005
@@ -35,6 +35,9 @@
setProperty("Signature.SHA1withRSA", "org.apache.security.jce.SHA1withRSA");
setProperty("Cipher.RSA", "org.apache.security.jce.RSA");
+
+ setProperty("SecureRandom.SHA1PRNG","org.apache.security.jce.SecureRandom");
+ //setProperty("Alg.Alias.SecureRandom.SHA1PRNG",)
setProperty("Alg.Alias.MessageDigest.SHA-1", "SHA");
setProperty("Alg.Alias.MessageDigest.SHA1", "SHA");
Modified: incubator/juice/java/src/org/apache/security/jce/SHA.java
Url: http://svn.apache.org/viewcvs/incubator/juice/java/src/org/apache/security/jce/SHA.java?view=diff&rev=124268&p1=incubator/juice/java/src/org/apache/security/jce/SHA.java&r1=124267&p2=incubator/juice/java/src/org/apache/security/jce/SHA.java&r2=124268
==============================================================================
--- incubator/juice/java/src/org/apache/security/jce/SHA.java (original)
+++ incubator/juice/java/src/org/apache/security/jce/SHA.java Wed Jan 5 12:20:54 2005
@@ -25,7 +25,8 @@
public class SHA extends MessageDigestSpi {
static final int SHA_DIGEST_LENGTH = 20;
- private ByteArrayOutputStream byteStore = new ByteArrayOutputStream();
+ private long pointerToStruct=0L;
+ //private ByteArrayOutputStream byteStore = new ByteArrayOutputStream();
static {
System.loadLibrary("juice");
@@ -40,21 +41,40 @@
}
protected void engineReset() {
- byteStore.reset();
+ if (pointerToStruct!=0) {
+ reset(pointerToStruct);
+ }
}
protected byte[] engineDigest() {
- return digest(byteStore.toByteArray());
+ if (pointerToStruct!=0) {
+ return digest(pointerToStruct);
+ }
+ return null;
}
protected void engineUpdate(byte input) {
- byteStore.write(input);
+ byte a[]={input};
+ pointerToStruct=update(a,0,1,pointerToStruct);
}
protected void engineUpdate(byte[] input, int offset, int len) {
- byteStore.write(input, offset, len);
+ pointerToStruct=update(input,offset,len,pointerToStruct);
}
-
- private native byte[] digest(byte[] clear);
+
+ //Just do the free of the mallocs
+ protected void finalize() throws Throwable {
+ if (pointerToStruct!=0L) {
+ destroy(pointerToStruct);
+ }
+ super.finalize();
+ }
+
+ static private native void reset(long handle);
+ static private native long update(byte[] clear, int offset,int len,long handle);
+
+ static private native byte[] digest(long handle);
+
+ static private native void destroy(long handle);
}
Added: incubator/juice/java/src/org/apache/security/jce/SecureRandom.java
Url: http://svn.apache.org/viewcvs/incubator/juice/java/src/org/apache/security/jce/SecureRandom.java?view=auto&rev=124268
==============================================================================
--- (empty file)
+++ incubator/juice/java/src/org/apache/security/jce/SecureRandom.java Wed Jan 5 12:20:54 2005
@@ -0,0 +1,44 @@
+/*
+ * Created on Dec 26, 2004
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package org.apache.security.jce;
+
+import java.security.SecureRandomSpi;
+
+/**
+ * @author raul
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class SecureRandom extends SecureRandomSpi {
+ /* (non-Javadoc)
+ * @see java.security.SecureRandomSpi#engineGenerateSeed(int)
+ */
+ protected byte[] engineGenerateSeed(int arg0) {
+ throw new RuntimeException("engineGenerateSeed not implemented.");
+ }
+
+ /* (non-Javadoc)
+ * @see java.security.SecureRandomSpi#engineNextBytes(byte[])
+ */
+ protected void engineNextBytes(byte[] randomHolder) {
+ if (getBytes(randomHolder)!=0) {
+ throw new RuntimeException("Error while getting random.");
+ }
+
+ }
+
+ /* (non-Javadoc)
+ * @see java.security.SecureRandomSpi#engineSetSeed(byte[])
+ */
+ protected void engineSetSeed(byte[] arg0) {
+ throw new RuntimeException("engineSetSeed not implemented.");
+
+ }
+ static private native int getBytes(byte[] holder);
+
+}
Modified: incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java
Url: http://svn.apache.org/viewcvs/incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java?view=diff&rev=124268&p1=incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java&r1=124267&p2=incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java&r2=124268
==============================================================================
--- incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java (original)
+++ incubator/juice/java/tests/org/apache/security/jce/CryptoTests.java Wed Jan 5 12:20:54 2005
@@ -21,12 +21,15 @@
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
+import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;
import java.util.Arrays;
import javax.crypto.Cipher;
+import sun.misc.BASE64Encoder;
+
import com.clarkware.junitperf.TimedTest;
import junit.framework.Test;
@@ -293,6 +296,12 @@
fail("Failed to apply signature alogithm: " + e);
}
}
+ public void testSecureRandom() throws Exception {
+ SecureRandom sr=SecureRandom.getInstance("SHA1PRNG","JuiCE");
+ byte []a=new byte[100];
+ sr.nextBytes(a);
+ System.out.println(new String(new BASE64Encoder().encode(a)));
+ }
protected static String byteToHex(byte data) {
StringBuffer buf = new StringBuffer();
Modified: incubator/juice/native/src/Makefile.am
Url: http://svn.apache.org/viewcvs/incubator/juice/native/src/Makefile.am?view=diff&rev=124268&p1=incubator/juice/native/src/Makefile.am&r1=124267&p2=incubator/juice/native/src/Makefile.am&r2=124268
==============================================================================
--- incubator/juice/native/src/Makefile.am (original)
+++ incubator/juice/native/src/Makefile.am Wed Jan 5 12:20:54 2005
@@ -13,6 +13,7 @@
rsa.c \
sha-1.c \
digest.h \
+ random.c \
digest.c
Modified: incubator/juice/native/src/digest.c
Url: http://svn.apache.org/viewcvs/incubator/juice/native/src/digest.c?view=diff&rev=124268&p1=incubator/juice/native/src/digest.c&r1=124267&p2=incubator/juice/native/src/digest.c&r2=124268
==============================================================================
--- incubator/juice/native/src/digest.c (original)
+++ incubator/juice/native/src/digest.c Wed Jan 5 12:20:54 2005
@@ -42,3 +42,32 @@
#endif
}
+EVP_MD_CTX *_juice_evp_create(const EVP_MD *type) {
+ EVP_MD_CTX *ctx=EVP_MD_CTX_create();
+ EVP_DigestInit_ex (ctx, type,NULL);
+ return ctx;
+}
+
+void _juice_evp_destroy(EVP_MD_CTX *ctx) {
+ EVP_MD_CTX_destroy(ctx);
+}
+
+void _juice_evp_reset(EVP_MD_CTX *ctx,const EVP_MD *type) {
+ EVP_DigestInit_ex(ctx,type,NULL);
+}
+int _juice_evp_update (void *data,
+ unsigned int count,
+ EVP_MD_CTX *ctx)
+{
+ EVP_DigestUpdate (ctx, data, count);
+
+ return 1;
+}
+
+int _juice_evp_digestCtx(unsigned char *md,
+ unsigned int *size,
+ EVP_MD_CTX *ctx) {
+ EVP_DigestFinal_ex(ctx, md, size);
+ return 1;
+}
+
Modified: incubator/juice/native/src/digest.h
Url: http://svn.apache.org/viewcvs/incubator/juice/native/src/digest.h?view=diff&rev=124268&p1=incubator/juice/native/src/digest.h&r1=124267&p2=incubator/juice/native/src/digest.h&r2=124268
==============================================================================
--- incubator/juice/native/src/digest.h (original)
+++ incubator/juice/native/src/digest.h Wed Jan 5 12:20:54 2005
@@ -31,6 +31,15 @@
unsigned int *size,
const EVP_MD *type);
+EVP_MD_CTX *_juice_evp_create(const EVP_MD *type);
+
+void _juice_evp_reset(EVP_MD_CTX *ctx,const EVP_MD *type);
+
+int _juice_evp_update (void *data, unsigned int count,EVP_MD_CTX *ctx);
+
+int _juice_evp_digestCtx(unsigned char *md,unsigned int *size,EVP_MD_CTX *ctx) ;
+
+void _juice_evp_destroy(EVP_MD_CTX *ctx);
#ifdef __cplusplus
}
#endif
Modified: incubator/juice/native/src/juice.h
Url: http://svn.apache.org/viewcvs/incubator/juice/native/src/juice.h?view=diff&rev=124268&p1=incubator/juice/native/src/juice.h&r1=124267&p2=incubator/juice/native/src/juice.h&r2=124268
==============================================================================
--- incubator/juice/native/src/juice.h (original)
+++ incubator/juice/native/src/juice.h Wed Jan 5 12:20:54 2005
@@ -1,87 +1,130 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
-#include <jni.h>
+#include <jni.h>
/* Header for class org_apache_security_jce_MD5 */
#ifndef _Included_org_apache_security_jce_MD5
-#define _Included_org_apache_security_jce_MD5
+#define _Included_org_apache_security_jce_MD5
#ifdef __cplusplus
extern "C" {
-#endif
+#endif
#undef org_apache_security_jce_MD5_MD5_DIGEST_LENGTH
-#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
-/*
- * Class: org_apache_security_jce_MD5
- * Method: digest
- * Signature: ([B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
+#define org_apache_security_jce_MD5_MD5_DIGEST_LENGTH 16L
+/*
+ * Class: org_apache_security_jce_MD5
+ * Method: digest
+ * Signature: ([B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_MD5_digest
(JNIEnv *, jobject, jbyteArray);
-
+
#ifdef __cplusplus
}
-#endif
-#endif
+#endif
+#endif
/* Header for class org_apache_security_jce_SHA */
#ifndef _Included_org_apache_security_jce_SHA
-#define _Included_org_apache_security_jce_SHA
+#define _Included_org_apache_security_jce_SHA
#ifdef __cplusplus
extern "C" {
-#endif
+#endif
#undef org_apache_security_jce_SHA_SHA_DIGEST_LENGTH
-#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
-/*
- * Class: org_apache_security_jce_SHA
- * Method: digest
- * Signature: ([B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
- (JNIEnv *, jobject, jbyteArray);
-
+#define org_apache_security_jce_SHA_SHA_DIGEST_LENGTH 20L
+/*
+ * Class: org_apache_security_jce_SHA
+ * Method: reset
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
+ (JNIEnv *, jclass, jlong);
+
+/*
+ * Class: org_apache_security_jce_SHA
+ * Method: update
+ * Signature: ([BIIJ)J
+ */
+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
+ (JNIEnv *, jclass, jbyteArray, jint, jint, jlong);
+
+/*
+ * Class: org_apache_security_jce_SHA
+ * Method: digest
+ * Signature: (J)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
+ (JNIEnv *, jclass, jlong);
+
+/*
+ * Class: org_apache_security_jce_SHA
+ * Method: destroy
+ * Signature: (J)V
+ */
+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_destroy
+ (JNIEnv *, jclass, jlong);
+
#ifdef __cplusplus
}
-#endif
-#endif
+#endif
+#endif
/* Header for class org_apache_security_jce_RSA */
#ifndef _Included_org_apache_security_jce_RSA
-#define _Included_org_apache_security_jce_RSA
+#define _Included_org_apache_security_jce_RSA
#ifdef __cplusplus
extern "C" {
-#endif
-/*
- * Class: org_apache_security_jce_RSA
- * Method: publicEncrypt
- * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
+#endif
+/*
+ * Class: org_apache_security_jce_RSA
+ * Method: publicEncrypt
+ * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicEncrypt
(JNIEnv *, jobject, jobject, jbyteArray);
-
-/*
- * Class: org_apache_security_jce_RSA
- * Method: publicDecrypt
- * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
+
+/*
+ * Class: org_apache_security_jce_RSA
+ * Method: publicDecrypt
+ * Signature: (Ljava/security/interfaces/RSAPublicKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_publicDecrypt
(JNIEnv *, jobject, jobject, jbyteArray);
-
-/*
- * Class: org_apache_security_jce_RSA
- * Method: privateEncrypt
- * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_privateEncrypt
+
+/*
+ * Class: org_apache_security_jce_RSA
+ * Method: privateEncrypt
+ * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_privateEncrypt
(JNIEnv *, jobject, jobject, jbyteArray);
-
-/*
- * Class: org_apache_security_jce_RSA
- * Method: privateDecrypt
- * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
- */
-JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_privateDecrypt
+
+/*
+ * Class: org_apache_security_jce_RSA
+ * Method: privateDecrypt
+ * Signature: (Ljava/security/interfaces/RSAPrivateCrtKey;[B)[B
+ */
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_RSA_privateDecrypt
(JNIEnv *, jobject, jobject, jbyteArray);
-
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+/* Header for class org_apache_security_jce_SecureRandom */
+
+#ifndef _Included_org_apache_security_jce_SecureRandom
+#define _Included_org_apache_security_jce_SecureRandom
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_apache_security_jce_SecureRandom
+ * Method: getBytes
+ * Signature: ([B)I
+ */
+JNIEXPORT jint JNICALL Java_org_apache_security_jce_SecureRandom_getBytes
+ (JNIEnv *, jclass, jbyteArray);
+
#ifdef __cplusplus
}
-#endif
-#endif
+#endif
+#endif
Added: incubator/juice/native/src/random.c
Url: http://svn.apache.org/viewcvs/incubator/juice/native/src/random.c?view=auto&rev=124268
==============================================================================
--- (empty file)
+++ incubator/juice/native/src/random.c Wed Jan 5 12:20:54 2005
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2002-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * imitations under the License.
+ */
+
+/* $Id: rsa.c,v 1.7 2004/01/13 23:03:09 nlevitt Exp $ */
+
+
+#include <openssl/rand.h>
+#include <jni.h>
+#include "juice.h"
+
+JNIEXPORT jint JNICALL
+Java_org_apache_security_jce_SecureRandom_getBytes(JNIEnv *env, jobject obj, jbyteArray input) {
+ jbyte *holderBytes = (*env)->GetByteArrayElements(env, input, NULL);
+ jsize arrayLength = (*env)->GetArrayLength(env, input);
+ if(!RAND_bytes(holderBytes,arrayLength)) {
+ //throws an exception
+ return -1;
+ }
+ (*env)->ReleaseByteArrayElements(env, input, holderBytes, 0);
+ return 0;
+}
\ No newline at end of file
Modified: incubator/juice/native/src/sha-1.c
Url: http://svn.apache.org/viewcvs/incubator/juice/native/src/sha-1.c?view=diff&rev=124268&p1=incubator/juice/native/src/sha-1.c&r1=124267&p2=incubator/juice/native/src/sha-1.c&r2=124268
==============================================================================
--- incubator/juice/native/src/sha-1.c (original)
+++ incubator/juice/native/src/sha-1.c Wed Jan 5 12:20:54 2005
@@ -29,7 +29,7 @@
#include "digest.h"
JNIEXPORT jbyteArray JNICALL
-Java_org_apache_security_jce_SHA_digest(JNIEnv *env, jobject obj, jbyteArray input) {
+Java_org_apache_security_jce_SHA_digest1(JNIEnv *env, jobject obj, jbyteArray input) {
jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
jsize arrayLength = (*env)->GetArrayLength(env, input);
@@ -43,4 +43,37 @@
jb = (*env)->NewByteArray(env, SHA_DIGEST_LENGTH);
(*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
return jb;
+}
+
+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_reset
+ (JNIEnv *env, jclass cl, jlong handle) {
+ _juice_evp_reset((EVP_MD_CTX *)handle,EVP_sha1());
+}
+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_SHA_update
+ (JNIEnv *env, jclass cl, jbyteArray input, jint offset, jint lon, jlong handle) {
+ jbyte *inputBytes = (*env)->GetByteArrayElements(env, input, NULL);
+ if (handle==0) {
+ handle=(jlong)_juice_evp_create(EVP_sha1());
+ }
+ printf("poraqui");
+ _juice_evp_update ((void*)(inputBytes+offset), lon,
+ (EVP_MD_CTX *)handle);
+ (*env)->ReleaseByteArrayElements(env, input, inputBytes, JNI_ABORT);
+ return handle;
+}
+
+JNIEXPORT jbyteArray JNICALL Java_org_apache_security_jce_SHA_digest
+ (JNIEnv *env, jclass cl, jlong handle) {
+ jbyteArray jb;
+ unsigned char md[SHA_DIGEST_LENGTH];
+
+ _juice_evp_digestCtx (md, NULL, (EVP_MD_CTX *)handle);
+
+ jb = (*env)->NewByteArray(env, SHA_DIGEST_LENGTH);
+ (*env)->SetByteArrayRegion(env, jb, 0, SHA_DIGEST_LENGTH, (jbyte *)md);
+ return jb;
+}
+JNIEXPORT void JNICALL Java_org_apache_security_jce_SHA_destroy
+ (JNIEnv *env, jclass cl, jlong handle) {
+ _juice_evp_destroy((EVP_MD_CTX*)handle);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: juice-svn-unsubscribe@xml.apache.org
For additional commands, e-mail: juice-svn-help@xml.apache.org