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 binding for EVP_BytesToKey() key derivation function #74

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

Conversation

xakep666
Copy link

This function generates key and IV using salt and passphrase.
It used to decrypt data with "Salted__" header

Copy link
Member

@zeebo zeebo 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 the pull request. The code seems functional, just a couple small efficiency/cleanups.

Also, would you mind trying to wrap the code at 80 columns? We try to keep the code base around that line length if possible.

key.go Outdated
iv = make([]byte, cipher.IVSize())

var saltPtr, ivPtr, passwordPtr, keyPtr *C.uchar
if len(salt) != 0 && salt != nil {
Copy link
Member

Choose a reason for hiding this comment

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

checking if len(salt) != 0 is sufficient: salt == nil implies len(salt) == 0.

key.go Outdated
if len(salt) != 0 && salt != nil {
saltPtr = (*C.uchar)(unsafe.Pointer(&salt[0]))
}
if len(iv) != 0 && iv != nil {
Copy link
Member

Choose a reason for hiding this comment

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

ditto for iv as for salt

key.go Outdated
if iterations < 1 {
return nil, nil, errors.New("iterations count must be 1 or greater")
}
passwordSize := C.int(len([]byte(password)))
Copy link
Member

Choose a reason for hiding this comment

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

len([]byte(password)) is the same as len(password) but the latter does not make an extra copy.

key.go Outdated
return nil, nil, errors.New("iterations count must be 1 or greater")
}
passwordSize := C.int(len([]byte(password)))
passwordPtr = (*C.uchar)(unsafe.Pointer(&([]byte(password))[0]))
Copy link
Member

Choose a reason for hiding this comment

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

similarly, it's ok to pass &password[0] to c even though password is a string.

Copy link
Author

Choose a reason for hiding this comment

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

(*C.uchar)(&password[0]) gives error "can not take address of password[0]" (language version 1.7.1). I think this is because of strings in go is not just a byte array

Copy link
Member

Choose a reason for hiding this comment

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

My mistake! Of course this would be unsafe to do because strings are immutable.

key.go Outdated
if derivedKeySize != C.int(cipher.KeySize()) {
return nil, nil, errors.New("key derivation failed")
}
return
Copy link
Member

Choose a reason for hiding this comment

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

please explicitly return the values

@xakep666 xakep666 changed the title Add binging for EVB_BytesToKey() key derivation function Add binding for EVB_BytesToKey() key derivation function Apr 15, 2017
@xakep666
Copy link
Author

xakep666 commented Apr 15, 2017

Changed "password" parameter type to []byte because EVP_BytesToKey takes password as (unsigned char *)

iv = make([]byte, cipher.IVSize())

var saltPtr, ivPtr, passwordPtr, keyPtr *C.uchar
if len(salt) != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

so reading some openssl docs (https://wiki.openssl.org/index.php/Manual:EVP_BytesToKey(3)) this field should be either 8 bytes or null. can we have the function return an error if salt is passed and not 8 bytes?

Copy link
Member

@zeebo zeebo left a comment

Choose a reason for hiding this comment

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

You also have a typo in the pull request/commit message: EVB_BytesToKey instead of EVP_BytesToKey

@xakep666 xakep666 changed the title Add binding for EVB_BytesToKey() key derivation function Add binding for EVP_BytesToKey() key derivation function Apr 17, 2017
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