Start work on _aead_

This commit is contained in:
Ugljesa Jovanovic 2020-08-30 11:21:53 +02:00 committed by Ugljesa Jovanovic
parent 99b9ee5e9d
commit 54489ef6cb
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
5 changed files with 570 additions and 3 deletions

View File

@ -0,0 +1,121 @@
package com.ionspin.kotlin.crypto.aead
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 30-Aug-2020
*/
//X - Ietf
val crypto_aead_xchacha20poly1305_ietf_KEYBYTES = 32
val crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = 24
val crypto_aead_xchacha20poly1305_ietf_ABYTES = 16
// Ietf
val crypto_aead_chacha20poly1305_ietf_KEYBYTES = 32
val crypto_aead_chacha20poly1305_ietf_NPUBBYTES = 12
val crypto_aead_chacha20poly1305_ietf_ABYTES = 16
// original chacha20poly1305
val crypto_aead_chacha20poly1305_KEYBYTES = 32
val crypto_aead_chacha20poly1305_NPUBBYTES = 8
val crypto_aead_chacha20poly1305_ABYTES = 16
data class AeadEncryptedDataAndTag(val data: UByteArray, val tag: UByteArray)
expect object AuthenticatedEncryptionWithAssociatedData {
// X - Ietf
fun xChaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
fun xChaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
fun xChaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag
fun xChaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
// Ietf
fun chaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
fun chaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
fun chaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag
fun chaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
// Original chacha20poly1305
fun chaCha20Poly1305Encrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
fun chaCha20Poly1305Decrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
fun chaCha20Poly1305EncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag
fun chaCha20Poly1305DecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray
}

View File

@ -14,9 +14,7 @@ val crypto_secretbox_NONCEBYTES = 24
class SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey() : RuntimeException("MAC validation failed. Data is corrupted or tampered with.")
data class SecretBoxEncryptedDataAndTag(
@JsName("data")
val data: UByteArray, val tag: UByteArray)
data class SecretBoxEncryptedDataAndTag(val data: UByteArray, val tag: UByteArray)
expect object SecretBox {

View File

@ -0,0 +1,119 @@
package com.ionspin.kotlin.crypto.aead
actual object AuthenticatedEncryptionWithAssociatedData {
// Ietf
// Original chacha20poly1305
actual fun xChaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305Encrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305Decrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305EncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305DecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
}

View File

@ -0,0 +1,119 @@
package com.ionspin.kotlin.crypto.aead
actual object AuthenticatedEncryptionWithAssociatedData {
// Ietf
// Original chacha20poly1305
actual fun xChaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305Encrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305Decrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305EncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305DecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
}

View File

@ -0,0 +1,210 @@
package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert
import kotlinx.cinterop.pin
import libsodium.crypto_aead_chacha20poly1305_encrypt
import libsodium.crypto_aead_chacha20poly1305_ietf_encrypt
import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt
actual object AuthenticatedEncryptionWithAssociatedData {
// Ietf
// Original chacha20poly1305
actual fun xChaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
val messagePinned = message.pin()
val associatedDataPinned = associatedData.pin()
val noncePinned = nonce.pin()
val keyPinned = key.pin()
val ciphertext = UByteArray(message.size + crypto_aead_xchacha20poly1305_ietf_ABYTES)
val ciphertextPinned = ciphertext.pin()
crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertextPinned.toPtr(),
null,
messagePinned.toPtr(),
message.size.convert(),
associatedDataPinned.toPtr(),
associatedData.size.convert(),
null, // nsec not used in this construct
noncePinned.toPtr(),
keyPinned.toPtr()
)
ciphertextPinned.unpin()
messagePinned.unpin()
associatedDataPinned.unpin()
noncePinned.unpin()
keyPinned.unpin()
return ciphertext
}
actual fun xChaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfEncrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
val messagePinned = message.pin()
val associatedDataPinned = associatedData.pin()
val noncePinned = nonce.pin()
val keyPinned = key.pin()
val ciphertext = UByteArray(message.size + crypto_aead_chacha20poly1305_ietf_ABYTES)
val ciphertextPinned = ciphertext.pin()
crypto_aead_chacha20poly1305_ietf_encrypt(
ciphertextPinned.toPtr(),
null,
messagePinned.toPtr(),
message.size.convert(),
associatedDataPinned.toPtr(),
associatedData.size.convert(),
null, // nsec not used in this construct
noncePinned.toPtr(),
keyPinned.toPtr()
)
ciphertextPinned.unpin()
messagePinned.unpin()
associatedDataPinned.unpin()
noncePinned.unpin()
keyPinned.unpin()
return ciphertext
}
actual fun chaCha20Poly1305IetfDecrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfEncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305IetfDecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305Encrypt(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
val messagePinned = message.pin()
val associatedDataPinned = associatedData.pin()
val noncePinned = nonce.pin()
val keyPinned = key.pin()
val ciphertext = UByteArray(message.size + crypto_aead_chacha20poly1305_ABYTES)
val ciphertextPinned = ciphertext.pin()
crypto_aead_chacha20poly1305_encrypt(
ciphertextPinned.toPtr(),
null,
messagePinned.toPtr(),
message.size.convert(),
associatedDataPinned.toPtr(),
associatedData.size.convert(),
null, // nsec not used in this construct
noncePinned.toPtr(),
keyPinned.toPtr()
)
ciphertextPinned.unpin()
messagePinned.unpin()
associatedDataPinned.unpin()
noncePinned.unpin()
keyPinned.unpin()
return ciphertext
}
actual fun chaCha20Poly1305Decrypt(
ciphertext: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305EncryptDetached(
message: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): AeadEncryptedDataAndTag {
TODO("not implemented yet")
}
actual fun chaCha20Poly1305DecryptDetached(
ciphertext: UByteArray,
tag: UByteArray,
associatedData: UByteArray,
nonce: UByteArray,
key: UByteArray
): UByteArray {
TODO("not implemented yet")
}
}