Class OpenSslNative

java.lang.Object
org.apache.commons.crypto.cipher.OpenSslNative

final class OpenSslNative extends Object
JNI implementation for OpenSSL 1.x called from OpenSsl. The native methods in this class are defined in OpenSslNative.h (generated by javah).
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Hides this constructor from external access.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    clean(long context)
    Cleans the context at native.
    static int
    ctrl(long context, int type, int arg, byte[] data)
    Allows various cipher specific parameters to be determined and set.
    static int
    doFinal(long context, ByteBuffer output, int offset, int maxOutputLength)
    Finishes a multiple-part operation.
    static int
    doFinalByteArray(long context, byte[] output, int offset, int maxOutputLength)
    Finishes a multiple-part operation.
    static long
    init(long context, int mode, int alg, int padding, byte[] key, byte[] iv)
    Declares a native method to initialize the cipher context.
    static long
    initContext(int algorithm, int padding)
    Declares a native method to initialize the cipher context.
    static void
    Declares a native method to initialize JNI field and method IDs.
    static int
    update(long context, ByteBuffer input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength)
    Continues a multiple-part encryption/decryption operation.
    static int
    updateByteArray(long context, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
    Continues a multiple-part encryption/decryption operation.
    static int
    updateByteArrayByteBuffer(long context, byte[] input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength)
    Continues a multiple-part encryption/decryption operation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • OpenSslNative

      private OpenSslNative()
      Hides this constructor from external access.
  • Method Details

    • clean

      public static void clean(long context)
      Cleans the context at native.
      Parameters:
      context - The cipher context address
    • ctrl

      public static int ctrl(long context, int type, int arg, byte[] data)
      Allows various cipher specific parameters to be determined and set. it will call OpenSSL's API int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) In OpenSSL, data type of ptr can be char* or long*. Here, we map java's byte[] to native void*ptr. Note that the byte order is ByteOrder.nativeOrder.
      Parameters:
      context - The cipher context address
      type - CtrlValues
      arg - argument like a tag length
      data - byte buffer or null
      Returns:
      return 0 if there is any error, else return 1.
    • doFinal

      public static int doFinal(long context, ByteBuffer output, int offset, int maxOutputLength)
      Finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      context - The cipher context address
      output - The byte buffer for the result
      offset - The offset in output where the result is stored
      maxOutputLength - The maximum length for output
      Returns:
      The number of bytes stored in output
    • doFinalByteArray

      public static int doFinalByteArray(long context, byte[] output, int offset, int maxOutputLength)
      Finishes a multiple-part operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      context - The cipher context address
      output - The byte array for the result
      offset - The offset in output where the result is stored
      maxOutputLength - The maximum length for output
      Returns:
      The number of bytes stored in output
    • init

      public static long init(long context, int mode, int alg, int padding, byte[] key, byte[] iv)
      Declares a native method to initialize the cipher context.
      Parameters:
      context - The cipher context address
      mode - ENCRYPT_MODE or DECRYPT_MODE
      alg - Algorithm Mode of OpenSsl
      padding - the padding mode of OpenSsl cipher
      key - crypto key
      iv - crypto iv
      Returns:
      the context address of cipher
    • initContext

      public static long initContext(int algorithm, int padding)
      Declares a native method to initialize the cipher context.
      Parameters:
      algorithm - The algorithm name of cipher
      padding - The padding name of cipher
      Returns:
      the context address of cipher
    • initIDs

      public static void initIDs()
      Declares a native method to initialize JNI field and method IDs.
    • update

      public static int update(long context, ByteBuffer input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength)
      Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      context - The cipher context address
      input - The input byte buffer
      inputOffset - The offset in input where the input starts
      inputLength - The input length
      output - The byte buffer for the result
      outputOffset - The offset in output where the result is stored
      maxOutputLength - The maximum length for output
      Returns:
      The number of bytes stored in output
    • updateByteArray

      public static int updateByteArray(long context, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset, int maxOutputLength)
      Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      context - The cipher context address
      input - The input byte array
      inputOffset - The offset in input where the input starts
      inputLength - The input length
      output - The byte array for the result
      outputOffset - The offset in output where the result is stored
      maxOutputLength - The maximum length for output
      Returns:
      The number of bytes stored in output
    • updateByteArrayByteBuffer

      public static int updateByteArrayByteBuffer(long context, byte[] input, int inputOffset, int inputLength, ByteBuffer output, int outputOffset, int maxOutputLength)
      Continues a multiple-part encryption/decryption operation. The data is encrypted or decrypted, depending on how this cipher was initialized.
      Parameters:
      context - The cipher context address
      input - The input byte array
      inputOffset - The offset in input where the input starts
      inputLength - The input length
      output - The byte buffer for the result
      outputOffset - The offset in output where the result is stored
      maxOutputLength - The maximum length for output
      Returns:
      The number of bytes stored in output