-
Notifications
You must be signed in to change notification settings - Fork 236
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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))) |
There was a problem hiding this comment.
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])) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this 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
This function generates key and IV using salt and passphrase.
It used to decrypt data with "Salted__" header