Skip to content

Commit 7d0074c

Browse files
jgfgopherbot
authored andcommitted
scrypt: fix panic on parameters <= 0
Providing 0 as argument for r or p results in a panic: panic: runtime error: integer divide by zero Providing negative values for r or p returns a misleading error: scrypt: parameters are too large This change avoids the panic and introduces a new error that is returned when r or p are <= 0: scrypt: parameters must be > 0 Change-Id: I68987b27d1eedd66644d2ec9436cba364fc1d46d Reviewed-on: https://go-review.googlesource.com/c/crypto/+/731780 Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]>
1 parent 506e022 commit 7d0074c

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

scrypt/scrypt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ func Key(password, salt []byte, N, r, p, keyLen int) ([]byte, error) {
196196
if N <= 1 || N&(N-1) != 0 {
197197
return nil, errors.New("scrypt: N must be > 1 and a power of 2")
198198
}
199+
if r <= 0 || p <= 0 {
200+
return nil, errors.New("scrypt: parameters must be > 0")
201+
}
199202
if uint64(r)*uint64(p) >= 1<<30 || r > maxInt/128/p || r > maxInt/256 || N > maxInt/128/r {
200203
return nil, errors.New("scrypt: parameters are too large")
201204
}

scrypt/scrypt_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ var bad = []testVector{
133133
{"p", "s", 1, 1, 1, nil}, // N == 1
134134
{"p", "s", 7, 8, 1, nil}, // N is not power of 2
135135
{"p", "s", 16, maxInt / 2, maxInt / 2, nil}, // p * r too large
136+
{"p", "s", 2, 0, 1, nil}, // r too small
137+
{"p", "s", 2, 1, 0, nil}, // p too small
138+
{"p", "s", 2, -1, 1, nil}, // r is negative
139+
{"p", "s", 2, 1, -1, nil}, // p is negative
136140
}
137141

138142
func TestKey(t *testing.T) {

0 commit comments

Comments
 (0)