Blame SOURCES/1002-vendor-use-pbkdf2-from-OpenSSL.patch

05d305
use pbkdf2 from OpenSSL if FIPS mode is enabled
05d305
05d305
This patch modifies the x/crypto/pbkdf2 function to use OpenSSL
05d305
if FIPS mode is enabled.
05d305
3d91f6
diff --git a/vendor/golang.org/x/crypto/internal/boring/boring.go b/vendor/golang.org/x/crypto/internal/boring/boring.go
3d91f6
new file mode 100644
05d305
index 0000000000..5a06918832
3d91f6
--- /dev/null
3d91f6
+++ b/vendor/golang.org/x/crypto/internal/boring/boring.go
3d91f6
@@ -0,0 +1,74 @@
3d91f6
+// Copyright 2017 The Go Authors. All rights reserved.
3d91f6
+// Copyright 2021 Red Hat.
3d91f6
+// Use of this source code is governed by a BSD-style
3d91f6
+// license that can be found in the LICENSE file.
3d91f6
+
3d91f6
+// +build linux
3d91f6
+// +build !android
3d91f6
+// +build !no_openssl
3d91f6
+// +build !cmd_go_bootstrap
3d91f6
+// +build !msan
3d91f6
+
3d91f6
+package boring
3d91f6
+
3d91f6
+// #include "openssl_pbkdf2.h"
3d91f6
+// #cgo LDFLAGS: -ldl
3d91f6
+import "C"
3d91f6
+import (
3d91f6
+	"bytes"
3d91f6
+	"crypto/sha1"
3d91f6
+	"crypto/sha256"
3d91f6
+	"hash"
3d91f6
+	"unsafe"
3d91f6
+)
3d91f6
+
3d91f6
+var (
3d91f6
+	emptySha1   = sha1.Sum([]byte{})
3d91f6
+	emptySha256 = sha256.Sum256([]byte{})
3d91f6
+)
3d91f6
+
3d91f6
+func hashToMD(h hash.Hash) *C.GO_EVP_MD {
3d91f6
+	emptyHash := h.Sum([]byte{})
3d91f6
+
3d91f6
+	switch {
3d91f6
+	case bytes.Equal(emptyHash, emptySha1[:]):
3d91f6
+		return C._goboringcrypto_EVP_sha1()
3d91f6
+	case bytes.Equal(emptyHash, emptySha256[:]):
3d91f6
+		return C._goboringcrypto_EVP_sha256()
3d91f6
+	}
3d91f6
+	return nil
3d91f6
+}
3d91f6
+
3d91f6
+// charptr returns the address of the underlying array in b,
3d91f6
+// being careful not to panic when b has zero length.
3d91f6
+func charptr(b []byte) *C.char {
3d91f6
+	if len(b) == 0 {
3d91f6
+		return nil
3d91f6
+	}
3d91f6
+	return (*C.char)(unsafe.Pointer(&b[0]))
3d91f6
+}
3d91f6
+
3d91f6
+// ucharptr returns the address of the underlying array in b,
3d91f6
+// being careful not to panic when b has zero length.
3d91f6
+func ucharptr(b []byte) *C.uchar {
3d91f6
+	if len(b) == 0 {
3d91f6
+		return nil
3d91f6
+	}
3d91f6
+	return (*C.uchar)(unsafe.Pointer(&b[0]))
3d91f6
+}
3d91f6
+
3d91f6
+func Pbkdf2Key(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
3d91f6
+	// println("[debug] using pbkdf2 from OpenSSL")
3d91f6
+	ch := h()
3d91f6
+	md := hashToMD(ch)
3d91f6
+	if md == nil {
3d91f6
+		return nil
3d91f6
+	}
3d91f6
+
3d91f6
+	out := make([]byte, keyLen)
3d91f6
+	ok := C._goboringcrypto_PKCS5_PBKDF2_HMAC(charptr(password), C.int(len(password)), ucharptr(salt), C.int(len(salt)), C.int(iter), md, C.int(keyLen), ucharptr(out))
3d91f6
+	if ok != 1 {
3d91f6
+		panic("boringcrypto: PKCS5_PBKDF2_HMAC failed")
3d91f6
+	}
3d91f6
+	return out
3d91f6
+}
3d91f6
diff --git a/vendor/golang.org/x/crypto/internal/boring/notboring.go b/vendor/golang.org/x/crypto/internal/boring/notboring.go
3d91f6
new file mode 100644
05d305
index 0000000000..e244fb5663
3d91f6
--- /dev/null
3d91f6
+++ b/vendor/golang.org/x/crypto/internal/boring/notboring.go
3d91f6
@@ -0,0 +1,16 @@
3d91f6
+// Copyright 2017 The Go Authors. All rights reserved.
3d91f6
+// Copyright 2021 Red Hat.
3d91f6
+// Use of this source code is governed by a BSD-style
3d91f6
+// license that can be found in the LICENSE file.
3d91f6
+
3d91f6
+// +build !linux !cgo android cmd_go_bootstrap msan no_openssl
3d91f6
+
3d91f6
+package boring
3d91f6
+
3d91f6
+import (
3d91f6
+	"hash"
3d91f6
+)
3d91f6
+
3d91f6
+func Pbkdf2Key(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
3d91f6
+	panic("boringcrypto: not available")
3d91f6
+}
3d91f6
diff --git a/vendor/golang.org/x/crypto/internal/boring/openssl_pbkdf2.h b/vendor/golang.org/x/crypto/internal/boring/openssl_pbkdf2.h
3d91f6
new file mode 100644
05d305
index 0000000000..6dfdf10424
3d91f6
--- /dev/null
3d91f6
+++ b/vendor/golang.org/x/crypto/internal/boring/openssl_pbkdf2.h
3d91f6
@@ -0,0 +1,5 @@
05d305
+#include "/usr/lib/golang/src/vendor/github.com/golang-fips/openssl-fips/openssl/goopenssl.h"
3d91f6
+
3d91f6
+DEFINEFUNC(int, PKCS5_PBKDF2_HMAC,
3d91f6
+    (const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, EVP_MD *digest, int keylen, unsigned char *out),
3d91f6
+    (pass, passlen, salt, saltlen, iter, digest, keylen, out))
3d91f6
diff --git a/vendor/golang.org/x/crypto/pbkdf2/pbkdf2.go b/vendor/golang.org/x/crypto/pbkdf2/pbkdf2.go
05d305
index 593f653008..799a611f94 100644
3d91f6
--- a/vendor/golang.org/x/crypto/pbkdf2/pbkdf2.go
3d91f6
+++ b/vendor/golang.org/x/crypto/pbkdf2/pbkdf2.go
3d91f6
@@ -19,8 +19,11 @@ pbkdf2.Key.
3d91f6
 package pbkdf2 // import "golang.org/x/crypto/pbkdf2"
3d91f6
 
3d91f6
 import (
3d91f6
+	"crypto/boring"
3d91f6
 	"crypto/hmac"
3d91f6
 	"hash"
3d91f6
+
3d91f6
+	xboring "golang.org/x/crypto/internal/boring"
3d91f6
 )
3d91f6
 
3d91f6
 // Key derives a key from the password, salt and iteration count, returning a
3d91f6
@@ -40,6 +43,10 @@ import (
3d91f6
 // Using a higher iteration count will increase the cost of an exhaustive
3d91f6
 // search but will also make derivation proportionally slower.
3d91f6
 func Key(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
3d91f6
+	if boring.Enabled() {
3d91f6
+		return xboring.Pbkdf2Key(password, salt, iter, keyLen, h)
3d91f6
+	}
3d91f6
+
3d91f6
 	prf := hmac.New(h, password)
3d91f6
 	hashLen := prf.Size()
3d91f6
 	numBlocks := (keyLen + hashLen - 1) / hashLen