Completed cha cha aead variants

This commit is contained in:
Ugljesa Jovanovic 2020-08-30 13:10:31 +02:00 committed by Ugljesa Jovanovic
parent 3f9f316e04
commit dd0895b5f3
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
5 changed files with 395 additions and 27 deletions

View File

@ -120,4 +120,9 @@ expect object AuthenticatedEncryptionWithAssociatedData {
key: UByteArray key: UByteArray
): UByteArray ): UByteArray
fun xChaCha20Poly1305IetfKeygen() : UByteArray
fun chaCha20Poly1305IetfKeygen() : UByteArray
fun chaCha20Poly1305Keygen() : UByteArray
} }

View File

@ -44,9 +44,9 @@ interface JsSodiumInterface {
fun crypto_hash_sha512_final(state: dynamic): Uint8Array fun crypto_hash_sha512_final(state: dynamic): Uint8Array
//XChaCha20Poly1305 //XChaCha20Poly1305 - also in bindings
fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array //fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_decrypt(secretNonce: Uint8Array, ciphertext: Uint8Array, associatedData: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array //fun crypto_aead_xchacha20poly1305_ietf_decrypt(secretNonce: Uint8Array, ciphertext: Uint8Array, associatedData: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
//XChaCha20Poly1305 //XChaCha20Poly1305
//encrypt //encrypt
@ -71,6 +71,26 @@ interface JsSodiumInterface {
// ---- SecretBox End ---- // ---- SecretBox End ----
// ---- AEAD ----
fun crypto_aead_chacha20poly1305_decrypt(nsec : Uint8Array?, ciphertext: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_chacha20poly1305_decrypt_detached(nsec: Uint8Array?, ciphertext: Uint8Array, mac: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array): Uint8Array
fun crypto_aead_chacha20poly1305_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_chacha20poly1305_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : dynamic
fun crypto_aead_chacha20poly1305_ietf_decrypt(nsec : Uint8Array?, ciphertext: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_chacha20poly1305_ietf_decrypt_detached(nsec: Uint8Array?, ciphertext: Uint8Array, mac: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array): Uint8Array
fun crypto_aead_chacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_chacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : dynamic
fun crypto_aead_chacha20poly1305_ietf_keygen() : Uint8Array
fun crypto_aead_chacha20poly1305_keygen() : Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_decrypt(nsec : Uint8Array?, ciphertext: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_decrypt_detached(nsec: Uint8Array?, ciphertext: Uint8Array, mac: Uint8Array, associatedData: Uint8Array, npub: Uint8Array, key: Uint8Array): Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_encrypt_detached(message: Uint8Array, associatedData: Uint8Array, nsec: Uint8Array?, npub: Uint8Array, key: Uint8Array) : dynamic
fun crypto_aead_xchacha20poly1305_ietf_keygen(): Uint8Array
// ---- AEAD end ----
//util //util
fun memzero(array: Uint8Array) fun memzero(array: Uint8Array)

View File

@ -1,5 +1,10 @@
package com.ionspin.kotlin.crypto.aead package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
actual object AuthenticatedEncryptionWithAssociatedData { actual object AuthenticatedEncryptionWithAssociatedData {
// Ietf // Ietf
@ -11,7 +16,13 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") return getSodium().crypto_aead_xchacha20poly1305_ietf_encrypt(
message.toUInt8Array(),
associatedData.toUInt8Array(),
null,
nonce.toUInt8Array(),
key.toUInt8Array(),
).toUByteArray()
} }
actual fun xChaCha20Poly1305IetfDecrypt( actual fun xChaCha20Poly1305IetfDecrypt(
@ -20,7 +31,17 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") try {
return getSodium().crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
ciphertext.toUInt8Array(),
associatedData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
).toUByteArray()
} catch (error: Throwable) {
throw AeadCorrupedOrTamperedDataException()
}
} }
actual fun xChaCha20Poly1305IetfEncryptDetached( actual fun xChaCha20Poly1305IetfEncryptDetached(
@ -29,7 +50,17 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): AeadEncryptedDataAndTag { ): AeadEncryptedDataAndTag {
TODO("not implemented yet") val result = getSodium().crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
message.toUInt8Array(),
associatedData.toUInt8Array(),
null,
nonce.toUInt8Array(),
key.toUInt8Array(),
)
return AeadEncryptedDataAndTag(
(result.ciphertext as Uint8Array).toUByteArray(),
(result.mac as Uint8Array).toUByteArray()
)
} }
actual fun xChaCha20Poly1305IetfDecryptDetached( actual fun xChaCha20Poly1305IetfDecryptDetached(
@ -39,7 +70,18 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") try {
return getSodium().crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
null,
ciphertext.toUInt8Array(),
tag.toUInt8Array(),
associatedData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
).toUByteArray()
} catch (error: Throwable) {
throw AeadCorrupedOrTamperedDataException()
}
} }
actual fun chaCha20Poly1305IetfEncrypt( actual fun chaCha20Poly1305IetfEncrypt(
@ -48,7 +90,13 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") return getSodium().crypto_aead_chacha20poly1305_ietf_encrypt(
message.toUInt8Array(),
associatedData.toUInt8Array(),
null,
nonce.toUInt8Array(),
key.toUInt8Array(),
).toUByteArray()
} }
actual fun chaCha20Poly1305IetfDecrypt( actual fun chaCha20Poly1305IetfDecrypt(
@ -57,7 +105,17 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") try {
return getSodium().crypto_aead_chacha20poly1305_ietf_decrypt(
null,
ciphertext.toUInt8Array(),
associatedData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
).toUByteArray()
} catch (error: Throwable) {
throw AeadCorrupedOrTamperedDataException()
}
} }
actual fun chaCha20Poly1305IetfEncryptDetached( actual fun chaCha20Poly1305IetfEncryptDetached(
@ -66,7 +124,17 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): AeadEncryptedDataAndTag { ): AeadEncryptedDataAndTag {
TODO("not implemented yet") val result = getSodium().crypto_aead_chacha20poly1305_ietf_encrypt_detached(
message.toUInt8Array(),
associatedData.toUInt8Array(),
null,
nonce.toUInt8Array(),
key.toUInt8Array(),
)
return AeadEncryptedDataAndTag(
(result.ciphertext as Uint8Array).toUByteArray(),
(result.mac as Uint8Array).toUByteArray()
)
} }
actual fun chaCha20Poly1305IetfDecryptDetached( actual fun chaCha20Poly1305IetfDecryptDetached(
@ -76,7 +144,18 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") try {
return getSodium().crypto_aead_chacha20poly1305_ietf_decrypt_detached(
null,
ciphertext.toUInt8Array(),
tag.toUInt8Array(),
associatedData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
).toUByteArray()
} catch (error: Throwable) {
throw AeadCorrupedOrTamperedDataException()
}
} }
actual fun chaCha20Poly1305Encrypt( actual fun chaCha20Poly1305Encrypt(
@ -85,7 +164,13 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") return getSodium().crypto_aead_chacha20poly1305_encrypt(
message.toUInt8Array(),
associatedData.toUInt8Array(),
null,
nonce.toUInt8Array(),
key.toUInt8Array(),
).toUByteArray()
} }
actual fun chaCha20Poly1305Decrypt( actual fun chaCha20Poly1305Decrypt(
@ -94,7 +179,17 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") try {
return getSodium().crypto_aead_chacha20poly1305_decrypt(
null,
ciphertext.toUInt8Array(),
associatedData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
).toUByteArray()
} catch (error: Throwable) {
throw AeadCorrupedOrTamperedDataException()
}
} }
actual fun chaCha20Poly1305EncryptDetached( actual fun chaCha20Poly1305EncryptDetached(
@ -103,7 +198,17 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): AeadEncryptedDataAndTag { ): AeadEncryptedDataAndTag {
TODO("not implemented yet") val result = getSodium().crypto_aead_chacha20poly1305_encrypt_detached(
message.toUInt8Array(),
associatedData.toUInt8Array(),
null,
nonce.toUInt8Array(),
key.toUInt8Array(),
)
return AeadEncryptedDataAndTag(
(result.ciphertext as Uint8Array).toUByteArray(),
(result.mac as Uint8Array).toUByteArray()
)
} }
actual fun chaCha20Poly1305DecryptDetached( actual fun chaCha20Poly1305DecryptDetached(
@ -113,7 +218,30 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") try {
return getSodium().crypto_aead_chacha20poly1305_decrypt_detached(
null,
ciphertext.toUInt8Array(),
tag.toUInt8Array(),
associatedData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
).toUByteArray()
} catch (error: Throwable) {
throw AeadCorrupedOrTamperedDataException()
}
}
actual fun xChaCha20Poly1305IetfKeygen(): UByteArray {
return getSodium().crypto_aead_xchacha20poly1305_ietf_keygen().toUByteArray()
}
actual fun chaCha20Poly1305IetfKeygen(): UByteArray {
return getSodium().crypto_aead_chacha20poly1305_ietf_keygen().toUByteArray()
}
actual fun chaCha20Poly1305Keygen(): UByteArray {
return getSodium().crypto_aead_chacha20poly1305_keygen().toUByteArray()
} }
} }

