PBKDF2SHA512: don't allow negative count or dkLen

This commit is contained in:
Sean Gilligan 2023-09-14 11:29:02 -07:00 committed by Andreas Schildbach
parent 140deb0669
commit ee23c2d6ff

View file

@ -23,6 +23,8 @@
package org.bitcoinj.crypto; package org.bitcoinj.crypto;
import org.bitcoinj.base.internal.Preconditions;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -41,6 +43,8 @@ public class PBKDF2SHA512 {
private static final int H_LEN = 64; private static final int H_LEN = 64;
public static byte[] derive(String P, String S, int c, int dkLen) { public static byte[] derive(String P, String S, int c, int dkLen) {
Preconditions.checkArgument(c > 0, () -> "count must be greater than zero");
Preconditions.checkArgument(dkLen > 0, () -> "derived key length must be greater than zero");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (dkLen > ((Math.pow(2, 32)) - 1) * H_LEN) { if (dkLen > ((Math.pow(2, 32)) - 1) * H_LEN) {