Loading ...
Sorry, an error occurred while loading the content.

crypto sample code from discussion on wednesday

Expand Messages
  • Eraticus Majoricus
    Thank you to everyone that came to the talk. There were definitely some good question and comments which kept a potentially boring subject lively. Here is the
    Message 1 of 1 , Dec 1, 2006
    View Source
    • 0 Attachment
      Thank you to everyone that came to the talk. There were definitely
      some good question and comments which kept a potentially boring
      subject lively.

      Here is the source code for the glue code we wrote for engineDoFinal,
      followed by a version with error checking and garbage collection.

      JNIEXPORT jint JNICALL
      Java_net_sulawesi_security_OpenSSLCipherDES_do_1final
      (JNIEnv *env, jobject obj, jint ctxp, jbyteArray jin, jint offset,
      jint len,
      jbyteArray jout, jint outoffset)
      {

      int retval = -1;

      int count1;
      int count2;

      jbyte *in;
      jbyte *out;

      EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *) ctxp;

      in = (*env)->GetPrimitiveArrayCritical(env, jin, NULL);
      out = (*env)->GetPrimitiveArrayCritical(env, jout, NULL);

      /*** do crypto here ***/
      EVP_CipherUpdate(ctx, out + outoffset, &count1, in + offset, len);
      EVP_CipherFinal_ex(ctx, out + outoffset + count1, &count2);
      retval = count1 + count2;
      /*** end crypto here ***/

      return retval;
      }

      Here's a more complete version, since it checks the error values from
      the JNI code and properly releases pinned memory. Also, it does all
      "required" casts to keep the compiler quiet.

      JNIEXPORT jint JNICALL
      Java_net_sulawesi_security_OpenSSLCipherDES_do_1final
      (JNIEnv *env, jobject obj, jint ctxp, jbyteArray jin, jint offset,
      jint len,
      jbyteArray jout, jint outoffset)
      {

      int retval = -1;

      int count1;
      int count2;

      jbyte *in;
      jbyte *out;

      EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *) ctxp;

      in = (*env)->GetPrimitiveArrayCritical(env, jin, NULL);
      if (in == NULL) /* exception thrown, so short circuit out */
      {
      goto error1;
      }
      out = (*env)->GetPrimitiveArrayCritical(env, jout, NULL);
      if (in == NULL) /* exception thrown, so short circuit out */
      {
      goto error2;
      }

      /*** do crypto here ***/
      EVP_CipherUpdate(ctx, (unsigned char *) out + outoffset, &count1,
      (unsigned char *) in + offset, len);
      EVP_CipherFinal_ex(ctx, (unsigned char *) out + outoffset +
      count1,
      &count2);
      retval = count1 + count2;
      /*** end crypto here ***/

      (**env).ReleasePrimitiveArrayCritical(env, jout, out, 0);
      error2:
      (**env).ReleasePrimitiveArrayCritical(env, jin, in, JNI_ABORT);
      error1:

      return retval;
    Your message has been successfully submitted and would be delivered to recipients shortly.