@@ -6,6 +6,7 @@ package hkdf
6
6
7
7
import (
8
8
"crypto/internal/fips140/hkdf"
9
+ "crypto/internal/fips140only"
9
10
"errors"
10
11
"hash"
11
12
)
@@ -17,6 +18,9 @@ import (
17
18
// Expand invocations and different context values. Most common scenarios,
18
19
// including the generation of multiple keys, should use [Key] instead.
19
20
func Extract [H hash.Hash ](h func () H , secret , salt []byte ) ([]byte , error ) {
21
+ if err := checkFIPS140Only (h , secret ); err != nil {
22
+ return nil , err
23
+ }
20
24
return hkdf .Extract (h , secret , salt ), nil
21
25
}
22
26
@@ -28,6 +32,10 @@ func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error) {
28
32
// random or pseudorandom cryptographically strong key. See RFC 5869, Section
29
33
// 3.3. Most common scenarios will want to use [Key] instead.
30
34
func Expand [H hash.Hash ](h func () H , pseudorandomKey []byte , info string , keyLength int ) ([]byte , error ) {
35
+ if err := checkFIPS140Only (h , pseudorandomKey ); err != nil {
36
+ return nil , err
37
+ }
38
+
31
39
limit := h ().Size () * 255
32
40
if keyLength > limit {
33
41
return nil , errors .New ("hkdf: requested key length too large" )
@@ -40,10 +48,27 @@ func Expand[H hash.Hash](h func() H, pseudorandomKey []byte, info string, keyLen
40
48
// returning a []byte of length keyLength that can be used as cryptographic key.
41
49
// Salt and info can be nil.
42
50
func Key [Hash hash.Hash ](h func () Hash , secret , salt []byte , info string , keyLength int ) ([]byte , error ) {
51
+ if err := checkFIPS140Only (h , secret ); err != nil {
52
+ return nil , err
53
+ }
54
+
43
55
limit := h ().Size () * 255
44
56
if keyLength > limit {
45
57
return nil , errors .New ("hkdf: requested key length too large" )
46
58
}
47
59
48
60
return hkdf .Key (h , secret , salt , info , keyLength ), nil
49
61
}
62
+
63
+ func checkFIPS140Only [H hash.Hash ](h func () H , key []byte ) error {
64
+ if ! fips140only .Enabled {
65
+ return nil
66
+ }
67
+ if len (key ) < 112 / 8 {
68
+ return errors .New ("crypto/hkdf: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode" )
69
+ }
70
+ if ! fips140only .ApprovedHash (h ()) {
71
+ return errors .New ("crypto/hkdf: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode" )
72
+ }
73
+ return nil
74
+ }
0 commit comments