From 355e5dcdede7ae432990cd01e48db7f0c837de8a Mon Sep 17 00:00:00 2001 From: David Michael Date: Sun, 18 Jun 2017 18:46:39 +0100 Subject: [PATCH 1/2] Added key derivation --- cryptokdf/crypto_kdf.go | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 cryptokdf/crypto_kdf.go diff --git a/cryptokdf/crypto_kdf.go b/cryptokdf/crypto_kdf.go new file mode 100644 index 0000000..bf9ac31 --- /dev/null +++ b/cryptokdf/crypto_kdf.go @@ -0,0 +1,44 @@ +package cryptokdf + +// #cgo pkg-config: libsodium +// #include +// #include +import "C" +import "github.com/GoKillers/libsodium-go/support" + +func CryptoKdfKeybytes() int { + return int(C.crypto_kdf_keybytes()) +} + +func CryptoKdfContextbytes() int { + return int(C.crypto_kdf_contextbytes()) +} + +func CryptoKdfBytesMin() int { + return int(C.crypto_kdf_bytes_min()) +} + +func CryptoKdfBytesMax() int { + return int(C.crypto_kdf_bytes_max()) +} + +func CryptoKdfKeygen(k []byte) { + support.CheckSize(k, CryptoKdfKeybytes(), "keybytes") + C.crypto_kdf_keygen((*C.uchar)(&k[0])) +} + +func CryptoKdfDeriveFromKey(l int, i uint64, c string, k []byte) ([]byte, int) { + support.CheckSize(k, CryptoKdfKeybytes(), "keybytes") + support.CheckSize([]byte(c), CryptoKdfContextbytes(), "contextbytes") + support.CheckSizeInRange(l, CryptoKdfBytesMin(), CryptoKdfBytesMax(), "subkey_len") + out := make([]byte, l) + + exit := int(C.crypto_kdf_derive_from_key( + (*C.uchar)(&out[0]), + (C.size_t)(l), + (C.uint64_t)(i), + C.CString(c), + (*C.uchar)(&k[0]))) + + return out, exit +} From a0dd7f90ba821e9984acb8625ac0b132c94ff898 Mon Sep 17 00:00:00 2001 From: David Michael Date: Sun, 18 Jun 2017 19:12:42 +0100 Subject: [PATCH 2/2] Simplified CryptKdfKeygen --- cryptokdf/crypto_kdf.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cryptokdf/crypto_kdf.go b/cryptokdf/crypto_kdf.go index bf9ac31..660150c 100644 --- a/cryptokdf/crypto_kdf.go +++ b/cryptokdf/crypto_kdf.go @@ -22,9 +22,10 @@ func CryptoKdfBytesMax() int { return int(C.crypto_kdf_bytes_max()) } -func CryptoKdfKeygen(k []byte) { - support.CheckSize(k, CryptoKdfKeybytes(), "keybytes") +func CryptoKdfKeygen() []byte { + k := make([]byte, CryptoKdfKeybytes()) C.crypto_kdf_keygen((*C.uchar)(&k[0])) + return k } func CryptoKdfDeriveFromKey(l int, i uint64, c string, k []byte) ([]byte, int) {