diff --git a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/PrimitivesApi.kt b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/PrimitivesApi.kt index 300f80c..494521b 100644 --- a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/PrimitivesApi.kt +++ b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/PrimitivesApi.kt @@ -3,7 +3,7 @@ package com.ionspin.kotlin.crypto import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bMultipart import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties import com.ionspin.kotlin.crypto.hash.sha.Sha256 -import com.ionspin.kotlin.crypto.hash.sha.Sha512 +import com.ionspin.kotlin.crypto.hash.sha.Sha512Multipart import com.ionspin.kotlin.crypto.keyderivation.ArgonResult /** @@ -18,7 +18,7 @@ interface PrimitivesApi { fun hashSha256Multipart(): Sha256 fun hashSha256(message: UByteArray) : UByteArray - fun hashSha512Multipart(): Sha512 + fun hashSha512Multipart(): Sha512Multipart fun hashSha512(message: UByteArray) : UByteArray fun deriveKey( diff --git a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bMultipart.kt b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2b.kt similarity index 100% rename from multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bMultipart.kt rename to multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2b.kt diff --git a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt index e176c1a..2268fc6 100644 --- a/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt +++ b/multiplatform-crypto-api/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512.kt @@ -11,11 +11,11 @@ import com.ionspin.kotlin.crypto.hash.MultipartHash object Sha512Properties { const val MAX_HASH_BYTES = 64 } -interface Sha512 : MultipartHash { +interface Sha512Multipart : MultipartHash { override val MAX_HASH_BYTES: Int get() = Sha256Properties.MAX_HASH_BYTES } -interface MultipartSha512 : Hash { +interface Sha512 : Hash { override val MAX_HASH_BYTES: Int get() = Sha512Properties.MAX_HASH_BYTES diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index aab902c..cd5e701 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -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) } diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt index 4b70cc9..6e007a7 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt @@ -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 diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt index e2bc79b..8d5b857 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt @@ -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 diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt index 187264b..0e2235f 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/SRNGTest.kt @@ -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) } } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt new file mode 100644 index 0000000..35513ae --- /dev/null +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt @@ -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) + } + + + } +} diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bTest.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bTest.kt index 6f98052..1fea8a7 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bTest.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bTest.kt @@ -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 } } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt index 4a73910..ae5e010 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Test.kt @@ -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 } } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Test.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Test.kt index 88ea916..14eec63 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Test.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Test.kt @@ -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 } } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index 9233b77..0ddeb41 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -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 + } diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt index 47b3867..b445487 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumLoader.kt @@ -53,4 +53,4 @@ object JsSodiumLoader { doneCallback.invoke() } } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt new file mode 100644 index 0000000..958d1a5 --- /dev/null +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt @@ -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 +} diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt new file mode 100644 index 0000000..1a79a25 --- /dev/null +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -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 { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt index 2774b8d..552932b 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt @@ -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())) diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt index f2767cc..992de98 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -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() } } diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt index 312a61a..f6ff62b 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt @@ -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) diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt new file mode 100644 index 0000000..993d7f0 --- /dev/null +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -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 { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 7b49789..1206f1b 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -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 { } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt index 7e5991e..f4b80a6 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt @@ -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 } -} \ No newline at end of file +} diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index f34fdb8..33c4cb3 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -52,7 +52,7 @@ object CryptoPrimitives : PrimitivesApi { return Sha256Pure.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 Sha512Pure() } diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/Aes256GcmPure.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/Aes256GcmPure.kt deleted file mode 100644 index afd913e..0000000 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/Aes256GcmPure.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.ionspin.kotlin.crypto.authenticated - -import com.ionspin.kotlin.crypto.SRNG - -/** - * Created by Ugljesa Jovanovic - * ugljesa.jovanovic@ionspin.com - * on 14-Jun-2020 - */ -internal class Aes256GcmStatelessPure : Aes256GcmStateless { - /** - * Nonce autogenerated - */ - override fun encrypt(message: UByteArray, additionalData: UByteArray, rawData : UByteArray, key: Aes256GcmKey) : Aes256GcmEncryptionResult { - - TODO() - - } - - override fun decrypt(encryptedData: UByteArray, nonce: UByteArray, key: Aes256GcmKey): UByteArray { - TODO("not implemented yet") - } - -} diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt index 2382247..f96bbbf 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt @@ -1,5 +1,6 @@ package com.ionspin.kotlin.crypto.authenticated +import com.ionspin.kotlin.crypto.AuthenticatedEncryption import com.ionspin.kotlin.crypto.InvalidTagException import com.ionspin.kotlin.crypto.MultipartAuthenticatedDecryption import com.ionspin.kotlin.crypto.SRNG @@ -13,7 +14,7 @@ import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray * ugljesa.jovanovic@ionspin.com * on 17-Jun-2020 */ -class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) : { +class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) { companion object : AuthenticatedEncryption { override fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray { @@ -68,7 +69,7 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) } - val nonce = SRNG.getRandomBytes(24) + internal val nonce = SRNG.getRandomBytes(24) private val updateableEncryptionPrimitive = XChaCha20Pure(key, nonce, initialCounter = 1U) private val updateableMacPrimitive : Poly1305 diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Pure.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Pure.kt index 0617869..786f42b 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Pure.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Pure.kt @@ -25,11 +25,11 @@ import com.ionspin.kotlin.crypto.util.rotateRight */ -class Sha512Pure : Sha512 { +class Sha512Pure : Sha512Multipart { override val MAX_HASH_BYTES: Int = 32 - companion object : MultipartSha512 { + companion object : Sha512 { const val BLOCK_SIZE = 1024 const val BLOCK_SIZE_IN_BYTES = 128 const val CHUNK_SIZE = 80 diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Pure.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Pure.kt index 260aef4..f1064cf 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Pure.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Pure.kt @@ -19,8 +19,9 @@ package com.ionspin.kotlin.crypto.keyderivation.argon2 import com.ionspin.kotlin.bignum.integer.toBigInteger -import com.ionspin.kotlin.crypto.Blake2bPureStateless + import com.ionspin.kotlin.crypto.SRNG +import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure import com.ionspin.kotlin.crypto.keyderivation.ArgonResult import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bArbitraryLenghtHash import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG @@ -290,7 +291,7 @@ class Argon2Pure( salt.size.toUInt().toLittleEndianUByteArray() + salt + key.size.toUInt().toLittleEndianUByteArray() + key + associatedData.size.toUInt().toLittleEndianUByteArray() + associatedData - val h0 = Blake2bPureStateless.digest( + val h0 = Blake2bPure.digest( blakeInput ) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt index 4cac54d..1dc57f9 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt @@ -26,30 +26,6 @@ val _emitIntArray: IntArray = intArrayOf(1) * ugljesa.jovanovic@ionspin.com * on 15-Jul-2019 */ -fun Array.hexColumsPrint() { - val printout = this.map { it.toString(16) }.chunked(16) - printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } -} - -fun Array.hexColumsPrint(chunk : Int = 16) { - val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk) - printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } -} - -fun UByteArray.hexColumsPrint(chunk : Int = 16) { - val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk) - printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } -} - -fun UIntArray.hexColumsPrint(chunk : Int = 4) { - val printout = this.map { it.toString(16).padStart(8, '0') }.chunked(chunk) - printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } -} - -fun Array.hexColumsPrint(chunk: Int = 3) { - val printout = this.map { it.toString(16) }.chunked(chunk) - printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } -} inline fun Array.chunked(sliceSize: Int): Array> { val last = this.size % sliceSize @@ -122,37 +98,6 @@ fun UByteArray.xorWithPositionsAndInsertIntoArray( } } -fun String.hexStringToTypedUByteArray() : Array { - return this.chunked(2).map { it.toUByte(16) }.toTypedArray() -} - - -fun String.hexStringToUByteArray() : UByteArray { - return this.chunked(2).map { it.toUByte(16) }.toUByteArray() -} - - -fun Array.toHexString() : String { - return this.joinToString(separator = "") { - if (it <= 0x0FU) { - "0${it.toString(16)}" - } else { - it.toString(16) - } - } -} - - -fun UByteArray.toHexString() : String { - return this.joinToString(separator = "") { - if (it <= 0x0FU) { - "0${it.toString(16)}" - } else { - it.toString(16) - } - } -} - // UInt / Array utils fun UInt.toBigEndianUByteArray() : Array { @@ -356,4 +301,4 @@ fun Collection.flattenToUByteArray(): UByteArray { } } return result -} \ No newline at end of file +} diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt index 8b92c99..eedc82a 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/ReadmeTest.kt @@ -159,9 +159,9 @@ class ReadmeTest { @Test fun debugTest() { - val result = Blake2bPureStateless.digest("test".encodeToUByteArray()) + val result = Blake2bPure.digest("test".encodeToUByteArray()) println(result.toHexString()) } -} \ No newline at end of file +} diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt index 05e0be4..7c2b6d4 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt @@ -1,6 +1,7 @@ package com.ionspin.kotlin.crypto.authenticated import com.ionspin.kotlin.crypto.hash.encodeToUByteArray +import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertTrue @@ -90,7 +91,7 @@ class XChaCha20Poly1305Test { } - + @Ignore() //"Will fail because nonce is not a parameter any more" @Test fun updateableXChaCha20Poly1305() { assertTrue { @@ -132,9 +133,9 @@ class XChaCha20Poly1305Test { 0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU, 0xcfU, 0x49U ) - val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData) + val xChaChaPoly = XChaCha20Poly1305Pure(key, additionalData) val firstChunk = xChaChaPoly.encryptPartialData(message) - val finalChunk = xChaChaPoly.finishEncryption() + val finalChunk = xChaChaPoly.finishEncryption().first val result = firstChunk + finalChunk result.contentEquals(expected) @@ -163,9 +164,9 @@ class XChaCha20Poly1305Test { 0xbdU, 0x3bU, 0x8aU, 0xd7U, 0xa1U, 0x9dU, 0xe8U, 0xc4U, 0x55U, 0x84U, 0x6fU, 0xfcU, 0x75U, 0x31U, 0xbfU, 0x0cU, 0x2dU ) - val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData) + val xChaChaPoly = XChaCha20Poly1305Pure(key, additionalData) val firstChunk = xChaChaPoly.encryptPartialData(message) - val finalChunk = xChaChaPoly.finishEncryption() + val finalChunk = xChaChaPoly.finishEncryption().first val result = firstChunk + finalChunk result.contentEquals(expected) }