age: reject leading zeroes and sign in scrypt work factor

This commit is contained in:
Filippo Valsorda
2022-06-19 17:28:39 +02:00
parent 2088adf268
commit 2e090545df
42 changed files with 456 additions and 11 deletions

View File

@@ -90,6 +90,11 @@ func (f *TestFile) ArgsLine(args ...string) {
f.TextLine(strings.Join(append([]string{"->"}, args...), " ")) f.TextLine(strings.Join(append([]string{"->"}, args...), " "))
} }
func (f *TestFile) UnreadArgsLine() []string {
line := strings.TrimPrefix(f.UnreadLine(), "-> ")
return strings.Split(line, " ")
}
var b64 = base64.RawStdEncoding.EncodeToString var b64 = base64.RawStdEncoding.EncodeToString
func (f *TestFile) Body(body []byte) { func (f *TestFile) Body(body []byte) {
@@ -160,6 +165,10 @@ func (f *TestFile) ScryptRecordPassphrase(passphrase string) {
func (f *TestFile) ScryptNoRecordPassphrase(passphrase string, workFactor int) { func (f *TestFile) ScryptNoRecordPassphrase(passphrase string, workFactor int) {
salt := f.Rand(16) salt := f.Rand(16)
f.ScryptNoRecordPassphraseWithSalt(passphrase, workFactor, salt)
}
func (f *TestFile) ScryptNoRecordPassphraseWithSalt(passphrase string, workFactor int, salt []byte) {
f.ArgsLine("scrypt", b64(salt), strconv.Itoa(workFactor)) f.ArgsLine("scrypt", b64(salt), strconv.Itoa(workFactor))
key, err := scrypt.Key([]byte(passphrase), append([]byte("age-encryption.org/v1/scrypt"), salt...), key, err := scrypt.Key([]byte(passphrase), append([]byte("age-encryption.org/v1/scrypt"), salt...),
1<<workFactor, 8, 1, 32) 1<<workFactor, 8, 1, 32)

View File

@@ -8,6 +8,7 @@ import (
"crypto/rand" "crypto/rand"
"errors" "errors"
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"filippo.io/age/internal/format" "filippo.io/age/internal/format"
@@ -128,6 +129,8 @@ func (i *ScryptIdentity) Unwrap(stanzas []*Stanza) ([]byte, error) {
return multiUnwrap(i.unwrap, stanzas) return multiUnwrap(i.unwrap, stanzas)
} }
var digitsRe = regexp.MustCompile(`^[1-9][0-9]*$`)
func (i *ScryptIdentity) unwrap(block *Stanza) ([]byte, error) { func (i *ScryptIdentity) unwrap(block *Stanza) ([]byte, error) {
if block.Type != "scrypt" { if block.Type != "scrypt" {
return nil, ErrIncorrectIdentity return nil, ErrIncorrectIdentity
@@ -142,6 +145,9 @@ func (i *ScryptIdentity) unwrap(block *Stanza) ([]byte, error) {
if len(salt) != scryptSaltSize { if len(salt) != scryptSaltSize {
return nil, errors.New("invalid scrypt recipient block") return nil, errors.New("invalid scrypt recipient block")
} }
if w := block.Args[1]; !digitsRe.MatchString(w) {
return nil, fmt.Errorf("scrypt work factor encoding invalid: %q", w)
}
logN, err := strconv.Atoi(block.Args[1]) logN, err := strconv.Atoi(block.Args[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse scrypt work factor: %v", err) return nil, fmt.Errorf("failed to parse scrypt work factor: %v", err)
@@ -149,13 +155,13 @@ func (i *ScryptIdentity) unwrap(block *Stanza) ([]byte, error) {
if logN > i.maxWorkFactor { if logN > i.maxWorkFactor {
return nil, fmt.Errorf("scrypt work factor too large: %v", logN) return nil, fmt.Errorf("scrypt work factor too large: %v", logN)
} }
if logN <= 0 { if logN <= 0 { // unreachable
return nil, fmt.Errorf("invalid scrypt work factor: %v", logN) return nil, fmt.Errorf("invalid scrypt work factor: %v", logN)
} }
salt = append([]byte(scryptLabel), salt...) salt = append([]byte(scryptLabel), salt...)
k, err := scrypt.Key(i.password, salt, 1<<logN, 8, 1, chacha20poly1305.KeySize) k, err := scrypt.Key(i.password, salt, 1<<logN, 8, 1, chacha20poly1305.KeySize)
if err != nil { if err != nil { // unreachable
return nil, fmt.Errorf("failed to generate scrypt hash: %v", err) return nil, fmt.Errorf("failed to generate scrypt hash: %v", err)
} }

BIN
testdata/testkit/scrypt_bad_tag vendored Normal file

Binary file not shown.

BIN
testdata/testkit/scrypt_extra_argument vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
testdata/testkit/scrypt_salt_long vendored Normal file

Binary file not shown.

9
testdata/testkit/scrypt_salt_missing vendored Normal file
View File

@@ -0,0 +1,9 @@
expect: header failure
file key: 59454c4c4f57205355424d4152494e45
passphrase: password
age-encryption.org/v1
-> scrypt 10
W0mMthyhNJOV3debCwkQcUlNx/i6Ss/A07aQCrG5Gcw
--- 1QsPcEbBSylfP4apakJqtDBJMrpd81rPuSLTCvdZx6E
¬]?7åPqÓ¦ F—¹ •Â÷õÛ®è zŒ(rŠóÎ|

BIN
testdata/testkit/scrypt_salt_short vendored Normal file

Binary file not shown.

BIN
testdata/testkit/scrypt_uppercase vendored Normal file

Binary file not shown.

BIN
testdata/testkit/scrypt_work_factor_hex vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
testdata/testkit/scrypt_work_factor_zero vendored Normal file

Binary file not shown.

27
tests/scrypt_bad_tag.go Normal file
View File

@@ -0,0 +1,27 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import (
"encoding/base64"
"filippo.io/age/internal/testkit"
)
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, _ := base64.RawStdEncoding.DecodeString(f.UnreadLine())
body[len(body)-1] ^= 0xff
f.TextLine(base64.RawStdEncoding.EncodeToString(body))
f.HMAC()
f.Payload("age")
f.ExpectNoMatch()
f.Comment("the ChaCha20Poly1305 authentication tag on the body of the scrypt stanza is wrong")
f.Generate()
}

View File

@@ -0,0 +1,23 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadLine()
f.TextLine(args + " 10")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Comment("the base64 encoding of the share is not canonical")
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body := f.UnreadLine()
f.TextLine(testkit.NotCanonicalBase64(body))
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Comment("the base64 encoding of the share is not canonical")
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], testkit.NotCanonicalBase64(args[1]), args[2])
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

20
tests/scrypt_salt_long.go Normal file
View File

@@ -0,0 +1,20 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.ScryptRecordPassphrase("password")
f.ScryptNoRecordPassphraseWithSalt("password", 10, f.Rand(20))
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,23 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.ScryptRecordPassphrase("password")
f.ScryptNoRecordPassphraseWithSalt("password", 10, nil)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[2])
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,20 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.ScryptRecordPassphrase("password")
f.ScryptNoRecordPassphraseWithSalt("password", 10, f.Rand(12))
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

22
tests/scrypt_uppercase.go Normal file
View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine("Scrypt", args[1], args[2])
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectNoMatch()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "0xa")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "aaaa10")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "+10")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "010")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "012")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 18) // cmd/age default
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1])
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "-10")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,27 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import (
"math"
"strconv"
"filippo.io/age/internal/testkit"
)
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], strconv.FormatUint(math.MaxInt64+1+10, 10))
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "aaaa10")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 18) // cmd/go default
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "10")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectNoMatch()
f.Generate()
}

