Completed cha cha aead variants

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-08-30 13:10:31 +02:00
1 parent 3f9f316e04
commit dd0895b5f3
5 files changed
+395 -27

No files matched your search

@@ -1,5 +1,7 @@
package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
actual object AuthenticatedEncryptionWithAssociatedData {
// Ietf
@@ -11,7 +13,19 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -20,7 +34,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -29,7 +58,21 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: UByteArray
): 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(
@@ -39,7 +82,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -48,7 +106,19 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -57,7 +127,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -66,7 +151,21 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: UByteArray
): 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(
@@ -76,7 +175,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -85,7 +199,19 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -94,7 +220,22 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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(
@@ -103,7 +244,21 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: UByteArray
): 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(
@@ -113,7 +268,40 @@ actual object AuthenticatedEncryptionWithAssociatedData {
nonce: UByteArray,
key: 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
}
}