Added generic hash
This commit is contained in:
parent
6a6119dec1
commit
92058a7ba5
@ -0,0 +1,10 @@
|
||||
package com.ionspin.kotlin.crypto.generichash
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 21-Aug-2020
|
||||
*/
|
||||
expect object GenericHashing {
|
||||
fun genericHash(message : UByteArray, requestedHashLength: Int, key : UByteArray? = null) : UByteArray
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.ionspin.kotlin.crypto.shortinputhash
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 21-Aug-2020
|
||||
*/
|
||||
object ShortInputHashing {
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
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
|
||||
@ -20,17 +21,24 @@ class SmokeTest {
|
||||
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")
|
||||
// //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)
|
||||
println(hashResult.toHexString())
|
||||
assertTrue {
|
||||
"185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969" == resultString
|
||||
"EF15EAF92D5E335345A3E1D977BC7D8797C3D275717CC1B10AF79C93CDA01AEB2A0C59BC02E2BDF9380FD1B54EB9E1669026930CCC24BD49748E65F9A6B2EE68".toLowerCase() == hashResult.toHexString()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,76 +1,76 @@
|
||||
package com.ionspin.kotlin.crypto.secretstream
|
||||
|
||||
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
|
||||
import com.ionspin.kotlin.crypto.Initializer
|
||||
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import debug.test.Crypto
|
||||
import kotlin.math.exp
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 15-Aug-2020
|
||||
*/
|
||||
class SecretStreamTest {
|
||||
@Test
|
||||
fun testSecretStream() = testBlocking {
|
||||
Initializer.initializeWithCallback {
|
||||
assertTrue {
|
||||
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
|
||||
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
|
||||
|
||||
val additionalData = ubyteArrayOf(
|
||||
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
|
||||
)
|
||||
val key = ubyteArrayOf(
|
||||
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
|
||||
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
|
||||
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
|
||||
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
|
||||
)
|
||||
|
||||
val nonce = ubyteArrayOf(
|
||||
0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
|
||||
0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
|
||||
0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
|
||||
)
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
|
||||
0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
|
||||
0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
|
||||
0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
|
||||
0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
|
||||
0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U,
|
||||
0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U,
|
||||
0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U,
|
||||
0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U,
|
||||
0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU,
|
||||
0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU,
|
||||
0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U,
|
||||
0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U,
|
||||
0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U,
|
||||
0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U,
|
||||
0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
|
||||
0xcfU, 0x49U
|
||||
)
|
||||
message.hexColumsPrint()
|
||||
val crypto = Crypto()
|
||||
val stateAndHeader = crypto.crypto_secretstream_xchacha20poly1305_init_push(key)
|
||||
val encrypted =
|
||||
crypto.crypto_secretstream_xchacha20poly1305_push(stateAndHeader.state, message, ubyteArrayOf(), 0U)
|
||||
encrypted.hexColumsPrint()
|
||||
val decryptState = crypto.crypto_secretstream_xchacha20poly1305_init_pull(stateAndHeader.header, key)
|
||||
val decrypted =
|
||||
crypto.crypto_secretstream_xchacha20poly1305_pull(decryptState, encrypted, ubyteArrayOf())
|
||||
decrypted.hexColumsPrint()
|
||||
decrypted.contentEquals(message)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//package com.ionspin.kotlin.crypto.secretstream
|
||||
//
|
||||
//import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
|
||||
//import com.ionspin.kotlin.crypto.Initializer
|
||||
//import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
//import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
//import debug.test.Crypto
|
||||
//import kotlin.math.exp
|
||||
//import kotlin.test.Test
|
||||
//import kotlin.test.assertTrue
|
||||
//
|
||||
///**
|
||||
// * Created by Ugljesa Jovanovic
|
||||
// * ugljesa.jovanovic@ionspin.com
|
||||
// * on 15-Aug-2020
|
||||
// */
|
||||
//class SecretStreamTest {
|
||||
// @Test
|
||||
// fun testSecretStream() = testBlocking {
|
||||
// Initializer.initializeWithCallback {
|
||||
// assertTrue {
|
||||
// val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
|
||||
// "only one tip for the future, sunscreen would be it.").encodeToUByteArray()
|
||||
//
|
||||
// val additionalData = ubyteArrayOf(
|
||||
// 0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
|
||||
// )
|
||||
// val key = ubyteArrayOf(
|
||||
// 0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
|
||||
// 0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
|
||||
// 0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
|
||||
// 0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
|
||||
// )
|
||||
//
|
||||
// val nonce = ubyteArrayOf(
|
||||
// 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
|
||||
// 0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
|
||||
// 0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
|
||||
// )
|
||||
//
|
||||
// val expected = ubyteArrayOf(
|
||||
// 0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
|
||||
// 0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
|
||||
// 0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
|
||||
// 0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
|
||||
// 0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
|
||||
// 0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U,
|
||||
// 0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U,
|
||||
// 0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U,
|
||||
// 0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U,
|
||||
// 0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU,
|
||||
// 0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU,
|
||||
// 0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U,
|
||||
// 0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U,
|
||||
// 0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U,
|
||||
// 0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U,
|
||||
// 0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
|
||||
// 0xcfU, 0x49U
|
||||
// )
|
||||
// message.hexColumsPrint()
|
||||
// val crypto = Crypto()
|
||||
// val stateAndHeader = crypto.crypto_secretstream_xchacha20poly1305_init_push(key)
|
||||
// val encrypted =
|
||||
// crypto.crypto_secretstream_xchacha20poly1305_push(stateAndHeader.state, message, ubyteArrayOf(), 0U)
|
||||
// encrypted.hexColumsPrint()
|
||||
// val decryptState = crypto.crypto_secretstream_xchacha20poly1305_init_pull(stateAndHeader.header, key)
|
||||
// val decrypted =
|
||||
// crypto.crypto_secretstream_xchacha20poly1305_pull(decryptState, encrypted, ubyteArrayOf())
|
||||
// decrypted.hexColumsPrint()
|
||||
// decrypted.contentEquals(message)
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
@ -12,7 +12,7 @@ interface JsSodiumInterface {
|
||||
|
||||
fun randombytes_buf(numberOfBytes: Int): Uint8Array
|
||||
|
||||
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array,): Uint8Array
|
||||
fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array
|
||||
|
||||
fun crypto_hash_sha256(message: Uint8Array): Uint8Array
|
||||
|
||||
@ -57,4 +57,5 @@ interface JsSodiumInterface {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.ionspin.kotlin.crypto.generichash
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||
import org.khronos.webgl.Uint8Array
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 21-Aug-2020
|
||||
*/
|
||||
actual object GenericHashing {
|
||||
actual fun genericHash(
|
||||
message: UByteArray,
|
||||
requestedHashLength: Int,
|
||||
key: UByteArray?
|
||||
): UByteArray {
|
||||
return getSodium().crypto_generichash(
|
||||
requestedHashLength,
|
||||
message.toUInt8Array(),
|
||||
key?.toUInt8Array() ?: Uint8Array(0)
|
||||
).toUByteArray()
|
||||
}
|
||||
}
|
@ -104,9 +104,10 @@ actual class Crypto internal actual constructor() {
|
||||
state: SecretStreamState,
|
||||
c: UByteArray,
|
||||
ad: UByteArray
|
||||
): UByte {
|
||||
): DecryptedDataAndTag {
|
||||
println("Debug crypto_secretstream_xchacha20poly1305_pull")
|
||||
return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
|
||||
ad.toUInt8Array())
|
||||
// return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
|
||||
// ad.toUInt8Array())
|
||||
return DecryptedDataAndTag(ubyteArrayOf(), 0U)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.ionspin.kotlin.crypto.generichash
|
||||
|
||||
import com.ionspin.kotlin.crypto.Initializer.sodium
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 21-Aug-2020
|
||||
*/
|
||||
actual object GenericHashing {
|
||||
actual fun genericHash(
|
||||
message: UByteArray,
|
||||
requestedHashLength: Int,
|
||||
key: UByteArray?
|
||||
): UByteArray {
|
||||
val hash = UByteArray(requestedHashLength)
|
||||
sodium.crypto_generichash(
|
||||
hash.asByteArray(),
|
||||
requestedHashLength,
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key?.asByteArray(),
|
||||
(key?.size ?: 0)
|
||||
)
|
||||
return hash
|
||||
}
|
||||
}
|
@ -121,7 +121,7 @@ actual class Crypto internal actual constructor() {
|
||||
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, tag_p.toByte(),
|
||||
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)
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
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 {
|
||||
actual fun genericHash(message: UByteArray, requestedHashLength: Int, key: UByteArray?) : UByteArray {
|
||||
val hash = UByteArray(requestedHashLength)
|
||||
val pinnedHash = hash.pin()
|
||||
val pinnedKey = key?.pin()
|
||||
val pinnedMessage = message.pin()
|
||||
crypto_generichash(
|
||||
pinnedHash.addressOf(0),
|
||||
requestedHashLength.convert(),
|
||||
pinnedMessage.addressOf(0),
|
||||
message.size.convert(),
|
||||
pinnedKey?.addressOf(0),
|
||||
(key?.size ?: 0).convert()
|
||||
)
|
||||
pinnedHash.unpin()
|
||||
pinnedKey?.unpin()
|
||||
pinnedMessage.unpin()
|
||||
return hash
|
||||
}
|
||||
}
|
@ -170,7 +170,7 @@ actual class Crypto internal actual constructor() {
|
||||
val pinnedC = c.pin()
|
||||
val pinnedAd = ad.pin()
|
||||
libsodium.crypto_secretstream_xchacha20poly1305_pull(state.ptr, pinnedM.addressOf(0), null,
|
||||
tag_p, 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()
|
||||
pinnedC.unpin()
|
||||
pinnedAd.unpin()
|
||||
|
Loading…
x
Reference in New Issue
Block a user