Adopting libsodium secret stream approach for multipart, currently correct state and keystream, but mac is still incorrect on pure implementation

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-29 21:46:29 +02:00
1 parent e6f560ba8e
commit 85e3e2e4ee
13 files changed
+220 -37

No files matched your search

@@ -55,7 +55,7 @@ object CryptoPrimitives : PrimitivesApi {
}
object Sha512 {
fun updateable(): com.ionspin.kotlin.crypto.hash.sha.Sha512Multipart {
fun updateable(): Sha512Multipart {
checkInitialization()
return Sha512Delegated()
}
@@ -7,6 +7,7 @@ package com.ionspin.kotlin.crypto.authenticated
* on 14-Jun-2020
*/
expect class XChaCha20Poly1305Delegated constructor(key: UByteArray, additionalData: UByteArray) {
internal constructor(key: UByteArray, additionalData: UByteArray, testState : UByteArray, testHeader: 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
@@ -184,4 +184,32 @@ class XChaCha20Poly1305Test {
}
@Test
fun testStreamingImpl() {
val key = UByteArray(32) { 0U}
val state = ubyteArrayOf(
0x2DU, 0xDBU, 0xC7U, 0xB2U, 0x03U, 0xBCU, 0xC3U, 0x22U, 0xBDU, 0x0CU, 0xBAU, 0x82U, 0xADU, 0x77U, 0x79U, 0x44U,
0xE6U, 0x8FU, 0xA9U, 0x94U, 0x89U, 0xB1U, 0xDFU, 0xBEU, 0x00U, 0x9FU, 0x69U, 0xECU, 0x21U, 0x88U, 0x47U, 0x55U,
0x01U, 0x00U, 0x00U, 0x00U, 0xC5U, 0x55U, 0x06U, 0x38U, 0xEBU, 0xA3U, 0x12U, 0x7BU, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U,
)
val header = ubyteArrayOf(
0x49U, 0x62U, 0x22U, 0x03U, 0xB7U, 0x46U, 0x11U, 0x97U, 0x8FU, 0x46U, 0x4AU, 0x3BU, 0x2FU, 0x2AU, 0x81U, 0x03U,
0xC5U, 0x55U, 0x06U, 0x38U, 0xEBU, 0xA3U, 0x12U, 0x7BU,
)
val expected = ubyteArrayOf(
0xAFU, 0xD3U, 0x2DU, 0x59U, 0xB8U, 0xC4U, 0x66U, 0x2EU, 0x47U, 0x29U, 0xC6U, 0xF9U, 0x93U, 0x4BU, 0x09U, 0x27U,
0x24U, 0xDDU, 0xF3U, 0x05U, 0x48U, 0x94U, 0x67U, 0x10U, 0x00U, 0x21U, 0x85U, 0x22U, 0x96U, 0x3CU, 0xCEU, 0x8EU,
0xB7U, 0x53U, 0x9DU, 0x46U, 0xF5U, 0x3CU, 0x5EU, 0x48U, 0x9BU, 0x8CU, 0x13U, 0xB7U, 0x28U, 0x6BU, 0xB3U, 0x6CU,
0x3AU, 0x04U, 0xB7U, 0x25U, 0xB9U, 0x50U, 0x45U, 0x08U, 0x0BU, 0x89U, 0xA2U, 0x0FU, 0x70U, 0xCCU, 0x60U, 0x1BU,
0xC3U, 0x17U, 0x35U, 0x9FU, 0xAEU, 0x82U, 0x51U, 0x43U, 0x1BU, 0x9DU, 0x53U, 0x9EU, 0xE2U, 0xAFU, 0x20U, 0x1FU,
0xFDU, 0x03U, 0x59U, 0x11U, 0x51U, 0x9EU, 0xACU, 0x83U, 0xCDU, 0x78U, 0xD1U, 0xD0U, 0xE5U, 0xD7U, 0x0EU, 0x41U,
0xDEU, 0xFBU, 0x5CU, 0x7FU, 0x1CU, 0x26U, 0x32U, 0x2CU, 0x51U, 0xF6U, 0xEFU, 0xC6U, 0x34U, 0xC4U, 0xACU, 0x6CU,
0xE8U, 0xF9U, 0x4BU, 0xABU, 0xA3U,
)
val xcha = XChaCha20Poly1305Delegated(key, ubyteArrayOf(), state, header)
val data = UByteArray(100) { 0U }
xcha.encryptPartialData(data)
}
}
@@ -51,6 +51,10 @@ actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, addi
}
}
internal actual constructor(key: UByteArray, additionalData: UByteArray, testState : UByteArray, testHeader: UByteArray) {
}
actual fun encryptPartialData(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
@@ -55,6 +55,10 @@ actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, addi
}
}
internal actual constructor(key: UByteArray, additionalData: UByteArray, testState : UByteArray, testHeader: UByteArray) {
}
actual fun encryptPartialData(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
@@ -1,19 +1,16 @@
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_decrypt
import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.malloc
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, additionalData: UByteArray) {
actual class XChaCha20Poly1305Delegated actual constructor(val key: UByteArray,val additionalData: UByteArray) {
actual companion object {
actual fun encrypt(
key: UByteArray,
@@ -64,8 +61,58 @@ actual class XChaCha20Poly1305Delegated actual constructor(key: UByteArray, addi
}
}
actual internal constructor(key: UByteArray, additionalData: UByteArray, testState : UByteArray, testHeader: UByteArray) : this(key, additionalData) {
val pointer = state.ptr.reinterpret<UByteVar>()
for (i in 0 until crypto_secretstream_xchacha20poly1305_state.size.toInt()) {
pointer[i] = testState[i]
}
println("state after setting-----------")
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).toUByteArray().hexColumsPrint()
println("state after setting-----------")
println("header after setting-----------")
testHeader.copyInto(header)
header.hexColumsPrint()
println("header after setting-----------")
}
var state =
malloc(crypto_secretstream_xchacha20poly1305_state.size.convert())!!
.reinterpret<crypto_secretstream_xchacha20poly1305_state>()
.pointed
val header = UByteArray(crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt()) { 0U }
init {
val pinnedHeader = header.pin()
crypto_secretstream_xchacha20poly1305_init_push(state.ptr, pinnedHeader.addressOf(0), key.toCValues())
println("state-----------")
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).toUByteArray().hexColumsPrint()
println("state-----------")
println("--------header-----------")
header.hexColumsPrint()
println("--------header-----------")
}
actual fun encryptPartialData(data: UByteArray): UByteArray {
TODO("not implemented yet")
val ciphertextWithTag = UByteArray(data.size + crypto_secretstream_xchacha20poly1305_ABYTES.toInt())
val ciphertextWithTagPinned = ciphertextWithTag.pin()
crypto_secretstream_xchacha20poly1305_push(
state.ptr,
ciphertextWithTagPinned.addressOf(0),
null,
data.toCValues(),
data.size.convert(),
null,
0U,
0U,
)
println("Encrypt partial")
ciphertextWithTag.hexColumsPrint()
println("Encrypt partial end")
ciphertextWithTagPinned.unpin()
return ciphertextWithTag
}
actual fun verifyPartialData(data: UByteArray) {