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

Fix alignment of AEAD and Generic Hash states #32

Merged
merged 1 commit into from
Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cryptoaead/crypto_aead_aes256gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func CryptoAEADAES256GCMDecryptDetached(c, mac, ad, npub, k []byte) ([]byte, int
func CryptoAEADAES256GCMBeforeNM(k []byte) ([]byte, int) {
support.CheckSize(k, CryptoAEADAES256GCMKeyBytes(), "secret key")

ctx := make([]byte, CryptoAEADAES256GCMStateBytes())
ctx := support.AlignedSlice(CryptoAEADAES256GCMStateBytes(), 16)

exit := int(C.crypto_aead_aes256gcm_beforenm(
(*C.crypto_aead_aes256gcm_state)(unsafe.Pointer(&ctx[0])),
Expand Down
11 changes: 8 additions & 3 deletions cryptogenerichash/crypto_generichash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package generichash
// #include <stdlib.h>
// #include <sodium.h>
import "C"
import "github.com/GoKillers/libsodium-go/support"
import (
"github.com/GoKillers/libsodium-go/support"
"unsafe"
)

func CryptoGenericHashBytesMin() int {
return int(C.crypto_generichash_bytes_min())
Expand Down Expand Up @@ -68,9 +71,11 @@ func CryptoGenericHashInit(key []byte, outlen int) (*C.struct_crypto_generichash
support.CheckSizeInRange(len(key), CryptoGenericHashKeyBytesMin(), CryptoGenericHashKeyBytesMax(), "key")
}

state := new(C.struct_crypto_generichash_blake2b_state)
state := (*C.struct_crypto_generichash_blake2b_state)(
unsafe.Pointer(&support.AlignedSlice(CryptoGenericHashStateBytes(), 64)[0]))

exit := int(C.crypto_generichash_init(
(*C.struct_crypto_generichash_blake2b_state)(state),
state,
(*C.uchar)(support.BytePointer(key)),
(C.size_t)(len(key)),
(C.size_t)(outlen)))
Expand Down
12 changes: 11 additions & 1 deletion support/support.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package support

import "fmt"
import (
"fmt"
"unsafe"
)

//
// Internal support functions
Expand Down Expand Up @@ -32,3 +35,10 @@ func BytePointer(b []byte) *uint8 {
return nil
}
}

// AlignedSlice returns a memory aligned slice
func AlignedSlice(size, alignment int) []byte {
slice := make([]byte, size+alignment)
offset := alignment - int(uintptr(unsafe.Pointer(&slice[0])))%alignment
return slice[offset : offset+size]
}