Added secretbox

This commit is contained in:
Ugljesa Jovanovic 2021-02-21 19:57:08 +01:00
parent 723e32df76
commit 625a3e1f91
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
2 changed files with 65 additions and 18 deletions

View File

@ -564,19 +564,66 @@ interface JnaLibsodiumInterface : Library {
// ---- Secret stream end ----- // ---- Secret stream end -----
// //
// // ---- SecretBox ---- // // ---- SecretBox ----
// fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : dynamic
// fun crypto_secretbox_easy(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
// fun crypto_secretbox_keygen() : Uint8Array
// fun crypto_secretbox_open_detached(ciphertext : Uint8Array, tag : Uint8Array, nonce: Uint8Array, key: Uint8Array) : dynamic
// fun crypto_secretbox_open_easy(ciphertext : Uint8Array, nonce: Uint8Array, key: Uint8Array) : dynamic
//
//
// // ---- SecretBox End ----
//
//
// // ---- AEAD ----
// // int crypto_secretbox_detached(
// unsigned char *c, unsigned char *mac,
// const unsigned char *m,
// unsigned long long mlen,
// const unsigned char *n,
// const unsigned char *k)
fun crypto_secretbox_detached(
ciphertext: ByteArray,
mac: ByteArray,
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
// int crypto_secretbox_easy(
// unsigned char *c, const unsigned char *m,
// unsigned long long mlen, const unsigned char *n,
// const unsigned char *k)
fun crypto_secretbox_easy(
ciphertext: ByteArray,
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
// void crypto_secretbox_keygen(unsigned char k[crypto_secretbox_KEYBYTES])
fun crypto_secretbox_keygen(key: ByteArray)
// int crypto_secretbox_open_detached(
// unsigned char *m,
// const unsigned char *c,
// const unsigned char *mac,
// unsigned long long clen,
// const unsigned char *n,
// const unsigned char *k)
fun crypto_secretbox_open_detached(
message: ByteArray,
ciphertext: ByteArray,
mac: ByteArray,
ciphertextLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
// int crypto_secretbox_open_easy(
// unsigned char *m, const unsigned char *c,
// unsigned long long clen, const unsigned char *n,
// const unsigned char *k)
fun crypto_secretbox_open_easy(
message: ByteArray,
ciphertext: ByteArray,
ciphertextLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
// // ---- SecretBox End ----
// // ---- Auth ---- // // ---- Auth ----
// //
// fun crypto_auth(message: Uint8Array, key: Uint8Array) : Uint8Array // fun crypto_auth(message: Uint8Array, key: Uint8Array) : Uint8Array

View File

@ -1,11 +1,11 @@
package com.ionspin.kotlin.crypto.secretbox package com.ionspin.kotlin.crypto.secretbox
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object SecretBox { actual object SecretBox {
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray { actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
val ciphertext = UByteArray(message.size + crypto_secretbox_MACBYTES) val ciphertext = UByteArray(message.size + crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_easy( sodiumJna.crypto_secretbox_easy(
ciphertext.asByteArray(), ciphertext.asByteArray(),
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
@ -21,7 +21,7 @@ actual object SecretBox {
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
val decrypted = UByteArray(ciphertext.size - crypto_secretbox_MACBYTES) val decrypted = UByteArray(ciphertext.size - crypto_secretbox_MACBYTES)
val validationResult = sodium.crypto_secretbox_open_easy( val validationResult = sodiumJna.crypto_secretbox_open_easy(
decrypted.asByteArray(), decrypted.asByteArray(),
ciphertext.asByteArray(), ciphertext.asByteArray(),
ciphertext.size.toLong(), ciphertext.size.toLong(),
@ -41,7 +41,7 @@ actual object SecretBox {
): SecretBoxEncryptedDataAndTag { ): SecretBoxEncryptedDataAndTag {
val ciphertext = UByteArray(message.size) val ciphertext = UByteArray(message.size)
val authenticationTag = UByteArray(crypto_secretbox_MACBYTES) val authenticationTag = UByteArray(crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_detached( sodiumJna.crypto_secretbox_detached(
ciphertext.asByteArray(), ciphertext.asByteArray(),
authenticationTag.asByteArray(), authenticationTag.asByteArray(),
message.asByteArray(), message.asByteArray(),
@ -59,7 +59,7 @@ actual object SecretBox {
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
val message = UByteArray(ciphertext.size) val message = UByteArray(ciphertext.size)
val validationResult = sodium.crypto_secretbox_open_detached( val validationResult = sodiumJna.crypto_secretbox_open_detached(
message.asByteArray(), message.asByteArray(),
ciphertext.asByteArray(), ciphertext.asByteArray(),
tag.asByteArray(), tag.asByteArray(),
@ -75,7 +75,7 @@ actual object SecretBox {
actual fun keygen() : UByteArray { actual fun keygen() : UByteArray {
val generatedKey = UByteArray(crypto_secretbox_KEYBYTES) val generatedKey = UByteArray(crypto_secretbox_KEYBYTES)
sodium.crypto_secretbox_keygen(generatedKey.asByteArray()) sodiumJna.crypto_secretbox_keygen(generatedKey.asByteArray())
return generatedKey return generatedKey
} }