View File

@@ -0,0 +1,22 @@
// Copyright 2022 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import "filippo.io/age/internal/testkit"
func main() {
f := testkit.NewTestFile()
f.VersionLine("v1")
f.Scrypt("password", 10)
body, args := f.UnreadLine(), f.UnreadArgsLine()
f.ArgsLine(args[0], args[1], "0")
f.TextLine(body)
f.HMAC()
f.Payload("age")
f.ExpectHeaderFailure()
f.Generate()
}

View File

@@ -6,18 +6,14 @@
package main package main
import ( import "filippo.io/age/internal/testkit"
"strings"
"filippo.io/age/internal/testkit"
)
func main() { func main() {
f := testkit.NewTestFile() f := testkit.NewTestFile()
f.VersionLine("v1") f.VersionLine("v1")
f.X25519(testkit.TestX25519Recipient) f.X25519(testkit.TestX25519Recipient)
body, args := f.UnreadLine(), f.UnreadLine() body, args := f.UnreadLine(), f.UnreadArgsLine()
f.TextLine(strings.Replace(args, "X25519", "x25519", -1)) f.ArgsLine("x25519", args[1])
f.TextLine(body) f.TextLine(body)
f.HMAC() f.HMAC()
f.Payload("age") f.Payload("age")

View File

@@ -12,8 +12,7 @@ func main() {
f := testkit.NewTestFile() f := testkit.NewTestFile()
f.VersionLine("v1") f.VersionLine("v1")
f.X25519(testkit.TestX25519Recipient) f.X25519(testkit.TestX25519Recipient)
body, args := f.UnreadLine(), f.UnreadLine() body := f.UnreadLine()
f.TextLine(args)
f.TextLine(testkit.NotCanonicalBase64(body)) f.TextLine(testkit.NotCanonicalBase64(body))
f.HMAC() f.HMAC()
f.Payload("age") f.Payload("age")