Skip to content

Commit

Permalink
Add, test and fix CryptoAEADAES256GCM functions
Browse files Browse the repository at this point in the history
- Fix CryptoAEADAES256GCMNSecBytes()
- Remove nsec from CryptoAEADAES256GCMEncrypt() and Decrypt()
- Add support.CheckSizeMin for things with a minimum size
- Add support.BytePointer to get a pointer for things than can have 0 length
- Add CryptoAEADAES256GCMIsAvailable(), CryptoAEADAES256GCMEncryptDetached(),
  CryptoAEADAES256GCMDecryptDetached() and CryptoAEADAES256GCMKeyGen()
- Add tests for all encryption and decryption functions (related to GoKillers#1)

Closes GoKillers#9
  • Loading branch information
silkeh committed Mar 20, 2017
1 parent ca026aa commit ff1d7ef
Show file tree
Hide file tree
Showing 3 changed files with 3,237 additions and 20 deletions.
93 changes: 73 additions & 20 deletions cryptoaead/crypto_aead_aes256gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ package cryptoaead
import "C"
import "github.com/GoKillers/libsodium-go/support"

func CryptoAEADAES256GCMIsAvailable() bool {
C.sodium_init()
return int(C.crypto_aead_aes256gcm_is_available()) != 0
}

func CryptoAEADAES256GCMKeyBytes() int {
return int(C.crypto_aead_aes256gcm_keybytes())
}

func CryptoAEADAES256GCMNSecBytes() int {
return int(C.crypto_aead_aes256gcm_keybytes())
return int(C.crypto_aead_aes256gcm_nsecbytes())
}

func CryptoAEADAES256GCMNPubBytes() int {
Expand All @@ -26,47 +31,95 @@ func CryptoAEADAES256GCMStateBytes() int {
return int(C.crypto_aead_aes256gcm_statebytes())
}

func CryptoAESAES256GCMIsAvailable() int {
return int(C.crypto_aead_aes256gcm_is_available())
}

func CryptoAEADAES256GCMEncrypt(m []byte, ad []byte, nsec []byte, npub []byte, k []byte) ([]byte, int) {
func CryptoAEADAES256GCMEncrypt(m []byte, ad []byte, npub []byte, k []byte) ([]byte, int) {
support.CheckSize(k, CryptoAEADAES256GCMKeyBytes(), "secret key")
support.CheckSize(npub, CryptoAEADAES256GCMNPubBytes(), "public nonce")

c := make([]byte, len(m)+CryptoAEADAES256GCMABytes())
cLen := len(c)
cLenLongLong := (C.ulonglong(cLen))
cLen := C.ulonglong(len(c))

exit := int(C.crypto_aead_aes256gcm_encrypt(
(*C.uchar)(&c[0]),
&cLenLongLong,
(*C.uchar)(&m[0]),
(*C.uchar)(support.BytePointer(c)),
(*C.ulonglong)(&cLen),
(*C.uchar)(support.BytePointer(m)),
(C.ulonglong)(len(m)),
(*C.uchar)(&ad[0]),
(*C.uchar)(support.BytePointer(ad)),
(C.ulonglong)(len(ad)),
(*C.uchar)(&nsec[0]),
(*C.uchar)(nil),
(*C.uchar)(&npub[0]),
(*C.uchar)(&k[0])))

return c, exit
}

func CryptoAEADAES256GCMDecrypt(nsec []byte, c []byte, ad []byte, npub []byte, k []byte) ([]byte, int) {
func CryptoAEADAES256GCMDecrypt(c []byte, ad []byte, npub []byte, k []byte) ([]byte, int) {
support.CheckSize(k, CryptoAEADAES256GCMKeyBytes(), "secret key")
support.CheckSize(npub, CryptoAEADAES256GCMNPubBytes(), "public nonce")
support.CheckSizeMin(c, CryptoAEADAES256GCMABytes(), "ciphertext")

m := make([]byte, len(c)-CryptoAEADAES256GCMABytes())
mLen := len(m)
mLenLongLong := (C.ulonglong)(mLen)
mLen := (C.ulonglong)(len(m))

exit := int(C.crypto_aead_aes256gcm_decrypt(
(*C.uchar)(&m[0]),
&mLenLongLong,
(*C.uchar)(&nsec[0]),
(*C.uchar)(support.BytePointer(m)),
(*C.ulonglong)(&mLen),
(*C.uchar)(nil),
(*C.uchar)(&c[0]),
(C.ulonglong)(len(c)),
(*C.uchar)(&ad[0]),
(*C.uchar)(support.BytePointer(ad)),
(C.ulonglong)(len(ad)),
(*C.uchar)(&npub[0]),
(*C.uchar)(&k[0])))

return m, exit
}

func CryptoAEADAES256GCMEncryptDetached(m []byte, ad []byte, npub []byte, k []byte) ([]byte, []byte, int) {
support.CheckSize(k, CryptoAEADAES256GCMKeyBytes(), "secret key")
support.CheckSize(npub, CryptoAEADAES256GCMNPubBytes(), "public nonce")

c := make([]byte, len(m))
mac := make([]byte , CryptoAEADAES256GCMABytes())
macLen := C.ulonglong(len(c))

exit := int(C.crypto_aead_aes256gcm_encrypt_detached(
(*C.uchar)(support.BytePointer(c)),
(*C.uchar)(&mac[0]),
(*C.ulonglong)(&macLen),
(*C.uchar)(support.BytePointer(m)),
(C.ulonglong)(len(m)),
(*C.uchar)(support.BytePointer(ad)),
(C.ulonglong)(len(ad)),
(*C.uchar)(nil),
(*C.uchar)(&npub[0]),
(*C.uchar)(&k[0])))

return c, mac, exit
}

func CryptoAEADAES256GCMDecryptDetached(c, mac, ad, npub, k []byte) ([]byte, int) {
support.CheckSize(k, CryptoAEADAES256GCMKeyBytes(), "secret key")
support.CheckSize(npub, CryptoAEADAES256GCMNPubBytes(), "public nonce")
support.CheckSize(mac, CryptoAEADAES256GCMABytes(), "mac")

m := make([]byte, len(c))

exit := int(C.crypto_aead_aes256gcm_decrypt_detached(
(*C.uchar)(support.BytePointer(m)),
(*C.uchar)(nil),
(*C.uchar)(support.BytePointer(c)),
(C.ulonglong)(len(c)),
(*C.uchar)(&mac[0]),
(*C.uchar)(support.BytePointer(ad)),
(C.ulonglong)(len(ad)),
(*C.uchar)(&npub[0]),
(*C.uchar)(&k[0])))

return m, exit
}

func CryptoAEADAES256GCMKeyGen() []byte {
k := make([]byte, CryptoAEADAES256GCMKeyBytes())
C.crypto_aead_aes256gcm_keygen((*C.uchar)(&k[0]))
return k
}
Loading

0 comments on commit ff1d7ef

Please sign in to comment.