Skip to content

Commit b360de8

Browse files
committed
added wrapper for EVP_BPE_scrypt
1 parent eb155da commit b360de8

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

scrypt.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build !cmd_go_bootstrap
2+
3+
package openssl
4+
5+
// #include "goopenssl.h"
6+
import "C"
7+
import (
8+
"unsafe"
9+
)
10+
11+
func Scrypt(password string, salt []byte, N, r, p, maxmem, keylen uint64) ([]byte, error) {
12+
cpassword := C.CString(password)
13+
defer C.free((unsafe.Pointer)(cpassword))
14+
15+
csalt := C.CBytes(salt)
16+
defer C.free((unsafe.Pointer)(csalt))
17+
18+
key := C.malloc(C.size_t(keylen))
19+
defer C.free(key)
20+
21+
res := C.go_openssl_EVP_PBE_scrypt(
22+
cpassword,
23+
C.size_t(len(password)),
24+
(*C.uchar)(csalt),
25+
C.size_t(len(salt)),
26+
C.uint64_t(N),
27+
C.uint64_t(r),
28+
C.uint64_t(p),
29+
C.uint64_t(maxmem),
30+
(*C.uchar)(key),
31+
C.size_t(keylen),
32+
)
33+
if res != 1 {
34+
return nil, newOpenSSLError("EVP_PBE_scrypt")
35+
}
36+
return C.GoBytes(key, C.int(keylen)), nil
37+
}

scrypt_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//go:build !cmd_go_bootstrap
2+
3+
package openssl_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/golang-fips/openssl/v2"
10+
)
11+
12+
func TestScrypt(t *testing.T) {
13+
hash, err := openssl.Scrypt("testpass01", []byte("abc123"), 1<<14, 8, 1, 32<<20, 64)
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
if len(hash) == 0 {
18+
t.Error("zero length hash")
19+
}
20+
hexhash := fmt.Sprintf("%x", hash)
21+
exp := "e6a3569950bf90e88d2f114c3d43f2d103cf3c13cf1579095e88ff5b3b3eb379cad31d26aa533e0f32c10666bbcc9c1cac2775a8a60d55d55d63c401927e905e"
22+
if hexhash != exp {
23+
t.Errorf("expected %q, got %q", exp, hexhash)
24+
}
25+
}
26+
27+
func TestScrypt_Err(t *testing.T) {
28+
_, err := openssl.Scrypt("testpass01", []byte("abc123"), 1<<14, 0, 0, 32<<20, 64)
29+
if err == nil {
30+
t.Error("expected an error when using zeros for r and p")
31+
}
32+
_, err = openssl.Scrypt("x", []byte("y"), 1<<14, 8, 1, 1<<20, 32)
33+
if err == nil {
34+
t.Error("expected memory limit error")
35+
}
36+
}

shims.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,4 @@ DEFINEFUNC_3_0(int, EVP_KDF_CTX_set_params, (GO_EVP_KDF_CTX_PTR ctx, const GO_OS
410410
DEFINEFUNC_3_0(void, EVP_KDF_CTX_free, (GO_EVP_KDF_CTX_PTR ctx), (ctx)) \
411411
DEFINEFUNC_3_0(size_t, EVP_KDF_CTX_get_kdf_size, (GO_EVP_KDF_CTX_PTR ctx), (ctx)) \
412412
DEFINEFUNC_3_0(int, EVP_KDF_derive, (GO_EVP_KDF_CTX_PTR ctx, unsigned char *key, size_t keylen, const GO_OSSL_PARAM_PTR params), (ctx, key, keylen, params)) \
413-
413+
DEFINEFUNC_1_1_1(int, EVP_PBE_scrypt, (const char *pass, size_t passlen, const unsigned char *salt, size_t saltlen, uint64_t N, uint64_t r, uint64_t p, uint64_t maxmem, unsigned char *key, size_t keylen), (pass, passlen, salt, saltlen, N, r, p, maxmem, key, keylen))

0 commit comments

Comments
 (0)