Working single shot xchacha20poly1305 encryption and decryption in delegated flavour

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-27 18:20:53 +02:00
1 parent 6228263978
commit e6f560ba8e
13 files changed
+66 -48

No files matched your search

@@ -40,7 +40,8 @@ interface JsSodiumInterface {
fun crypto_hash_sha512_final(state: dynamic): Uint8Array
//XChaCha20Poly1305
fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, additionalData: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, additionalData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
fun crypto_aead_xchacha20poly1305_ietf_decrypt(secretNonce: Uint8Array, ciphertext: Uint8Array, additionalData: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
}
@@ -8,10 +8,12 @@ import org.khronos.webgl.set
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-Jun-2020
*
* TODO investigate using unsafe cast
*/
fun UByteArray.toUInt8Array() : Uint8Array {
val uint8Result = Uint8Array(toByteArray().toTypedArray())
console.log("Uint8: $uint8Result")
// console.log("Uint8: $uint8Result")
return uint8Result
}
@@ -21,7 +23,7 @@ fun Uint8Array.toUByteArray() : UByteArray {
for (i in 0 until length) {
result[i] = get(i).toUByte()
}
console.log("UbyteArray: ${result.joinToString()}")
// console.log("UbyteArray: ${result.joinToString()}")
return result
}
@@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.authenticated
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
/**
* Created by Ugljesa Jovanovic
@@ -26,8 +27,9 @@ actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, addi
val encrypted = getSodium().crypto_aead_xchacha20poly1305_ietf_encrypt(
message.toUInt8Array(),
additionalData.toUInt8Array(),
key.toUInt8Array(),
nonce.toUInt8Array()
Uint8Array(0),
nonce.toUInt8Array(),
key.toUInt8Array()
)
return encrypted.toUByteArray()
}
@@ -38,7 +40,14 @@ actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, addi
ciphertext: UByteArray,
additionalData: UByteArray
): UByteArray {
TODO("not implemented yet")
val decrypted = getSodium().crypto_aead_xchacha20poly1305_ietf_decrypt(
Uint8Array(0),
ciphertext.toUInt8Array(),
additionalData.toUInt8Array(),
nonce.toUInt8Array(),
key.toUInt8Array()
)
return decrypted.toUByteArray()
}
}