Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6B3F080F2 for ; Fri, 9 Sep 2011 11:20:32 +0000 (UTC) Received: (qmail 53176 invoked by uid 500); 9 Sep 2011 11:20:29 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 52821 invoked by uid 500); 9 Sep 2011 11:20:05 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 52809 invoked by uid 99); 9 Sep 2011 11:19:55 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Sep 2011 11:19:55 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 09 Sep 2011 11:19:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 3A8E123889E3 for ; Fri, 9 Sep 2011 11:19:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1167100 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/ssl/Random.java native/modules/openssl/rand.c test/org/apache/commons/runtime/TestOpenSSL.java Date: Fri, 09 Sep 2011 11:19:33 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110909111933.3A8E123889E3@eris.apache.org> Author: mturk Date: Fri Sep 9 11:19:32 2011 New Revision: 1167100 URL: http://svn.apache.org/viewvc?rev=1167100&view=rev Log: Allow both standard and direct byte buffers to be used Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java?rev=1167100&r1=1167099&r2=1167100&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ssl/Random.java Fri Sep 9 11:19:32 2011 @@ -49,8 +49,8 @@ public final class Random private static native String getdef0(); private static native void setdef0(String path); - private static native int bytes0(byte[] b, int off, int len); - private static native int bytes1(ByteBuffer b, int off, int len); + private static native boolean bytes0(byte[] b, int off, int len); + private static native boolean bytes1(ByteBuffer b, int off, int len); private static native boolean seteng0(long ep); public static boolean seed(String path) @@ -96,7 +96,7 @@ public final class Random throw new IndexOutOfBoundsException(); synchronized(buf) { if (siz > 0) { - int n = siz > len ? len : pos; + int n = siz > len ? len : siz; System.arraycopy(buf, pos, bytes, off, n); off += n; len -= n; @@ -122,14 +122,32 @@ public final class Random synchronized(buf) { if (siz > 0) { int s = buffer.remaining(); - int n = siz > s ? s : pos; + int n = siz > s ? s : siz; buffer.put(buf, pos, n); pos += n; siz -= n; } } - if (buffer.remaining() > 0) - bytes1(buffer, buffer.position(), buffer.remaining()); + if (buffer.isDirect()) { + int nr = buffer.remaining(); + if (nr > 0) { + int bp = buffer.position(); + if (bytes1(buffer, bp, nr)) + buffer.position(bp + nr); + } + } + else { + int s; + while ((s = buffer.remaining()) > 0) { + bytes0(buf, 0, buf.length); + siz = buf.length; + pos = 0; + int n = siz > s ? s : siz; + buffer.put(buf, pos, n); + pos += n; + siz -= n; + } + } } public byte nextByte() Modified: commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c?rev=1167100&r1=1167099&r2=1167100&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/modules/openssl/rand.c Fri Sep 9 11:19:32 2011 @@ -157,11 +157,12 @@ ACR_SSL_EXPORT(void, Random, setdef0)(JN ACR_SSL_EXPORT(jint, Random, bytes0)(JNI_STDARGS, jbyteArray ba, jint off, jint len) { - jint rv = -1; + jboolean rv = JNI_FALSE; unsigned char *sb = (*env)->GetPrimitiveArrayCritical(env, ba, 0); if (sb != 0) { - rv = RAND_bytes(sb + off, len); + if (RAND_bytes(sb + off, len) > 0) + rv = JNI_TRUE; (*env)->ReleasePrimitiveArrayCritical(env, ba, sb, 0); } return rv; @@ -170,13 +171,12 @@ ACR_SSL_EXPORT(jint, Random, bytes0)(JNI ACR_SSL_EXPORT(jint, Random, bytes1)(JNI_STDARGS, jobject bb, jint off, jint len) { - jint rv = -1; unsigned char *sb = (*env)->GetDirectBufferAddress(env, bb); - if (sb != 0) { - rv = RAND_bytes(sb + off, len); - } - return rv; + if (sb != 0 && RAND_bytes(sb + off, len) > 0) + return JNI_TRUE; + else + return JNI_FALSE; } ACR_SSL_EXPORT(jboolean, Random, seteng0)(JNI_STDARGS, jlong ep) Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java?rev=1167100&r1=1167099&r2=1167100&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java (original) +++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestOpenSSL.java Fri Sep 9 11:19:32 2011 @@ -20,6 +20,7 @@ import org.testng.annotations.*; import org.testng.Assert; import java.io.IOException; import java.io.File; +import java.nio.ByteBuffer; import org.apache.commons.runtime.Native; public class TestOpenSSL extends Assert @@ -75,5 +76,32 @@ public class TestOpenSSL extends Assert } } + @Test(groups = { "openssl" }) + public void randomBytes() + throws Exception + { + byte[] b = new byte[1024]; + Random r = new Random(); + r.nextBytes(b); + r.nextByte(); + r.nextBytes(b); + ByteBuffer bb = ByteBuffer.allocateDirect(1024); + r.nextBytes(bb); + } + + @Test(groups = { "openssl" }) + public void randomBuffer() + throws Exception + { + Random r = new Random(); + r.nextByte(); + ByteBuffer db = ByteBuffer.allocateDirect(1964); + r.nextBytes(db); + assertEquals(db.remaining(), 0); + ByteBuffer bb = ByteBuffer.allocate(2303); + r.nextBytes(bb); + assertEquals(bb.remaining(), 0); + } + }