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