Fix MemoryCost minimum value according to specs

This commit is contained in:
Oscar Mira 2020-02-09 12:46:49 +01:00
parent 45ad41deb3
commit b0f538525a
3 changed files with 21 additions and 15 deletions

View file

@ -16,7 +16,7 @@ import static org.signal.argon2.Type.Argon2id;
public final class Argon2BuilderTest {
@Test
public void memory_too_low() {
public void memory_negative() {
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
.type(Argon2id);
@ -24,6 +24,24 @@ public final class Argon2BuilderTest {
.isExactlyInstanceOf(IllegalArgumentException.class);
}
@Test
public void memory_too_low() {
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
.type(Argon2id);
assertThatThrownBy(() -> builder.memoryCostKiB(7))
.isExactlyInstanceOf(IllegalArgumentException.class);
}
@Test
public void memory_too_low_for_parallelism() {
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
.type(Argon2id);
assertThatThrownBy(() -> builder.memoryCostKiB(8).parallelism(2).build())
.isExactlyInstanceOf(IllegalArgumentException.class);
}
@Test
public void memory_too_high() {
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)

View file

@ -222,18 +222,6 @@ public final class Argon2Test {
ascii("password"), Argon2i));
}
@Test
public void memory_too_little() {
Argon2 argon2 = new Argon2.Builder(Version.LATEST)
.type(Argon2id)
.memoryCostOrder(2)
.build();
assertThatThrownBy(() -> argon2.hash(ascii("password"), ascii("diffsalt")))
.isExactlyInstanceOf(Argon2Exception.class)
.hasMessageContaining("Memory cost is too small");
}
@Test
public void salt_too_short() {
Argon2 argon2 = new Argon2.Builder(Version.LATEST)

View file

@ -92,8 +92,7 @@ public final class Argon2 {
* Sets the memory usage of {@param kib} KiB.
*/
public Builder memoryCostKiB(int kib) {
if (kib < 4) throw new IllegalArgumentException("kib too small, minimum 4");
if (kib % 4 != 0) throw new IllegalArgumentException("kib must be multiple of 4");
if (kib < 8) throw new IllegalArgumentException("kib too small, minimum 8");
this.mCostKiB = kib;
return this;
}
@ -130,6 +129,7 @@ public final class Argon2 {
}
public Argon2 build() {
if (mCostKiB < (8 * parallelism)) throw new IllegalArgumentException("memory cost too small for given value of parallelism");
return new Argon2(this);
}
}