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:
parent
9962198aad
commit
231a84af67
@ -46,6 +46,7 @@ val sonatypeUsernameEnv: String? = System.getenv()["SONATYPE_USERNAME"]
|
||||
repositories {
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
maven("https://dl.bintray.com/terl/lazysodium-maven")
|
||||
|
||||
}
|
||||
group = ReleaseInfo.group
|
||||
@ -61,6 +62,7 @@ android {
|
||||
targetSdkVersion(29)
|
||||
versionCode = 1
|
||||
versionName = "1.0"
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
buildTypes {
|
||||
getByName("release") {
|
||||
@ -69,6 +71,7 @@ android {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
kotlin {
|
||||
val hostOsName = getHostOsName()
|
||||
runningOnLinuxx86_64 {
|
||||
@ -128,26 +131,8 @@ kotlin {
|
||||
// >>> referenced by randombytes_sysrandom.c
|
||||
// >>> 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 {
|
||||
println("Configuring Linux Arm 64 targets")
|
||||
|
||||
@ -428,6 +413,7 @@ kotlin {
|
||||
runningOnLinuxx86_64 {
|
||||
println("Configuring Linux 64 Bit source sets")
|
||||
val jvmMain by getting {
|
||||
kotlin.srcDirs("src/jvmSpecific", "src/jvmMain/kotlin")
|
||||
dependencies {
|
||||
implementation(kotlin(Deps.Jvm.stdLib))
|
||||
implementation(kotlin(Deps.Jvm.test))
|
||||
@ -446,8 +432,24 @@ kotlin {
|
||||
}
|
||||
}
|
||||
val androidMain by getting {
|
||||
isNotRunningInIdea {
|
||||
kotlin.srcDirs("src/androidSpecific", "src/jvmMain/kotlin")
|
||||
}
|
||||
isRunningInIdea {
|
||||
kotlin.srcDirs("src/androidSpecific")
|
||||
}
|
||||
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 {
|
||||
|
||||
@ -588,6 +594,8 @@ tasks {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// val legacyjsNodeTest by getting(KotlinJsTest::class) {
|
||||
//
|
||||
// testLogging {
|
||||
|
@ -1,11 +1,12 @@
|
||||
package com.ionspin.kotlin.crypto
|
||||
|
||||
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
|
||||
import com.goterl.lazycode.lazysodium.SodiumAndroid
|
||||
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 22-Aug-2020
|
||||
*/
|
||||
class SodiumWrapper : LazySodiumAndroid {
|
||||
|
||||
}
|
||||
typealias SodiumWrapper = SodiumAndroid
|
||||
|
@ -3,10 +3,10 @@ package com.ionspin.kotlin.crypto
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 02/Aug/2020
|
||||
*/
|
||||
expect object Initializer {
|
||||
expect object LibsodiumInitializer {
|
||||
fun isInitialized() : Boolean
|
||||
|
||||
suspend fun initialize()
|
||||
|
||||
fun initializeWithCallback(done: () -> (Unit))
|
||||
}
|
||||
}
|
||||
|
@ -1,79 +1,79 @@
|
||||
package debug.test
|
||||
|
||||
import kotlin.Int
|
||||
import kotlin.UByte
|
||||
import kotlin.UByteArray
|
||||
import kotlin.js.JsName
|
||||
|
||||
expect class Sha256State
|
||||
|
||||
expect class Sha512State
|
||||
|
||||
expect class GenericHashState
|
||||
|
||||
expect class SecretStreamState
|
||||
|
||||
data class SecretStreamStateAndHeader(
|
||||
@JsName("state")
|
||||
val state: SecretStreamState,
|
||||
@JsName("header")
|
||||
val header: UByteArray
|
||||
)
|
||||
|
||||
data class DecryptedDataAndTag(
|
||||
@JsName("decrypted")
|
||||
val decrypted: UByteArray,
|
||||
@JsName("tag")
|
||||
val tag: UByte
|
||||
)
|
||||
|
||||
expect class Crypto internal constructor() {
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
fun crypto_hash_sha256_init(): Sha256State
|
||||
|
||||
fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
||||
|
||||
fun crypto_hash_sha256_final(state: Sha256State): UByteArray
|
||||
|
||||
fun crypto_hash_sha512_init(): Sha512State
|
||||
|
||||
fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray)
|
||||
|
||||
fun crypto_hash_sha512_final(state: Sha512State): UByteArray
|
||||
|
||||
fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
|
||||
|
||||
/**
|
||||
* Initialize a state and generate a random header. Both are returned inside
|
||||
* `SecretStreamStateAndHeader` object.
|
||||
*/
|
||||
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader
|
||||
|
||||
/**
|
||||
* Initialize state from header and key. The state can then be used for decryption.
|
||||
*/
|
||||
fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
|
||||
SecretStreamState
|
||||
|
||||
/**
|
||||
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
*/
|
||||
fun crypto_secretstream_xchacha20poly1305_push(
|
||||
state: SecretStreamState,
|
||||
m: UByteArray,
|
||||
ad: UByteArray,
|
||||
tag: UByte
|
||||
): UByteArray
|
||||
|
||||
/**
|
||||
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
*/
|
||||
fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
state: SecretStreamState,
|
||||
c: UByteArray,
|
||||
ad: UByteArray
|
||||
): DecryptedDataAndTag
|
||||
}
|
||||
//package debug.test
|
||||
//
|
||||
//import kotlin.Int
|
||||
//import kotlin.UByte
|
||||
//import kotlin.UByteArray
|
||||
//import kotlin.js.JsName
|
||||
//
|
||||
//expect class Sha256State
|
||||
//
|
||||
//expect class Sha512State
|
||||
//
|
||||
//expect class GenericHashState
|
||||
//
|
||||
//expect class SecretStreamState
|
||||
//
|
||||
//data class SecretStreamStateAndHeader(
|
||||
// @JsName("state")
|
||||
// val state: SecretStreamState,
|
||||
// @JsName("header")
|
||||
// val header: UByteArray
|
||||
//)
|
||||
//
|
||||
//data class DecryptedDataAndTag(
|
||||
// @JsName("decrypted")
|
||||
// val decrypted: UByteArray,
|
||||
// @JsName("tag")
|
||||
// val tag: UByte
|
||||
//)
|
||||
//
|
||||
//expect class Crypto internal constructor() {
|
||||
// /**
|
||||
// * Initialize the SHA256 hash
|
||||
// * returns sha 256 state
|
||||
// */
|
||||
// fun crypto_hash_sha256_init(): Sha256State
|
||||
//
|
||||
// fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray)
|
||||
//
|
||||
// fun crypto_hash_sha256_final(state: Sha256State): UByteArray
|
||||
//
|
||||
// fun crypto_hash_sha512_init(): Sha512State
|
||||
//
|
||||
// fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray)
|
||||
//
|
||||
// fun crypto_hash_sha512_final(state: Sha512State): UByteArray
|
||||
//
|
||||
// fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
|
||||
//
|
||||
// /**
|
||||
// * Initialize a state and generate a random header. Both are returned inside
|
||||
// * `SecretStreamStateAndHeader` object.
|
||||
// */
|
||||
// fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader
|
||||
//
|
||||
// /**
|
||||
// * Initialize state from header and key. The state can then be used for decryption.
|
||||
// */
|
||||
// fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
|
||||
// SecretStreamState
|
||||
//
|
||||
// /**
|
||||
// * Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
// */
|
||||
// fun crypto_secretstream_xchacha20poly1305_push(
|
||||
// state: SecretStreamState,
|
||||
// m: UByteArray,
|
||||
// ad: UByteArray,
|
||||
// tag: UByte
|
||||
// ): UByteArray
|
||||
//
|
||||
// /**
|
||||
// * Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
// */
|
||||
// fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
// state: SecretStreamState,
|
||||
// c: UByteArray,
|
||||
// ad: UByteArray
|
||||
// ): DecryptedDataAndTag
|
||||
//}
|
||||
|
@ -1,11 +1,9 @@
|
||||
package com.ionspin.kotlin.crypto
|
||||
|
||||
import com.ionspin.kotlin.bignum.integer.BigInteger
|
||||
import com.ionspin.kotlin.crypto.generichash.GenericHashing
|
||||
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import debug.test.Crypto
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
@ -19,19 +17,7 @@ class SmokeTest {
|
||||
@Test
|
||||
fun testIfLibraryIsNotOnFire() {
|
||||
testBlocking {
|
||||
Initializer.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
|
||||
LibsodiumInitializer.initialize()
|
||||
val hashResult = GenericHashing.genericHash("Hello".encodeToUByteArray(), 64)
|
||||
println(hashResult.toHexString())
|
||||
assertTrue {
|
||||
@ -41,4 +27,5 @@ class SmokeTest {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ fun setSodiumLoaded(loaded: Boolean) {
|
||||
js("sodiumLoaded = loaded")
|
||||
}
|
||||
|
||||
actual object Initializer {
|
||||
actual object LibsodiumInitializer {
|
||||
private var isPlatformInitialized = false
|
||||
|
||||
actual suspend fun initialize() {
|
||||
@ -40,4 +40,4 @@ actual object Initializer {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,113 +1,113 @@
|
||||
package debug.test
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||
import kotlin.Any
|
||||
import kotlin.Int
|
||||
import kotlin.UByte
|
||||
import kotlin.UByteArray
|
||||
import org.khronos.webgl.Uint8Array
|
||||
|
||||
actual typealias Sha256State = Any
|
||||
|
||||
actual typealias Sha512State = Any
|
||||
|
||||
actual typealias GenericHashState = Any
|
||||
|
||||
actual typealias SecretStreamState = Any
|
||||
|
||||
actual class Crypto internal actual constructor() {
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
actual fun crypto_hash_sha256_init(): dynamic {
|
||||
println("Debug crypto_hash_sha256_init")
|
||||
val result = js("getSodium().crypto_hash_sha256_init()")
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||
println("Debug crypto_hash_sha256_update")
|
||||
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array())
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||
println("Debug crypto_hash_sha256_final")
|
||||
return getSodium().crypto_hash_sha256_final(state).toUByteArray()
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_init(): dynamic {
|
||||
println("Debug crypto_hash_sha512_init")
|
||||
val result = js("getSodium().crypto_hash_sha512_init()")
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||
println("Debug crypto_hash_sha512_update")
|
||||
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array())
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||
println("Debug crypto_hash_sha512_final")
|
||||
return getSodium().crypto_hash_sha512_final(state).toUByteArray()
|
||||
}
|
||||
|
||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic {
|
||||
println("Debug crypto_generichash_init")
|
||||
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a state and generate a random header. Both are returned inside
|
||||
* `SecretStreamStateAndHeader` object.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
|
||||
SecretStreamStateAndHeader {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||
val stateAndHeader =
|
||||
getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
|
||||
val state = stateAndHeader.state
|
||||
val header = (stateAndHeader.header as Uint8Array).toUByteArray()
|
||||
return SecretStreamStateAndHeader(state, header)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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):
|
||||
dynamic {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
|
||||
return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(),
|
||||
key.toUInt8Array())
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_push(
|
||||
state: SecretStreamState,
|
||||
m: UByteArray,
|
||||
ad: UByteArray,
|
||||
tag: UByte
|
||||
): UByteArray {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_push")
|
||||
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(),
|
||||
ad.toUInt8Array(), tag).toUByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
state: SecretStreamState,
|
||||
c: UByteArray,
|
||||
ad: UByteArray
|
||||
): DecryptedDataAndTag {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
// return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
|
||||
// ad.toUInt8Array())
|
||||
return DecryptedDataAndTag(ubyteArrayOf(), 0U)
|
||||
}
|
||||
}
|
||||
//package debug.test
|
||||
//
|
||||
//import com.ionspin.kotlin.crypto.getSodium
|
||||
//import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
||||
//import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||
//import kotlin.Any
|
||||
//import kotlin.Int
|
||||
//import kotlin.UByte
|
||||
//import kotlin.UByteArray
|
||||
//import org.khronos.webgl.Uint8Array
|
||||
//
|
||||
//actual typealias Sha256State = Any
|
||||
//
|
||||
//actual typealias Sha512State = Any
|
||||
//
|
||||
//actual typealias GenericHashState = Any
|
||||
//
|
||||
//actual typealias SecretStreamState = Any
|
||||
//
|
||||
//actual class Crypto internal actual constructor() {
|
||||
// /**
|
||||
// * Initialize the SHA256 hash
|
||||
// * returns sha 256 state
|
||||
// */
|
||||
// actual fun crypto_hash_sha256_init(): dynamic {
|
||||
// println("Debug crypto_hash_sha256_init")
|
||||
// val result = js("getSodium().crypto_hash_sha256_init()")
|
||||
// return result
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||
// println("Debug crypto_hash_sha256_update")
|
||||
// getSodium().crypto_hash_sha256_update(state, input.toUInt8Array())
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||
// println("Debug crypto_hash_sha256_final")
|
||||
// return getSodium().crypto_hash_sha256_final(state).toUByteArray()
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_init(): dynamic {
|
||||
// println("Debug crypto_hash_sha512_init")
|
||||
// val result = js("getSodium().crypto_hash_sha512_init()")
|
||||
// return result
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||
// println("Debug crypto_hash_sha512_update")
|
||||
// getSodium().crypto_hash_sha512_update(state, input.toUInt8Array())
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||
// println("Debug crypto_hash_sha512_final")
|
||||
// return getSodium().crypto_hash_sha512_final(state).toUByteArray()
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic {
|
||||
// println("Debug crypto_generichash_init")
|
||||
// return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Initialize a state and generate a random header. Both are returned inside
|
||||
// * `SecretStreamStateAndHeader` object.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
|
||||
// SecretStreamStateAndHeader {
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||
// val stateAndHeader =
|
||||
// getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
|
||||
// val state = stateAndHeader.state
|
||||
// val header = (stateAndHeader.header as Uint8Array).toUByteArray()
|
||||
// return SecretStreamStateAndHeader(state, header)
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 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):
|
||||
// dynamic {
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
|
||||
// return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(),
|
||||
// key.toUInt8Array())
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_push(
|
||||
// state: SecretStreamState,
|
||||
// m: UByteArray,
|
||||
// ad: UByteArray,
|
||||
// tag: UByte
|
||||
// ): UByteArray {
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_push")
|
||||
// return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(),
|
||||
// ad.toUInt8Array(), tag).toUByteArray()
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
// state: SecretStreamState,
|
||||
// c: UByteArray,
|
||||
// ad: UByteArray
|
||||
// ): DecryptedDataAndTag {
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
//// return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
|
||||
//// ad.toUInt8Array())
|
||||
// return DecryptedDataAndTag(ubyteArrayOf(), 0U)
|
||||
// }
|
||||
//}
|
||||
|
@ -1,21 +1,19 @@
|
||||
package com.ionspin.kotlin.crypto
|
||||
|
||||
import com.goterl.lazycode.lazysodium.SodiumJava
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 02/Aug/2020
|
||||
*/
|
||||
actual object Initializer {
|
||||
actual object LibsodiumInitializer {
|
||||
private var isPlatformInitialized = false
|
||||
|
||||
lateinit var sodium : SodiumJava
|
||||
lateinit var sodium : SodiumWrapper
|
||||
actual suspend fun initialize() {
|
||||
sodium = SodiumJava()
|
||||
sodium = SodiumWrapper()
|
||||
isPlatformInitialized = true
|
||||
}
|
||||
|
||||
actual fun initializeWithCallback(done: () -> Unit) {
|
||||
sodium = SodiumJava()
|
||||
sodium = SodiumWrapper()
|
||||
isPlatformInitialized = true
|
||||
done()
|
||||
}
|
||||
@ -24,4 +22,4 @@ actual object Initializer {
|
||||
return isPlatformInitialized
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.generichash
|
||||
|
||||
import com.ionspin.kotlin.crypto.Initializer.sodium
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
|
@ -1,128 +1,128 @@
|
||||
package debug.test
|
||||
|
||||
import com.goterl.lazycode.lazysodium.SodiumJava
|
||||
import com.goterl.lazycode.lazysodium.interfaces.Hash
|
||||
import com.goterl.lazycode.lazysodium.interfaces.SecretStream
|
||||
import kotlin.ByteArray
|
||||
import kotlin.Int
|
||||
import kotlin.UByte
|
||||
import kotlin.UByteArray
|
||||
|
||||
val sodium: SodiumJava = SodiumJava()
|
||||
|
||||
actual typealias Sha256State = Hash.State256
|
||||
|
||||
actual typealias Sha512State = Hash.State512
|
||||
|
||||
actual typealias GenericHashState = ByteArray
|
||||
|
||||
actual typealias SecretStreamState = SecretStream.State
|
||||
|
||||
actual class Crypto internal actual constructor() {
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
||||
val state = debug.test.Sha256State()
|
||||
println("Debug crypto_hash_sha256_init")
|
||||
sodium.crypto_hash_sha256_init(state)
|
||||
return state
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||
println("Debug crypto_hash_sha256_update")
|
||||
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||
val out = UByteArray(32)
|
||||
println("Debug crypto_hash_sha256_final")
|
||||
sodium.crypto_hash_sha256_final(state, out.asByteArray())
|
||||
return out
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_init(): Sha512State {
|
||||
val state = debug.test.Sha512State()
|
||||
println("Debug crypto_hash_sha512_init")
|
||||
sodium.crypto_hash_sha512_init(state)
|
||||
return state
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||
println("Debug crypto_hash_sha512_update")
|
||||
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||
val out = UByteArray(64)
|
||||
println("Debug crypto_hash_sha512_final")
|
||||
sodium.crypto_hash_sha512_final(state, out.asByteArray())
|
||||
return out
|
||||
}
|
||||
|
||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
||||
val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
|
||||
println("Debug crypto_generichash_init")
|
||||
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a state and generate a random header. Both are returned inside
|
||||
* `SecretStreamStateAndHeader` object.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
|
||||
SecretStreamStateAndHeader {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||
val header = UByteArray(24)
|
||||
val state = SecretStream.State()
|
||||
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(),
|
||||
key.asByteArray())
|
||||
return SecretStreamStateAndHeader(state, header)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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):
|
||||
SecretStreamState {
|
||||
val state = debug.test.SecretStreamState()
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
|
||||
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(),
|
||||
key.asByteArray())
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_push(
|
||||
state: SecretStreamState,
|
||||
m: UByteArray,
|
||||
ad: UByteArray,
|
||||
tag: UByte
|
||||
): UByteArray {
|
||||
val c = UByteArray(m.size + 17)
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_push")
|
||||
sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), null, m.asByteArray(),
|
||||
m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag.toByte())
|
||||
return c
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
state: SecretStreamState,
|
||||
c: UByteArray,
|
||||
ad: UByteArray
|
||||
): DecryptedDataAndTag {
|
||||
val m = UByteArray(c.size - 17)
|
||||
var tag_p : UByte = 0U
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m.asByteArray(), null, byteArrayOf(),
|
||||
c.asByteArray(), c.size.toLong(), ad.asByteArray(), ad.size.toLong())
|
||||
return debug.test.DecryptedDataAndTag(m, tag_p)
|
||||
}
|
||||
}
|
||||
//package debug.test
|
||||
//
|
||||
//import com.goterl.lazycode.lazysodium.SodiumJava
|
||||
//import com.goterl.lazycode.lazysodium.interfaces.Hash
|
||||
//import com.goterl.lazycode.lazysodium.interfaces.SecretStream
|
||||
//import kotlin.ByteArray
|
||||
//import kotlin.Int
|
||||
//import kotlin.UByte
|
||||
//import kotlin.UByteArray
|
||||
//
|
||||
//val sodium: SodiumJava = SodiumJava()
|
||||
//
|
||||
//actual typealias Sha256State = Hash.State256
|
||||
//
|
||||
//actual typealias Sha512State = Hash.State512
|
||||
//
|
||||
//actual typealias GenericHashState = ByteArray
|
||||
//
|
||||
//actual typealias SecretStreamState = SecretStream.State
|
||||
//
|
||||
//actual class Crypto internal actual constructor() {
|
||||
// /**
|
||||
// * Initialize the SHA256 hash
|
||||
// * returns sha 256 state
|
||||
// */
|
||||
// actual fun crypto_hash_sha256_init(): Sha256State {
|
||||
// val state = debug.test.Sha256State()
|
||||
// println("Debug crypto_hash_sha256_init")
|
||||
// sodium.crypto_hash_sha256_init(state)
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||
// println("Debug crypto_hash_sha256_update")
|
||||
// sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||
// val out = UByteArray(32)
|
||||
// println("Debug crypto_hash_sha256_final")
|
||||
// sodium.crypto_hash_sha256_final(state, out.asByteArray())
|
||||
// return out
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_init(): Sha512State {
|
||||
// val state = debug.test.Sha512State()
|
||||
// println("Debug crypto_hash_sha512_init")
|
||||
// sodium.crypto_hash_sha512_init(state)
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||
// println("Debug crypto_hash_sha512_update")
|
||||
// sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||
// val out = UByteArray(64)
|
||||
// println("Debug crypto_hash_sha512_final")
|
||||
// sodium.crypto_hash_sha512_final(state, out.asByteArray())
|
||||
// return out
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
||||
// val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
|
||||
// println("Debug crypto_generichash_init")
|
||||
// sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Initialize a state and generate a random header. Both are returned inside
|
||||
// * `SecretStreamStateAndHeader` object.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
|
||||
// SecretStreamStateAndHeader {
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||
// val header = UByteArray(24)
|
||||
// val state = SecretStream.State()
|
||||
// sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(),
|
||||
// key.asByteArray())
|
||||
// return SecretStreamStateAndHeader(state, header)
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 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):
|
||||
// SecretStreamState {
|
||||
// val state = debug.test.SecretStreamState()
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
|
||||
// sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(),
|
||||
// key.asByteArray())
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_push(
|
||||
// state: SecretStreamState,
|
||||
// m: UByteArray,
|
||||
// ad: UByteArray,
|
||||
// tag: UByte
|
||||
// ): UByteArray {
|
||||
// val c = UByteArray(m.size + 17)
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_push")
|
||||
// sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), null, m.asByteArray(),
|
||||
// m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag.toByte())
|
||||
// return c
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
// state: SecretStreamState,
|
||||
// c: UByteArray,
|
||||
// ad: UByteArray
|
||||
// ): DecryptedDataAndTag {
|
||||
// val m = UByteArray(c.size - 17)
|
||||
// var tag_p : UByte = 0U
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
// sodium.crypto_secretstream_xchacha20poly1305_pull(state, m.asByteArray(), null, byteArrayOf(),
|
||||
// c.asByteArray(), c.size.toLong(), ad.asByteArray(), ad.size.toLong())
|
||||
// return debug.test.DecryptedDataAndTag(m, tag_p)
|
||||
// }
|
||||
//}
|
||||
|
@ -8,6 +8,4 @@ import com.goterl.lazycode.lazysodium.SodiumJava
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 22-Aug-2020
|
||||
*/
|
||||
class ASodiumWrapper : SodiumJava() {
|
||||
|
||||
}
|
||||
typealias SodiumWrapper = SodiumJava
|
||||
|
@ -5,7 +5,7 @@ package com.ionspin.kotlin.crypto
|
||||
import libsodium.sodium_init
|
||||
import kotlin.native.concurrent.AtomicInt
|
||||
|
||||
actual object Initializer {
|
||||
actual object LibsodiumInitializer {
|
||||
|
||||
private var isPlatformInitialized : AtomicInt = AtomicInt(0)
|
||||
|
||||
|
@ -1,29 +1,19 @@
|
||||
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.convert
|
||||
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_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
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 21-Aug-2020
|
||||
*/
|
||||
actual object GenericHashing {
|
||||
val _emitByte: Byte = 0
|
||||
val _emitByteArray: ByteArray = ByteArray(0)
|
||||
|
||||
actual fun genericHash(message: UByteArray, requestedHashLength: Int, key: UByteArray?) : UByteArray {
|
||||
val hash = UByteArray(requestedHashLength)
|
||||
val pinnedHash = hash.pin()
|
||||
|
@ -1,179 +1,179 @@
|
||||
package debug.test
|
||||
|
||||
import kotlin.Byte
|
||||
import kotlin.ByteArray
|
||||
import kotlin.Int
|
||||
import kotlin.UByte
|
||||
import kotlin.UByteArray
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.convert
|
||||
import kotlinx.cinterop.pin
|
||||
import kotlinx.cinterop.pointed
|
||||
import kotlinx.cinterop.ptr
|
||||
import kotlinx.cinterop.reinterpret
|
||||
import kotlinx.cinterop.toCValues
|
||||
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
|
||||
|
||||
actual typealias Sha256State = crypto_hash_sha256_state
|
||||
|
||||
actual typealias Sha512State = crypto_hash_sha512_state
|
||||
|
||||
actual typealias GenericHashState = crypto_generichash_blake2b_state
|
||||
|
||||
actual typealias SecretStreamState = crypto_secretstream_xchacha20poly1305_state
|
||||
|
||||
actual class Crypto internal actual constructor() {
|
||||
val _emitByte: Byte = 0
|
||||
|
||||
val _emitByteArray: ByteArray = ByteArray(0)
|
||||
|
||||
/**
|
||||
* Initialize the SHA256 hash
|
||||
* returns sha 256 state
|
||||
*/
|
||||
actual fun crypto_hash_sha256_init(): Sha256State {
|
||||
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
|
||||
val state = allocated.reinterpret<debug.test.Sha256State>().pointed
|
||||
println("Debug crypto_hash_sha256_init")
|
||||
libsodium.crypto_hash_sha256_init(state.ptr)
|
||||
return state
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||
println("Debug crypto_hash_sha256_update")
|
||||
val pinnedInput = input.pin()
|
||||
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
||||
pinnedInput.unpin()
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||
val out = UByteArray(32)
|
||||
println("Debug crypto_hash_sha256_final")
|
||||
val pinnedOut = out.pin()
|
||||
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
|
||||
pinnedOut.unpin()
|
||||
return out
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_init(): Sha512State {
|
||||
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
|
||||
val state = allocated.reinterpret<debug.test.Sha512State>().pointed
|
||||
println("Debug crypto_hash_sha512_init")
|
||||
libsodium.crypto_hash_sha512_init(state.ptr)
|
||||
return state
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||
println("Debug crypto_hash_sha512_update")
|
||||
val pinnedInput = input.pin()
|
||||
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
||||
pinnedInput.unpin()
|
||||
}
|
||||
|
||||
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||
val out = UByteArray(64)
|
||||
println("Debug crypto_hash_sha512_final")
|
||||
val pinnedOut = out.pin()
|
||||
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
|
||||
pinnedOut.unpin()
|
||||
return out
|
||||
}
|
||||
|
||||
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
||||
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
|
||||
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
|
||||
println("Debug crypto_generichash_init")
|
||||
val pinnedKey = key.pin()
|
||||
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
|
||||
outlen.convert())
|
||||
pinnedKey.unpin()
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a state and generate a random header. Both are returned inside
|
||||
* `SecretStreamStateAndHeader` object.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
|
||||
SecretStreamStateAndHeader {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||
val pinnedKey = key.pin()
|
||||
val state =
|
||||
sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!!
|
||||
.reinterpret<libsodium.crypto_secretstream_xchacha20poly1305_state>()
|
||||
.pointed
|
||||
val header = UByteArray(libsodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt())
|
||||
{ 0U }
|
||||
val pinnedHeader = header.pin()
|
||||
libsodium.crypto_secretstream_xchacha20poly1305_init_push(state.ptr,
|
||||
pinnedHeader.addressOf(0), pinnedKey.addressOf(0))
|
||||
pinnedHeader.unpin()
|
||||
pinnedKey.unpin()
|
||||
return SecretStreamStateAndHeader(state, header)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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):
|
||||
SecretStreamState {
|
||||
val allocated = sodium_malloc(debug.test.SecretStreamState.size.convert())!!
|
||||
val state = allocated.reinterpret<debug.test.SecretStreamState>().pointed
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
|
||||
val pinnedHeader = header.pin()
|
||||
val pinnedKey = key.pin()
|
||||
libsodium.crypto_secretstream_xchacha20poly1305_init_pull(state.ptr, pinnedHeader.addressOf(0),
|
||||
pinnedKey.addressOf(0))
|
||||
pinnedHeader.unpin()
|
||||
pinnedKey.unpin()
|
||||
return state
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_push(
|
||||
state: SecretStreamState,
|
||||
m: UByteArray,
|
||||
ad: UByteArray,
|
||||
tag: UByte
|
||||
): UByteArray {
|
||||
val c = UByteArray(m.size + 17)
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_push")
|
||||
val pinnedC = c.pin()
|
||||
val pinnedM = m.pin()
|
||||
val pinnedAd = ad.pin()
|
||||
libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0), null,
|
||||
pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0), ad.size.convert(), tag)
|
||||
pinnedC.unpin()
|
||||
pinnedM.unpin()
|
||||
pinnedAd.unpin()
|
||||
return c
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
*/
|
||||
actual fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
state: SecretStreamState,
|
||||
c: UByteArray,
|
||||
ad: UByteArray
|
||||
): DecryptedDataAndTag {
|
||||
val m = UByteArray(c.size - 17)
|
||||
var tag_p : UByte = 0U
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
val pinnedM = m.pin()
|
||||
val pinnedC = c.pin()
|
||||
val pinnedAd = ad.pin()
|
||||
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())
|
||||
pinnedM.unpin()
|
||||
pinnedC.unpin()
|
||||
pinnedAd.unpin()
|
||||
return debug.test.DecryptedDataAndTag(m, tag_p)
|
||||
}
|
||||
}
|
||||
//package debug.test
|
||||
//
|
||||
//import kotlin.Byte
|
||||
//import kotlin.ByteArray
|
||||
//import kotlin.Int
|
||||
//import kotlin.UByte
|
||||
//import kotlin.UByteArray
|
||||
//import kotlinx.cinterop.addressOf
|
||||
//import kotlinx.cinterop.convert
|
||||
//import kotlinx.cinterop.pin
|
||||
//import kotlinx.cinterop.pointed
|
||||
//import kotlinx.cinterop.ptr
|
||||
//import kotlinx.cinterop.reinterpret
|
||||
//import kotlinx.cinterop.toCValues
|
||||
//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
|
||||
//
|
||||
//actual typealias Sha256State = crypto_hash_sha256_state
|
||||
//
|
||||
//actual typealias Sha512State = crypto_hash_sha512_state
|
||||
//
|
||||
//actual typealias GenericHashState = crypto_generichash_blake2b_state
|
||||
//
|
||||
//actual typealias SecretStreamState = crypto_secretstream_xchacha20poly1305_state
|
||||
//
|
||||
//actual class Crypto internal actual constructor() {
|
||||
// val _emitByte: Byte = 0
|
||||
//
|
||||
// val _emitByteArray: ByteArray = ByteArray(0)
|
||||
//
|
||||
// /**
|
||||
// * Initialize the SHA256 hash
|
||||
// * returns sha 256 state
|
||||
// */
|
||||
// actual fun crypto_hash_sha256_init(): Sha256State {
|
||||
// val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
|
||||
// val state = allocated.reinterpret<debug.test.Sha256State>().pointed
|
||||
// println("Debug crypto_hash_sha256_init")
|
||||
// libsodium.crypto_hash_sha256_init(state.ptr)
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
|
||||
// println("Debug crypto_hash_sha256_update")
|
||||
// val pinnedInput = input.pin()
|
||||
// libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
||||
// pinnedInput.unpin()
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
|
||||
// val out = UByteArray(32)
|
||||
// println("Debug crypto_hash_sha256_final")
|
||||
// val pinnedOut = out.pin()
|
||||
// libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
|
||||
// pinnedOut.unpin()
|
||||
// return out
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_init(): Sha512State {
|
||||
// val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
|
||||
// val state = allocated.reinterpret<debug.test.Sha512State>().pointed
|
||||
// println("Debug crypto_hash_sha512_init")
|
||||
// libsodium.crypto_hash_sha512_init(state.ptr)
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
|
||||
// println("Debug crypto_hash_sha512_update")
|
||||
// val pinnedInput = input.pin()
|
||||
// libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
|
||||
// pinnedInput.unpin()
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
|
||||
// val out = UByteArray(64)
|
||||
// println("Debug crypto_hash_sha512_final")
|
||||
// val pinnedOut = out.pin()
|
||||
// libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
|
||||
// pinnedOut.unpin()
|
||||
// return out
|
||||
// }
|
||||
//
|
||||
// actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
|
||||
// val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
|
||||
// val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
|
||||
// println("Debug crypto_generichash_init")
|
||||
// val pinnedKey = key.pin()
|
||||
// libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
|
||||
// outlen.convert())
|
||||
// pinnedKey.unpin()
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Initialize a state and generate a random header. Both are returned inside
|
||||
// * `SecretStreamStateAndHeader` object.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
|
||||
// SecretStreamStateAndHeader {
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_init_push")
|
||||
// val pinnedKey = key.pin()
|
||||
// val state =
|
||||
// sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!!
|
||||
// .reinterpret<libsodium.crypto_secretstream_xchacha20poly1305_state>()
|
||||
// .pointed
|
||||
// val header = UByteArray(libsodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt())
|
||||
// { 0U }
|
||||
// val pinnedHeader = header.pin()
|
||||
// libsodium.crypto_secretstream_xchacha20poly1305_init_push(state.ptr,
|
||||
// pinnedHeader.addressOf(0), pinnedKey.addressOf(0))
|
||||
// pinnedHeader.unpin()
|
||||
// pinnedKey.unpin()
|
||||
// return SecretStreamStateAndHeader(state, header)
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 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):
|
||||
// SecretStreamState {
|
||||
// val allocated = sodium_malloc(debug.test.SecretStreamState.size.convert())!!
|
||||
// val state = allocated.reinterpret<debug.test.SecretStreamState>().pointed
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
|
||||
// val pinnedHeader = header.pin()
|
||||
// val pinnedKey = key.pin()
|
||||
// libsodium.crypto_secretstream_xchacha20poly1305_init_pull(state.ptr, pinnedHeader.addressOf(0),
|
||||
// pinnedKey.addressOf(0))
|
||||
// pinnedHeader.unpin()
|
||||
// pinnedKey.unpin()
|
||||
// return state
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Encrypt next block of data using the previously initialized state. Returns encrypted block.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_push(
|
||||
// state: SecretStreamState,
|
||||
// m: UByteArray,
|
||||
// ad: UByteArray,
|
||||
// tag: UByte
|
||||
// ): UByteArray {
|
||||
// val c = UByteArray(m.size + 17)
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_push")
|
||||
// val pinnedC = c.pin()
|
||||
// val pinnedM = m.pin()
|
||||
// val pinnedAd = ad.pin()
|
||||
// libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0), null,
|
||||
// pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0), ad.size.convert(), tag)
|
||||
// pinnedC.unpin()
|
||||
// pinnedM.unpin()
|
||||
// pinnedAd.unpin()
|
||||
// return c
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Decrypt next block of data using the previously initialized state. Returns decrypted block.
|
||||
// */
|
||||
// actual fun crypto_secretstream_xchacha20poly1305_pull(
|
||||
// state: SecretStreamState,
|
||||
// c: UByteArray,
|
||||
// ad: UByteArray
|
||||
// ): DecryptedDataAndTag {
|
||||
// val m = UByteArray(c.size - 17)
|
||||
// var tag_p : UByte = 0U
|
||||
// println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
// val pinnedM = m.pin()
|
||||
// val pinnedC = c.pin()
|
||||
// val pinnedAd = ad.pin()
|
||||
// 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())
|
||||
// pinnedM.unpin()
|
||||
// pinnedC.unpin()
|
||||
// pinnedAd.unpin()
|
||||
// return debug.test.DecryptedDataAndTag(m, tag_p)
|
||||
// }
|
||||
//}
|
||||
|
Loading…
x
Reference in New Issue
Block a user