Skip to content

Commit

Permalink
Merge 49b00e5 into 9456415
Browse files Browse the repository at this point in the history
  • Loading branch information
aremily committed Feb 16, 2019
2 parents 9456415 + 49b00e5 commit e70895c
Show file tree
Hide file tree
Showing 13 changed files with 1,011 additions and 425 deletions.
6 changes: 3 additions & 3 deletions Makefile.common
Expand Up @@ -179,10 +179,10 @@ Mac-x86_LIBNAME := libcommons-crypto.jnilib
Mac-x86_COMMONS_CRYPTO_FLAGS :=

Mac-x86_64_CC := gcc -arch $(OS_ARCH)
Mac-x86_64_CXX := g++ -arch $(OS_ARCH)
Mac-x86_64_CXX := gcc -arch $(OS_ARCH)
Mac-x86_64_STRIP := strip -x
Mac-x86_64_CFLAGS := -Ilib/inc_mac -I$(JAVA_HOME)/include -O2 -fPIC -mmacosx-version-min=10.5 -fvisibility=hidden -I/usr/local/include -I/usr/local/opt/openssl/include
Mac-x86_64_CXXFLAGS := -Ilib/inc_mac -I$(JAVA_HOME)/include -O2 -fPIC -mmacosx-version-min=10.5 -fvisibility=hidden -I/usr/local/include -I/usr/local/opt/openssl/include
Mac-x86_64_CFLAGS := -Ilib/inc_mac -I$(JAVA_HOME)/include -O2 -fPIC -mmacosx-version-min=10.7 -fvisibility=hidden -I/usr/local/include -I/usr/local/opt/openssl/include
Mac-x86_64_CXXFLAGS := -Ilib/inc_mac -I$(JAVA_HOME)/include -O2 -fPIC -mmacosx-version-min=10.7 -fvisibility=hidden -I/usr/local/include -I/usr/local/opt/openssl/include
Mac-x86_64_LINKFLAGS := -dynamiclib -L/usr/local/lib
Mac-x86_64_LIBNAME := libcommons-crypto.jnilib
Mac-x86_64_COMMONS_CRYPTO_FLAGS :=
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/apache/commons/crypto/Crypto.java
Expand Up @@ -132,8 +132,8 @@ public static void main(String args[]) throws Exception {
System.out.println("Native code loaded OK " + OpenSslInfoNative.NativeVersion());
System.out.println("Native Name " + OpenSslInfoNative.NativeName());
System.out.println("Native Built " + OpenSslInfoNative.NativeTimeStamp());
System.out.println("OpenSSL library loaded OK, version: 0x" + Long.toHexString(OpenSslInfoNative.SSLeay()));
System.out.println(OpenSslInfoNative.SSLeayVersion(0));
System.out.println("OpenSSL library loaded OK, version: 0x" + Long.toHexString(OpenSslInfoNative.OpenSSL()));
System.out.println("OpenSSL library info " + OpenSslInfoNative.OpenSSLVersion(0));
{
Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());
Expand All @@ -146,9 +146,9 @@ public static void main(String args[]) throws Exception {
CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", props);
System.out.println("Cipher instance created OK");
}
System.out.println("Additional SSLeay_version(n) details:");
System.out.println("Additional OpenSSL_version(n) details:");
for(int j=1;j<6;j++) {
System.out.println(j+": "+ OpenSslInfoNative.SSLeayVersion(j));
System.out.println(j+": "+ OpenSslInfoNative.OpenSSLVersion(j));
}
} else {
System.out.println("Native load failed: " + getLoadingError());
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/apache/commons/crypto/OpenSslInfoNative.java
Expand Up @@ -18,14 +18,17 @@
package org.apache.commons.crypto;

/**
* JNI interface of {@see CryptoRandom} implementation for OpenSSL.
* JNI interface of {@link CryptoRandom} implementation for OpenSSL.
* The native method in this class is defined in
* OpenSslCryptoRandomNative.h (generated at build time by javah)
* and implemented in the file
* src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c
*/
class OpenSslInfoNative {

public static final long VERSION_1_0_2X = 0x10002000;
public static final long VERSION_1_1_0X = 0x10100000;

/**
* Makes the constructor private.
*/
Expand All @@ -46,16 +49,18 @@ private OpenSslInfoNative() {
* @return timestamp of native
*/
public static native String NativeTimeStamp();



/**
* @return the value of SSLEAY_VERSION_NUMBER.
* @return the value of OPENSSL_VERSION_NUMBER.
*/
public static native long SSLeay();

public static native long OpenSSL();
/**
* Returns SSLeay_version according the version type.
* Returns OpenSSL_version according the version type.
*
* @param type The version type
* @return The text variant of the version number and the release date.
*/
public static native String SSLeayVersion(int type);
public static native String OpenSSLVersion(int type);
}
298 changes: 298 additions & 0 deletions src/main/java/org/apache/commons/crypto/jna/OpenSsl10XNativeJna.java
@@ -0,0 +1,298 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* limitations under the License.
*/

package org.apache.commons.crypto.jna;

import java.nio.ByteBuffer;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.PointerByReference;

class OpenSsl10XNativeJna {

static final boolean INIT_OK;

static final Throwable INIT_ERROR;

static {
boolean ok = false;
Throwable thrown = null;
try {
Native.register("crypto");
ok = true;
} catch (Exception e) {
thrown = e;
} catch (UnsatisfiedLinkError e) {
thrown = e;
} finally {
INIT_OK = ok;
INIT_ERROR = thrown;
}
}

/**
* @return OPENSSL_VERSION_NUMBER which is a numeric release version identifier
*/
public static native NativeLong SSLeay();

/**
* Retrieves version/build information about OpenSSL library.
*
* @param type
* type can be SSLEAY_VERSION, SSLEAY_CFLAGS, SSLEAY_BUILT_ON...
* @return A pointer to a constant string describing the version of the OpenSSL library or
* giving information about the library build.
*/
public static native String SSLeay_version(int type);

/**
* Registers the error strings for all libcrypto functions.
*/
public static native void ERR_load_crypto_strings();

/**
* @return the earliest error code from the thread's error queue without modifying it.
*/
public static native NativeLong ERR_peek_error();

/**
* Generates a human-readable string representing the error code e.
*
* @see <a>https://www.openssl.org/docs/manmaster/crypto/ERR_error_string.html</a>
*
* @param err
* the error code
* @param null_
* buf is NULL, the error string is placed in a static buffer
* @return the human-readable error messages.
*/
public static native String ERR_error_string(NativeLong err, char[] null_);

/**
* Creates a cipher context.
*
* @return a pointer to a newly created EVP_CIPHER_CTX for success and NULL for failure.
*/
public static native PointerByReference EVP_CIPHER_CTX_new();

/**
* EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset
*
* @param p
* cipher context
*/
public static native void EVP_CIPHER_CTX_init(PointerByReference p);

/**
* Enables or disables padding
*
* @param c
* cipher context
* @param pad
* If the pad parameter is zero then no padding is performed
* @return always returns 1
*/
public static native int EVP_CIPHER_CTX_set_padding(PointerByReference c, int pad);

/**
* @return an openssl AES evp cipher instance with a 128-bit key CBC mode
*/
public static native PointerByReference EVP_aes_128_cbc();

/**
* @return an openssl AES evp cipher instance with a 128-bit key CTR mode
*/
public static native PointerByReference EVP_aes_128_ctr();

/**
* @return an openssl AES evp cipher instance with a 192-bit key CBC mode
*/
public static native PointerByReference EVP_aes_192_cbc();

/**
* @return an openssl AES evp cipher instance with a 192-bit key CTR mode
*/
public static native PointerByReference EVP_aes_192_ctr();

/**
* @return an openssl AES evp cipher instance with a 256-bit key CBC mode
*/
public static native PointerByReference EVP_aes_256_cbc();

/**
* @return an openssl AES evp cipher instance with a 256-bit key CTR mode
*/
public static native PointerByReference EVP_aes_256_ctr();

/**
* Init a cipher.
*
* @param ctx
* cipher context
* @param cipher
* evp cipher instance
* @param impl
* engine
* @param key
* key
* @param iv
* iv
* @param enc
* 1 for encryption, 0 for decryption
* @return 1 for success and 0 for failure.
*/
public static native int EVP_CipherInit_ex(PointerByReference ctx, PointerByReference cipher,
PointerByReference impl, byte key[], byte iv[], int enc);

/**
* Continues a multiple-part encryption/decryption operation.
*
* @param ctx
* cipher context
* @param bout
* output byte buffer
* @param outl
* output length
* @param in
* input byte buffer
* @param inl
* input length
* @return 1 for success and 0 for failure.
*/
public static native int EVP_CipherUpdate(PointerByReference ctx, ByteBuffer bout, int[] outl,
ByteBuffer in, int inl);

/**
* Finishes a multiple-part operation.
*
* @param ctx
* cipher context
* @param bout
* output byte buffer
* @param outl
* output length
* @return 1 for success and 0 for failure.
*/
public static native int EVP_CipherFinal_ex(PointerByReference ctx, ByteBuffer bout,
int[] outl);

/**
* Clears all information from a cipher context and free up any allocated memory associate with
* it, including ctx itself.
*
* @param c
* openssl evp cipher
*/
public static native void EVP_CIPHER_CTX_free(PointerByReference c);

/**
* Clears all information from a cipher context and free up any allocated * memory associate
* with it.
*
* @param c
* openssl evp cipher
*/
public static native void EVP_CIPHER_CTX_cleanup(PointerByReference c);

// Random generator
/**
* OpenSSL uses for random number generation
*
* @return pointers to the respective methods
*/
public static native PointerByReference RAND_get_rand_method();

/**
* OpenSSL uses for random number generation.
*
* @return pointers to the respective methods
*/
public static native PointerByReference RAND_SSLeay();

/**
* Generates random data
*
* @param buf
* the bytes for generated random.
* @param num
* buffer length
* @return 1 on success, 0 otherwise.
*/
public static native int RAND_bytes(ByteBuffer buf, int num);

/**
* Releases all functional references.
*
* @param e
* engine reference.
* @return 0 on success, 1 otherwise.
*/
public static native int ENGINE_finish(PointerByReference e);

/**
* Frees the structural reference
*
* @param e
* engine reference.
* @return 0 on success, 1 otherwise.
*/
public static native int ENGINE_free(PointerByReference e);

/**
* Cleanups before program exit, it will avoid memory leaks.
*
* @return 0 on success, 1 otherwise.
*/
public static native int ENGINE_cleanup();

/**
* Obtains a functional reference from an existing structural reference.
*
* @param e
* engine reference
* @return zero if the ENGINE was not already operational and couldn't be successfully
* initialised
*/
public static native int ENGINE_init(PointerByReference e);

/**
* Sets the engine as the default for random number generation.
*
* @param e
* engine reference
* @param flags
* ENGINE_METHOD_RAND
* @return zero if failed.
*/
public static native int ENGINE_set_default(PointerByReference e, int flags);

/**
* Gets engine by id
*
* @param id
* engine id
* @return engine instance
*/
public static native PointerByReference ENGINE_by_id(String id);

/**
* Initializes the engine.
*/
public static native void ENGINE_load_rdrand();
}

0 comments on commit e70895c

Please sign in to comment.