Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for EVP_BPE_scrypt #246

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from

Conversation

harrybrwn
Copy link

Pretty simple, just a wrapper function around EVP_BPE_scrypt with some simple tests.

@karianna karianna requested review from qmuntal and dagood January 15, 2025 01:20
Copy link
Collaborator

@qmuntal qmuntal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for submitting. Adding Scrypt support seems like a good addition. Added some comments.

defer C.free(key)

res := C.go_openssl_EVP_PBE_scrypt(
cpassword,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The password content is not being modified, so there is no need to create a copy of it. unsafe is our friend here:

Suggested change
cpassword,
base(unsafe.StringData(password)),

res := C.go_openssl_EVP_PBE_scrypt(
cpassword,
C.size_t(len(password)),
(*C.uchar)(csalt),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as with password, no need to copy salt as it is not being modified:

Suggested change
(*C.uchar)(csalt),
base(salt),

Comment on lines +18 to +19
key := C.malloc(C.size_t(keylen))
defer C.free(key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key can directly be allocated in Go memory and passed to go_openssl_EVP_PBE_scrypt using base(key).

Suggested change
key := C.malloc(C.size_t(keylen))
defer C.free(key)
key := make([]byte, keylen)


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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EVP_PBE_scrypt is defined since OpenSSL 3.0

Suggested change
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))
DEFINEFUNC_3_0(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)) \

"unsafe"
)

func Scrypt(password string, salt []byte, N, r, p, maxmem, keylen uint64) ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use Go naming conventions

Suggested change
func Scrypt(password string, salt []byte, N, r, p, maxmem, keylen uint64) ([]byte, error) {
func Scrypt(password string, salt []byte, N, r, p, maxMem, keyLen uint64) ([]byte, error) {

import (
"unsafe"
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EVP_PBE_scrypt is supported since OpenSSL 3, and maybe not for all providers. Please implement the SupportsSscrypt() bool function. The implementation will be almost identical to

func SupportsPBKDF2() bool {
, replacing PBKDF2 with SCRYPT and returning false in the OpenSSL 1 case.

Then use this function in the new test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants