@@ -96,7 +96,11 @@ JNIEXPORT jobject JNICALL Java_io_pijun_george_Sodium_symmetricKeyEncrypt(
9696 JNIEnv * env , jclass cls , jbyteArray msg , jbyteArray key ) {
9797 unsigned long long msgLen = (unsigned long long )(* env )-> GetArrayLength (env , msg );
9898 unsigned long long cipherTextLen = msgLen + crypto_secretbox_MACBYTES ;
99- unsigned char cipherText [cipherTextLen ];
99+ unsigned char * cipherText = malloc (cipherTextLen );
100+ if (cipherText == NULL ) {
101+ __android_log_print (ANDROID_LOG_ERROR , "ZoodLoc" , "symmetricKeyEncrypt: failed to allocate cipherText" );
102+ return NULL ;
103+ }
100104 unsigned char * msgUChar = ucharArray (env , msg );
101105 unsigned char nonceUChar [crypto_secretbox_NONCEBYTES ];
102106 unsigned char * keyUChar = ucharArray (env , key );
@@ -109,6 +113,7 @@ JNIEXPORT jobject JNICALL Java_io_pijun_george_Sodium_symmetricKeyEncrypt(
109113 free (msgUChar );
110114 free (keyUChar );
111115 if (result != 0 ) {
116+ free (cipherText );
112117 return NULL ;
113118 }
114119
@@ -124,6 +129,7 @@ JNIEXPORT jobject JNICALL Java_io_pijun_george_Sodium_symmetricKeyEncrypt(
124129 jbyteArray nonceByteArray = byteArray (env , (unsigned char * )nonceUChar , crypto_secretbox_NONCEBYTES );
125130 (* env )-> SetObjectField (env , skem , nonceFieldId , nonceByteArray );
126131
132+ free (cipherText );
127133 return skem ;
128134}
129135
@@ -136,7 +142,11 @@ JNIEXPORT jbyteArray JNICALL Java_io_pijun_george_Sodium_symmetricKeyDecrypt(
136142 return NULL ;
137143 }
138144
139- unsigned char msgUChar [msgLen ];
145+ unsigned char * msgUChar = malloc (msgLen );
146+ if (msgUChar == NULL ) {
147+ __android_log_print (ANDROID_LOG_ERROR , "ZoodLoc" , "symmetricKeyDecrypt: failed to allocate msgUChar" );
148+ return NULL ;
149+ }
140150 unsigned char * cipherTextUChar = ucharArray (env , cipherText );
141151 unsigned char * nonceUChar = ucharArray (env , nonce );
142152 unsigned char * keyUChar = ucharArray (env , key );
@@ -150,10 +160,12 @@ JNIEXPORT jbyteArray JNICALL Java_io_pijun_george_Sodium_symmetricKeyDecrypt(
150160 free (keyUChar );
151161 if (result != 0 ) {
152162 __android_log_print (ANDROID_LOG_INFO , "ZoodLoc" , "symmetricKeyDecrypt: non-zero result (%d)" , result );
163+ free (msgUChar );
153164 return NULL ;
154165 }
155166
156167 jbyteArray msg = byteArray (env , msgUChar , msgLen );
168+ free (msgUChar );
157169 return msg ;
158170}
159171
@@ -162,7 +174,11 @@ JNIEXPORT jobject JNICALL Java_io_pijun_george_Sodium_publicKeyEncrypt(
162174 JNIEnv * env , jclass cls , jbyteArray msg , jbyteArray rcvrPubKey , jbyteArray sndrSecKey ) {
163175 int msgLen = (* env )-> GetArrayLength (env , msg );
164176 int cipherTextLen = crypto_box_MACBYTES + msgLen ;
165- unsigned char cipherText [cipherTextLen ];
177+ unsigned char * cipherText = malloc (cipherTextLen );
178+ if (cipherText == NULL ) {
179+ __android_log_print (ANDROID_LOG_ERROR , "ZoodLoc" , "publicKeyEncrypt: failed to allocate cipherText" );
180+ return NULL ;
181+ }
166182 unsigned char nonce [crypto_box_NONCEBYTES ];
167183 randombytes_buf (nonce , sizeof nonce );
168184 unsigned char * msgUChar = ucharArray (env , msg );
@@ -179,6 +195,7 @@ JNIEXPORT jobject JNICALL Java_io_pijun_george_Sodium_publicKeyEncrypt(
179195 free (sndrKeyUChar );
180196 if (result != 0 ) {
181197 __android_log_print (ANDROID_LOG_INFO , "ZoodLoc" , "publicKeyEncrypt: non-zero result (%d)" , result );
198+ free (cipherText );
182199 return NULL ;
183200 }
184201
@@ -194,6 +211,7 @@ JNIEXPORT jobject JNICALL Java_io_pijun_george_Sodium_publicKeyEncrypt(
194211 jbyteArray nonceByteArray = byteArray (env , nonce , sizeof nonce );
195212 (* env )-> SetObjectField (env , skem , nonceFieldId , nonceByteArray );
196213
214+ free (cipherText );
197215 return skem ;
198216}
199217
@@ -206,7 +224,11 @@ JNIEXPORT jbyteArray JNICALL Java_io_pijun_george_Sodium_publicKeyDecrypt(
206224 __android_log_print (ANDROID_LOG_INFO , "ZoodLoc" , "publicKeyDecrypt: message is too short" );
207225 return NULL ;
208226 }
209- unsigned char msg [msgLen ];
227+ unsigned char * msg = malloc (msgLen );
228+ if (msg == NULL ) {
229+ __android_log_print (ANDROID_LOG_ERROR , "ZoodLoc" , "publicKeyDecrypt: failed to allocate msg" );
230+ return NULL ;
231+ }
210232 unsigned char * cipherTextUChar = ucharArray (env , cipherText );
211233 unsigned char * nonceUChar = ucharArray (env , nonce );
212234 unsigned char * pubKeyUChar = ucharArray (env , senderPubKey );
@@ -223,10 +245,13 @@ JNIEXPORT jbyteArray JNICALL Java_io_pijun_george_Sodium_publicKeyDecrypt(
223245 free (secKeyUChar );
224246 if (result != 0 ) {
225247 __android_log_print (ANDROID_LOG_INFO , "ZoodLoc" , "publicKeyDecrypt: non-zero result (%d)" , result );
248+ free (msg );
226249 return NULL ;
227250 }
228251
229- return byteArray (env , msg , msgLen );
252+ jbyteArray result_array = byteArray (env , msg , msgLen );
253+ free (msg );
254+ return result_array ;
230255}
231256
232257#pragma clang diagnostic pop
0 commit comments