setRandomInBuffer uses java.lang.SecureRandom.

release-v2.2.0
ylecollen 2016-12-21 15:10:54 +01:00
parent bacdc3c539
commit 643165067f
5 changed files with 68 additions and 14 deletions

View File

@ -104,7 +104,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
LOGD("## initNewAccount(): randomSize=%lu", static_cast<long unsigned int>(randomSize)); LOGD("## initNewAccount(): randomSize=%lu", static_cast<long unsigned int>(randomSize));
// allocate random buffer // allocate random buffer
if((0!=randomSize) && !setRandomInBuffer(&randomBuffPtr, randomSize)) if((0!=randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
{ {
LOGE("## initNewAccount(): failure - random buffer init"); LOGE("## initNewAccount(): failure - random buffer init");
} }
@ -234,7 +234,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject
randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys); randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys);
LOGD("## generateOneTimeKeysJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength)); LOGD("## generateOneTimeKeysJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength));
if((0!=randomLength) && !setRandomInBuffer(&randomBufferPtr, randomLength)) if((0!=randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength))
{ {
LOGE("## generateOneTimeKeysJni(): failure - random buffer init"); LOGE("## generateOneTimeKeysJni(): failure - random buffer init");
} }

View File

@ -67,7 +67,7 @@ extern "C" {
#endif #endif
// internal helper functions // internal helper functions
bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize); bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize);
jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);

View File

@ -29,10 +29,11 @@ using namespace AndroidOlmSdk;
* @param aRandomSize the number of random values to apply * @param aRandomSize the number of random values to apply
* @return true if operation succeed, false otherwise * @return true if operation succeed, false otherwise
**/ **/
bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize) bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize)
{ {
bool retCode = false; bool retCode = false;
struct timeval timeValue; struct timeval timeValue;
int bufferLen = aRandomSize*sizeof(uint8_t);
if(NULL == aBuffer2Ptr) if(NULL == aBuffer2Ptr)
{ {
@ -42,7 +43,7 @@ bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize)
{ {
LOGE("## setRandomInBuffer(): failure - random size=0"); LOGE("## setRandomInBuffer(): failure - random size=0");
} }
else if(NULL == (*aBuffer2Ptr = (uint8_t*)malloc(aRandomSize*sizeof(uint8_t)))) else if(NULL == (*aBuffer2Ptr = (uint8_t*)malloc(bufferLen)))
{ {
LOGE("## setRandomInBuffer(): failure - alloc mem OOM"); LOGE("## setRandomInBuffer(): failure - alloc mem OOM");
} }
@ -50,16 +51,69 @@ bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize)
{ {
LOGD("## setRandomInBuffer(): randomSize=%lu",static_cast<long unsigned int>(aRandomSize)); LOGD("## setRandomInBuffer(): randomSize=%lu",static_cast<long unsigned int>(aRandomSize));
gettimeofday(&timeValue, NULL); bool secureRandomSucceeds = false;
srand(timeValue.tv_usec); // init seed
for(size_t i=0;i<aRandomSize;i++) // clear the buffer
memset(*aBuffer2Ptr, 0, bufferLen);
// use the secureRandom class
jclass cls = env->FindClass("java/security/SecureRandom");
if (cls)
{ {
(*aBuffer2Ptr)[i] = (uint8_t)(rand()%ACCOUNT_CREATION_RANDOM_MODULO); jobject newObj = 0;
// debug purpose jmethodID constructor = env->GetMethodID(cls, "<init>", "()V");
//LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]); jmethodID nextByteMethod = env->GetMethodID(cls, "nextBytes", "([B)V");
if (constructor)
{
newObj = env->NewObject(cls, constructor);
jbyteArray tempByteArray = env->NewByteArray(bufferLen);
if (newObj && tempByteArray)
{
env->CallVoidMethod(newObj, nextByteMethod, tempByteArray);
jbyte* buffer = env->GetByteArrayElements(tempByteArray,0);
if (buffer)
{
memcpy(*aBuffer2Ptr, buffer, bufferLen);
secureRandomSucceeds = true;
}
}
if (tempByteArray)
{
env->DeleteLocalRef(tempByteArray);
}
if (newObj)
{
env->DeleteLocalRef(newObj);
}
}
} }
if (!secureRandomSucceeds)
{
LOGE("## setRandomInBuffer(): SecureRandom failed, use a fallback");
gettimeofday(&timeValue, NULL);
srand(timeValue.tv_usec); // init seed
for(size_t i=0;i<aRandomSize;i++)
{
(*aBuffer2Ptr)[i] = (uint8_t)(rand()%ACCOUNT_CREATION_RANDOM_MODULO);
}
}
// debug purpose
/*for(int i = 0; i < aRandomSize; i++)
{
LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]);
}*/
retCode = true; retCode = true;
} }
return retCode; return retCode;

View File

@ -102,7 +102,7 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(
// compute random buffer // compute random buffer
size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr);
LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength)); LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength));
if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength)) if((0!=randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
{ {
LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init"); LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init");
} }

View File

@ -132,7 +132,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
{ // allocate random buffer { // allocate random buffer
size_t randomSize = olm_create_outbound_session_random_length(sessionPtr); size_t randomSize = olm_create_outbound_session_random_length(sessionPtr);
LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize)); LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize));
if((0!=randomSize) && !setRandomInBuffer(&randomBuffPtr, randomSize)) if((0!=randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
{ {
LOGE("## initOutboundSessionJni(): failure - random buffer init"); LOGE("## initOutboundSessionJni(): failure - random buffer init");
} }
@ -497,7 +497,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
// it just does not need new random data to encrypt a new message // it just does not need new random data to encrypt a new message
size_t randomLength = olm_encrypt_random_length(sessionPtr); size_t randomLength = olm_encrypt_random_length(sessionPtr);
LOGD("## encryptMessageJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength)); LOGD("## encryptMessageJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength));
if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength)) if((0!=randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
{ {
LOGE("## encryptMessageJni(): failure - random buffer init"); LOGE("## encryptMessageJni(): failure - random buffer init");
} }