Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRYPTO-161 Add SM4 encryption support #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class OpenSsl {

/** Currently only support AES/CTR/NoPadding. */
private enum AlgorithmMode {
AES_CTR, AES_CBC, AES_GCM;
AES_CTR, AES_CBC, AES_GCM, SM4_CTR, SM4_CBC;

/**
* Gets the mode.
Expand Down
39 changes: 36 additions & 3 deletions src/main/native/org/apache/commons/crypto/cipher/OpenSslNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static EVP_CIPHER * (*dlsym_EVP_aes_128_cbc)(void);
static EVP_CIPHER * (*dlsym_EVP_aes_256_gcm)(void);
static EVP_CIPHER * (*dlsym_EVP_aes_192_gcm)(void);
static EVP_CIPHER * (*dlsym_EVP_aes_128_gcm)(void);
static EVP_CIPHER * (*dlsym_EVP_sm4_ctr)(void);
static EVP_CIPHER * (*dlsym_EVP_sm4_cbc)(void);
#endif

#ifdef WINDOWS
Expand Down Expand Up @@ -82,6 +84,8 @@ typedef EVP_CIPHER * (__cdecl *__dlsym_EVP_aes_128_cbc)(void);
typedef EVP_CIPHER * (__cdecl *__dlsym_EVP_aes_256_gcm)(void);
typedef EVP_CIPHER * (__cdecl *__dlsym_EVP_aes_192_gcm)(void);
typedef EVP_CIPHER * (__cdecl *__dlsym_EVP_aes_128_gcm)(void);
typedef EVP_CIPHER * (__cdecl *__dlsym_EVP_sm4_ctr)(void);
typedef EVP_CIPHER * (__cdecl *__dlsym_EVP_sm4_cbc)(void);
static __dlsym_EVP_CIPHER_CTX_new dlsym_EVP_CIPHER_CTX_new;
static __dlsym_EVP_CIPHER_CTX_free dlsym_EVP_CIPHER_CTX_free;
static __dlsym_EVP_CIPHER_CTX_set_padding dlsym_EVP_CIPHER_CTX_set_padding;
Expand All @@ -102,6 +106,8 @@ static __dlsym_EVP_aes_128_cbc dlsym_EVP_aes_128_cbc;
static __dlsym_EVP_aes_256_gcm dlsym_EVP_aes_256_gcm;
static __dlsym_EVP_aes_192_gcm dlsym_EVP_aes_192_gcm;
static __dlsym_EVP_aes_128_gcm dlsym_EVP_aes_128_gcm;
static __dlsym_EVP_sm4_ctr dlsym_EVP_sm4_ctr;
static __dlsym_EVP_sm4_cbc dlsym_EVP_sm4_cbc;
#endif

static void loadAes(JNIEnv *env, HMODULE openssl)
Expand All @@ -124,6 +130,10 @@ static void loadAes(JNIEnv *env, HMODULE openssl)
env, openssl, "EVP_aes_192_gcm");
LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_aes_128_gcm, dlsym_EVP_aes_128_gcm, \
env, openssl, "EVP_aes_128_gcm");
LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_sm4_ctr, dlsym_EVP_sm4_ctr, \
env, openssl, "EVP_sm4_ctr");
LOAD_DYNAMIC_SYMBOL(__dlsym_EVP_sm4_cbc, dlsym_EVP_sm4_cbc, \
env, openssl, "EVP_sm4_cbc");
}

JNIEXPORT void JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initIDs
Expand Down Expand Up @@ -240,13 +250,16 @@ static EVP_CIPHER_CTX * get_context(JNIEnv *env, jlong addr) {
JNIEXPORT jlong JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_initContext
(JNIEnv *env, jclass clazz, jint alg, jint padding)
{
if (alg != AES_CTR && alg != AES_CBC && alg != AES_GCM) {
if (alg != AES_CTR && alg != AES_CBC && alg != AES_GCM
&& alg != SM4_CTR && alg != SM4_CBC) {
THROW(env, "java/security/NoSuchAlgorithmException", NULL);
return (jlong)0;
}
if (!(alg == AES_CTR && padding == NOPADDING)
&& !(alg == AES_CBC && (padding == NOPADDING|| padding == PKCS5PADDING))
&& !(alg == AES_GCM && padding == NOPADDING)) {
&& !(alg == AES_GCM && padding == NOPADDING)
&& !(alg == SM4_CTR && (padding == NOPADDING || padding == PKCS5PADDING))
&& !(alg == SM4_CBC && (padding == NOPADDING || padding == PKCS5PADDING))) {
THROW(env, "javax/crypto/NoSuchPaddingException", NULL);
return (jlong)0;
}
Expand All @@ -272,6 +285,18 @@ JNIEXPORT jlong JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_init
return (jlong)0;
}

if (dlsym_EVP_sm4_ctr == NULL) {
THROW(env, "java/security/NoSuchAlgorithmException", \
"Doesn't support SM4 CTR.");
return (jlong)0;
}

if (dlsym_EVP_sm4_cbc == NULL) {
THROW(env, "java/security/NoSuchAlgorithmException", \
"Doesn't support SM4 CBC.");
return (jlong)0;
}

EVP_CTX_Wrapper *wrapper = new_context_wrapper(env);
return JLONG(wrapper);
}
Expand Down Expand Up @@ -304,6 +329,14 @@ static EVP_CIPHER * getEvpCipher(int alg, int keyLen)
} else if (keyLen == KEY_LENGTH_128) {
cipher = dlsym_EVP_aes_128_gcm();
}
} else if (alg == SM4_CTR) {
if (keyLen == KEY_LENGTH_128) {
cipher = dlsym_EVP_sm4_ctr();
}
} else if (alg == SM4_CBC) {
if (keyLen == KEY_LENGTH_128) {
cipher = dlsym_EVP_sm4_cbc();
}
}
return cipher;
}
Expand Down Expand Up @@ -350,7 +383,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_commons_crypto_cipher_OpenSslNative_init
goto cleanup;
}

if (!(alg == AES_CTR || alg == AES_CBC || alg == AES_GCM)) {
if (!(alg == AES_CTR || alg == AES_CBC || alg == AES_GCM || alg == SM4_CTR || alg == SM4_CBC)) {
THROW(env, "java/security/NoSuchAlgorithmException", "The algorithm is not supported.");
goto cleanup;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ static FARPROC WINAPI do_dlsym_fallback(JNIEnv *env, HMODULE handle, LPCSTR symb
#define AES_CTR 0
#define AES_CBC 1
#define AES_GCM 2
#define SM4_CTR 3
#define SM4_CBC 4

#define NOPADDING 0
#define PKCS5PADDING 1
Expand Down