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 2006/01/13 18:11:10 UTC

svn commit: r368788 - /incubator/juice/native/src/rsa.c

Author: raul
Date: Fri Jan 13 09:11:02 2006
New Revision: 368788

URL: http://svn.apache.org/viewcvs?rev=368788&view=rev
Log:
OPTIMIZATION: Use the fastest getPrimitiveArrayCritical
Added methods to get key objects.
Change mallocs for local arrays(possible problems in stack
 limited platforms, are there any?).

Modified:
    incubator/juice/native/src/rsa.c

Modified: incubator/juice/native/src/rsa.c
URL: http://svn.apache.org/viewcvs/incubator/juice/native/src/rsa.c?rev=368788&r1=368787&r2=368788&view=diff
==============================================================================
--- incubator/juice/native/src/rsa.c (original)
+++ incubator/juice/native/src/rsa.c Fri Jan 13 09:11:02 2006
@@ -62,7 +62,10 @@
 
   return key;
 }
-
+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_RSA_generatePublicKey
+  (JNIEnv *env, jclass cls, jobject publicKey) {
+	return (jlong)make_public_key (env,publicKey);
+};
 
 static RSA *
 make_private_key (JNIEnv  *env,
@@ -150,6 +153,15 @@
   return key;
 }
 
+JNIEXPORT jlong JNICALL Java_org_apache_security_jce_RSA_generatePrivateKey
+
+  (JNIEnv *env, jclass cls, jobject publicKey) {
+	return (jlong)make_private_key (env,publicKey);
+};
+JNIEXPORT void JNICALL Java_org_apache_security_jce_RSA_freeKey
+  (JNIEnv *env, jclass cls, jlong key) {
+  	RSA_free(key);
+  }
 /*
  * Class:     org_apache_security_jce_RSA
  * Method:    publicEncrypt
@@ -157,31 +169,32 @@
  */
 JNIEXPORT jbyteArray JNICALL 
 Java_org_apache_security_jce_RSA_publicEncrypt (JNIEnv    *env, 
-                                                           jobject    rsa, 
-                                                           jobject    publicKey,
+                                                           jclass    rsa, 
+                                                           jlong    publicKey,
                                                            jbyteArray inputByteArray)
 {
   RSA *key;
-  jbyte *inputBytes, *outputBytes;
+  jbyte *inputBytes;
   jbyteArray outputByteArray;
   int flen, tlen, outlen;
 
-  key  = make_public_key (env, publicKey);
+  key  = (RSA*)publicKey;
   tlen = RSA_size (key);
-  outputBytes = malloc (tlen);
+  jbyte outputBytes[tlen];
 
-  inputBytes = (*env)->GetByteArrayElements (env, inputByteArray, 0);
   flen = (*env)->GetArrayLength (env, inputByteArray);
-
+  inputBytes = (*env)->GetPrimitiveArrayCritical (env, inputByteArray, 0);
+  
   /* XXX: check output */
   outlen = RSA_public_encrypt (flen, (unsigned char *) inputBytes, (unsigned char *) outputBytes,
                                key, RSA_PKCS1_PADDING);
+  (*env)->ReleasePrimitiveArrayCritical (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);
+                                 
   outputByteArray = (*env)->NewByteArray (env, outlen);
   (*env)->SetByteArrayRegion (env, outputByteArray, 0, outlen, (jbyte *) outputBytes);
 
-  RSA_free (key);
-  free (outputBytes);
-  (*env)->ReleaseByteArrayElements (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);
+  //RSA_free (key);
+  // free (outputBytes);
 
   return outputByteArray;
 }
@@ -193,31 +206,31 @@
  */
 JNIEXPORT jbyteArray JNICALL 
 Java_org_apache_security_jce_RSA_publicDecrypt (JNIEnv    *env, 
-                                                           jobject    rsa, 
-                                                           jobject    publicKey, 
+                                                           jclass    rsa, 
+                                                           jlong    publicKey, 
                                                            jbyteArray inputByteArray)
 {
   RSA *key;
-  jbyte *inputBytes, *outputBytes;
+  jbyte *inputBytes;
   jbyteArray outputByteArray;
   int flen, tlen, outlen;
 
-  key  = make_public_key (env, publicKey);
+  key  = (RSA*)publicKey;
   tlen = RSA_size (key) - 11;
-  outputBytes = malloc (tlen);
+  jbyte outputBytes[tlen];
 
-  inputBytes = (*env)->GetByteArrayElements (env, inputByteArray, 0);
-  flen = (*env)->GetArrayLength (env, inputByteArray);
 
+  flen = (*env)->GetArrayLength (env, inputByteArray);
+  inputBytes = (*env)->GetPrimitiveArrayCritical (env, inputByteArray, 0);
   /* XXX: check output */
   outlen = RSA_public_decrypt (flen, (unsigned char *) inputBytes, (unsigned char *) outputBytes, 
                                key, RSA_PKCS1_PADDING);
+  (*env)->ReleasePrimitiveArrayCritical (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);                               
   outputByteArray = (*env)->NewByteArray (env, outlen);
   (*env)->SetByteArrayRegion (env, outputByteArray, 0, outlen, (jbyte *) outputBytes);
 
-  RSA_free (key);
-  free (outputBytes);
-  (*env)->ReleaseByteArrayElements (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);
+  //RSA_free (key);
+  //free (outputBytes);
 
   return outputByteArray;
 }