View File

@ -1,5 +1,7 @@
package com.ionspin.kotlin.crypto.aead package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
actual object AuthenticatedEncryptionWithAssociatedData { actual object AuthenticatedEncryptionWithAssociatedData {
// Ietf // Ietf
@ -11,7 +13,19 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val ciphertext = UByteArray(message.size + crypto_aead_xchacha20poly1305_ietf_ABYTES)
sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertext.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
null,
nonce.asByteArray(),
key.asByteArray(),
)
return ciphertext
} }
actual fun xChaCha20Poly1305IetfDecrypt( actual fun xChaCha20Poly1305IetfDecrypt(
@ -20,7 +34,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val message = UByteArray(ciphertext.size - crypto_aead_xchacha20poly1305_ietf_ABYTES)
val validationResult = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
message.asByteArray(),
null,
null,
ciphertext.asByteArray(),
ciphertext.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
nonce.asByteArray(),
key.asByteArray(),
)
if (validationResult != 0) {
throw AeadCorrupedOrTamperedDataException()
}
return message
} }
actual fun xChaCha20Poly1305IetfEncryptDetached( actual fun xChaCha20Poly1305IetfEncryptDetached(
@ -29,7 +58,21 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): AeadEncryptedDataAndTag { ): AeadEncryptedDataAndTag {
TODO("not implemented yet") val ciphertext = UByteArray(message.size)
val authenticationTag = UByteArray(crypto_aead_xchacha20poly1305_ietf_ABYTES)
sodium.crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
ciphertext.asByteArray(),
authenticationTag.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
null,
nonce.asByteArray(),
key.asByteArray(),
)
return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
} }
actual fun xChaCha20Poly1305IetfDecryptDetached( actual fun xChaCha20Poly1305IetfDecryptDetached(
@ -39,7 +82,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val message = UByteArray(ciphertext.size)
val validationResult = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
message.asByteArray(),
null,
ciphertext.asByteArray(),
ciphertext.size.toLong(),
tag.asByteArray(),
associatedData.asByteArray(),
associatedData.size.toLong(),
nonce.asByteArray(),
key.asByteArray(),
)
if (validationResult != 0) {
throw AeadCorrupedOrTamperedDataException()
}
return message
} }
actual fun chaCha20Poly1305IetfEncrypt( actual fun chaCha20Poly1305IetfEncrypt(
@ -48,7 +106,19 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val ciphertext = UByteArray(message.size + crypto_aead_chacha20poly1305_ietf_ABYTES)
sodium.crypto_aead_chacha20poly1305_ietf_encrypt(
ciphertext.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
null,
nonce.asByteArray(),
key.asByteArray(),
)
return ciphertext
} }
actual fun chaCha20Poly1305IetfDecrypt( actual fun chaCha20Poly1305IetfDecrypt(
@ -57,7 +127,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val message = UByteArray(ciphertext.size - crypto_aead_chacha20poly1305_ietf_ABYTES)
val validationResult = sodium.crypto_aead_chacha20poly1305_ietf_decrypt(
message.asByteArray(),
null,
null,
ciphertext.asByteArray(),
ciphertext.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
nonce.asByteArray(),
key.asByteArray(),
)
if (validationResult != 0) {
throw AeadCorrupedOrTamperedDataException()
}
return message
} }
actual fun chaCha20Poly1305IetfEncryptDetached( actual fun chaCha20Poly1305IetfEncryptDetached(
@ -66,7 +151,21 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): AeadEncryptedDataAndTag { ): AeadEncryptedDataAndTag {
TODO("not implemented yet") val ciphertext = UByteArray(message.size)
val authenticationTag = UByteArray(crypto_aead_chacha20poly1305_ietf_ABYTES)
sodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached(
ciphertext.asByteArray(),
authenticationTag.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
null,
nonce.asByteArray(),
key.asByteArray(),
)
return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
} }
actual fun chaCha20Poly1305IetfDecryptDetached( actual fun chaCha20Poly1305IetfDecryptDetached(
@ -76,7 +175,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val message = UByteArray(ciphertext.size)
val validationResult = sodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached(
message.asByteArray(),
null,
ciphertext.asByteArray(),
ciphertext.size.toLong(),
tag.asByteArray(),
associatedData.asByteArray(),
associatedData.size.toLong(),
nonce.asByteArray(),
key.asByteArray(),
)
if (validationResult != 0) {
throw AeadCorrupedOrTamperedDataException()
}
return message
} }
actual fun chaCha20Poly1305Encrypt( actual fun chaCha20Poly1305Encrypt(
@ -85,7 +199,19 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val ciphertext = UByteArray(message.size + crypto_aead_chacha20poly1305_ABYTES)
sodium.crypto_aead_chacha20poly1305_encrypt(
ciphertext.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
null,
nonce.asByteArray(),
key.asByteArray(),
)
return ciphertext
} }
actual fun chaCha20Poly1305Decrypt( actual fun chaCha20Poly1305Decrypt(
@ -94,7 +220,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val message = UByteArray(ciphertext.size - crypto_aead_chacha20poly1305_ABYTES)
val validationResult = sodium.crypto_aead_chacha20poly1305_decrypt(
message.asByteArray(),
null,
null,
ciphertext.asByteArray(),
ciphertext.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
nonce.asByteArray(),
key.asByteArray(),
)
if (validationResult != 0) {
throw AeadCorrupedOrTamperedDataException()
}
return message
} }
actual fun chaCha20Poly1305EncryptDetached( actual fun chaCha20Poly1305EncryptDetached(
@ -103,7 +244,21 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): AeadEncryptedDataAndTag { ): AeadEncryptedDataAndTag {
TODO("not implemented yet") val ciphertext = UByteArray(message.size)
val authenticationTag = UByteArray(crypto_aead_chacha20poly1305_ABYTES)
sodium.crypto_aead_chacha20poly1305_encrypt_detached(
ciphertext.asByteArray(),
authenticationTag.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
associatedData.asByteArray(),
associatedData.size.toLong(),
null,
nonce.asByteArray(),
key.asByteArray(),
)
return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
} }
actual fun chaCha20Poly1305DecryptDetached( actual fun chaCha20Poly1305DecryptDetached(
@ -113,7 +268,40 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray, nonce: UByteArray,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
TODO("not implemented yet") val message = UByteArray(ciphertext.size)
val validationResult = sodium.crypto_aead_chacha20poly1305_decrypt_detached(
message.asByteArray(),
null,
ciphertext.asByteArray(),
ciphertext.size.toLong(),
tag.asByteArray(),
associatedData.asByteArray(),
associatedData.size.toLong(),
nonce.asByteArray(),
key.asByteArray(),
)
if (validationResult != 0) {
throw AeadCorrupedOrTamperedDataException()
}
return message
}
actual fun xChaCha20Poly1305IetfKeygen(): UByteArray {
val generatedKey = UByteArray(crypto_aead_xchacha20poly1305_ietf_KEYBYTES)
sodium.crypto_aead_xchacha20poly1305_ietf_keygen(generatedKey.asByteArray())
return generatedKey
}
actual fun chaCha20Poly1305IetfKeygen(): UByteArray {
val generatedKey = UByteArray(crypto_aead_chacha20poly1305_ietf_KEYBYTES)
sodium.crypto_aead_chacha20poly1305_ietf_keygen(generatedKey.asByteArray())
return generatedKey
}
actual fun chaCha20Poly1305Keygen(): UByteArray {
val generatedKey = UByteArray(crypto_aead_chacha20poly1305_KEYBYTES)
sodium.crypto_aead_chacha20poly1305_keygen(generatedKey.asByteArray())
return generatedKey
} }
} }

