Further progress on delegated xchacha poly, working jvm , js in progress

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-25 21:16:14 +02:00
1 parent e3fe276e4c
commit 6228263978
29 files changed
+419 -131

No files matched your search

@@ -58,7 +58,7 @@ object CryptoPrimitives : PrimitivesApi {
}
object Sha512 {
fun updateable(): com.ionspin.kotlin.crypto.hash.sha.Sha512 {
fun updateable(): com.ionspin.kotlin.crypto.hash.sha.Sha512Multipart {
checkInitialization()
return Sha512Delegated()
}
@@ -95,7 +95,7 @@ object CryptoPrimitives : PrimitivesApi {
return Sha256StatelessDelegated.digest(inputMessage = message)
}
override fun hashSha512Multipart(): com.ionspin.kotlin.crypto.hash.sha.Sha512 {
override fun hashSha512Multipart(): com.ionspin.kotlin.crypto.hash.sha.Sha512Multipart {
checkInitialization()
return Sha512Delegated()
}
@@ -179,7 +179,7 @@ class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKe
}
override fun finish(): MultipartEncryptedDataDescriptor {
val finished = primitive.finish()
val finished = primitive.finishEncryption()
return MultipartEncryptedDataDescriptor(finished.first, finished.second)
}
@@ -1,14 +1,15 @@
package com.ionspin.kotlin.crypto.authenticated
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
expect class XChaCha20Poly1305Delegated {
expect class XChaCha20Poly1305Delegated constructor(key: UByteArray, additionalData: UByteArray) {
companion object {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray
fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray
fun decrypt(key: UByteArray, nonce: UByteArray, ciphertext: UByteArray, additionalData: UByteArray) : UByteArray
}
fun encryptPartialData(data: UByteArray) : UByteArray
@@ -24,6 +24,6 @@ package com.ionspin.kotlin.crypto.hash.sha
*/
expect class Sha512Delegated() : Sha512
expect class Sha512Delegated() : Sha512Multipart
expect object Sha512StatelessDelegated : MultipartSha512
expect object Sha512StatelessDelegated : Sha512
@@ -28,10 +28,10 @@ import kotlin.test.assertTrue
class SRNGTest {
@Test
fun testSrng() = testBlocking {
Crypto.initialize()
CryptoInitializerDelegated.initialize()
//Just a sanity test, need to add better srng tests.
val randomBytes1 = SRNG.getRandomBytes(10)
val randomBytes2 = SRNG.getRandomBytes(10)
assertTrue { !randomBytes1.contentEquals(randomBytes2) }
}
}
}
@@ -0,0 +1,184 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.CryptoInitializerDelegated
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class XChaCha20Poly1305Test {
@Test
fun xChaCha20Poly1305() {
testBlocking {
CryptoInitializerDelegated.initialize()
}
assertTrue {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
val additionalData = ubyteArrayOf(
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
)
val key = ubyteArrayOf(
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
)
val nonce = ubyteArrayOf(
0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
)
val expected = ubyteArrayOf(
0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U,
0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U,
0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U,
0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U,
0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU,
0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU,
0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U,
0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U,
0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U,
0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U,
0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
0xcfU, 0x49U
)
val encrypted = XChaCha20Poly1305Delegated.encrypt(key, nonce, message, additionalData)
// val decrypted = XChaCha20Poly1305Delegated.decrypt(key, nonce, encrypted, additionalData)
encrypted.contentEquals(expected) // && decrypted.contentEquals(message)
}
assertTrue {
val message = ubyteArrayOf(
0x00U
)
val additionalData = ubyteArrayOf(
0x00U
)
val key = ubyteArrayOf(
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
)
val nonce = ubyteArrayOf(
0x00U, 0x01U, 0x02U, 0x03U, 0x04U, 0x05U, 0x06U, 0x07U, 0x08U, 0x09U, 0x0aU, 0x0bU,
0x0cU, 0x0dU, 0x0eU, 0x0fU, 0x10U, 0x11U, 0x12U, 0x13U, 0x14U, 0x15U, 0x16U, 0x17U,
)
val expected = ubyteArrayOf(
0xbdU, 0x3bU, 0x8aU, 0xd7U, 0xa1U, 0x9dU, 0xe8U, 0xc4U, 0x55U,
0x84U, 0x6fU, 0xfcU, 0x75U, 0x31U, 0xbfU, 0x0cU, 0x2dU
)
val encrypted = XChaCha20Poly1305Delegated.encrypt(key, nonce, message, additionalData)
// val decrypted = XChaCha20Poly1305Delegated.decrypt(key, nonce, encrypted, additionalData)
encrypted.contentEquals(expected) // && decrypted.contentEquals(message)
}
}
@Ignore() //"Will fail because nonce is not a parameter any more"
@Test
fun updateableXChaCha20Poly1305() {
assertTrue {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
val additionalData = ubyteArrayOf(
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
)
val key = ubyteArrayOf(
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
)
val nonce = ubyteArrayOf(
0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
)
val expected = ubyteArrayOf(
0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U,
0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U,
0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U,
0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U,
0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU,
0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU,
0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U,
0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U,
0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U,
0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U,
0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
0xcfU, 0x49U
)
val xChaChaPoly = XChaCha20Poly1305Delegated(key, additionalData)
val firstChunk = xChaChaPoly.encryptPartialData(message)
val finalChunk = xChaChaPoly.finishEncryption().first
val result = firstChunk + finalChunk
result.contentEquals(expected)
}
assertTrue {
val message = ubyteArrayOf(
0x00U
)
val additionalData = ubyteArrayOf(
0x00U
)
val key = ubyteArrayOf(
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
)
val nonce = ubyteArrayOf(
0x00U, 0x01U, 0x02U, 0x03U, 0x04U, 0x05U, 0x06U, 0x07U, 0x08U, 0x09U, 0x0aU, 0x0bU,
0x0cU, 0x0dU, 0x0eU, 0x0fU, 0x10U, 0x11U, 0x12U, 0x13U, 0x14U, 0x15U, 0x16U, 0x17U,
)
val expected = ubyteArrayOf(
0xbdU, 0x3bU, 0x8aU, 0xd7U, 0xa1U, 0x9dU, 0xe8U, 0xc4U, 0x55U,
0x84U, 0x6fU, 0xfcU, 0x75U, 0x31U, 0xbfU, 0x0cU, 0x2dU
)
val xChaChaPoly = XChaCha20Poly1305Delegated(key, additionalData)
val firstChunk = xChaChaPoly.encryptPartialData(message)
val finalChunk = xChaChaPoly.finishEncryption().first
val result = firstChunk + finalChunk
result.contentEquals(expected)
}
}
}
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.CryptoPrimitives
import com.ionspin.kotlin.crypto.Initializer
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
@@ -23,7 +24,7 @@ class Blake2bTest {
Initializer.initialize()
val expected = "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a483aa9bc33b582f77d30a65e6f29a89" +
"6c0411f38312e1d66e0bf16386c86a89bea572"
val result = Crypto.Blake2b.stateless("test".encodeToUByteArray()).toHexString()
val result = CryptoPrimitives.Blake2b.stateless("test".encodeToUByteArray()).toHexString()
// println("Result: $result")
assertTrue { result == expected }
}
@@ -35,11 +36,11 @@ class Blake2bTest {
Initializer.initialize()
val expected = "a71079d42853dea26e453004338670a53814b78137ffbed07603a41d76a483aa9bc33b582f77d30a65e6f29a89" +
"6c0411f38312e1d66e0bf16386c86a89bea572"
val blake2b = Crypto.Blake2b.updateable()
val blake2b = CryptoPrimitives.Blake2b.updateable()
blake2b.update("t".encodeToUByteArray())
blake2b.update(("est".encodeToUByteArray()))
val result = blake2b.digest().toHexString()
// println("Result: $result")
assertTrue { result == expected }
}
}
}
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.CryptoPrimitives
import com.ionspin.kotlin.crypto.Initializer
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
@@ -23,7 +24,7 @@ class Sha256Test {
@Test
fun statelessSimpleTest() {
val expected = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
val result = Crypto.Sha256.stateless("test".encodeToUByteArray()).toHexString()
val result = CryptoPrimitives.Sha256.stateless("test".encodeToUByteArray()).toHexString()
// println("Result: $result")
assertTrue { result == expected }
}
@@ -33,11 +34,11 @@ class Sha256Test {
@Test
fun updateableSimpleTest() {
val expected = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
val sha256 = Crypto.Sha256.updateable()
val sha256 = CryptoPrimitives.Sha256.updateable()
sha256.update("t".encodeToUByteArray())
sha256.update(("est".encodeToUByteArray()))
val result = sha256.digest().toHexString()
// println("Result: $result")
assertTrue { result == expected }
}
}
}
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.Crypto
import com.ionspin.kotlin.crypto.CryptoPrimitives
import com.ionspin.kotlin.crypto.Initializer
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
@@ -24,7 +25,7 @@ class Sha512Test {
fun statelessSimpleTest() {
val expected = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67" +
"b143732c304cc5fa9ad8e6f57f50028a8ff"
val result = Crypto.Sha512.stateless("test".encodeToUByteArray()).toHexString()
val result = CryptoPrimitives.Sha512.stateless("test".encodeToUByteArray()).toHexString()
// println("Result: $result")
assertTrue { result == expected }
}
@@ -35,11 +36,11 @@ class Sha512Test {
fun updateableSimpleTest() {
val expected = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67" +
"b143732c304cc5fa9ad8e6f57f50028a8ff"
val sha512 = Crypto.Sha512.updateable()
val sha512 = CryptoPrimitives.Sha512.updateable()
sha512.update("t".encodeToUByteArray())
sha512.update(("est".encodeToUByteArray()))
val result = sha512.digest().toHexString()
// println("Result: $result")
assertTrue { result == expected }
}
}
}
@@ -39,5 +39,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
}
@@ -53,4 +53,4 @@ object JsSodiumLoader {
doneCallback.invoke()
}
}
}
}
@@ -0,0 +1,27 @@
package ext.libsodium.com.ionspin.kotlin.crypto
import org.khronos.webgl.Uint8Array
import org.khronos.webgl.get
import org.khronos.webgl.set
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-Jun-2020
*/
fun UByteArray.toUInt8Array() : Uint8Array {
val uint8Result = Uint8Array(toByteArray().toTypedArray())
console.log("Uint8: $uint8Result")
return uint8Result
}
fun Uint8Array.toUByteArray() : UByteArray {
val result = UByteArray(length)
for (i in 0 until length) {
result[i] = get(i).toUByte()
}
console.log("UbyteArray: ${result.joinToString()}")
return result
}
@@ -0,0 +1,63 @@
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
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 25-Jun-2020
*/
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, additionalData: UByteArray) {
actual companion object {
actual fun encrypt(
key: UByteArray,
nonce: UByteArray,
message: UByteArray,
additionalData: UByteArray
): UByteArray {
val encrypted = getSodium().crypto_aead_xchacha20poly1305_ietf_encrypt(
message.toUInt8Array(),
additionalData.toUInt8Array(),
key.toUInt8Array(),
nonce.toUInt8Array()
)
return encrypted.toUByteArray()
}
actual fun decrypt(
key: UByteArray,
nonce: UByteArray,
ciphertext: UByteArray,
additionalData: UByteArray
): UByteArray {
TODO("not implemented yet")
}
}
actual fun encryptPartialData(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
actual fun verifyPartialData(data: UByteArray) {
}
actual fun checkTag(expectedTag: UByteArray) {
}
actual fun decrypt(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
actual fun finishEncryption(): Pair<UByteArray, UByteArray> {
TODO("not implemented yet")
}
}
@@ -12,7 +12,7 @@ import org.khronos.webgl.get
*/
actual class Sha512Delegated : Sha512 {
actual class Sha512Delegated : Sha512Multipart {
val state : dynamic
init {
@@ -34,7 +34,7 @@ actual class Sha512Delegated : Sha512 {
}
actual object Sha512StatelessDelegated : MultipartSha512 {
actual object Sha512StatelessDelegated : Sha512 {
override fun digest(inputMessage: UByteArray): UByteArray {
val hashed = getSodium().crypto_hash_sha512(Uint8Array(inputMessage.toByteArray().toTypedArray()))
@@ -8,7 +8,7 @@ import com.ionspin.kotlin.crypto.Initializer.sodium
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
actual class XChaCha20Poly1305Delegated {
actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, additionalData: UByteArray) {
actual companion object {
actual fun encrypt(
key: UByteArray,
@@ -16,7 +16,7 @@ actual class XChaCha20Poly1305Delegated {
message: UByteArray,
additionalData: UByteArray
): UByteArray {
val ciphertext = ByteArray(message.size + sodium.crypto_secretstream_xchacha20poly1305_abytes())
val ciphertext = ByteArray(message.size + 16)
SodiumJava().crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertext,
longArrayOf(ciphertext.size.toLong()),
@@ -35,10 +35,24 @@ actual class XChaCha20Poly1305Delegated {
actual fun decrypt(
key: UByteArray,
nonce: UByteArray,
cipherText: UByteArray,
ciphertext: UByteArray,
additionalData: UByteArray
): UByteArray {
TODO("not implemented yet")
val message = ByteArray(ciphertext.size - sodium.crypto_secretstream_xchacha20poly1305_abytes())
SodiumJava().crypto_aead_xchacha20poly1305_ietf_encrypt(
message,
longArrayOf(ciphertext.size.toLong()),
ciphertext.toByteArray(),
ciphertext.size.toLong(),
additionalData.toByteArray(),
additionalData.size.toLong(),
null,
nonce.toByteArray(),
key.toByteArray()
)
return message.toUByteArray()
}
}
@@ -10,7 +10,7 @@ import com.ionspin.kotlin.crypto.Initializer
*/
actual class Sha512Delegated : Sha512 {
actual class Sha512Delegated : Sha512Multipart {
val state = Hash.State512()
@@ -30,7 +30,7 @@ actual class Sha512Delegated : Sha512 {
}
actual object Sha512StatelessDelegated : MultipartSha512 {
actual object Sha512StatelessDelegated : Sha512 {
override fun digest(inputMessage: UByteArray): UByteArray {
val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES)
@@ -0,0 +1,69 @@
package com.ionspin.kotlin.crypto.authenticated
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert
import kotlinx.cinterop.pin
import kotlinx.cinterop.toCValues
import libsodium.crypto_aead_xchacha20poly1305_IETF_ABYTES
import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, additionalData: UByteArray) {
actual companion object {
actual fun encrypt(
key: UByteArray,
nonce: UByteArray,
message: UByteArray,
additionalData: UByteArray
): UByteArray {
val ciphertextLength = message.size + crypto_aead_xchacha20poly1305_IETF_ABYTES.toInt()
val ciphertext = UByteArray(ciphertextLength)
val ciphertextPinned = ciphertext.pin()
crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertextPinned.addressOf(0),
ciphertextLength.convert(),
message.toCValues(),
message.size.convert(),
additionalData.toCValues(),
additionalData.size.convert(),
null,
nonce.toCValues(),
key.toCValues()
)
ciphertextPinned.unpin()
return ciphertext
}
actual fun decrypt(
key: UByteArray,
nonce: UByteArray,
ciphertext: UByteArray,
additionalData: UByteArray
): UByteArray {
TODO("not implemented yet")
}
}
actual fun encryptPartialData(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
actual fun verifyPartialData(data: UByteArray) {
}
actual fun checkTag(expectedTag: UByteArray) {
}
actual fun decrypt(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
actual fun finishEncryption(): Pair<UByteArray, UByteArray> {
TODO("not implemented yet")
}
}
@@ -11,7 +11,7 @@ import platform.posix.malloc
*/
actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b {
actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2bMultipart {
override val MAX_HASH_BYTES: Int = 64
val requestedHashLength : Int
@@ -38,7 +38,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
}
@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
actual object Blake2bDelegatedStateless : Blake2bStateless {
actual object Blake2bDelegatedStateless : Blake2b {
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
val hashResult = UByteArray(MAX_HASH_BYTES)
@@ -57,4 +57,4 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
}
}
}
@@ -12,7 +12,7 @@ import platform.posix.malloc
*/
actual class Sha512Delegated : Sha512 {
actual class Sha512Delegated : Sha512Multipart {
val state : crypto_hash_sha512_state
init {
@@ -37,7 +37,7 @@ actual class Sha512Delegated : Sha512 {
}
actual object Sha512StatelessDelegated : StatelessSha512 {
actual object Sha512StatelessDelegated : Sha512 {
override fun digest(inputMessage: UByteArray): UByteArray {
val hashResult = UByteArray(Sha512StatelessDelegated.MAX_HASH_BYTES)
@@ -46,4 +46,4 @@ actual object Sha512StatelessDelegated : StatelessSha512 {
hashResultPinned.unpin()
return hashResult
}
}
}