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

This commit is contained in:
Ugljesa Jovanovic 2020-06-25 21:16:14 +02:00 committed by Ugljesa Jovanovic
parent e3fe276e4c
commit 6228263978
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
29 changed files with 419 additions and 131 deletions

View File

@ -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(

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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) }
}
}
}

View File

@ -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)
}
}
}

View File

@ -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 }
}
}
}

View File

@ -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 }
}
}
}

View File

@ -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 }
}
}
}

View File

@ -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
}

View File

@ -53,4 +53,4 @@ object JsSodiumLoader {
doneCallback.invoke()
}
}
}
}

View File

@ -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
}

View File

@ -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")
}
}

View File

@ -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()))

View File

@ -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()
}
}

View File

@ -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)

View File

@ -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")
}
}

View File

@ -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 {
}
}
}

View File

@ -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
}
}
}

View File

@ -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()
}

View File

@ -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")
}
}

View File

@ -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

View File

@ -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

View File

@ -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
)

View File

@ -26,30 +26,6 @@ val _emitIntArray: IntArray = intArrayOf(1)
* ugljesa.jovanovic@ionspin.com
* on 15-Jul-2019
*/
fun Array<Byte>.hexColumsPrint() {
val printout = this.map { it.toString(16) }.chunked(16)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun Array<UByte>.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<ULong>.hexColumsPrint(chunk: Int = 3) {
val printout = this.map { it.toString(16) }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
inline fun <reified T> Array<T>.chunked(sliceSize: Int): Array<Array<T>> {
val last = this.size % sliceSize
@ -122,37 +98,6 @@ fun UByteArray.xorWithPositionsAndInsertIntoArray(
}
}
fun String.hexStringToTypedUByteArray() : Array<UByte> {
return this.chunked(2).map { it.toUByte(16) }.toTypedArray()
}
fun String.hexStringToUByteArray() : UByteArray {
return this.chunked(2).map { it.toUByte(16) }.toUByteArray()
}
fun Array<UByte>.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<UByte> {
@ -356,4 +301,4 @@ fun Collection<UByteArray>.flattenToUByteArray(): UByteArray {
}
}
return result
}
}

View File

@ -159,9 +159,9 @@ class ReadmeTest {
@Test
fun debugTest() {
val result = Blake2bPureStateless.digest("test".encodeToUByteArray())
val result = Blake2bPure.digest("test".encodeToUByteArray())
println(result.toHexString())
}
}
}

View File

@ -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)
}