Repository: commons-crypto Updated Branches: refs/heads/master 859bdaa04 -> 4a19325fd
http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c ---------------------------------------------------------------------- diff --git a/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c b/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c new file mode 100644 index 0000000..07dd4f9 --- /dev/null +++ b/src/main/native/org/apache/commons/crypto/random/OpenSslCryptoRandomNative.c @@ -0,0 +1,340 @@ +/** + * 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. + */ + +#include "org_apache_commons_crypto_random.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifdef UNIX +#include <pthread.h> +#include <unistd.h> +#include <sys/syscall.h> +#include <sys/types.h> +#endif + +#ifdef WINDOWS +#include <windows.h> +#endif + +// export the native interfaces +#ifdef JNIEXPORT +#undef JNIEXPORT +#endif +#define JNIEXPORT __attribute__((__visibility__("default"))) +#include "OpenSslCryptoRandomNative.h" + +#ifdef UNIX +static void * (*dlsym_CRYPTO_malloc) (int, const char *, int); +static void (*dlsym_CRYPTO_free) (void *); +static int (*dlsym_CRYPTO_num_locks) (void); +static void (*dlsym_CRYPTO_set_locking_callback) (void (*)()); +static void (*dlsym_CRYPTO_set_id_callback) (unsigned long (*)()); +static void (*dlsym_ENGINE_load_rdrand) (void); +static ENGINE * (*dlsym_ENGINE_by_id) (const char *); +static int (*dlsym_ENGINE_init) (ENGINE *); +static int (*dlsym_ENGINE_set_default) (ENGINE *, unsigned int); +static int (*dlsym_ENGINE_finish) (ENGINE *); +static int (*dlsym_ENGINE_free) (ENGINE *); +static void (*dlsym_ENGINE_cleanup) (void); +static int (*dlsym_RAND_bytes) (unsigned char *, int); +static unsigned long (*dlsym_ERR_get_error) (void); +#endif + +#ifdef WINDOWS +typedef void * (__cdecl *__dlsym_CRYPTO_malloc) (int, const char *, int); +typedef void (__cdecl *__dlsym_CRYPTO_free) (void *); +typedef int (__cdecl *__dlsym_CRYPTO_num_locks) (void); +typedef void (__cdecl *__dlsym_CRYPTO_set_locking_callback) \ + (void (*)(int, int, char *, int); +typedef void (__cdecl *__dlsym_ENGINE_load_rdrand) (void); +typedef ENGINE * (__cdecl *__dlsym_ENGINE_by_id) (const char *); +typedef int (__cdecl *__dlsym_ENGINE_init) (ENGINE *); +typedef int (__cdecl *__dlsym_ENGINE_set_default) (ENGINE *, unsigned int); +typedef int (__cdecl *__dlsym_ENGINE_finish) (ENGINE *); +typedef int (__cdecl *__dlsym_ENGINE_free) (ENGINE *); +typedef void (__cdecl *__dlsym_ENGINE_cleanup) (void); +typedef int (__cdecl *__dlsym_RAND_bytes) (unsigned char *, int); +typedef unsigned long (__cdecl *__dlsym_ERR_get_error) (void); +static __dlsym_CRYPTO_malloc dlsym_CRYPTO_malloc; +static __dlsym_CRYPTO_free dlsym_CRYPTO_free; +static __dlsym_CRYPTO_num_locks dlsym_CRYPTO_num_locks; +static __dlsym_CRYPTO_set_locking_callback dlsym_CRYPTO_set_locking_callback; +static __dlsym_ENGINE_load_rdrand dlsym_ENGINE_load_rdrand; +static __dlsym_ENGINE_by_id dlsym_ENGINE_by_id; +static __dlsym_ENGINE_init dlsym_ENGINE_init; +static __dlsym_ENGINE_set_default dlsym_ENGINE_set_default; +static __dlsym_ENGINE_finish dlsym_ENGINE_finish; +static __dlsym_ENGINE_free dlsym_ENGINE_free; +static __dlsym_ENGINE_cleanup dlsym_ENGINE_cleanup; +static __dlsym_RAND_bytes dlsym_RAND_bytes; +static __dlsym_ERR_get_error dlsym_ERR_get_error; +#endif + +static ENGINE * openssl_rand_init(void); +static void openssl_rand_clean(ENGINE *eng, int clean_locks); +static int openssl_rand_bytes(unsigned char *buf, int num); + +JNIEXPORT void JNICALL Java_org_apache_commons_crypto_random_OpenSslCryptoRandomNative_initSR + (JNIEnv *env, jclass clazz) +{ + char msg[1000]; +#ifdef UNIX + void *openssl = dlopen(COMMONS_CRYPTO_OPENSSL_LIBRARY, RTLD_LAZY | RTLD_GLOBAL); +#endif + +#ifdef WINDOWS + HMODULE openssl = LoadLibrary(COMMONS_CRYPTO_OPENSSL_LIBRARY); +#endif + + if (!openssl) { + snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \ + dlerror()); + THROW(env, "java/lang/UnsatisfiedLinkError", msg); + return; + } + +#ifdef UNIX + dlerror(); // Clear any existing error + LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_malloc, env, openssl, "CRYPTO_malloc"); + LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_free, env, openssl, "CRYPTO_free"); + LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_num_locks, env, openssl, "CRYPTO_num_locks"); + LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_set_locking_callback, \ + env, openssl, "CRYPTO_set_locking_callback"); + LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_set_id_callback, env, \ + openssl, "CRYPTO_set_id_callback"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_load_rdrand, env, \ + openssl, "ENGINE_load_rdrand"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_by_id, env, openssl, "ENGINE_by_id"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_init, env, openssl, "ENGINE_init"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_set_default, env, \ + openssl, "ENGINE_set_default"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_finish, env, openssl, "ENGINE_finish"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_free, env, openssl, "ENGINE_free"); + LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_cleanup, env, openssl, "ENGINE_cleanup"); + LOAD_DYNAMIC_SYMBOL(dlsym_RAND_bytes, env, openssl, "RAND_bytes"); + LOAD_DYNAMIC_SYMBOL(dlsym_ERR_get_error, env, openssl, "ERR_get_error"); +#endif + +#ifdef WINDOWS + LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_malloc, dlsym_CRYPTO_malloc, \ + env, openssl, "CRYPTO_malloc"); + LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_free, dlsym_CRYPTO_free, \ + env, openssl, "CRYPTO_free"); + LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_num_locks, dlsym_CRYPTO_num_locks, \ + env, openssl, "CRYPTO_num_locks"); + LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_set_locking_callback, \ + dlsym_CRYPTO_set_locking_callback, \ + env, openssl, "CRYPTO_set_locking_callback"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_load_rdrand, dlsym_ENGINE_load_rdrand, \ + env, openssl, "ENGINE_load_rdrand"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_by_id, dlsym_ENGINE_by_id, \ + env, openssl, "ENGINE_by_id"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_init, dlsym_ENGINE_init, \ + env, openssl, "ENGINE_init"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_set_default, dlsym_ENGINE_set_default, \ + env, openssl, "ENGINE_set_default"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_finish, dlsym_ENGINE_finish, \ + env, openssl, "ENGINE_finish"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_free, dlsym_ENGINE_free, \ + env, openssl, "ENGINE_free"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_cleanup, dlsym_ENGINE_cleanup, \ + env, openssl, "ENGINE_cleanup"); + LOAD_DYNAMIC_SYMBOL(__dlsym_RAND_bytes, dlsym_RAND_bytes, \ + env, openssl, "RAND_bytes"); + LOAD_DYNAMIC_SYMBOL(__dlsym_ERR_get_error, dlsym_ERR_get_error, \ + env, openssl, "ERR_get_error"); +#endif + + openssl_rand_init(); +} + +JNIEXPORT jboolean JNICALL Java_org_apache_commons_crypto_random_OpenSslCryptoRandomNative_nextRandBytes___3B + (JNIEnv *env, jobject object, jbyteArray bytes) +{ + if (NULL == bytes) { + THROW(env, "java/lang/NullPointerException", "Buffer cannot be null."); + return JNI_FALSE; + } + jbyte *b = (*env)->GetByteArrayElements(env, bytes, NULL); + if (NULL == b) { + THROW(env, "java/lang/InternalError", "Cannot get bytes array."); + return JNI_FALSE; + } + int b_len = (*env)->GetArrayLength(env, bytes); + int ret = openssl_rand_bytes((unsigned char *)b, b_len); + (*env)->ReleaseByteArrayElements(env, bytes, b, 0); + + if (1 != ret) { + return JNI_FALSE; + } + return JNI_TRUE; +} + +/** + * To ensure thread safety for random number generators, we need to call + * CRYPTO_set_locking_callback. + * http://wiki.openssl.org/index.php/Random_Numbers + * Example: crypto/threads/mttest.c + */ + +#ifdef WINDOWS +static void windows_locking_callback(int mode, int type, char *file, int line); +static HANDLE *lock_cs; + +static void locks_setup(void) +{ + int i; + lock_cs = dlsym_CRYPTO_malloc(dlsym_CRYPTO_num_locks() * sizeof(HANDLE), \ + __FILE__, __LINE__); + + for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { + lock_cs[i] = CreateMutex(NULL, FALSE, NULL); + } + dlsym_CRYPTO_set_locking_callback((void (*)(int, int, char *, int)) \ + windows_locking_callback); + /* id callback defined */ +} + +static void locks_cleanup(void) +{ + int i; + dlsym_CRYPTO_set_locking_callback(NULL); + + for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { + CloseHandle(lock_cs[i]); + } + dlsym_CRYPTO_free(lock_cs); +} + +static void windows_locking_callback(int mode, int type, char *file, int line) +{ + UNUSED(file), UNUSED(line); + + if (mode & CRYPTO_LOCK) { + WaitForSingleObject(lock_cs[type], INFINITE); + } else { + ReleaseMutex(lock_cs[type]); + } +} +#endif /* WINDOWS */ + +#ifdef UNIX +static void pthreads_locking_callback(int mode, int type, char *file, int line); +static unsigned long pthreads_thread_id(void); +static pthread_mutex_t *lock_cs; + +static void locks_setup(void) +{ + int i; + lock_cs = dlsym_CRYPTO_malloc(dlsym_CRYPTO_num_locks() * \ + sizeof(pthread_mutex_t), __FILE__, __LINE__); + + for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { + pthread_mutex_init(&(lock_cs[i]), NULL); + } + + dlsym_CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id); + dlsym_CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); +} + +static void locks_cleanup(void) +{ + int i; + dlsym_CRYPTO_set_locking_callback(NULL); + + for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { + pthread_mutex_destroy(&(lock_cs[i])); + } + + dlsym_CRYPTO_free(lock_cs); +} + +static void pthreads_locking_callback(int mode, int type, char *file, int line) +{ + UNUSED(file), UNUSED(line); + + if (mode & CRYPTO_LOCK) { + pthread_mutex_lock(&(lock_cs[type])); + } else { + pthread_mutex_unlock(&(lock_cs[type])); + } +} + +static unsigned long pthreads_thread_id(void) +{ + return (unsigned long)syscall(SYS_gettid); +} + +#endif /* UNIX */ + +/** + * If using an Intel chipset with RDRAND, the high-performance hardware + * random number generator will be used. + */ +static ENGINE * openssl_rand_init(void) +{ + locks_setup(); + + dlsym_ENGINE_load_rdrand(); + ENGINE *eng = dlsym_ENGINE_by_id("rdrand"); + + int ret = -1; + do { + if (NULL == eng) { + break; + } + + int rc = dlsym_ENGINE_init(eng); + if (0 == rc) { + break; + } + + rc = dlsym_ENGINE_set_default(eng, ENGINE_METHOD_RAND); + if (0 == rc) { + break; + } + + ret = 0; + } while(0); + + if (ret == -1) { + openssl_rand_clean(eng, 0); + } + + return eng; +} + +static void openssl_rand_clean(ENGINE *eng, int clean_locks) +{ + if (NULL != eng) { + dlsym_ENGINE_finish(eng); + dlsym_ENGINE_free(eng); + } + + dlsym_ENGINE_cleanup(); + if (clean_locks) { + locks_cleanup(); + } +} + +static int openssl_rand_bytes(unsigned char *buf, int num) +{ + return dlsym_RAND_bytes(buf, num); +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/main/native/org/apache/commons/crypto/random/OpensslCryptoRandomNative.c ---------------------------------------------------------------------- diff --git a/src/main/native/org/apache/commons/crypto/random/OpensslCryptoRandomNative.c b/src/main/native/org/apache/commons/crypto/random/OpensslCryptoRandomNative.c deleted file mode 100644 index 09c9af4..0000000 --- a/src/main/native/org/apache/commons/crypto/random/OpensslCryptoRandomNative.c +++ /dev/null @@ -1,340 +0,0 @@ -/** - * 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. - */ - -#include "org_apache_commons_crypto_random.h" - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#ifdef UNIX -#include <pthread.h> -#include <unistd.h> -#include <sys/syscall.h> -#include <sys/types.h> -#endif - -#ifdef WINDOWS -#include <windows.h> -#endif - -// export the native interfaces -#ifdef JNIEXPORT -#undef JNIEXPORT -#endif -#define JNIEXPORT __attribute__((__visibility__("default"))) -#include "OpensslCryptoRandomNative.h" - -#ifdef UNIX -static void * (*dlsym_CRYPTO_malloc) (int, const char *, int); -static void (*dlsym_CRYPTO_free) (void *); -static int (*dlsym_CRYPTO_num_locks) (void); -static void (*dlsym_CRYPTO_set_locking_callback) (void (*)()); -static void (*dlsym_CRYPTO_set_id_callback) (unsigned long (*)()); -static void (*dlsym_ENGINE_load_rdrand) (void); -static ENGINE * (*dlsym_ENGINE_by_id) (const char *); -static int (*dlsym_ENGINE_init) (ENGINE *); -static int (*dlsym_ENGINE_set_default) (ENGINE *, unsigned int); -static int (*dlsym_ENGINE_finish) (ENGINE *); -static int (*dlsym_ENGINE_free) (ENGINE *); -static void (*dlsym_ENGINE_cleanup) (void); -static int (*dlsym_RAND_bytes) (unsigned char *, int); -static unsigned long (*dlsym_ERR_get_error) (void); -#endif - -#ifdef WINDOWS -typedef void * (__cdecl *__dlsym_CRYPTO_malloc) (int, const char *, int); -typedef void (__cdecl *__dlsym_CRYPTO_free) (void *); -typedef int (__cdecl *__dlsym_CRYPTO_num_locks) (void); -typedef void (__cdecl *__dlsym_CRYPTO_set_locking_callback) \ - (void (*)(int, int, char *, int); -typedef void (__cdecl *__dlsym_ENGINE_load_rdrand) (void); -typedef ENGINE * (__cdecl *__dlsym_ENGINE_by_id) (const char *); -typedef int (__cdecl *__dlsym_ENGINE_init) (ENGINE *); -typedef int (__cdecl *__dlsym_ENGINE_set_default) (ENGINE *, unsigned int); -typedef int (__cdecl *__dlsym_ENGINE_finish) (ENGINE *); -typedef int (__cdecl *__dlsym_ENGINE_free) (ENGINE *); -typedef void (__cdecl *__dlsym_ENGINE_cleanup) (void); -typedef int (__cdecl *__dlsym_RAND_bytes) (unsigned char *, int); -typedef unsigned long (__cdecl *__dlsym_ERR_get_error) (void); -static __dlsym_CRYPTO_malloc dlsym_CRYPTO_malloc; -static __dlsym_CRYPTO_free dlsym_CRYPTO_free; -static __dlsym_CRYPTO_num_locks dlsym_CRYPTO_num_locks; -static __dlsym_CRYPTO_set_locking_callback dlsym_CRYPTO_set_locking_callback; -static __dlsym_ENGINE_load_rdrand dlsym_ENGINE_load_rdrand; -static __dlsym_ENGINE_by_id dlsym_ENGINE_by_id; -static __dlsym_ENGINE_init dlsym_ENGINE_init; -static __dlsym_ENGINE_set_default dlsym_ENGINE_set_default; -static __dlsym_ENGINE_finish dlsym_ENGINE_finish; -static __dlsym_ENGINE_free dlsym_ENGINE_free; -static __dlsym_ENGINE_cleanup dlsym_ENGINE_cleanup; -static __dlsym_RAND_bytes dlsym_RAND_bytes; -static __dlsym_ERR_get_error dlsym_ERR_get_error; -#endif - -static ENGINE * openssl_rand_init(void); -static void openssl_rand_clean(ENGINE *eng, int clean_locks); -static int openssl_rand_bytes(unsigned char *buf, int num); - -JNIEXPORT void JNICALL Java_org_apache_commons_crypto_random_OpensslCryptoRandomNative_initSR - (JNIEnv *env, jclass clazz) -{ - char msg[1000]; -#ifdef UNIX - void *openssl = dlopen(COMMONS_CRYPTO_OPENSSL_LIBRARY, RTLD_LAZY | RTLD_GLOBAL); -#endif - -#ifdef WINDOWS - HMODULE openssl = LoadLibrary(COMMONS_CRYPTO_OPENSSL_LIBRARY); -#endif - - if (!openssl) { - snprintf(msg, sizeof(msg), "Cannot load %s (%s)!", COMMONS_CRYPTO_OPENSSL_LIBRARY, \ - dlerror()); - THROW(env, "java/lang/UnsatisfiedLinkError", msg); - return; - } - -#ifdef UNIX - dlerror(); // Clear any existing error - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_malloc, env, openssl, "CRYPTO_malloc"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_free, env, openssl, "CRYPTO_free"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_num_locks, env, openssl, "CRYPTO_num_locks"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_set_locking_callback, \ - env, openssl, "CRYPTO_set_locking_callback"); - LOAD_DYNAMIC_SYMBOL(dlsym_CRYPTO_set_id_callback, env, \ - openssl, "CRYPTO_set_id_callback"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_load_rdrand, env, \ - openssl, "ENGINE_load_rdrand"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_by_id, env, openssl, "ENGINE_by_id"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_init, env, openssl, "ENGINE_init"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_set_default, env, \ - openssl, "ENGINE_set_default"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_finish, env, openssl, "ENGINE_finish"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_free, env, openssl, "ENGINE_free"); - LOAD_DYNAMIC_SYMBOL(dlsym_ENGINE_cleanup, env, openssl, "ENGINE_cleanup"); - LOAD_DYNAMIC_SYMBOL(dlsym_RAND_bytes, env, openssl, "RAND_bytes"); - LOAD_DYNAMIC_SYMBOL(dlsym_ERR_get_error, env, openssl, "ERR_get_error"); -#endif - -#ifdef WINDOWS - LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_malloc, dlsym_CRYPTO_malloc, \ - env, openssl, "CRYPTO_malloc"); - LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_free, dlsym_CRYPTO_free, \ - env, openssl, "CRYPTO_free"); - LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_num_locks, dlsym_CRYPTO_num_locks, \ - env, openssl, "CRYPTO_num_locks"); - LOAD_DYNAMIC_SYMBOL(__dlsym_CRYPTO_set_locking_callback, \ - dlsym_CRYPTO_set_locking_callback, \ - env, openssl, "CRYPTO_set_locking_callback"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_load_rdrand, dlsym_ENGINE_load_rdrand, \ - env, openssl, "ENGINE_load_rdrand"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_by_id, dlsym_ENGINE_by_id, \ - env, openssl, "ENGINE_by_id"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_init, dlsym_ENGINE_init, \ - env, openssl, "ENGINE_init"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_set_default, dlsym_ENGINE_set_default, \ - env, openssl, "ENGINE_set_default"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_finish, dlsym_ENGINE_finish, \ - env, openssl, "ENGINE_finish"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_free, dlsym_ENGINE_free, \ - env, openssl, "ENGINE_free"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ENGINE_cleanup, dlsym_ENGINE_cleanup, \ - env, openssl, "ENGINE_cleanup"); - LOAD_DYNAMIC_SYMBOL(__dlsym_RAND_bytes, dlsym_RAND_bytes, \ - env, openssl, "RAND_bytes"); - LOAD_DYNAMIC_SYMBOL(__dlsym_ERR_get_error, dlsym_ERR_get_error, \ - env, openssl, "ERR_get_error"); -#endif - - openssl_rand_init(); -} - -JNIEXPORT jboolean JNICALL Java_org_apache_commons_crypto_random_OpensslCryptoRandomNative_nextRandBytes___3B - (JNIEnv *env, jobject object, jbyteArray bytes) -{ - if (NULL == bytes) { - THROW(env, "java/lang/NullPointerException", "Buffer cannot be null."); - return JNI_FALSE; - } - jbyte *b = (*env)->GetByteArrayElements(env, bytes, NULL); - if (NULL == b) { - THROW(env, "java/lang/InternalError", "Cannot get bytes array."); - return JNI_FALSE; - } - int b_len = (*env)->GetArrayLength(env, bytes); - int ret = openssl_rand_bytes((unsigned char *)b, b_len); - (*env)->ReleaseByteArrayElements(env, bytes, b, 0); - - if (1 != ret) { - return JNI_FALSE; - } - return JNI_TRUE; -} - -/** - * To ensure thread safety for random number generators, we need to call - * CRYPTO_set_locking_callback. - * http://wiki.openssl.org/index.php/Random_Numbers - * Example: crypto/threads/mttest.c - */ - -#ifdef WINDOWS -static void windows_locking_callback(int mode, int type, char *file, int line); -static HANDLE *lock_cs; - -static void locks_setup(void) -{ - int i; - lock_cs = dlsym_CRYPTO_malloc(dlsym_CRYPTO_num_locks() * sizeof(HANDLE), \ - __FILE__, __LINE__); - - for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { - lock_cs[i] = CreateMutex(NULL, FALSE, NULL); - } - dlsym_CRYPTO_set_locking_callback((void (*)(int, int, char *, int)) \ - windows_locking_callback); - /* id callback defined */ -} - -static void locks_cleanup(void) -{ - int i; - dlsym_CRYPTO_set_locking_callback(NULL); - - for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { - CloseHandle(lock_cs[i]); - } - dlsym_CRYPTO_free(lock_cs); -} - -static void windows_locking_callback(int mode, int type, char *file, int line) -{ - UNUSED(file), UNUSED(line); - - if (mode & CRYPTO_LOCK) { - WaitForSingleObject(lock_cs[type], INFINITE); - } else { - ReleaseMutex(lock_cs[type]); - } -} -#endif /* WINDOWS */ - -#ifdef UNIX -static void pthreads_locking_callback(int mode, int type, char *file, int line); -static unsigned long pthreads_thread_id(void); -static pthread_mutex_t *lock_cs; - -static void locks_setup(void) -{ - int i; - lock_cs = dlsym_CRYPTO_malloc(dlsym_CRYPTO_num_locks() * \ - sizeof(pthread_mutex_t), __FILE__, __LINE__); - - for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { - pthread_mutex_init(&(lock_cs[i]), NULL); - } - - dlsym_CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id); - dlsym_CRYPTO_set_locking_callback((void (*)())pthreads_locking_callback); -} - -static void locks_cleanup(void) -{ - int i; - dlsym_CRYPTO_set_locking_callback(NULL); - - for (i = 0; i < dlsym_CRYPTO_num_locks(); i++) { - pthread_mutex_destroy(&(lock_cs[i])); - } - - dlsym_CRYPTO_free(lock_cs); -} - -static void pthreads_locking_callback(int mode, int type, char *file, int line) -{ - UNUSED(file), UNUSED(line); - - if (mode & CRYPTO_LOCK) { - pthread_mutex_lock(&(lock_cs[type])); - } else { - pthread_mutex_unlock(&(lock_cs[type])); - } -} - -static unsigned long pthreads_thread_id(void) -{ - return (unsigned long)syscall(SYS_gettid); -} - -#endif /* UNIX */ - -/** - * If using an Intel chipset with RDRAND, the high-performance hardware - * random number generator will be used. - */ -static ENGINE * openssl_rand_init(void) -{ - locks_setup(); - - dlsym_ENGINE_load_rdrand(); - ENGINE *eng = dlsym_ENGINE_by_id("rdrand"); - - int ret = -1; - do { - if (NULL == eng) { - break; - } - - int rc = dlsym_ENGINE_init(eng); - if (0 == rc) { - break; - } - - rc = dlsym_ENGINE_set_default(eng, ENGINE_METHOD_RAND); - if (0 == rc) { - break; - } - - ret = 0; - } while(0); - - if (ret == -1) { - openssl_rand_clean(eng, 0); - } - - return eng; -} - -static void openssl_rand_clean(ENGINE *eng, int clean_locks) -{ - if (NULL != eng) { - dlsym_ENGINE_finish(eng); - dlsym_ENGINE_free(eng); - } - - dlsym_ENGINE_cleanup(); - if (clean_locks) { - locks_cleanup(); - } -} - -static int openssl_rand_bytes(unsigned char *buf, int num) -{ - return dlsym_RAND_bytes(buf, num); -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java index 9f5f3f3..11908a1 100644 --- a/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java +++ b/src/test/java/org/apache/commons/crypto/cipher/AbstractCipherTest.java @@ -31,7 +31,7 @@ import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import org.apache.commons.crypto.conf.ConfigurationKeys; -import org.apache.commons.crypto.jna.OpensslJna; +import org.apache.commons.crypto.jna.OpenSslJna; import org.apache.commons.crypto.utils.ReflectionUtils; import org.apache.commons.crypto.utils.Utils; import org.junit.Assert; @@ -44,7 +44,7 @@ public abstract class AbstractCipherTest { public static final String JCE_CIPHER_CLASSNAME = JceCipher.class.getName(); - public static final String OPENSSLJNA_CIPHER_CLASSNAME = OpensslJna.getCipherClass().getName(); + public static final String OPENSSLJNA_CIPHER_CLASSNAME = OpenSslJna.getCipherClass().getName(); // data public static final int BYTEBUFFER_SIZE = 1000; http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java new file mode 100644 index 0000000..d144228 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/cipher/OpenSslCipherTest.java @@ -0,0 +1,175 @@ +/** + * 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.cipher; + +import java.nio.ByteBuffer; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.ShortBufferException; + +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Test; + +public class OpenSslCipherTest extends AbstractCipherTest { + + @Override + public void init() { + transformations = new String[] { + "AES/CBC/NoPadding", + "AES/CBC/PKCS5Padding", + "AES/CTR/NoPadding"}; + cipherClass = OPENSSL_CIPHER_CLASSNAME; + } + + @Test(expected = NoSuchAlgorithmException.class, timeout = 120000) + public void testInvalidAlgorithm() throws Exception { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + + try { + OpenSsl.getInstance("AES2/CTR/NoPadding"); + Assert.fail("Should specify correct algorithm."); + } catch (NoSuchAlgorithmException e) { + Assert.assertTrue(e.getMessage().contains( + "Doesn't support algorithm: AES2 and mode: CTR")); + throw e; + } + } + + @Test(expected = NoSuchPaddingException.class, timeout = 120000) + public void testInvalidPadding() throws Exception { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + + try { + OpenSsl.getInstance("AES/CTR/NoPadding2"); + Assert.fail("Should specify correct padding."); + } catch (NoSuchPaddingException e) { + Assert.assertTrue(e.getMessage().contains( + "Doesn't support padding: NoPadding2")); + throw e; + } + } + + @Test(expected = NoSuchAlgorithmException.class, timeout = 120000) + public void testInvalidMode() throws Exception { + try { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + OpenSsl.getInstance("AES/CTR2/NoPadding"); + Assert.fail("java.security.NoSuchAlgorithmException should be thrown."); + } catch (NoSuchAlgorithmException e) { + Assert.assertTrue(e.getMessage().contains( + "Doesn't support algorithm: AES and mode: CTR2")); + throw e; + } + } + + @Test(timeout = 120000) + public void testUpdateArguments() throws Exception { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + OpenSsl cipher = OpenSsl + .getInstance("AES/CTR/NoPadding"); + Assert.assertNotNull(cipher); + + cipher.init(OpenSsl.ENCRYPT_MODE, KEY, IV); + + // Require direct buffers + ByteBuffer input = ByteBuffer.allocate(1024); + ByteBuffer output = ByteBuffer.allocate(1024); + + try { + cipher.update(input, output); + Assert.fail("Input and output buffer should be direct buffer."); + } catch (IllegalArgumentException e) { + Assert.assertTrue(e.getMessage().contains( + "Direct buffers are required")); + } + + // Output buffer length should be sufficient to store output data + input = ByteBuffer.allocateDirect(1024); + output = ByteBuffer.allocateDirect(1000); + try { + cipher.update(input, output); + Assert.fail("Output buffer length should be sufficient " + + "to store output data"); + } catch (ShortBufferException e) { + Assert.assertTrue(e.getMessage().contains( + "Output buffer is not sufficient")); + } + } + + @Test(timeout = 120000) + public void testDoFinalArguments() throws Exception { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + OpenSsl cipher = OpenSsl + .getInstance("AES/CTR/NoPadding"); + Assert.assertNotNull(cipher); + + cipher.init(OpenSsl.ENCRYPT_MODE, KEY, IV); + + // Require direct buffer + ByteBuffer output = ByteBuffer.allocate(1024); + + try { + cipher.doFinal(output); + Assert.fail("Output buffer should be direct buffer."); + } catch (IllegalArgumentException e) { + Assert.assertTrue(e.getMessage().contains( + "Direct buffer is required")); + } + } + + @Test(expected = InvalidKeyException.class, timeout = 120000) + public void testInvalidKey() throws Exception { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + OpenSsl cipher = OpenSsl + .getInstance("AES/CTR/NoPadding"); + Assert.assertNotNull(cipher); + + final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, + 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 }; + try { + cipher.init(OpenSsl.ENCRYPT_MODE, invalidKey, IV); + Assert.fail("java.security.InvalidKeyException should be thrown."); + } catch (Exception e) { + Assert.assertTrue(e.getMessage().contains("Invalid AES key length: " + invalidKey.length + " bytes")); + throw e; + } + } + + @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000) + public void testInvalidIV() throws Exception { + Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); + OpenSsl cipher = OpenSsl + .getInstance("AES/CTR/NoPadding"); + Assert.assertNotNull(cipher); + + final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, + 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 }; + try { + cipher.init(OpenSsl.ENCRYPT_MODE, KEY, invalidIV); + Assert.fail("java.security.InvalidAlgorithmParameterException should be thrown."); + } catch (Exception e) { + Assert.assertTrue(e.getMessage().contains("Wrong IV length: must be 16 bytes long")); + throw e; + } + } + +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java b/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java deleted file mode 100644 index c5f0497..0000000 --- a/src/test/java/org/apache/commons/crypto/cipher/OpensslCipherTest.java +++ /dev/null @@ -1,175 +0,0 @@ -/** - * 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.cipher; - -import java.nio.ByteBuffer; -import java.security.InvalidAlgorithmParameterException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.ShortBufferException; - -import org.junit.Assert; -import org.junit.Assume; -import org.junit.Test; - -public class OpensslCipherTest extends AbstractCipherTest { - - @Override - public void init() { - transformations = new String[] { - "AES/CBC/NoPadding", - "AES/CBC/PKCS5Padding", - "AES/CTR/NoPadding"}; - cipherClass = OPENSSL_CIPHER_CLASSNAME; - } - - @Test(expected = NoSuchAlgorithmException.class, timeout = 120000) - public void testInvalidAlgorithm() throws Exception { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - - try { - OpenSsl.getInstance("AES2/CTR/NoPadding"); - Assert.fail("Should specify correct algorithm."); - } catch (NoSuchAlgorithmException e) { - Assert.assertTrue(e.getMessage().contains( - "Doesn't support algorithm: AES2 and mode: CTR")); - throw e; - } - } - - @Test(expected = NoSuchPaddingException.class, timeout = 120000) - public void testInvalidPadding() throws Exception { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - - try { - OpenSsl.getInstance("AES/CTR/NoPadding2"); - Assert.fail("Should specify correct padding."); - } catch (NoSuchPaddingException e) { - Assert.assertTrue(e.getMessage().contains( - "Doesn't support padding: NoPadding2")); - throw e; - } - } - - @Test(expected = NoSuchAlgorithmException.class, timeout = 120000) - public void testInvalidMode() throws Exception { - try { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - OpenSsl.getInstance("AES/CTR2/NoPadding"); - Assert.fail("java.security.NoSuchAlgorithmException should be thrown."); - } catch (NoSuchAlgorithmException e) { - Assert.assertTrue(e.getMessage().contains( - "Doesn't support algorithm: AES and mode: CTR2")); - throw e; - } - } - - @Test(timeout = 120000) - public void testUpdateArguments() throws Exception { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - OpenSsl cipher = OpenSsl - .getInstance("AES/CTR/NoPadding"); - Assert.assertNotNull(cipher); - - cipher.init(OpenSsl.ENCRYPT_MODE, KEY, IV); - - // Require direct buffers - ByteBuffer input = ByteBuffer.allocate(1024); - ByteBuffer output = ByteBuffer.allocate(1024); - - try { - cipher.update(input, output); - Assert.fail("Input and output buffer should be direct buffer."); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains( - "Direct buffers are required")); - } - - // Output buffer length should be sufficient to store output data - input = ByteBuffer.allocateDirect(1024); - output = ByteBuffer.allocateDirect(1000); - try { - cipher.update(input, output); - Assert.fail("Output buffer length should be sufficient " - + "to store output data"); - } catch (ShortBufferException e) { - Assert.assertTrue(e.getMessage().contains( - "Output buffer is not sufficient")); - } - } - - @Test(timeout = 120000) - public void testDoFinalArguments() throws Exception { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - OpenSsl cipher = OpenSsl - .getInstance("AES/CTR/NoPadding"); - Assert.assertNotNull(cipher); - - cipher.init(OpenSsl.ENCRYPT_MODE, KEY, IV); - - // Require direct buffer - ByteBuffer output = ByteBuffer.allocate(1024); - - try { - cipher.doFinal(output); - Assert.fail("Output buffer should be direct buffer."); - } catch (IllegalArgumentException e) { - Assert.assertTrue(e.getMessage().contains( - "Direct buffer is required")); - } - } - - @Test(expected = InvalidKeyException.class, timeout = 120000) - public void testInvalidKey() throws Exception { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - OpenSsl cipher = OpenSsl - .getInstance("AES/CTR/NoPadding"); - Assert.assertNotNull(cipher); - - final byte[] invalidKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, - 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 }; - try { - cipher.init(OpenSsl.ENCRYPT_MODE, invalidKey, IV); - Assert.fail("java.security.InvalidKeyException should be thrown."); - } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Invalid AES key length: " + invalidKey.length + " bytes")); - throw e; - } - } - - @Test(expected = InvalidAlgorithmParameterException.class, timeout = 120000) - public void testInvalidIV() throws Exception { - Assume.assumeTrue(OpenSsl.getLoadingFailureReason() == null); - OpenSsl cipher = OpenSsl - .getInstance("AES/CTR/NoPadding"); - Assert.assertNotNull(cipher); - - final byte[] invalidIV = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, - 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 }; - try { - cipher.init(OpenSsl.ENCRYPT_MODE, KEY, invalidIV); - Assert.fail("java.security.InvalidAlgorithmParameterException should be thrown."); - } catch (Exception e) { - Assert.assertTrue(e.getMessage().contains("Wrong IV length: must be 16 bytes long")); - throw e; - } - } - -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java new file mode 100644 index 0000000..6f5990a --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCipherTest.java @@ -0,0 +1,45 @@ +/** + * 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 org.apache.commons.crypto.cipher.AbstractCipherTest; +import org.apache.commons.crypto.jna.OpenSslJnaCipher; + +public class OpenSslJnaCipherTest extends AbstractCipherTest { + + @Override + public void init() { + transformations = new String[] { + "AES/CBC/NoPadding", + "AES/CBC/PKCS5Padding", + "AES/CTR/NoPadding" + }; + cipherClass = OpenSslJnaCipher.class.getName(); + } + + @Override + public void closeTestRepeat() { + System.err.println("closeTestRepeat() Causes JVM crash"); + } + + @Override + public void reInitAfterClose() { + System.err.println("reInitAfterClose() Causes JVM crash"); + } +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java new file mode 100644 index 0000000..990bfd2 --- /dev/null +++ b/src/test/java/org/apache/commons/crypto/jna/OpenSslJnaCryptoRandomTest.java @@ -0,0 +1,47 @@ +/** + * 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.security.GeneralSecurityException; +import java.util.Properties; + +import org.apache.commons.crypto.conf.ConfigurationKeys; +import org.apache.commons.crypto.jna.OpenSslJnaCryptoRandom; +import org.apache.commons.crypto.random.AbstractRandomTest; +import org.apache.commons.crypto.random.CryptoRandom; +import org.apache.commons.crypto.random.CryptoRandomFactory; + +import static org.junit.Assert.fail; + +public class OpenSslJnaCryptoRandomTest extends AbstractRandomTest { + + @Override + public CryptoRandom getCryptoRandom() throws GeneralSecurityException { + Properties props = new Properties(); + props.setProperty( + ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, + OpenSslJnaCryptoRandom.class.getName()); + CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); + if (!(random instanceof OpenSslJnaCryptoRandom)) { + fail("The CryptoRandom should be: " + + OpenSslJnaCryptoRandom.class.getName()); + } + return random; + } + +} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCipherTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCipherTest.java b/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCipherTest.java deleted file mode 100644 index 30b7fde..0000000 --- a/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCipherTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * 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 org.apache.commons.crypto.cipher.AbstractCipherTest; -import org.apache.commons.crypto.jna.OpenSslJnaCipher; - -public class OpensslJnaCipherTest extends AbstractCipherTest { - - @Override - public void init() { - transformations = new String[] { - "AES/CBC/NoPadding", - "AES/CBC/PKCS5Padding", - "AES/CTR/NoPadding" - }; - cipherClass = OpenSslJnaCipher.class.getName(); - } - - @Override - public void closeTestRepeat() { - System.err.println("closeTestRepeat() Causes JVM crash"); - } - - @Override - public void reInitAfterClose() { - System.err.println("reInitAfterClose() Causes JVM crash"); - } -} http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/4a19325f/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCryptoRandomTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCryptoRandomTest.java b/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCryptoRandomTest.java deleted file mode 100644 index 550b4a6..0000000 --- a/src/test/java/org/apache/commons/crypto/jna/OpensslJnaCryptoRandomTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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.security.GeneralSecurityException; -import java.util.Properties; - -import org.apache.commons.crypto.conf.ConfigurationKeys; -import org.apache.commons.crypto.jna.OpenSslJnaCryptoRandom; -import org.apache.commons.crypto.random.AbstractRandomTest; -import org.apache.commons.crypto.random.CryptoRandom; -import org.apache.commons.crypto.random.CryptoRandomFactory; - -import static org.junit.Assert.fail; - -public class OpensslJnaCryptoRandomTest extends AbstractRandomTest { - - @Override - public CryptoRandom getCryptoRandom() throws GeneralSecurityException { - Properties props = new Properties(); - props.setProperty( - ConfigurationKeys.SECURE_RANDOM_CLASSES_KEY, - OpenSslJnaCryptoRandom.class.getName()); - CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props); - if (!(random instanceof OpenSslJnaCryptoRandom)) { - fail("The CryptoRandom should be: " - + OpenSslJnaCryptoRandom.class.getName()); - } - return random; - } - -}
