Skip to content

Commit 734b433

Browse files
committed
JNI/JCE: fix MessageDigest parameter validation, add RuntimeException unit tests
1 parent 32498c6 commit 734b433

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

src/main/java/com/wolfssl/wolfcrypt/MessageDigest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,17 @@ public synchronized void update(ByteBuffer data)
162162
*
163163
* @throws WolfCryptException if native operation fails
164164
* @throws IllegalStateException object fails to initialize properly
165+
* @throws RuntimeException if offset or length are invalid
165166
*/
166167
public synchronized void update(byte[] data, int offset, int len)
167168
throws WolfCryptException, IllegalStateException {
168169

169170
checkStateAndInitialize();
170171

171-
if (offset >= data.length || offset < 0 || len < 0)
172-
return;
173-
174-
if (data.length - offset < len)
175-
len = data.length - offset;
172+
if (((offset + len) > data.length) || offset < 0 || len < 0) {
173+
throw new RuntimeException(
174+
"Invalid offset or length");
175+
}
176176

177177
native_update(data, offset, len);
178178
}

src/test/java/com/wolfssl/provider/jce/test/WolfCryptMessageDigestSha256Test.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,53 @@ public void testSha256Threaded()
346346
}
347347
}
348348
}
349+
350+
@Test(expected = RuntimeException.class)
351+
public void testUpdateWithInvalidOffsetThrows()
352+
throws NoSuchProviderException, NoSuchAlgorithmException {
353+
354+
MessageDigest sha256 =
355+
MessageDigest.getInstance("SHA-256", "wolfJCE");
356+
byte[] data = new byte[10];
357+
358+
/* Test with negative offset */
359+
sha256.update(data, -1, 5);
360+
}
361+
362+
@Test(expected = RuntimeException.class)
363+
public void testUpdateWithInvalidLengthThrows()
364+
throws NoSuchProviderException, NoSuchAlgorithmException {
365+
366+
MessageDigest sha256 =
367+
MessageDigest.getInstance("SHA-256", "wolfJCE");
368+
byte[] data = new byte[10];
369+
370+
/* Test with negative length */
371+
sha256.update(data, 0, -1);
372+
}
373+
374+
@Test(expected = RuntimeException.class)
375+
public void testUpdateWithOffsetPlusLengthExceedingArraySizeThrows()
376+
throws NoSuchProviderException, NoSuchAlgorithmException {
377+
378+
MessageDigest sha256 =
379+
MessageDigest.getInstance("SHA-256", "wolfJCE");
380+
byte[] data = new byte[10];
381+
382+
/* Test with offset + length > data.length */
383+
sha256.update(data, 5, 6);
384+
}
385+
386+
@Test(expected = RuntimeException.class)
387+
public void testUpdateWithOffsetEqualToArraySizeThrows()
388+
throws NoSuchProviderException, NoSuchAlgorithmException {
389+
390+
MessageDigest sha256 =
391+
MessageDigest.getInstance("SHA-256", "wolfJCE");
392+
byte[] data = new byte[10];
393+
394+
/* Test with offset equal to array size */
395+
sha256.update(data, 10, 1);
396+
}
349397
}
350398

src/test/java/com/wolfssl/wolfcrypt/test/Sha256Test.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,41 @@ public void threadedHashTest() throws InterruptedException {
256256
}
257257
}
258258
}
259+
260+
@Test(expected = RuntimeException.class)
261+
public void testUpdateWithInvalidOffsetThrows() {
262+
Sha256 sha = new Sha256();
263+
byte[] data = new byte[10];
264+
265+
/* Test with negative offset */
266+
sha.update(data, -1, 5);
267+
}
268+
269+
@Test(expected = RuntimeException.class)
270+
public void testUpdateWithInvalidLengthThrows() {
271+
Sha256 sha = new Sha256();
272+
byte[] data = new byte[10];
273+
274+
/* Test with negative length */
275+
sha.update(data, 0, -1);
276+
}
277+
278+
@Test(expected = RuntimeException.class)
279+
public void testUpdateWithOffsetPlusLengthExceedingArraySizeThrows() {
280+
Sha256 sha = new Sha256();
281+
byte[] data = new byte[10];
282+
283+
/* Test with offset + length > data.length */
284+
sha.update(data, 5, 6);
285+
}
286+
287+
@Test(expected = RuntimeException.class)
288+
public void testUpdateWithOffsetEqualToArraySizeThrows() {
289+
Sha256 sha = new Sha256();
290+
byte[] data = new byte[10];
291+
292+
/* Test with offset equal to array size */
293+
sha.update(data, 10, 1);
294+
}
259295
}
260296

0 commit comments

Comments
 (0)