Completed random support functions

This commit is contained in:
Ugljesa Jovanovic committed 2020-10-03 18:16:26 +02:00
1 parent cb17d75deb
commit 0431f27e3e
10 files changed
+430 -290

No files matched your search

@@ -47,6 +47,9 @@ repositories {
mavenCentral()
jcenter()
maven("https://dl.bintray.com/terl/lazysodium-maven")
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
}
group = ReleaseInfo.group
@@ -5,6 +5,8 @@ package com.ionspin.kotlin.crypto.util
* ugljesa.jovanovic@ionspin.com
* on 27-Sep-2020
*/
val randombytes_SEEDBYTES = 32
expect object LibsodiumRandom {
/**
@@ -0,0 +1,56 @@
package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 03-Oct-2020
*/
class LibsodiumRandomTest {
@Test
fun testRandom() {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^32
LibsodiumInitializer.initializeWithCallback {
val random = LibsodiumRandom.random()
assertTrue { random != 0U }
}
}
@Test
fun testRandomUniform() {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^31
LibsodiumInitializer.initializeWithCallback {
val random = LibsodiumRandom.uniform(UInt.MAX_VALUE / 2U)
assertTrue { random != 0U && random < (UInt.MAX_VALUE / 2U)}
}
}
@Test
fun testRandomBuffer() {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^52
LibsodiumInitializer.initializeWithCallback {
val result = LibsodiumRandom.buf(20)
val lowProbability = UByteArray(20) { 0U }
assertFalse { result.contentEquals(lowProbability) }
}
}
@Test
fun testRandomBufferDeterministic() {
//This is just a sanity test, it should fail on occasion though, with probability of 1/2^52
LibsodiumInitializer.initializeWithCallback {
val seed = UByteArray(randombytes_SEEDBYTES) { 1U }
val result = LibsodiumRandom.bufDeterministic(20, seed)
val lowProbability = UByteArray(20) { 0U }
assertFalse { result.contentEquals(lowProbability) }
val secondResult = LibsodiumRandom.bufDeterministic(20, seed)
assertTrue { result.contentEquals(secondResult) }
}
}
}
@@ -207,9 +207,18 @@ interface JsSodiumInterface {
fun from_hex(data : String): Uint8Array
fun from_string(data : String): Uint8Array
// ---- > ---- Random ---- < -----
fun randombytes_buf(length: UInt) : Uint8Array
fun randombytes_buf_deterministic(length: UInt, seed : Uint8Array) : Uint8Array
fun randombytes_random() : UInt
fun randombytes_uniform(upper_bound: UInt) : UInt
// ---- Utils end ----
}
@@ -0,0 +1,48 @@
package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.crypto.getSodium
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 27-Sep-2020
*/
actual object LibsodiumRandom {
/**
* The randombytes_buf() function fills size bytes starting at buf with an unpredictable sequence of bytes.
*/
actual fun buf(size: Int): UByteArray {
return getSodium().randombytes_buf(size).toUByteArray()
}
/**
* The randombytes_buf_deterministic function stores size bytes into buf indistinguishable from random bytes without knowing seed.
* For a given seed, this function will always output the same sequence. size can be up to 2^31 (~8GB) because we use kotlin arrays
* and they are limited by Int primitive type
* seed is randombytes_SEEDBYTES bytes long.
* This function is mainly useful for writing tests, and was introduced in libsodium 1.0.12. Under the hood, it uses the ChaCha20 stream cipher.
*
*/
actual fun bufDeterministic(size: Int, seed: UByteArray): UByteArray {
return getSodium().randombytes_buf_deterministic(size.toUInt(), seed.toUInt8Array()).toUByteArray()
}
/**
* The randombytes_random() function returns an unpredictable value between 0 and 0xffffffff (included).
*/
actual fun random(): UInt {
return getSodium().randombytes_random()
}
/**
* The randombytes_uniform() function returns an unpredictable value between 0 and upper_bound (excluded). Unlike r
* andombytes_random() % upper_bound, it guarantees a uniform distribution of the possible output values even when
* upper_bound is not a power of 2. Note that an upper_bound < 2 leaves only a single element to be chosen, namely 0
*/
actual fun uniform(upperBound: UInt): UInt {
return getSodium().randombytes_uniform(upperBound)
}
}
@@ -27,7 +27,7 @@ actual object LibsodiumRandom {
*/
actual fun bufDeterministic(size: Int, seed: UByteArray): UByteArray {
val result = ByteArray(size)
sodium.randombytes_buf(result, size, seed.asByteArray())
sodium.randombytes_buf_deterministic(result, size, seed.asByteArray())
return result.asUByteArray()
}
@@ -36,7 +36,8 @@ actual object LibsodiumRandom {
*/
actual fun random(): UInt {
//Broken in lazysodium-java https://github.com/terl/lazysodium-java/issues/86
TODO()
//Using temporary forked and fixed build until pull request is accepted in original repo
return sodium.randombytes_random().toUInt()
}
@@ -48,7 +49,8 @@ actual object LibsodiumRandom {
*/
actual fun uniform(upperBound: UInt): UInt {
//Broken in lazysodium-java https://github.com/terl/lazysodium-java/issues/86
TODO("not implemented yet")
//Using temporary fixed build until pull request is accepted
return sodium.randombytes_uniform(upperBound.toInt()).toUInt()
}
}