Skip to content

Commit

Permalink
Sync NativeCrypto.c with Java17 counterpart
Browse files Browse the repository at this point in the history
  • Loading branch information
KostasTsiounis committed Jan 16, 2025
1 parent e89a87f commit 7ff900a
Showing 1 changed file with 44 additions and 46 deletions.
90 changes: 44 additions & 46 deletions closed/src/java.base/share/native/libjncrypto/NativeCrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#define OPENSSL_SAME_MODE (-1)

/* needed for OpenSSL 1.0.2 Thread handling routines */
# define CRYPTO_LOCK 1
#define CRYPTO_LOCK 1

#if defined(WINDOWS)
# include <windows.h>
Expand All @@ -89,15 +89,15 @@ int OSSL102_RSA_set0_crt_params(RSA *, BIGNUM *, BIGNUM *, BIGNUM *);

/* Define literals from OpenSSL 1.1.x so that it compiles with OpenSSL 1.0.x. */
#ifndef EVP_CTRL_AEAD_GET_TAG
# define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
#define EVP_CTRL_AEAD_GET_TAG EVP_CTRL_GCM_GET_TAG
#endif

#ifndef EVP_CTRL_AEAD_SET_IVLEN
# define EVP_CTRL_AEAD_SET_IVLEN EVP_CTRL_GCM_SET_IVLEN
#define EVP_CTRL_AEAD_SET_IVLEN EVP_CTRL_GCM_SET_IVLEN
#endif

#ifndef EVP_CTRL_AEAD_SET_TAG
# define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
#define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
#endif

/* Whether loaded library is in FIPS mode. */
Expand Down Expand Up @@ -346,12 +346,12 @@ typedef struct OpenSSLMDContext {
} OpenSSLMDContext;

