Comment out old debug generated code, but keep for short term reference, add android/jvm wrappers, seemingly solve android unit test problems

This commit is contained in:
Ugljesa Jovanovic 2020-08-23 13:39:59 +02:00 committed by Ugljesa Jovanovic
parent 9962198aad
commit 231a84af67
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
14 changed files with 549 additions and 567 deletions

View File

@ -46,6 +46,7 @@ val sonatypeUsernameEnv: String? = System.getenv()["SONATYPE_USERNAME"]
repositories { repositories {
mavenCentral() mavenCentral()
jcenter() jcenter()
maven("https://dl.bintray.com/terl/lazysodium-maven")
} }
group = ReleaseInfo.group group = ReleaseInfo.group
@ -61,6 +62,7 @@ android {
targetSdkVersion(29) targetSdkVersion(29)
versionCode = 1 versionCode = 1
versionName = "1.0" versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {
getByName("release") { getByName("release") {
@ -69,6 +71,7 @@ android {
} }
} }
kotlin { kotlin {
val hostOsName = getHostOsName() val hostOsName = getHostOsName()
runningOnLinuxx86_64 { runningOnLinuxx86_64 {
@ -128,26 +131,8 @@ kotlin {
// >>> referenced by randombytes_sysrandom.c // >>> referenced by randombytes_sysrandom.c
// >>> libsodium_la-randombytes_sysrandom.o:(_randombytes_linux_getrandom) in archive /tmp/included11051337748775083797/libsodium.a // >>> libsodium_la-randombytes_sysrandom.o:(_randombytes_linux_getrandom) in archive /tmp/included11051337748775083797/libsodium.a
// linuxArm32Hfp() {
// binaries {
// staticLib {
// }
// }
// compilations.getByName("main") {
// val libsodiumCinterop by cinterops.creating {
// defFile(project.file("src/nativeInterop/cinterop/libsodium.def"))
// compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-arm32/include/")
// }
// kotlinOptions.freeCompilerArgs = listOf(
// "-include-binary", "${project.rootDir}/sodiumWrapper/static-arm32/lib/libsodium.a"
// )
// }
// }
} }
runningOnLinuxArm64 { runningOnLinuxArm64 {
println("Configuring Linux Arm 64 targets") println("Configuring Linux Arm 64 targets")
@ -428,6 +413,7 @@ kotlin {
runningOnLinuxx86_64 { runningOnLinuxx86_64 {
println("Configuring Linux 64 Bit source sets") println("Configuring Linux 64 Bit source sets")
val jvmMain by getting { val jvmMain by getting {
kotlin.srcDirs("src/jvmSpecific", "src/jvmMain/kotlin")
dependencies { dependencies {
implementation(kotlin(Deps.Jvm.stdLib)) implementation(kotlin(Deps.Jvm.stdLib))
implementation(kotlin(Deps.Jvm.test)) implementation(kotlin(Deps.Jvm.test))
@ -446,8 +432,24 @@ kotlin {
} }
} }
val androidMain by getting { val androidMain by getting {
isNotRunningInIdea {
kotlin.srcDirs("src/androidSpecific", "src/jvmMain/kotlin")
}
isRunningInIdea {
kotlin.srcDirs("src/androidSpecific")
}
dependencies { dependencies {
implementation("androidx.core:core-ktx:1.2.0") implementation("com.goterl.lazycode:lazysodium-android:4.2.0@aar")
implementation("net.java.dev.jna:jna:5.5.0@aar")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin(Deps.Jvm.test))
implementation(kotlin(Deps.Jvm.testJUnit))
implementation("androidx.test:runner:1.2.0")
implementation("androidx.test:rules:1.2.0")
} }
} }
@ -544,7 +546,11 @@ kotlin {
} }
tasks.whenTaskAdded {
if("DebugUnitTest" in name || "ReleaseUnitTest" in name) {
enabled = false // https://youtrack.jetbrains.com/issue/KT-34662 otherwise common tests fail, because we require native android libs to be loaded
}
}
tasks { tasks {
@ -588,6 +594,8 @@ tasks {
} }
} }
// val legacyjsNodeTest by getting(KotlinJsTest::class) { // val legacyjsNodeTest by getting(KotlinJsTest::class) {
// //
// testLogging { // testLogging {

View File

@ -1,11 +1,12 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
import com.goterl.lazycode.lazysodium.SodiumAndroid
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
* on 22-Aug-2020 * on 22-Aug-2020
*/ */
class SodiumWrapper : LazySodiumAndroid { typealias SodiumWrapper = SodiumAndroid
}

View File

@ -3,10 +3,10 @@ package com.ionspin.kotlin.crypto
/** /**
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 02/Aug/2020 * Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 02/Aug/2020
*/ */
expect object Initializer { expect object LibsodiumInitializer {
fun isInitialized() : Boolean fun isInitialized() : Boolean
suspend fun initialize() suspend fun initialize()
fun initializeWithCallback(done: () -> (Unit)) fun initializeWithCallback(done: () -> (Unit))
} }

View File

@ -1,79 +1,79 @@
package debug.test //package debug.test
//
import kotlin.Int //import kotlin.Int
import kotlin.UByte //import kotlin.UByte
import kotlin.UByteArray //import kotlin.UByteArray
import kotlin.js.JsName //import kotlin.js.JsName
//
expect class Sha256State //expect class Sha256State
//
expect class Sha512State //expect class Sha512State
//
expect class GenericHashState //expect class GenericHashState
//
expect class SecretStreamState //expect class SecretStreamState
//
data class SecretStreamStateAndHeader( //data class SecretStreamStateAndHeader(
@JsName("state") // @JsName("state")
val state: SecretStreamState, // val state: SecretStreamState,
@JsName("header") // @JsName("header")
val header: UByteArray // val header: UByteArray
) //)
//
data class DecryptedDataAndTag( //data class DecryptedDataAndTag(
@JsName("decrypted") // @JsName("decrypted")
val decrypted: UByteArray, // val decrypted: UByteArray,
@JsName("tag") // @JsName("tag")
val tag: UByte // val tag: UByte
) //)
//
expect class Crypto internal constructor() { //expect class Crypto internal constructor() {
/** // /**
* Initialize the SHA256 hash // * Initialize the SHA256 hash
* returns sha 256 state // * returns sha 256 state
*/ // */
fun crypto_hash_sha256_init(): Sha256State // fun crypto_hash_sha256_init(): Sha256State
//
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) // fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
//
fun crypto_hash_sha256_final(state: Sha256State): UByteArray // fun crypto_hash_sha256_final(state: Sha256State): UByteArray
//
fun crypto_hash_sha512_init(): Sha512State // fun crypto_hash_sha512_init(): Sha512State
//
fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) // fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray)
//
fun crypto_hash_sha512_final(state: Sha512State): UByteArray // fun crypto_hash_sha512_final(state: Sha512State): UByteArray
//
fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState // fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
//
/** // /**
* Initialize a state and generate a random header. Both are returned inside // * Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object. // * `SecretStreamStateAndHeader` object.
*/ // */
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader // fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader
//
/** // /**
* Initialize state from header and key. The state can then be used for decryption. // * Initialize state from header and key. The state can then be used for decryption.
*/ // */
fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray): // fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState // SecretStreamState
//
/** // /**
* Encrypt next block of data using the previously initialized state. Returns encrypted block. // * Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/ // */
fun crypto_secretstream_xchacha20poly1305_push( // fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState, // state: SecretStreamState,
m: UByteArray, // m: UByteArray,
ad: UByteArray, // ad: UByteArray,
tag: UByte // tag: UByte
): UByteArray // ): UByteArray
//
/** // /**
* Decrypt next block of data using the previously initialized state. Returns decrypted block. // * Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/ // */
fun crypto_secretstream_xchacha20poly1305_pull( // fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState, // state: SecretStreamState,
c: UByteArray, // c: UByteArray,
ad: UByteArray // ad: UByteArray
): DecryptedDataAndTag // ): DecryptedDataAndTag
} //}

View File

@ -1,11 +1,9 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.crypto.generichash.GenericHashing import com.ionspin.kotlin.crypto.generichash.GenericHashing
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
import debug.test.Crypto
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -19,19 +17,7 @@ class SmokeTest {
@Test @Test
fun testIfLibraryIsNotOnFire() { fun testIfLibraryIsNotOnFire() {
testBlocking { testBlocking {
Initializer.initialize() LibsodiumInitializer.initialize()
val crypto = Crypto()
// //TODO seems to be a bug in JS compiler, if we have the same method name in crypto an in JsSodiumInterface, method tries to call wrong method name (unneeded suffix _0)
// //I've worked around this by making state functions with 1 parameter execute call with js("") wrap, but still might sail somewhere else
// val state256 = crypto.crypto_hash_sha256_init()
// crypto.crypto_hash_sha256_update(state256, "Hello".encodeToUByteArray())
// val result = crypto.crypto_hash_sha256_final(state256)
// val resultString = result.toHexString()
// println("Result: $resultString")
// assertTrue {
// "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" == resultString
// }
//Blake512 Hello - EF15EAF92D5E335345A3E1D977BC7D8797C3D275717CC1B10AF79C93CDA01AEB2A0C59BC02E2BDF9380FD1B54EB9E1669026930CCC24BD49748E65F9A6B2EE68
val hashResult = GenericHashing.genericHash("Hello".encodeToUByteArray(), 64) val hashResult = GenericHashing.genericHash("Hello".encodeToUByteArray(), 64)
println(hashResult.toHexString()) println(hashResult.toHexString())
assertTrue { assertTrue {
@ -41,4 +27,5 @@ class SmokeTest {
} }
} }
} }

View File

@ -20,7 +20,7 @@ fun setSodiumLoaded(loaded: Boolean) {
js("sodiumLoaded = loaded") js("sodiumLoaded = loaded")
} }
actual object Initializer { actual object LibsodiumInitializer {
private var isPlatformInitialized = false private var isPlatformInitialized = false
actual suspend fun initialize() { actual suspend fun initialize() {
@ -40,4 +40,4 @@ actual object Initializer {
} }
} }

View File

@ -1,113 +1,113 @@
package debug.test //package debug.test
//
import com.ionspin.kotlin.crypto.getSodium //import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray //import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array //import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import kotlin.Any //import kotlin.Any
import kotlin.Int //import kotlin.Int
import kotlin.UByte //import kotlin.UByte
import kotlin.UByteArray //import kotlin.UByteArray
import org.khronos.webgl.Uint8Array //import org.khronos.webgl.Uint8Array
//
actual typealias Sha256State = Any //actual typealias Sha256State = Any
//
actual typealias Sha512State = Any //actual typealias Sha512State = Any
//
actual typealias GenericHashState = Any //actual typealias GenericHashState = Any
//
actual typealias SecretStreamState = Any //actual typealias SecretStreamState = Any
//
actual class Crypto internal actual constructor() { //actual class Crypto internal actual constructor() {
/** // /**
* Initialize the SHA256 hash // * Initialize the SHA256 hash
* returns sha 256 state // * returns sha 256 state
*/ // */
actual fun crypto_hash_sha256_init(): dynamic { // actual fun crypto_hash_sha256_init(): dynamic {
println("Debug crypto_hash_sha256_init") // println("Debug crypto_hash_sha256_init")
val result = js("getSodium().crypto_hash_sha256_init()") // val result = js("getSodium().crypto_hash_sha256_init()")
return result // return result
} // }
//
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { // actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug crypto_hash_sha256_update") // println("Debug crypto_hash_sha256_update")
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array()) // getSodium().crypto_hash_sha256_update(state, input.toUInt8Array())
} // }
//
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray { // actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
println("Debug crypto_hash_sha256_final") // println("Debug crypto_hash_sha256_final")
return getSodium().crypto_hash_sha256_final(state).toUByteArray() // return getSodium().crypto_hash_sha256_final(state).toUByteArray()
} // }
//
actual fun crypto_hash_sha512_init(): dynamic { // actual fun crypto_hash_sha512_init(): dynamic {
println("Debug crypto_hash_sha512_init") // println("Debug crypto_hash_sha512_init")
val result = js("getSodium().crypto_hash_sha512_init()") // val result = js("getSodium().crypto_hash_sha512_init()")
return result // return result
} // }
//
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { // actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug crypto_hash_sha512_update") // println("Debug crypto_hash_sha512_update")
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array()) // getSodium().crypto_hash_sha512_update(state, input.toUInt8Array())
} // }
//
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray { // actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
println("Debug crypto_hash_sha512_final") // println("Debug crypto_hash_sha512_final")
return getSodium().crypto_hash_sha512_final(state).toUByteArray() // return getSodium().crypto_hash_sha512_final(state).toUByteArray()
} // }
//
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic { // actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic {
println("Debug crypto_generichash_init") // println("Debug crypto_generichash_init")
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen) // return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
} // }
//
/** // /**
* Initialize a state and generate a random header. Both are returned inside // * Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object. // * `SecretStreamStateAndHeader` object.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): // actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader { // SecretStreamStateAndHeader {
println("Debug crypto_secretstream_xchacha20poly1305_init_push") // println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val stateAndHeader = // val stateAndHeader =
getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) // getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
val state = stateAndHeader.state // val state = stateAndHeader.state
val header = (stateAndHeader.header as Uint8Array).toUByteArray() // val header = (stateAndHeader.header as Uint8Array).toUByteArray()
return SecretStreamStateAndHeader(state, header) // return SecretStreamStateAndHeader(state, header)
} // }
//
/** // /**
* Initialize state from header and key. The state can then be used for decryption. // * Initialize state from header and key. The state can then be used for decryption.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray): // actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
dynamic { // dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_pull") // println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), // return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(),
key.toUInt8Array()) // key.toUInt8Array())
} // }
//
/** // /**
* Encrypt next block of data using the previously initialized state. Returns encrypted block. // * Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_push( // actual fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState, // state: SecretStreamState,
m: UByteArray, // m: UByteArray,
ad: UByteArray, // ad: UByteArray,
tag: UByte // tag: UByte
): UByteArray { // ): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_push") // println("Debug crypto_secretstream_xchacha20poly1305_push")
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(), // return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(),
ad.toUInt8Array(), tag).toUByteArray() // ad.toUInt8Array(), tag).toUByteArray()
} // }
//
/** // /**
* Decrypt next block of data using the previously initialized state. Returns decrypted block. // * Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_pull( // actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState, // state: SecretStreamState,
c: UByteArray, // c: UByteArray,
ad: UByteArray // ad: UByteArray
): DecryptedDataAndTag { // ): DecryptedDataAndTag {
println("Debug crypto_secretstream_xchacha20poly1305_pull") // println("Debug crypto_secretstream_xchacha20poly1305_pull")
// return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(), //// return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
// ad.toUInt8Array()) //// ad.toUInt8Array())
return DecryptedDataAndTag(ubyteArrayOf(), 0U) // return DecryptedDataAndTag(ubyteArrayOf(), 0U)
} // }
} //}

View File

@ -1,21 +1,19 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.goterl.lazycode.lazysodium.SodiumJava
/** /**
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 02/Aug/2020 * Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 02/Aug/2020
*/ */
actual object Initializer { actual object LibsodiumInitializer {
private var isPlatformInitialized = false private var isPlatformInitialized = false
lateinit var sodium : SodiumJava lateinit var sodium : SodiumWrapper
actual suspend fun initialize() { actual suspend fun initialize() {
sodium = SodiumJava() sodium = SodiumWrapper()
isPlatformInitialized = true isPlatformInitialized = true
} }
actual fun initializeWithCallback(done: () -> Unit) { actual fun initializeWithCallback(done: () -> Unit) {
sodium = SodiumJava() sodium = SodiumWrapper()
isPlatformInitialized = true isPlatformInitialized = true
done() done()
} }
@ -24,4 +22,4 @@ actual object Initializer {
return isPlatformInitialized return isPlatformInitialized
} }
} }

View File

@ -1,6 +1,6 @@
package com.ionspin.kotlin.crypto.generichash package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.Initializer.sodium import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic

View File

@ -1,128 +1,128 @@
package debug.test //package debug.test
//
import com.goterl.lazycode.lazysodium.SodiumJava //import com.goterl.lazycode.lazysodium.SodiumJava
import com.goterl.lazycode.lazysodium.interfaces.Hash //import com.goterl.lazycode.lazysodium.interfaces.Hash
import com.goterl.lazycode.lazysodium.interfaces.SecretStream //import com.goterl.lazycode.lazysodium.interfaces.SecretStream
import kotlin.ByteArray //import kotlin.ByteArray
import kotlin.Int //import kotlin.Int
import kotlin.UByte //import kotlin.UByte
import kotlin.UByteArray //import kotlin.UByteArray
//
val sodium: SodiumJava = SodiumJava() //val sodium: SodiumJava = SodiumJava()
//
actual typealias Sha256State = Hash.State256 //actual typealias Sha256State = Hash.State256
//
actual typealias Sha512State = Hash.State512 //actual typealias Sha512State = Hash.State512
//
actual typealias GenericHashState = ByteArray //actual typealias GenericHashState = ByteArray
//
actual typealias SecretStreamState = SecretStream.State //actual typealias SecretStreamState = SecretStream.State
//
actual class Crypto internal actual constructor() { //actual class Crypto internal actual constructor() {
/** // /**
* Initialize the SHA256 hash // * Initialize the SHA256 hash
* returns sha 256 state // * returns sha 256 state
*/ // */
actual fun crypto_hash_sha256_init(): Sha256State { // actual fun crypto_hash_sha256_init(): Sha256State {
val state = debug.test.Sha256State() // val state = debug.test.Sha256State()
println("Debug crypto_hash_sha256_init") // println("Debug crypto_hash_sha256_init")
sodium.crypto_hash_sha256_init(state) // sodium.crypto_hash_sha256_init(state)
return state // return state
} // }
//
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { // actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug crypto_hash_sha256_update") // println("Debug crypto_hash_sha256_update")
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong()) // sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
} // }
//
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray { // actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
val out = UByteArray(32) // val out = UByteArray(32)
println("Debug crypto_hash_sha256_final") // println("Debug crypto_hash_sha256_final")
sodium.crypto_hash_sha256_final(state, out.asByteArray()) // sodium.crypto_hash_sha256_final(state, out.asByteArray())
return out // return out
} // }
//
actual fun crypto_hash_sha512_init(): Sha512State { // actual fun crypto_hash_sha512_init(): Sha512State {
val state = debug.test.Sha512State() // val state = debug.test.Sha512State()
println("Debug crypto_hash_sha512_init") // println("Debug crypto_hash_sha512_init")
sodium.crypto_hash_sha512_init(state) // sodium.crypto_hash_sha512_init(state)
return state // return state
} // }
//
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { // actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug crypto_hash_sha512_update") // println("Debug crypto_hash_sha512_update")
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong()) // sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
} // }
//
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray { // actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
val out = UByteArray(64) // val out = UByteArray(64)
println("Debug crypto_hash_sha512_final") // println("Debug crypto_hash_sha512_final")
sodium.crypto_hash_sha512_final(state, out.asByteArray()) // sodium.crypto_hash_sha512_final(state, out.asByteArray())
return out // return out
} // }
//
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState { // actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes()) // val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
println("Debug crypto_generichash_init") // println("Debug crypto_generichash_init")
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen) // sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
return state // return state
} // }
//
/** // /**
* Initialize a state and generate a random header. Both are returned inside // * Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object. // * `SecretStreamStateAndHeader` object.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): // actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader { // SecretStreamStateAndHeader {
println("Debug crypto_secretstream_xchacha20poly1305_init_push") // println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val header = UByteArray(24) // val header = UByteArray(24)
val state = SecretStream.State() // val state = SecretStream.State()
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(), // sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(),
key.asByteArray()) // key.asByteArray())
return SecretStreamStateAndHeader(state, header) // return SecretStreamStateAndHeader(state, header)
} // }
//
/** // /**
* Initialize state from header and key. The state can then be used for decryption. // * Initialize state from header and key. The state can then be used for decryption.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray): // actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState { // SecretStreamState {
val state = debug.test.SecretStreamState() // val state = debug.test.SecretStreamState()
println("Debug crypto_secretstream_xchacha20poly1305_init_pull") // println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(), // sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(),
key.asByteArray()) // key.asByteArray())
return state // return state
} // }
//
/** // /**
* Encrypt next block of data using the previously initialized state. Returns encrypted block. // * Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_push( // actual fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState, // state: SecretStreamState,
m: UByteArray, // m: UByteArray,
ad: UByteArray, // ad: UByteArray,
tag: UByte // tag: UByte
): UByteArray { // ): UByteArray {
val c = UByteArray(m.size + 17) // val c = UByteArray(m.size + 17)
println("Debug crypto_secretstream_xchacha20poly1305_push") // println("Debug crypto_secretstream_xchacha20poly1305_push")
sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), null, m.asByteArray(), // sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), null, m.asByteArray(),
m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag.toByte()) // m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag.toByte())
return c // return c
} // }
//
/** // /**
* Decrypt next block of data using the previously initialized state. Returns decrypted block. // * Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_pull( // actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState, // state: SecretStreamState,
c: UByteArray, // c: UByteArray,
ad: UByteArray // ad: UByteArray
): DecryptedDataAndTag { // ): DecryptedDataAndTag {
val m = UByteArray(c.size - 17) // val m = UByteArray(c.size - 17)
var tag_p : UByte = 0U // var tag_p : UByte = 0U
println("Debug crypto_secretstream_xchacha20poly1305_pull") // println("Debug crypto_secretstream_xchacha20poly1305_pull")
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m.asByteArray(), null, byteArrayOf(), // sodium.crypto_secretstream_xchacha20poly1305_pull(state, m.asByteArray(), null, byteArrayOf(),
c.asByteArray(), c.size.toLong(), ad.asByteArray(), ad.size.toLong()) // c.asByteArray(), c.size.toLong(), ad.asByteArray(), ad.size.toLong())
return debug.test.DecryptedDataAndTag(m, tag_p) // return debug.test.DecryptedDataAndTag(m, tag_p)
} // }
} //}

View File

@ -8,6 +8,4 @@ import com.goterl.lazycode.lazysodium.SodiumJava
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
* on 22-Aug-2020 * on 22-Aug-2020
*/ */
class ASodiumWrapper : SodiumJava() { typealias SodiumWrapper = SodiumJava
}

View File

@ -5,7 +5,7 @@ package com.ionspin.kotlin.crypto
import libsodium.sodium_init import libsodium.sodium_init
import kotlin.native.concurrent.AtomicInt import kotlin.native.concurrent.AtomicInt
actual object Initializer { actual object LibsodiumInitializer {
private var isPlatformInitialized : AtomicInt = AtomicInt(0) private var isPlatformInitialized : AtomicInt = AtomicInt(0)

View File

@ -1,29 +1,19 @@
package com.ionspin.kotlin.crypto.generichash package com.ionspin.kotlin.crypto.generichash
import kotlin.Byte
import kotlin.ByteArray
import kotlin.Int
import kotlin.UByte
import kotlin.UByteArray
import kotlinx.cinterop.addressOf import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
import kotlinx.cinterop.pointed
import kotlinx.cinterop.ptr
import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.toCValues
import libsodium.crypto_generichash import libsodium.crypto_generichash
import libsodium.crypto_generichash_blake2b_state
import libsodium.crypto_hash_sha256_state
import libsodium.crypto_hash_sha512_state
import libsodium.crypto_secretstream_xchacha20poly1305_state
import libsodium.sodium_malloc
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
* on 21-Aug-2020 * on 21-Aug-2020
*/ */
actual object GenericHashing { actual object GenericHashing {
val _emitByte: Byte = 0
val _emitByteArray: ByteArray = ByteArray(0)
actual fun genericHash(message: UByteArray, requestedHashLength: Int, key: UByteArray?) : UByteArray { actual fun genericHash(message: UByteArray, requestedHashLength: Int, key: UByteArray?) : UByteArray {
val hash = UByteArray(requestedHashLength) val hash = UByteArray(requestedHashLength)
val pinnedHash = hash.pin() val pinnedHash = hash.pin()

View File

@ -1,179 +1,179 @@
package debug.test //package debug.test
//
import kotlin.Byte //import kotlin.Byte
import kotlin.ByteArray //import kotlin.ByteArray
import kotlin.Int //import kotlin.Int
import kotlin.UByte //import kotlin.UByte
import kotlin.UByteArray //import kotlin.UByteArray
import kotlinx.cinterop.addressOf //import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert //import kotlinx.cinterop.convert
import kotlinx.cinterop.pin //import kotlinx.cinterop.pin
import kotlinx.cinterop.pointed //import kotlinx.cinterop.pointed
import kotlinx.cinterop.ptr //import kotlinx.cinterop.ptr
import kotlinx.cinterop.reinterpret //import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.toCValues //import kotlinx.cinterop.toCValues
import libsodium.crypto_generichash_blake2b_state //import libsodium.crypto_generichash_blake2b_state
import libsodium.crypto_hash_sha256_state //import libsodium.crypto_hash_sha256_state
import libsodium.crypto_hash_sha512_state //import libsodium.crypto_hash_sha512_state
import libsodium.crypto_secretstream_xchacha20poly1305_state //import libsodium.crypto_secretstream_xchacha20poly1305_state
import libsodium.sodium_malloc //import libsodium.sodium_malloc
//
actual typealias Sha256State = crypto_hash_sha256_state //actual typealias Sha256State = crypto_hash_sha256_state
//
actual typealias Sha512State = crypto_hash_sha512_state //actual typealias Sha512State = crypto_hash_sha512_state
//
actual typealias GenericHashState = crypto_generichash_blake2b_state //actual typealias GenericHashState = crypto_generichash_blake2b_state
//
actual typealias SecretStreamState = crypto_secretstream_xchacha20poly1305_state //actual typealias SecretStreamState = crypto_secretstream_xchacha20poly1305_state
//
actual class Crypto internal actual constructor() { //actual class Crypto internal actual constructor() {
val _emitByte: Byte = 0 // val _emitByte: Byte = 0
//
val _emitByteArray: ByteArray = ByteArray(0) // val _emitByteArray: ByteArray = ByteArray(0)
//
/** // /**
* Initialize the SHA256 hash // * Initialize the SHA256 hash
* returns sha 256 state // * returns sha 256 state
*/ // */
actual fun crypto_hash_sha256_init(): Sha256State { // actual fun crypto_hash_sha256_init(): Sha256State {
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!! // val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha256State>().pointed // val state = allocated.reinterpret<debug.test.Sha256State>().pointed
println("Debug crypto_hash_sha256_init") // println("Debug crypto_hash_sha256_init")
libsodium.crypto_hash_sha256_init(state.ptr) // libsodium.crypto_hash_sha256_init(state.ptr)
return state // return state
} // }
//
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { // actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug crypto_hash_sha256_update") // println("Debug crypto_hash_sha256_update")
val pinnedInput = input.pin() // val pinnedInput = input.pin()
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert()) // libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
pinnedInput.unpin() // pinnedInput.unpin()
} // }
//
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray { // actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
val out = UByteArray(32) // val out = UByteArray(32)
println("Debug crypto_hash_sha256_final") // println("Debug crypto_hash_sha256_final")
val pinnedOut = out.pin() // val pinnedOut = out.pin()
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0)) // libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
pinnedOut.unpin() // pinnedOut.unpin()
return out // return out
} // }
//
actual fun crypto_hash_sha512_init(): Sha512State { // actual fun crypto_hash_sha512_init(): Sha512State {
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!! // val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha512State>().pointed // val state = allocated.reinterpret<debug.test.Sha512State>().pointed
println("Debug crypto_hash_sha512_init") // println("Debug crypto_hash_sha512_init")
libsodium.crypto_hash_sha512_init(state.ptr) // libsodium.crypto_hash_sha512_init(state.ptr)
return state // return state
} // }
//
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { // actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug crypto_hash_sha512_update") // println("Debug crypto_hash_sha512_update")
val pinnedInput = input.pin() // val pinnedInput = input.pin()
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert()) // libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
pinnedInput.unpin() // pinnedInput.unpin()
} // }
//
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray { // actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
val out = UByteArray(64) // val out = UByteArray(64)
println("Debug crypto_hash_sha512_final") // println("Debug crypto_hash_sha512_final")
val pinnedOut = out.pin() // val pinnedOut = out.pin()
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0)) // libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
pinnedOut.unpin() // pinnedOut.unpin()
return out // return out
} // }
//
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState { // actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!! // val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed // val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
println("Debug crypto_generichash_init") // println("Debug crypto_generichash_init")
val pinnedKey = key.pin() // val pinnedKey = key.pin()
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(), // libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
outlen.convert()) // outlen.convert())
pinnedKey.unpin() // pinnedKey.unpin()
return state // return state
} // }
//
/** // /**
* Initialize a state and generate a random header. Both are returned inside // * Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object. // * `SecretStreamStateAndHeader` object.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): // actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader { // SecretStreamStateAndHeader {
println("Debug crypto_secretstream_xchacha20poly1305_init_push") // println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val pinnedKey = key.pin() // val pinnedKey = key.pin()
val state = // val state =
sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!! // sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!!
.reinterpret<libsodium.crypto_secretstream_xchacha20poly1305_state>() // .reinterpret<libsodium.crypto_secretstream_xchacha20poly1305_state>()
.pointed // .pointed
val header = UByteArray(libsodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt()) // val header = UByteArray(libsodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt())
{ 0U } // { 0U }
val pinnedHeader = header.pin() // val pinnedHeader = header.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_push(state.ptr, // libsodium.crypto_secretstream_xchacha20poly1305_init_push(state.ptr,
pinnedHeader.addressOf(0), pinnedKey.addressOf(0)) // pinnedHeader.addressOf(0), pinnedKey.addressOf(0))
pinnedHeader.unpin() // pinnedHeader.unpin()
pinnedKey.unpin() // pinnedKey.unpin()
return SecretStreamStateAndHeader(state, header) // return SecretStreamStateAndHeader(state, header)
} // }
//
/** // /**
* Initialize state from header and key. The state can then be used for decryption. // * Initialize state from header and key. The state can then be used for decryption.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray): // actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState { // SecretStreamState {
val allocated = sodium_malloc(debug.test.SecretStreamState.size.convert())!! // val allocated = sodium_malloc(debug.test.SecretStreamState.size.convert())!!
val state = allocated.reinterpret<debug.test.SecretStreamState>().pointed // val state = allocated.reinterpret<debug.test.SecretStreamState>().pointed
println("Debug crypto_secretstream_xchacha20poly1305_init_pull") // println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
val pinnedHeader = header.pin() // val pinnedHeader = header.pin()
val pinnedKey = key.pin() // val pinnedKey = key.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_pull(state.ptr, pinnedHeader.addressOf(0), // libsodium.crypto_secretstream_xchacha20poly1305_init_pull(state.ptr, pinnedHeader.addressOf(0),
pinnedKey.addressOf(0)) // pinnedKey.addressOf(0))
pinnedHeader.unpin() // pinnedHeader.unpin()
pinnedKey.unpin() // pinnedKey.unpin()
return state // return state
} // }
//
/** // /**
* Encrypt next block of data using the previously initialized state. Returns encrypted block. // * Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_push( // actual fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState, // state: SecretStreamState,
m: UByteArray, // m: UByteArray,
ad: UByteArray, // ad: UByteArray,
tag: UByte // tag: UByte
): UByteArray { // ): UByteArray {
val c = UByteArray(m.size + 17) // val c = UByteArray(m.size + 17)
println("Debug crypto_secretstream_xchacha20poly1305_push") // println("Debug crypto_secretstream_xchacha20poly1305_push")
val pinnedC = c.pin() // val pinnedC = c.pin()
val pinnedM = m.pin() // val pinnedM = m.pin()
val pinnedAd = ad.pin() // val pinnedAd = ad.pin()
libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0), null, // libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0), null,
pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0), ad.size.convert(), tag) // pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0), ad.size.convert(), tag)
pinnedC.unpin() // pinnedC.unpin()
pinnedM.unpin() // pinnedM.unpin()
pinnedAd.unpin() // pinnedAd.unpin()
return c // return c
} // }
//
/** // /**
* Decrypt next block of data using the previously initialized state. Returns decrypted block. // * Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/ // */
actual fun crypto_secretstream_xchacha20poly1305_pull( // actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState, // state: SecretStreamState,
c: UByteArray, // c: UByteArray,
ad: UByteArray // ad: UByteArray
): DecryptedDataAndTag { // ): DecryptedDataAndTag {
val m = UByteArray(c.size - 17) // val m = UByteArray(c.size - 17)
var tag_p : UByte = 0U // var tag_p : UByte = 0U
println("Debug crypto_secretstream_xchacha20poly1305_pull") // println("Debug crypto_secretstream_xchacha20poly1305_pull")
val pinnedM = m.pin() // val pinnedM = m.pin()
val pinnedC = c.pin() // val pinnedC = c.pin()
val pinnedAd = ad.pin() // val pinnedAd = ad.pin()
libsodium.crypto_secretstream_xchacha20poly1305_pull(state.ptr, pinnedM.addressOf(0), null, // libsodium.crypto_secretstream_xchacha20poly1305_pull(state.ptr, pinnedM.addressOf(0), null,
ubyteArrayOf().toCValues(), pinnedC.addressOf(0), c.size.convert(), pinnedAd.addressOf(0), ad.size.convert()) // ubyteArrayOf().toCValues(), pinnedC.addressOf(0), c.size.convert(), pinnedAd.addressOf(0), ad.size.convert())
pinnedM.unpin() // pinnedM.unpin()
pinnedC.unpin() // pinnedC.unpin()
pinnedAd.unpin() // pinnedAd.unpin()
return debug.test.DecryptedDataAndTag(m, tag_p) // return debug.test.DecryptedDataAndTag(m, tag_p)
} // }
} //}