View File

@ -11,10 +11,13 @@ import libsodium.crypto_aead_chacha20poly1305_ietf_decrypt
import libsodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached import libsodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached
import libsodium.crypto_aead_chacha20poly1305_ietf_encrypt import libsodium.crypto_aead_chacha20poly1305_ietf_encrypt
import libsodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached import libsodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached
import libsodium.crypto_aead_chacha20poly1305_ietf_keygen
import libsodium.crypto_aead_chacha20poly1305_keygen
import libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt import libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt
import libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached import libsodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached
import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt
import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt_detached import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt_detached
import libsodium.crypto_aead_xchacha20poly1305_ietf_keygen
actual object AuthenticatedEncryptionWithAssociatedData { actual object AuthenticatedEncryptionWithAssociatedData {
@ -504,4 +507,28 @@ actual object AuthenticatedEncryptionWithAssociatedData {
return message return message
} }
actual fun xChaCha20Poly1305IetfKeygen(): UByteArray {
val generatedKey = UByteArray(crypto_aead_xchacha20poly1305_ietf_KEYBYTES)
val generatedKeyPinned = generatedKey.pin()
crypto_aead_xchacha20poly1305_ietf_keygen(generatedKeyPinned.toPtr())
generatedKeyPinned.unpin()
return generatedKey
}
actual fun chaCha20Poly1305IetfKeygen(): UByteArray {
val generatedKey = UByteArray(crypto_aead_chacha20poly1305_ietf_KEYBYTES)
val generatedKeyPinned = generatedKey.pin()
crypto_aead_chacha20poly1305_ietf_keygen(generatedKeyPinned.toPtr())
generatedKeyPinned.unpin()
return generatedKey
}
actual fun chaCha20Poly1305Keygen(): UByteArray {
val generatedKey = UByteArray(crypto_aead_chacha20poly1305_KEYBYTES)
val generatedKeyPinned = generatedKey.pin()
crypto_aead_chacha20poly1305_keygen(generatedKeyPinned.toPtr())
generatedKeyPinned.unpin()
return generatedKey
}
} }