Skip to content

Commit

Permalink
keydata*test.go: add test case for unavailable KDF
Browse files Browse the repository at this point in the history
  • Loading branch information
sespiros committed Feb 7, 2024
1 parent 7f9a3b7 commit 8c92177
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ func MockKeyDataVersion(n int) (restore func()) {
keyDataVersion = orig
}
}

func MockHashAlgAvailable() (restore func()) {
orig := hashAlgAvailable
hashAlgAvailable = func(*hashAlg) bool {
return false
}
return func() {
hashAlgAvailable = orig
}
}
4 changes: 3 additions & 1 deletion keydata.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ type KeyDataReader interface {
// hashAlg corresponds to a digest algorithm.
type hashAlg crypto.Hash

var hashAlgAvailable = (*hashAlg).Available

func (a hashAlg) Available() bool {
return crypto.Hash(a).Available()
}
Expand Down Expand Up @@ -526,7 +528,7 @@ func (d *KeyData) derivePassphraseKeys(passphrase string, kdf KDF) (key, iv, aut
}

kdfAlg := d.data.KDFAlg
if !kdfAlg.Available() {
if !hashAlgAvailable(&kdfAlg) {
return nil, nil, nil, fmt.Errorf("unavailable leaf KDF digest algorithm %v", kdfAlg)
}

Expand Down
8 changes: 8 additions & 0 deletions keydata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,14 @@ func (s *keyDataSuite) TestRecoverKeysWithPassphraseInvalidAuthKeySize(c *C) {
})
}

func (s *keyDataSuite) TestRecoverKeysWithPassphraseUnavailableKDF(c *C) {
restore := MockHashAlgAvailable()
defer restore()
s.testRecoverKeysWithPassphraseErrorHandling(c, &testRecoverKeysWithPassphraseErrorHandlingData{
errMsg: fmt.Sprintf("unavailable leaf KDF digest algorithm %d", crypto.SHA256),
})
}

func (s *keyDataSuite) TestNewKeyDataWithPassphraseNotSupported(c *C) {
// Test that creation of a new key data with passphrase fails when the
// platform handler doesn't have passphrase support.
Expand Down

0 comments on commit 8c92177

Please sign in to comment.