1
2
3
4
5
6
7
8
9
10
11 package hkdf
12
13 import (
14 "crypto/internal/fips140/hkdf"
15 "crypto/internal/fips140only"
16 "errors"
17 "hash"
18 )
19
20
21
22
23
24
25
26 func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error) {
27 if err := checkFIPS140Only(h, secret); err != nil {
28 return nil, err
29 }
30 return hkdf.Extract(h, secret, salt), nil
31 }
32
33
34
35
36
37
38
39
40 func Expand[H hash.Hash](h func() H, pseudorandomKey []byte, info string, keyLength int) ([]byte, error) {
41 if err := checkFIPS140Only(h, pseudorandomKey); err != nil {
42 return nil, err
43 }
44
45 limit := h().Size() * 255
46 if keyLength > limit {
47 return nil, errors.New("hkdf: requested key length too large")
48 }
49
50 return hkdf.Expand(h, pseudorandomKey, info, keyLength), nil
51 }
52
53
54
55
56 func Key[Hash hash.Hash](h func() Hash, secret, salt []byte, info string, keyLength int) ([]byte, error) {
57 if err := checkFIPS140Only(h, secret); err != nil {
58 return nil, err
59 }
60
61 limit := h().Size() * 255
62 if keyLength > limit {
63 return nil, errors.New("hkdf: requested key length too large")
64 }
65
66 return hkdf.Key(h, secret, salt, info, keyLength), nil
67 }
68
69 func checkFIPS140Only[H hash.Hash](h func() H, key []byte) error {
70 if !fips140only.Enabled {
71 return nil
72 }
73 if len(key) < 112/8 {
74 return errors.New("crypto/hkdf: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode")
75 }
76 if !fips140only.ApprovedHash(h()) {
77 return errors.New("crypto/hkdf: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
78 }
79 return nil
80 }
81
View as plain text