mirror of
https://github.com/mollyim/argon2.git
synced 2025-05-13 05:00:35 +01:00
Fix MemoryCost minimum value according to specs
This commit is contained in:
parent
45ad41deb3
commit
b0f538525a
3 changed files with 21 additions and 15 deletions
|
@ -16,7 +16,7 @@ import static org.signal.argon2.Type.Argon2id;
|
||||||
public final class Argon2BuilderTest {
|
public final class Argon2BuilderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void memory_too_low() {
|
public void memory_negative() {
|
||||||
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
|
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
|
||||||
.type(Argon2id);
|
.type(Argon2id);
|
||||||
|
|
||||||
|
@ -24,6 +24,24 @@ public final class Argon2BuilderTest {
|
||||||
.isExactlyInstanceOf(IllegalArgumentException.class);
|
.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
|
@Test
|
||||||
public void memory_too_high() {
|
public void memory_too_high() {
|
||||||
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
|
Argon2.Builder builder = new Argon2.Builder(Version.LATEST)
|
||||||
|
|
|
@ -222,18 +222,6 @@ public final class Argon2Test {
|
||||||
ascii("password"), Argon2i));
|
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
|
@Test
|
||||||
public void salt_too_short() {
|
public void salt_too_short() {
|
||||||
Argon2 argon2 = new Argon2.Builder(Version.LATEST)
|
Argon2 argon2 = new Argon2.Builder(Version.LATEST)
|
||||||
|
|
|
@ -92,8 +92,7 @@ public final class Argon2 {
|
||||||
* Sets the memory usage of {@param kib} KiB.
|
* Sets the memory usage of {@param kib} KiB.
|
||||||
*/
|
*/
|
||||||
public Builder memoryCostKiB(int kib) {
|
public Builder memoryCostKiB(int kib) {
|
||||||
if (kib < 4) throw new IllegalArgumentException("kib too small, minimum 4");
|
if (kib < 8) throw new IllegalArgumentException("kib too small, minimum 8");
|
||||||
if (kib % 4 != 0) throw new IllegalArgumentException("kib must be multiple of 4");
|
|
||||||
this.mCostKiB = kib;
|
this.mCostKiB = kib;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -130,6 +129,7 @@ public final class Argon2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Argon2 build() {
|
public Argon2 build() {
|
||||||
|
if (mCostKiB < (8 * parallelism)) throw new IllegalArgumentException("memory cost too small for given value of parallelism");
|
||||||
return new Argon2(this);
|
return new Argon2(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue