hkdf: forward Extract to the standard library
Change-Id: I82406eb153c53e7cbf167360e472724a6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/769720 Reviewed-by: Daniel McCarney <daniel@binaryparadox.net> Reviewed-by: Roland Shoemaker <roland@golang.org> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Auto-Submit: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
committed by
Gopher Robot
parent
a8e9237a21
commit
cc0e4fc1d4
+13
-8
@@ -11,6 +11,7 @@
|
|||||||
package hkdf
|
package hkdf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/hkdf"
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"errors"
|
"errors"
|
||||||
"hash"
|
"hash"
|
||||||
@@ -24,15 +25,19 @@ import (
|
|||||||
// Expand invocations and different context values. Most common scenarios,
|
// Expand invocations and different context values. Most common scenarios,
|
||||||
// including the generation of multiple keys, should use New instead.
|
// including the generation of multiple keys, should use New instead.
|
||||||
func Extract(hash func() hash.Hash, secret, salt []byte) []byte {
|
func Extract(hash func() hash.Hash, secret, salt []byte) []byte {
|
||||||
if salt == nil {
|
// Use the stdlib Extract, which disables FIPS 140 enforcement of the HMAC
|
||||||
salt = make([]byte, hash().Size())
|
// key (which in HKDF is the salt). The only possible error is FIPS 140
|
||||||
|
// enforcement of the hash, which had to panic under this API anyway. We
|
||||||
|
// don't use the stdlib Expand, because it switched to returning a []byte
|
||||||
|
// instead of an io.Reader, and Expand uses the HMAC key as a key.
|
||||||
|
out, err := hkdf.Extract(hash, secret, salt)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
extractor := hmac.New(hash, salt)
|
return out
|
||||||
extractor.Write(secret)
|
|
||||||
return extractor.Sum(nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type hkdf struct {
|
type hkdfReader struct {
|
||||||
expander hash.Hash
|
expander hash.Hash
|
||||||
size int
|
size int
|
||||||
|
|
||||||
@@ -43,7 +48,7 @@ type hkdf struct {
|
|||||||
buf []byte
|
buf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *hkdf) Read(p []byte) (int, error) {
|
func (f *hkdfReader) Read(p []byte) (int, error) {
|
||||||
// Check whether enough data can be generated
|
// Check whether enough data can be generated
|
||||||
need := len(p)
|
need := len(p)
|
||||||
remains := len(f.buf) + int(255-f.counter+1)*f.size
|
remains := len(f.buf) + int(255-f.counter+1)*f.size
|
||||||
@@ -84,7 +89,7 @@ func (f *hkdf) Read(p []byte) (int, error) {
|
|||||||
// 3.3. Most common scenarios will want to use New instead.
|
// 3.3. Most common scenarios will want to use New instead.
|
||||||
func Expand(hash func() hash.Hash, pseudorandomKey, info []byte) io.Reader {
|
func Expand(hash func() hash.Hash, pseudorandomKey, info []byte) io.Reader {
|
||||||
expander := hmac.New(hash, pseudorandomKey)
|
expander := hmac.New(hash, pseudorandomKey)
|
||||||
return &hkdf{expander, expander.Size(), info, 1, nil, nil}
|
return &hkdfReader{expander, expander.Size(), info, 1, nil, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a Reader, from which keys can be read, using the given hash,
|
// New returns a Reader, from which keys can be read, using the given hash,
|
||||||
|
|||||||
Reference in New Issue
Block a user