Adding secret stream impl, completed generichash impl

This commit is contained in:
Ugljesa Jovanovic 2020-08-26 22:15:03 +02:00 committed by Ugljesa Jovanovic
parent 6f38a01195
commit 39f0817308
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
9 changed files with 162 additions and 63 deletions

View File

@ -6,14 +6,11 @@ package com.ionspin.kotlin.crypto.generichash
* on 21-Aug-2020 * on 21-Aug-2020
*/ */
data class GenericHashState(val hashLength: Int, val state: GenericHashStateInternal) data class GenericHashState(val hashLength: Int, val internalState: GenericHashStateInternal)
expect class GenericHashStateInternal expect class GenericHashStateInternal
expect object GenericHashing { expect object GenericHash {
fun genericHash(message : UByteArray, requestedHashLength: Int, key : UByteArray? = null) : UByteArray fun genericHash(message : UByteArray, requestedHashLength: Int, key : UByteArray? = null) : UByteArray

View File

@ -0,0 +1,21 @@
package com.ionspin.kotlin.crypto.secretstream
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 26-Aug-2020
*/
expect class SecretStreamState
data class SecretStreamStateAndHeader(val state: SecretStreamState, val header : UByteArray)
data class DecryptedDataAndTag(val decryptedData : UByteArray, val tag : UByte)
expect object SecretStream {
fun xChaCha20Poly1305InitPush(key: UByteArray) : SecretStreamStateAndHeader
fun xChaCha20Poly1305Push(state : SecretStreamState, message: UByteArray, additionalData : UByteArray = ubyteArrayOf(), tag: UByte) : UByteArray
fun xChaCha20Poly1305InitPull(key: UByteArray, header: UByteArray) : SecretStreamStateAndHeader
fun xChaCha20Poly1305Pull(state : SecretStreamState, ciphertext: UByteArray, additionalData : UByteArray = ubyteArrayOf()) : DecryptedDataAndTag
}

View File

@ -1,6 +1,6 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.generichash.GenericHashing import com.ionspin.kotlin.crypto.generichash.GenericHash
import com.ionspin.kotlin.crypto.util.encodeToUByteArray import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking import com.ionspin.kotlin.crypto.util.testBlocking
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
@ -18,7 +18,7 @@ class SmokeTest {
fun testIfLibraryIsNotOnFire() { fun testIfLibraryIsNotOnFire() {
testBlocking { testBlocking {
LibsodiumInitializer.initialize() LibsodiumInitializer.initialize()
val hashResult = GenericHashing.genericHash("Hello".encodeToUByteArray(), 64) val hashResult = GenericHash.genericHash("Hello".encodeToUByteArray(), 64)
println(hashResult.toHexString()) println(hashResult.toHexString())
assertTrue { assertTrue {
"EF15EAF92D5E335345A3E1D977BC7D8797C3D275717CC1B10AF79C93CDA01AEB2A0C59BC02E2BDF9380FD1B54EB9E1669026930CCC24BD49748E65F9A6B2EE68".toLowerCase() == hashResult.toHexString() "EF15EAF92D5E335345A3E1D977BC7D8797C3D275717CC1B10AF79C93CDA01AEB2A0C59BC02E2BDF9380FD1B54EB9E1669026930CCC24BD49748E65F9A6B2EE68".toLowerCase() == hashResult.toHexString()

View File

@ -0,0 +1,47 @@
package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual typealias GenericHashStateInternal = Any
actual object GenericHash {
actual fun genericHash(
message: UByteArray,
requestedHashLength: Int,
key: UByteArray?
): UByteArray {
return getSodium().crypto_generichash(
requestedHashLength,
message.toUInt8Array(),
key?.toUInt8Array() ?: Uint8Array(0)
).toUByteArray()
}
actual fun genericHashInit(
requestedHashLength: Int,
key: UByteArray?
): GenericHashState {
val state = getSodium().crypto_generichash_init(key.toUInt8Array(), requestedHashLength)
return GenericHashState(requestedHashLength, state)
}
actual fun genericHashUpdate(
state: GenericHashState,
messagePart: UByteArray
) {
getSodium().crypto_generichash_update(state.internalState, messagePart.toUInt8Array())
}
actual fun genericHashFinal(state: GenericHashState): UByteArray {
return getSodium().crypto_generichash_final(state.internalState, state.hashLength).toUByteArray()
}
}

View File

@ -1,25 +0,0 @@
package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual object GenericHashing {
actual fun genericHash(
message: UByteArray,
requestedHashLength: Int,
key: UByteArray?
): UByteArray {
return getSodium().crypto_generichash(
requestedHashLength,
message.toUInt8Array(),
key?.toUInt8Array() ?: Uint8Array(0)
).toUByteArray()
}
}

View File

@ -0,0 +1,53 @@
package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual class GenericHashStateInternal(internal val data: ByteArray)
actual object GenericHash {
actual fun genericHash(
message: UByteArray,
requestedHashLength: Int,
key: UByteArray?
): UByteArray {
val hash = UByteArray(requestedHashLength)
sodium.crypto_generichash(
hash.asByteArray(),
requestedHashLength,
message.asByteArray(),
message.size.toLong(),
key?.asByteArray(),
(key?.size ?: 0)
)
return hash
}
actual fun genericHashInit(
requestedHashLength: Int,
key: UByteArray?
): GenericHashState {
val state = GenericHashStateInternal(ByteArray(sodium.crypto_generichash_statebytes()))
sodium.crypto_generichash_init(state.data, key?.asByteArray(), key?.size ?: 0, requestedHashLength)
return GenericHashState(requestedHashLength, state)
}
actual fun genericHashUpdate(
state: GenericHashState,
messagePart: UByteArray
) {
sodium.crypto_generichash_update(state.internalState.data, messagePart.asByteArray(), messagePart.size.toLong())
}
actual fun genericHashFinal(state: GenericHashState): UByteArray {
val hashResult = ByteArray(state.hashLength)
sodium.crypto_generichash_final(state.internalState.data, hashResult, state.hashLength)
return hashResult.asUByteArray()
}
}

View File

@ -1,27 +0,0 @@
package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020
*/
actual object GenericHashing {
actual fun genericHash(
message: UByteArray,
requestedHashLength: Int,
key: UByteArray?
): UByteArray {
val hash = UByteArray(requestedHashLength)
sodium.crypto_generichash(
hash.asByteArray(),
requestedHashLength,
message.asByteArray(),
message.size.toLong(),
key?.asByteArray(),
(key?.size ?: 0)
)
return hash
}
}

View File

@ -9,7 +9,6 @@ import kotlinx.cinterop.reinterpret
import libsodium.crypto_generichash import libsodium.crypto_generichash
import libsodium.crypto_generichash_final import libsodium.crypto_generichash_final
import libsodium.crypto_generichash_init import libsodium.crypto_generichash_init
import libsodium.crypto_generichash_state
import libsodium.crypto_generichash_update import libsodium.crypto_generichash_update
import platform.posix.malloc import platform.posix.malloc
@ -21,7 +20,7 @@ import platform.posix.malloc
actual typealias GenericHashStateInternal = libsodium.crypto_generichash_blake2b_state actual typealias GenericHashStateInternal = libsodium.crypto_generichash_blake2b_state
actual object GenericHashing { actual object GenericHash {
val _emitByte: Byte = 0 val _emitByte: Byte = 0
val _emitByteArray: ByteArray = ByteArray(0) val _emitByteArray: ByteArray = ByteArray(0)
@ -69,7 +68,7 @@ actual object GenericHashing {
) { ) {
val pinnedMessage = messagePart.pin() val pinnedMessage = messagePart.pin()
crypto_generichash_update( crypto_generichash_update(
state.state.ptr, state.internalState.ptr,
pinnedMessage.addressOf(0), pinnedMessage.addressOf(0),
messagePart.size.convert() messagePart.size.convert()
) )
@ -80,7 +79,7 @@ actual object GenericHashing {
val hashResult = UByteArray(state.hashLength) val hashResult = UByteArray(state.hashLength)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
crypto_generichash_final( crypto_generichash_final(
state.state.ptr, state.internalState.ptr,
hashResultPinned.addressOf(0), hashResultPinned.addressOf(0),
state.hashLength.convert() state.hashLength.convert()
) )

View File

@ -0,0 +1,34 @@
package com.ionspin.kotlin.crypto.secretstream
actual typealias SecretStreamState = libsodium.crypto_secretstream_xchacha20poly1305_state
actual object SecretStream {
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305Push(
state: SecretStreamState,
message: UByteArray,
additionalData: UByteArray,
tag: UByte
): UByteArray {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305InitPull(
key: UByteArray,
header: UByteArray
): SecretStreamStateAndHeader {
TODO("not implemented yet")
}
actual fun xChaCha20Poly1305Pull(
state: SecretStreamState,
ciphertext: UByteArray,
additionalData: UByteArray
): DecryptedDataAndTag {
TODO("not implemented yet")
}
}