/* Handle errors from OpenSSL calls. */
static void printErrors(void) {
static void printErrors(void)
{
unsigned long errCode = 0;

fprintf(stderr, "An OpenSSL error occurred\n");
while(0 != (errCode = (*OSSL_get_error)()))
{
while (0 != (errCode = (*OSSL_get_error)())) {
char err_str[120];
(*OSSL_error_string_n)(errCode, err_str, (sizeof(err_str) / sizeof(char)));
fprintf(stderr, "%s\n", err_str);
Expand Down Expand Up @@ -515,7 +515,7 @@ get_crypto_library_version(jboolean traceEnabled, void *crypto_library, const ch
*/
OSSL_version = (OSSL_version_t*)find_crypto_symbol(crypto_library, "OpenSSL_version");

if (NULL == OSSL_version) {
if (NULL == OSSL_version) {
OSSL_version = (OSSL_version_t*)find_crypto_symbol(crypto_library, "SSLeay_version");

if (NULL == OSSL_version) {
Expand Down Expand Up @@ -557,11 +557,6 @@ get_crypto_library_version(jboolean traceEnabled, void *crypto_library, const ch
}
}

if (traceEnabled) {
fprintf(stderr, "Supported OpenSSL version: %s\n", openssl_version);
fflush(stderr);
}

/* Check whether the loaded OpenSSL library is in FIPS mode. */
if (ossl_ver >= OPENSSL_VERSION_3_0_0) {
typedef int OSSL_fipsmode_t(OSSL_LIB_CTX *);
Expand Down Expand Up @@ -893,7 +888,7 @@ Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
OSSL_sha384 = (OSSL_sha_t*)find_crypto_symbol(crypto_library, "EVP_sha384");
OSSL_sha512 = (OSSL_sha_t*)find_crypto_symbol(crypto_library, "EVP_sha512");

if(ossl_ver >= OPENSSL_VERSION_1_1_0) {
if (ossl_ver >= OPENSSL_VERSION_1_1_0) {
OSSL_MD_CTX_new = (OSSL_MD_CTX_new_t*)find_crypto_symbol(crypto_library, "EVP_MD_CTX_new");
OSSL_MD_CTX_reset = (OSSL_MD_CTX_reset_t*)find_crypto_symbol(crypto_library, "EVP_MD_CTX_reset");
OSSL_MD_CTX_free = (OSSL_MD_CTX_free_t*)find_crypto_symbol(crypto_library, "EVP_MD_CTX_free");
Expand Down Expand Up @@ -927,7 +922,7 @@ Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
OSSL_DecryptFinal = (OSSL_DecryptFinal_t*)find_crypto_symbol(crypto_library, "EVP_DecryptFinal");

/* Load the functions symbols for OpenSSL ChaCha20 algorithms. (Need OpenSSL 1.1.x or above) */
if(ossl_ver >= OPENSSL_VERSION_1_1_0) {
if (ossl_ver >= OPENSSL_VERSION_1_1_0) {
OSSL_chacha20 = (OSSL_cipher_t*)find_crypto_symbol(crypto_library, "EVP_chacha20");
OSSL_chacha20_poly1305 = (OSSL_cipher_t*)find_crypto_symbol(crypto_library, "EVP_chacha20_poly1305");
} else {
Expand All @@ -938,7 +933,7 @@ Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
/* Load the functions symbols for OpenSSL RSA algorithm. */
OSSL_RSA_new = (OSSL_RSA_new_t*)find_crypto_symbol(crypto_library, "RSA_new");

if(ossl_ver >= OPENSSL_VERSION_1_1_0) {
if (ossl_ver >= OPENSSL_VERSION_1_1_0) {
OSSL_RSA_set0_key = (OSSL_RSA_set0_key_t*)find_crypto_symbol(crypto_library, "RSA_set0_key");
OSSL_RSA_set0_factors = (OSSL_RSA_set0_factors_t*)find_crypto_symbol(crypto_library, "RSA_set0_factors");
OSSL_RSA_set0_crt_params = (OSSL_RSA_set0_key_t*)find_crypto_symbol(crypto_library, "RSA_set0_crt_params");
Expand Down Expand Up @@ -1341,8 +1336,8 @@ JNIEXPORT jboolean JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_isMD5Availab
* Signature: (JI)J
*/
JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestCreateContext
(JNIEnv *env, jclass thisObj, jlong copyContext, jint algoIdx) {

(JNIEnv *env, jclass thisObj, jlong copyContext, jint algoIdx)
{
EVP_MD_CTX *ctx = NULL;
const EVP_MD *digestAlg = NULL;
OpenSSLMDContext *context = NULL;
Expand Down Expand Up @@ -1429,8 +1424,8 @@ JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestCreateCon
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestDestroyContext
(JNIEnv *env, jclass thisObj, jlong c) {

(JNIEnv *env, jclass thisObj, jlong c)
{
OpenSSLMDContext *context = (OpenSSLMDContext*)(intptr_t) c;
if (NULL == context) {
return -1;
Expand Down Expand Up @@ -1458,8 +1453,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestDestroyCon
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestUpdate
(JNIEnv *env, jclass thisObj, jlong c, jbyteArray message, jint messageOffset,
jint messageLen) {

jint messageLen)
{
OpenSSLMDContext *context = (OpenSSLMDContext*)(intptr_t) c;
unsigned char* messageNative = NULL;

Expand Down Expand Up @@ -1494,8 +1489,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestUpdate
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestComputeAndReset
(JNIEnv *env, jclass thisObj, jlong c, jbyteArray message, jint messageOffset, jint messageLen,
jbyteArray digest, jint digestOffset, jint digestLen) {

jbyteArray digest, jint digestOffset, jint digestLen)
{
OpenSSLMDContext *context = (OpenSSLMDContext*)(intptr_t) c;

unsigned int size = 0;
Expand Down Expand Up @@ -1564,8 +1559,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestComputeAnd
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestReset
(JNIEnv *env, jclass thisObj, jlong c) {

(JNIEnv *env, jclass thisObj, jlong c)
{
OpenSSLMDContext *context = (OpenSSLMDContext*)(intptr_t) c;

if ((NULL == context) || (NULL == context->ctx) || (NULL == context->cachedInitializedDigestContext)) {
Expand Down Expand Up @@ -1601,8 +1596,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DigestReset
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CreateContext
(JNIEnv *env, jclass thisObj) {

(JNIEnv *env, jclass thisObj)
{
EVP_CIPHER_CTX *ctx = NULL;

/* Create and initialise the context */
Expand All @@ -1620,8 +1615,8 @@ JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CreateContext
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DestroyContext
(JNIEnv *env, jclass thisObj, jlong c) {

(JNIEnv *env, jclass thisObj, jlong c)
{
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX*)(intptr_t) c;
if (NULL == ctx) {
return -1;
Expand All @@ -1639,8 +1634,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_DestroyContext
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CBCInit
(JNIEnv *env, jclass thisObj, jlong c, jint mode, jbyteArray iv, jint iv_len,
jbyteArray key, jint key_len, jboolean doReset) {

jbyteArray key, jint key_len, jboolean doReset)
{
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX*)(intptr_t) c;
unsigned char* ivNative = NULL;
unsigned char* keyNative = NULL;
Expand Down Expand Up @@ -1701,8 +1696,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CBCInit
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CBCUpdate
(JNIEnv *env, jclass thisObj, jlong c, jbyteArray input, jint inputOffset, jint inputLen,
jbyteArray output, jint outputOffset) {

jbyteArray output, jint outputOffset)
{
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX*)(intptr_t) c;

int outputLen = 0;
Expand Down Expand Up @@ -1746,8 +1741,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CBCUpdate
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_CBCFinalEncrypt
(JNIEnv *env, jclass thisObj, jlong c, jbyteArray input, jint inputOffset, jint inputLen,
jbyteArray output, jint outputOffset) {

jbyteArray output, jint outputOffset)
{
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX*)(intptr_t) c;

if (NULL == ctx) {
Expand Down Expand Up @@ -2032,7 +2027,7 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_GCMDecrypt
}

if (inLen - tagLen > 0) {
if(0 == (*OSSL_DecryptUpdate)(ctx, outputNative + outOffset, &len, inputNative + inOffset, inLen - tagLen)) {
if (0 == (*OSSL_DecryptUpdate)(ctx, outputNative + outOffset, &len, inputNative + inOffset, inLen - tagLen)) {
printErrors();
goto cleanup;
}
Expand Down Expand Up @@ -2083,8 +2078,8 @@ BIGNUM* convertJavaBItoBN(unsigned char* in, int len);
* Signature: ([BI[BI)J
*/
JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_createRSAPublicKey
(JNIEnv *env, jclass obj, jbyteArray n, jint nLen, jbyteArray e, jint eLen) {

(JNIEnv *env, jclass obj, jbyteArray n, jint nLen, jbyteArray e, jint eLen)
{
unsigned char* nNative = NULL;
unsigned char* eNative = NULL;
RSA* publicRSAKey = NULL;
Expand Down Expand Up @@ -2134,7 +2129,8 @@ JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_createRSAPublic
* Signature: ([BI[BI[BI[BI[BI[BI[BI[BI)J
*/
JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_createRSAPrivateCrtKey
(JNIEnv *env, jclass obj, jbyteArray n, jint nLen, jbyteArray d, jint dLen, jbyteArray e, jint eLen, jbyteArray p, jint pLen, jbyteArray q, jint qLen, jbyteArray dp, jint dpLen, jbyteArray dq, jint dqLen, jbyteArray qinv, jint qinvLen) {
(JNIEnv *env, jclass obj, jbyteArray n, jint nLen, jbyteArray d, jint dLen, jbyteArray e, jint eLen, jbyteArray p, jint pLen, jbyteArray q, jint qLen, jbyteArray dp, jint dpLen, jbyteArray dq, jint dqLen, jbyteArray qinv, jint qinvLen)
{
unsigned char* nNative = NULL;
unsigned char* dNative = NULL;
unsigned char* eNative = NULL;
Expand Down Expand Up @@ -2301,7 +2297,8 @@ JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_createRSAPrivat
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_destroyRSAKey
(JNIEnv *env, jclass obj, jlong rsaKey) {
(JNIEnv *env, jclass obj, jlong rsaKey)
{
RSA* rsaKey2 = (RSA*)(intptr_t)rsaKey;
if (NULL != rsaKey2) {
(*OSSL_RSA_free)(rsaKey2);
Expand All @@ -2316,8 +2313,8 @@ JNIEXPORT void JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_destroyRSAKey
* Signature: ([BI[BJ)I
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_RSAEP
(JNIEnv *env, jclass obj, jbyteArray k, jint kLen, jbyteArray m, jlong publicRSAKey) {

(JNIEnv *env, jclass obj, jbyteArray k, jint kLen, jbyteArray m, jlong publicRSAKey)
{
unsigned char* kNative = NULL;
unsigned char* mNative = NULL;
RSA* rsaKey = NULL;
Expand Down Expand Up @@ -2353,8 +2350,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_RSAEP
* Signature: ([BI[BIJ)I
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_RSADP
(JNIEnv *env, jclass obj, jbyteArray k, jint kLen, jbyteArray m, jint verify, jlong privateRSAKey) {

(JNIEnv *env, jclass obj, jbyteArray k, jint kLen, jbyteArray m, jint verify, jlong privateRSAKey)
{
unsigned char* kNative = NULL;
unsigned char* mNative = NULL;
int msg_len = 0;
Expand Down Expand Up @@ -2435,7 +2432,8 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_RSADP
* Converts 2's complement representation of a big integer
* into an OpenSSL BIGNUM
*/
BIGNUM* convertJavaBItoBN(unsigned char* in, int len) {
BIGNUM* convertJavaBItoBN(unsigned char* in, int len)
{
/* first bit is neg */
int neg = (in[0] & 0x80);
int c = 1; /* carry bit */
Expand Down

0 comments on commit 7ff900a

Please sign in to comment.