mirror of
https://github.com/libp2p/go-openssl.git
synced 2025-04-24 17:50:13 +08:00
Fix init'ing non-authenticated decryption contexts
As pointed out by ishbir in issue 24, the applyKeyAndIV function was incorrectly always using EVP_EncryptInit_ex(), even when the underlying context being initialized was for a decryption. This change causes it to select the right initializer based on the "encrypt" field in the EVP_CIPHER_CTX. A test is also added which verifies a fix. Closes #24.
This commit is contained in:
parent
0e53dd5595
commit
d0177cb6a7
@ -153,7 +153,13 @@ func (ctx *cipherCtx) applyKeyAndIV(key, iv []byte) error {
|
||||
iptr = (*C.uchar)(&iv[0])
|
||||
}
|
||||
if kptr != nil || iptr != nil {
|
||||
if 1 != C.EVP_EncryptInit_ex(ctx.ctx, nil, nil, kptr, iptr) {
|
||||
var res C.int
|
||||
if ctx.ctx.encrypt != 0 {
|
||||
res = C.EVP_EncryptInit_ex(ctx.ctx, nil, nil, kptr, iptr)
|
||||
} else {
|
||||
res = C.EVP_DecryptInit_ex(ctx.ctx, nil, nil, kptr, iptr)
|
||||
}
|
||||
if 1 != res {
|
||||
return errors.New("failed to apply key/IV")
|
||||
}
|
||||
}
|
||||
|
@ -251,3 +251,57 @@ func TestBadAAD(t *testing.T) {
|
||||
}
|
||||
checkEqual(t, plaintext_out, plaintext)
|
||||
}
|
||||
|
||||
func TestNonAuthenticatedEncryption(t *testing.T) {
|
||||
key := []byte("never gonna give you up, never g")
|
||||
iv := []byte("onna let you dow")
|
||||
plaintext1 := "n, never gonna run around"
|
||||
plaintext2 := " and desert you"
|
||||
|
||||
cipher, err := GetCipherByName("aes-256-cbc")
|
||||
if err != nil {
|
||||
t.Fatal("Could not get cipher: ", err)
|
||||
}
|
||||
|
||||
eCtx, err := NewEncryptionCipherCtx(cipher, nil, key, iv)
|
||||
if err != nil {
|
||||
t.Fatal("Could not create encryption context: ", err)
|
||||
}
|
||||
cipherbytes, err := eCtx.EncryptUpdate([]byte(plaintext1))
|
||||
if err != nil {
|
||||
t.Fatal("EncryptUpdate(plaintext1) failure: ", err)
|
||||
}
|
||||
ciphertext := string(cipherbytes)
|
||||
cipherbytes, err = eCtx.EncryptUpdate([]byte(plaintext2))
|
||||
if err != nil {
|
||||
t.Fatal("EncryptUpdate(plaintext2) failure: ", err)
|
||||
}
|
||||
ciphertext += string(cipherbytes)
|
||||
cipherbytes, err = eCtx.EncryptFinal()
|
||||
if err != nil {
|
||||
t.Fatal("EncryptFinal() failure: ", err)
|
||||
}
|
||||
ciphertext += string(cipherbytes)
|
||||
|
||||
dCtx, err := NewDecryptionCipherCtx(cipher, nil, key, iv)
|
||||
if err != nil {
|
||||
t.Fatal("Could not create decryption context: ", err)
|
||||
}
|
||||
plainbytes, err := dCtx.DecryptUpdate([]byte(ciphertext[:15]))
|
||||
if err != nil {
|
||||
t.Fatal("DecryptUpdate(ciphertext part 1) failure: ", err)
|
||||
}
|
||||
plainOutput := string(plainbytes)
|
||||
plainbytes, err = dCtx.DecryptUpdate([]byte(ciphertext[15:]))
|
||||
if err != nil {
|
||||
t.Fatal("DecryptUpdate(ciphertext part 2) failure: ", err)
|
||||
}
|
||||
plainOutput += string(plainbytes)
|
||||
plainbytes, err = dCtx.DecryptFinal()
|
||||
if err != nil {
|
||||
t.Fatal("DecryptFinal() failure: ", err)
|
||||
}
|
||||
plainOutput += string(plainbytes)
|
||||
|
||||
checkEqual(t, []byte(plainOutput), plaintext1+plaintext2)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user