@@ -229,8 +242,8 @@
  */
 JNIEXPORT jbyteArray JNICALL 
 Java_org_apache_security_jce_RSA_privateEncrypt (JNIEnv    *env, 
-                                                            jobject    rsa, 
-                                                            jobject    privateCrtKey, 
+                                                            jclass    rsa, 
+                                                            jlong    privateCrtKey, 
                                                             jbyteArray inputByteArray)
 {
   RSA *key;
@@ -238,23 +251,25 @@
   jbyteArray outputByteArray;
   int flen, tlen, outlen;
 
-  key  = make_private_key (env, privateCrtKey);
+  key  = (RSA*)privateCrtKey;
 
   tlen = RSA_size (key);
   outputBytes = malloc (tlen);
 
-  inputBytes = (*env)->GetByteArrayElements (env, inputByteArray, 0);
   flen = (*env)->GetArrayLength (env, inputByteArray);
+  inputBytes = (*env)->GetPrimitiveArrayCritical (env, inputByteArray, 0);
+  
 
   /* XXX: check output */
   outlen = RSA_private_encrypt (flen, (unsigned char *) inputBytes, (unsigned char *) outputBytes, 
                                 key, RSA_PKCS1_PADDING);
+  (*env)->ReleasePrimitiveArrayCritical (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);                                
   outputByteArray = (*env)->NewByteArray (env, outlen);
   (*env)->SetByteArrayRegion (env, outputByteArray, 0, outlen, (jbyte *) outputBytes);
 
-  RSA_free (key);
+  //RSA_free (key);
   free (outputBytes);
-  (*env)->ReleaseByteArrayElements (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);
+
 
   return outputByteArray;
 }
@@ -266,8 +281,8 @@
  */
 JNIEXPORT jbyteArray JNICALL 
 Java_org_apache_security_jce_RSA_privateDecrypt (JNIEnv    *env, 
-                                                            jobject    rsa, 
-                                                            jobject    privateCrtKey, 
+                                                            jclass    rsa, 
+                                                            jlong    privateCrtKey, 
                                                             jbyteArray inputByteArray)
 {
   RSA *key;
@@ -275,21 +290,22 @@
   jbyteArray outputByteArray;
   int flen, tlen, outlen;
 
-  key  = make_private_key (env, privateCrtKey);
+  key  = (RSA*)privateCrtKey;
 
   tlen = RSA_size (key) - 11;
   outputBytes = malloc (tlen);
 
-  inputBytes = (*env)->GetByteArrayElements (env, inputByteArray, NULL);
+  
   flen = (*env)->GetArrayLength (env, inputByteArray);
-
+  inputBytes = (*env)->GetPrimitiveArrayCritical (env, inputByteArray, NULL);
   /* XXX: check output */
   outlen = RSA_private_decrypt (flen, (unsigned char *) inputBytes, (unsigned char *) outputBytes, 
                                 key, RSA_PKCS1_PADDING);
+  (*env)->ReleasePrimitiveArrayCritical (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);
   outputByteArray = (*env)->NewByteArray (env, outlen);
   (*env)->SetByteArrayRegion (env, outputByteArray, 0, outlen, (jbyte *) outputBytes);
 
-  RSA_free (key);
+  //RSA_free (key);
   free (outputBytes);
   (*env)->ReleaseByteArrayElements (env, inputByteArray, (jbyte *) inputBytes, JNI_ABORT);
 



---------------------------------------------------------------------
To unsubscribe, e-mail: juice-svn-unsubscribe@xml.apache.org
For additional commands, e-mail: juice-svn-help@xml.apache.org