From b073120ef0dfb7c873ef3dedd555e1c998beac67 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 10 Aug 2023 12:18:42 +0200 Subject: [PATCH 1/3] support PBKDF2 --- openssl.go | 7 ++ pbkdf2.go | 23 +++++++ pbkdf2_test.go | 173 +++++++++++++++++++++++++++++++++++++++++++++++++ shims.h | 1 + 4 files changed, 204 insertions(+) create mode 100644 pbkdf2.go create mode 100644 pbkdf2_test.go diff --git a/openssl.go b/openssl.go index 659b7e12..ba08c3e6 100644 --- a/openssl.go +++ b/openssl.go @@ -187,6 +187,13 @@ func base(b []byte) *C.uchar { return (*C.uchar)(unsafe.Pointer(&b[0])) } +func sbase(b []byte) *C.char { + if len(b) == 0 { + return nil + } + return (*C.char)(unsafe.Pointer(&b[0])) +} + func newOpenSSLError(msg string) error { var b strings.Builder b.WriteString(msg) diff --git a/pbkdf2.go b/pbkdf2.go new file mode 100644 index 00000000..f3fe21ad --- /dev/null +++ b/pbkdf2.go @@ -0,0 +1,23 @@ +//go:build linux && !cmd_go_bootstrap + +package openssl + +// #include "goopenssl.h" +import "C" +import ( + "errors" + "hash" +) + +func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) ([]byte, error) { + md := hashToMD(h()) + if md == nil { + return nil, errors.New("unsupported hash function") + } + out := make([]byte, keyLen) + ok := C.go_openssl_PKCS5_PBKDF2_HMAC(sbase(password), C.int(len(password)), base(salt), C.int(len(salt)), C.int(iter), md, C.int(keyLen), base(out)) + if ok != 1 { + return nil, newOpenSSLError("PKCS5_PBKDF2_HMAC") + } + return out, nil +} diff --git a/pbkdf2_test.go b/pbkdf2_test.go new file mode 100644 index 00000000..68876ef8 --- /dev/null +++ b/pbkdf2_test.go @@ -0,0 +1,173 @@ +//go:build linux + +package openssl_test + +import ( + "bytes" + "crypto/sha1" + "crypto/sha256" + "hash" + "testing" + + "github.com/golang-fips/openssl/v2" +) + +type testVector struct { + password string + salt string + iter int + output []byte +} + +// Test vectors from RFC 6070, http://tools.ietf.org/html/rfc6070 +var sha1TestVectors = []testVector{ + { + "password", + "salt", + 1, + []byte{ + 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71, + 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06, + 0x2f, 0xe0, 0x37, 0xa6, + }, + }, + { + "password", + "salt", + 2, + []byte{ + 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, + 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, + 0xd8, 0xde, 0x89, 0x57, + }, + }, + { + "password", + "salt", + 4096, + []byte{ + 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a, + 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0, + 0x65, 0xa4, 0x29, 0xc1, + }, + }, + { + "passwordPASSWORDpassword", + "saltSALTsaltSALTsaltSALTsaltSALTsalt", + 4096, + []byte{ + 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b, + 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a, + 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70, + 0x38, + }, + }, + { + "pass\000word", + "sa\000lt", + 4096, + []byte{ + 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, + 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3, + }, + }, +} + +// Test vectors from +// http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors +var sha256TestVectors = []testVector{ + { + "password", + "salt", + 1, + []byte{ + 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c, + 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37, + 0xa8, 0x65, 0x48, 0xc9, + }, + }, + { + "password", + "salt", + 2, + []byte{ + 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, + 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0, + 0x2a, 0x30, 0x3f, 0x8e, + }, + }, + { + "password", + "salt", + 4096, + []byte{ + 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41, + 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d, + 0x96, 0x28, 0x93, 0xa0, + }, + }, + { + "passwordPASSWORDpassword", + "saltSALTsaltSALTsaltSALTsaltSALTsalt", + 4096, + []byte{ + 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f, + 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, + 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, + 0x1c, + }, + }, + { + "pass\000word", + "sa\000lt", + 4096, + []byte{ + 0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89, + 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87, + }, + }, +} + +func testHash(t *testing.T, h func() hash.Hash, hashName string, vectors []testVector) { + for i, v := range vectors { + o, err := openssl.PBKDF2([]byte(v.password), []byte(v.salt), v.iter, len(v.output), h) + if err != nil { + t.Errorf("%s %d: %s", hashName, i, err) + continue + } + if !bytes.Equal(o, v.output) { + t.Errorf("%s %d: expected %x, got %x", hashName, i, v.output, o) + } + } +} + +func TestWithHMACSHA1(t *testing.T) { + testHash(t, openssl.NewSHA1, "SHA1", sha1TestVectors) +} + +func TestWithHMACSHA256(t *testing.T) { + testHash(t, openssl.NewSHA256, "SHA256", sha256TestVectors) +} + +var sink uint8 + +func benchmark(b *testing.B, h func() hash.Hash) { + password := make([]byte, h().Size()) + salt := make([]byte, 8) + var err error + for i := 0; i < b.N; i++ { + password, err = openssl.PBKDF2(password, salt, 4096, len(password), h) + if err != nil { + b.Fatal(err) + } + } + sink += password[0] +} + +func BenchmarkHMACSHA1(b *testing.B) { + benchmark(b, sha1.New) +} + +func BenchmarkHMACSHA256(b *testing.B) { + benchmark(b, sha256.New) +} diff --git a/shims.h b/shims.h index 56a29113..d1004238 100644 --- a/shims.h +++ b/shims.h @@ -340,4 +340,5 @@ DEFINEFUNC_3_0(int, EVP_PKEY_CTX_add1_hkdf_info, (GO_EVP_PKEY_CTX_PTR arg0, cons DEFINEFUNC_3_0(int, EVP_PKEY_up_ref, (GO_EVP_PKEY_PTR key), (key)) \ DEFINEFUNC_LEGACY_1(int, EVP_PKEY_set1_EC_KEY, (GO_EVP_PKEY_PTR pkey, GO_EC_KEY_PTR key), (pkey, key)) \ DEFINEFUNC_3_0(int, EVP_PKEY_CTX_set0_rsa_oaep_label, (GO_EVP_PKEY_CTX_PTR ctx, void *label, int len), (ctx, label, len)) \ +DEFINEFUNC(int, PKCS5_PBKDF2_HMAC, (const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, const GO_EVP_MD_PTR digest, int keylen, unsigned char *out), (pass, passlen, salt, saltlen, iter, digest, keylen, out)) \ From ac27194ad9b8668915f21dadedd40d6b2776dcb3 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 10 Aug 2023 14:45:19 +0200 Subject: [PATCH 2/3] add empty password pbkdf2 test --- pbkdf2_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pbkdf2_test.go b/pbkdf2_test.go index 68876ef8..6959b562 100644 --- a/pbkdf2_test.go +++ b/pbkdf2_test.go @@ -75,7 +75,18 @@ var sha1TestVectors = []testVector{ // Test vectors from // http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors +// except the first one, which is not copied from elsewhere. var sha256TestVectors = []testVector{ + { + "", + "salt", + 1, + []byte{ + 0xf1, 0x35, 0xc2, 0x79, 0x93, 0xba, 0xf9, 0x87, + 0x73, 0xc5, 0xcd, 0xb4, 0x0a, 0x57, 0x06, 0xce, + 0x6a, 0x34, 0x5c, 0xde, 0x61, + }, + }, { "password", "salt", From 7801840a6d7bbaa63b62a2d4c8b05313cc5b39d4 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 10 Aug 2023 15:03:58 +0200 Subject: [PATCH 3/3] support empty pbkdf2 password on openssl 1.0.2 --- pbkdf2.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pbkdf2.go b/pbkdf2.go index f3fe21ad..0dcddcf6 100644 --- a/pbkdf2.go +++ b/pbkdf2.go @@ -14,6 +14,11 @@ func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) ([]byte if md == nil { return nil, errors.New("unsupported hash function") } + if len(password) == 0 && vMajor == 1 && vMinor == 0 { + // x/crypto/pbkdf2 supports empty passwords, but OpenSSL 1.0.2 + // does not. As a workaround, we pass an "empty" password. + password = make([]byte, C.GO_EVP_MAX_MD_SIZE) + } out := make([]byte, keyLen) ok := C.go_openssl_PKCS5_PBKDF2_HMAC(sbase(password), C.int(len(password)), base(salt), C.int(len(salt)), C.int(iter), md, C.int(keyLen), base(out)) if ok != 1 {