-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] add support for PBKDF2 for passphrases
Passphrase support currently hardcodes the use of Argon2. This adds support for specifying PBKDF2, for use in environments where FIPS140 compliance is required.
- Loading branch information
1 parent
6399f2c
commit 66007e1
Showing
6 changed files
with
252 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package pbkdf2 | ||
|
||
import ( | ||
"crypto" | ||
"errors" | ||
"time" | ||
|
||
"golang.org/x/crypto/pbkdf2" | ||
) | ||
|
||
const ( | ||
benchmarkPassword = "foo" | ||
) | ||
|
||
var ( | ||
benchmarkSalt = []byte("0123456789abcdefghijklmnopqrstuv") | ||
) | ||
|
||
type BenchmarkParams struct { | ||
TargetDuration time.Duration | ||
|
||
HashAlg crypto.Hash | ||
} | ||
|
||
func Benchmark(params *BenchmarkParams, keyLen int32) (*Params, error) { | ||
if keyLen < 0 { | ||
return nil, errors.New("invalid key length") | ||
} | ||
|
||
outParams := &Params{ | ||
Iterations: 1000, | ||
HashAlg: params.HashAlg, | ||
} | ||
iterations := outParams.Iterations | ||
|
||
for i := 1; ; i++ { | ||
start := time.Now() | ||
Key(benchmarkPassword, benchmarkSalt, &Params{Iterations: iterations, HashAlg: params.HashAlg}, keyLen) | ||
duration := time.Now().Sub(start) | ||
|
||
newIterationsOut := int32(int64(iterations) * int64(params.TargetDuration/duration)) | ||
if newIterationsOut < outParams.Iterations { | ||
return outParams, nil | ||
} | ||
outParams.Iterations = newIterationsOut | ||
|
||
switch { | ||
case i > 10: | ||
return nil, errors.New("insufficient progress") | ||
case duration > 500*time.Millisecond: | ||
return outParams, nil | ||
case duration <= 62*time.Millisecond: | ||
iterations *= 16 | ||
case duration <= 125*time.Millisecond: | ||
iterations *= 8 | ||
case duration <= 250*time.Millisecond: | ||
iterations *= 4 | ||
default: | ||
iterations *= 2 | ||
} | ||
} | ||
} | ||
|
||
type Params struct { | ||
Iterations int32 | ||
|
||
HashAlg crypto.Hash | ||
} | ||
|
||
func Key(passphrase string, salt []byte, params *Params, keyLen int32) []byte { | ||
if params.Iterations < 0 { | ||
panic("invalid iterations") | ||
} | ||
if keyLen < 0 { | ||
panic("invalid key length") | ||
} | ||
return pbkdf2.Key([]byte(passphrase), salt, int(params.Iterations), int(keyLen), params.HashAlg.New) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package secboot | ||
|
||
// KDFOptions is an interface for supplying options for different | ||
// key derivation functions | ||
type KDFOptions interface { | ||
kdfParams(keyLen uint32) (*kdfParams, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package secboot | ||
|
||
import ( | ||
"crypto" | ||
"errors" | ||
"fmt" | ||
"math" | ||
"time" | ||
|
||
"github.com/snapcore/secboot/internal/pbkdf2" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
const ( | ||
pbkdf2Type = "pbkdf2" | ||
) | ||
|
||
type PBKDF2Options struct { | ||
TargetDuration time.Duration | ||
|
||
ForceIterations uint32 | ||
|
||
HashAlg crypto.Hash | ||
} | ||
|
||
func (o *PBKDF2Options) kdfParams(keyLen uint32) (*kdfParams, error) { | ||
if keyLen > math.MaxInt32 { | ||
return nil, errors.New("invalid key length") | ||
} | ||
|
||
switch { | ||
case o.ForceIterations > 0: | ||
// The non-benchmarked path. Ensure that ForceIterations | ||
// fits into an int32 so that it always fits into an int | ||
switch { | ||
case int64(o.ForceIterations) > math.MaxInt32: | ||
return nil, fmt.Errorf("invalid iterations count %d", o.ForceIterations) | ||
} | ||
|
||
params := &kdfParams{ | ||
Type: pbkdf2Type, | ||
Time: int(o.ForceIterations), // no limit to the time cost. | ||
Hash: hashAlg(crypto.SHA256), | ||
} | ||
if o.HashAlg != crypto.Hash(0) { | ||
switch o.HashAlg { | ||
case crypto.SHA1, crypto.SHA224, crypto.SHA256, crypto.SHA384, crypto.SHA512: | ||
params.Hash = hashAlg(o.HashAlg) | ||
default: | ||
return nil, errors.New("invalid hash algorithm") | ||
} | ||
} | ||
|
||
return params, nil | ||
default: | ||
benchmarkParams := &pbkdf2.BenchmarkParams{ | ||
TargetDuration: 2 * time.Second, // the default target duration is 2s. | ||
HashAlg: crypto.SHA256, | ||
} | ||
if o.HashAlg != crypto.Hash(0) { | ||
benchmarkParams.HashAlg = o.HashAlg | ||
} | ||
|
||
params, err := pbkdf2.Benchmark(benchmarkParams, int32(keyLen)) | ||
if err != nil { | ||
return nil, xerrors.Errorf("cannot benchmark KDF: %w", err) | ||
} | ||
|
||
o = &PBKDF2Options{ | ||
ForceIterations: uint32(params.Iterations), | ||
HashAlg: params.HashAlg} | ||
return o.kdfParams(keyLen) | ||
} | ||
} |