| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| package hkdf |
|
|
| import ( |
| "crypto/internal/fips140/hkdf" |
| "crypto/internal/fips140hash" |
| "crypto/internal/fips140only" |
| "errors" |
| "hash" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error) { |
| fh := fips140hash.UnwrapNew(h) |
| if err := checkFIPS140Only(fh, secret); err != nil { |
| return nil, err |
| } |
| return hkdf.Extract(fh, secret, salt), nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func Expand[H hash.Hash](h func() H, pseudorandomKey []byte, info string, keyLength int) ([]byte, error) { |
| fh := fips140hash.UnwrapNew(h) |
| if err := checkFIPS140Only(fh, pseudorandomKey); err != nil { |
| return nil, err |
| } |
|
|
| limit := fh().Size() * 255 |
| if keyLength > limit { |
| return nil, errors.New("hkdf: requested key length too large") |
| } |
|
|
| return hkdf.Expand(fh, pseudorandomKey, info, keyLength), nil |
| } |
|
|
| |
| |
| |
| func Key[Hash hash.Hash](h func() Hash, secret, salt []byte, info string, keyLength int) ([]byte, error) { |
| fh := fips140hash.UnwrapNew(h) |
| if err := checkFIPS140Only(fh, secret); err != nil { |
| return nil, err |
| } |
|
|
| limit := fh().Size() * 255 |
| if keyLength > limit { |
| return nil, errors.New("hkdf: requested key length too large") |
| } |
|
|
| return hkdf.Key(fh, secret, salt, info, keyLength), nil |
| } |
|
|
| func checkFIPS140Only[Hash hash.Hash](h func() Hash, key []byte) error { |
| if !fips140only.Enforced() { |
| return nil |
| } |
| if len(key) < 112/8 { |
| return errors.New("crypto/hkdf: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode") |
| } |
| if !fips140only.ApprovedHash(h()) { |
| return errors.New("crypto/hkdf: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode") |
| } |
| return nil |
| } |
|
|