Commit 9cf6e775 authored by Kenny Root's avatar Kenny Root
Browse files

Add NativeCrypto.EVP_PKEY_CTX_set_rsa_oaep_md

This will allow us to set the OAEP message digest function later on in
addition to the MGF1 message digest function.

Test:  mmma -j32 external/conscrypt && vogar --mode host --classpath out/host/common/obj/JAVA_LIBRARIES/core-tests-support-hostdex_intermediates/classes.jack --classpath out/host/common/obj/JAVA_LIBRARIES/conscrypt-hostdex_intermediates/classes.jack --classpath out/host/common/obj/JAVA_LIBRARIES/conscrypt-tests-hostdex_intermediates/classes.jack com.android.org.conscrypt.NativeCryptoTest
Change-Id: Iee45d973e253f3b5c60919d70571abb96d97bb08
parent 9f47ddf7
......@@ -248,6 +248,9 @@ public final class NativeCrypto {
public static native void EVP_PKEY_CTX_set_rsa_mgf1_md(long ctx, long evpMdRef)
throws InvalidAlgorithmParameterException;
public static native void EVP_PKEY_CTX_set_rsa_oaep_md(long ctx, long evpMdRef)
throws InvalidAlgorithmParameterException;
// --- Block ciphers -------------------------------------------------------
// These return const references
......
......@@ -3698,28 +3698,40 @@ static void NativeCrypto_EVP_PKEY_CTX_set_rsa_pss_saltlen(JNIEnv* env, jclass, j
JNI_TRACE("EVP_PKEY_CTX_set_rsa_pss_saltlen(%p, %d) => success", pkeyCtx, len);
}
static void NativeCrypto_EVP_PKEY_CTX_set_rsa_mgf1_md(JNIEnv* env, jclass, jlong ctx, jlong mdCtx) {
EVP_PKEY_CTX* pkeyCtx = reinterpret_cast<EVP_PKEY_CTX*>(ctx);
EVP_MD* md = reinterpret_cast<EVP_MD*>(mdCtx);
JNI_TRACE("EVP_PKEY_CTX_set_rsa_mgf1_md(%p, %p)", pkeyCtx, md);
static void evpPkeyCtxCtrlMdOp(JNIEnv* env, jlong pkeyCtxRef, jlong mdRef, const char* jniName,
int (*ctrl_func)(EVP_PKEY_CTX*, const EVP_MD*)) {
EVP_PKEY_CTX* pkeyCtx = reinterpret_cast<EVP_PKEY_CTX*>(pkeyCtxRef);
EVP_MD* md = reinterpret_cast<EVP_MD*>(mdRef);
JNI_TRACE("%s(%p, %p)", jniName, pkeyCtx, md);
if (pkeyCtx == nullptr) {
jniThrowNullPointerException(env, "ctx == null");
jniThrowNullPointerException(env, "pkeyCtx == null");
return;
}
if (md == nullptr) {
jniThrowNullPointerException(env, "mdCtx == null");
jniThrowNullPointerException(env, "md == null");
return;
}
int result = EVP_PKEY_CTX_set_rsa_mgf1_md(pkeyCtx, md);
int result = ctrl_func(pkeyCtx, md);
if (result <= 0) {
JNI_TRACE("ctx=%p EVP_PKEY_CTX_set_rsa_mgf1_md => threw exception", pkeyCtx);
throwExceptionIfNecessary(env, "EVP_PKEY_CTX_set_rsa_mgf1_md",
throwInvalidAlgorithmParameterException);
JNI_TRACE("ctx=%p %s => threw exception", pkeyCtx, jniName);
throwExceptionIfNecessary(env, jniName, throwInvalidAlgorithmParameterException);
return;
}
JNI_TRACE("EVP_PKEY_CTX_set_rsa_mgf1_md(%p, %p) => success", pkeyCtx, md);
JNI_TRACE("%s(%p, %p) => success", jniName, pkeyCtx, md);
}
static void NativeCrypto_EVP_PKEY_CTX_set_rsa_mgf1_md(JNIEnv* env, jclass, jlong pkeyCtxRef,
jlong mdRef) {
evpPkeyCtxCtrlMdOp(env, pkeyCtxRef, mdRef, "EVP_PKEY_CTX_set_rsa_mgf1_md",
EVP_PKEY_CTX_set_rsa_mgf1_md);
}
static void NativeCrypto_EVP_PKEY_CTX_set_rsa_oaep_md(JNIEnv* env, jclass, jlong pkeyCtxRef,
jlong mdRef) {
evpPkeyCtxCtrlMdOp(env, pkeyCtxRef, mdRef, "EVP_PKEY_CTX_set_rsa_oaep_md",
EVP_PKEY_CTX_set_rsa_oaep_md);
}
static jlong NativeCrypto_EVP_get_cipherbyname(JNIEnv* env, jclass, jstring algorithm) {
......@@ -9569,6 +9581,7 @@ static JNINativeMethod sNativeCryptoMethods[] = {
NATIVE_METHOD(NativeCrypto, EVP_PKEY_CTX_set_rsa_padding, "(JI)V"),
NATIVE_METHOD(NativeCrypto, EVP_PKEY_CTX_set_rsa_pss_saltlen, "(JI)V"),
NATIVE_METHOD(NativeCrypto, EVP_PKEY_CTX_set_rsa_mgf1_md, "(JJ)V"),
NATIVE_METHOD(NativeCrypto, EVP_PKEY_CTX_set_rsa_oaep_md, "(JJ)V"),
NATIVE_METHOD(NativeCrypto, EVP_get_cipherbyname, "(Ljava/lang/String;)J"),
NATIVE_METHOD(NativeCrypto, EVP_CipherInit_ex, "(" REF_EVP_CIPHER_CTX "J[B[BZ)V"),
NATIVE_METHOD(NativeCrypto, EVP_CipherUpdate, "(" REF_EVP_CIPHER_CTX "[BI[BII)I"),
......
......@@ -67,6 +67,7 @@ import javax.security.auth.x500.X500Principal;
import libcore.io.IoUtils;
import libcore.java.security.StandardNames;
import libcore.java.security.TestKeyStore;
import org.conscrypt.EvpMdRef;
import org.conscrypt.NativeCrypto.SSLHandshakeCallbacks;
import org.junit.After;
import org.junit.Test;
......@@ -2918,9 +2919,12 @@ public class NativeCryptoTest {
assertEqualByteArrays(expected, extension);
}
private static long getRawPkeyCtxForEncrypt() throws Exception {
return NativeCrypto.EVP_PKEY_encrypt_init(getRsaPkey(generateRsaKey()));
}
private static NativeRef.EVP_PKEY_CTX getPkeyCtxForEncrypt() throws Exception {
return new NativeRef.EVP_PKEY_CTX(
NativeCrypto.EVP_PKEY_encrypt_init(getRsaPkey(generateRsaKey())));
return new NativeRef.EVP_PKEY_CTX(getRawPkeyCtxForEncrypt());
}
@Test(expected = NullPointerException.class)
......@@ -2974,6 +2978,30 @@ public class NativeCryptoTest {
getPkeyCtxForEncrypt(), new byte[128], 0, new byte[128], 100, 29);
}
@Test(expected = NullPointerException.class)
public void EVP_PKEY_CTX_set_rsa_mgf1_md_NullPkeyCtx() throws Exception {
NativeCrypto.EVP_PKEY_CTX_set_rsa_mgf1_md(NULL, EvpMdRef.SHA256.EVP_MD);
}
@Test(expected = NullPointerException.class)
public void EVP_PKEY_CTX_set_rsa_mgf1_md_NullMdCtx() throws Exception {
long pkeyCtx = getRawPkeyCtxForEncrypt();
NativeRef.EVP_PKEY_CTX holder = new NativeRef.EVP_PKEY_CTX(pkeyCtx);
NativeCrypto.EVP_PKEY_CTX_set_rsa_mgf1_md(pkeyCtx, NULL);
}
@Test(expected = NullPointerException.class)
public void EVP_PKEY_CTX_set_rsa_oaep_md_NullPkeyCtx() throws Exception {
NativeCrypto.EVP_PKEY_CTX_set_rsa_oaep_md(NULL, EvpMdRef.SHA256.EVP_MD);
}
@Test(expected = NullPointerException.class)
public void EVP_PKEY_CTX_set_rsa_oaep_md_NullMdCtx() throws Exception {
long pkeyCtx = getRawPkeyCtxForEncrypt();
NativeRef.EVP_PKEY_CTX holder = new NativeRef.EVP_PKEY_CTX(pkeyCtx);
NativeCrypto.EVP_PKEY_CTX_set_rsa_oaep_md(pkeyCtx, NULL);
}
private static void assertContains(String actualValue, String expectedSubstring) {
if (actualValue == null) {
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment