Use unsafe cast instead of conversion that creates new array

This commit is contained in:
Ugljesa Jovanovic 2020-07-07 21:47:10 +02:00 committed by Ugljesa Jovanovic
parent 9751f80347
commit 5d3c14de2f
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
26 changed files with 101 additions and 86 deletions

View File

@ -40,6 +40,6 @@ interface Hash : HashFunction {
} }
fun String.encodeToUByteArray() : UByteArray{ fun String.encodeToUByteArray() : UByteArray{
return encodeToByteArray().toUByteArray() return encodeToByteArray().asUByteArray()
} }

View File

@ -29,6 +29,6 @@ actual object SRNG {
actual fun getRandomBytes(amount: Int): UByteArray { actual fun getRandomBytes(amount: Int): UByteArray {
val byteArray = ByteArray(amount) val byteArray = ByteArray(amount)
secureRandom.nextBytes(byteArray) secureRandom.nextBytes(byteArray)
return byteArray.toUByteArray() return byteArray.asUByteArray()
} }
} }

View File

@ -30,7 +30,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
key.toByteArray() key.toByteArray()
) )
return ciphertext.toUByteArray() return ciphertext.asUByteArray()
} }
actual fun decrypt( actual fun decrypt(
@ -52,7 +52,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
key.toByteArray() key.toByteArray()
) )
return message.toUByteArray() return message.asUByteArray()
} }
} }

View File

@ -23,7 +23,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLengt
override fun digest(): UByteArray { override fun digest(): UByteArray {
val hashed = ByteArray(hashLength) val hashed = ByteArray(hashLength)
sodium.crypto_generichash_final(state, hashed, hashLength) sodium.crypto_generichash_final(state, hashed, hashLength)
return hashed.toUByteArray() return hashed.asUByteArray()
} }
} }
@ -34,7 +34,7 @@ actual object Blake2bDelegatedStateless : Blake2b {
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
val hashed = ByteArray(hashLength) val hashed = ByteArray(hashLength)
sodium.crypto_generichash(hashed, hashed.size, inputMessage.toByteArray(), inputMessage.size.toLong(), key.toByteArray(), key.size) sodium.crypto_generichash(hashed, hashed.size, inputMessage.toByteArray(), inputMessage.size.toLong(), key.toByteArray(), key.size)
return hashed.toUByteArray() return hashed.asUByteArray()
} }
} }

View File

@ -26,7 +26,7 @@ actual class Sha256Delegated actual constructor() : Sha256 {
override fun digest(): UByteArray { override fun digest(): UByteArray {
val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES) val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES)
sodium.crypto_hash_sha256_final(state, hashed) sodium.crypto_hash_sha256_final(state, hashed)
return hashed.toUByteArray() return hashed.asUByteArray()
} }
} }
@ -36,6 +36,6 @@ actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputMessage: UByteArray): UByteArray { override fun digest(inputMessage: UByteArray): UByteArray {
val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES) val hashed = ByteArray(Sha256Properties.MAX_HASH_BYTES)
sodium.crypto_hash_sha256(hashed, inputMessage.toByteArray(), inputMessage.size.toLong()) sodium.crypto_hash_sha256(hashed, inputMessage.toByteArray(), inputMessage.size.toLong())
return hashed.toUByteArray() return hashed.asUByteArray()
} }
} }

View File

@ -25,7 +25,7 @@ actual class Sha512Delegated : Sha512Multipart {
override fun digest(): UByteArray { override fun digest(): UByteArray {
val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES) val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES)
Initializer.sodium.crypto_hash_sha512_final(state, hashed) Initializer.sodium.crypto_hash_sha512_final(state, hashed)
return hashed.toUByteArray() return hashed.asUByteArray()
} }
} }
@ -35,6 +35,6 @@ actual object Sha512StatelessDelegated : Sha512 {
override fun digest(inputMessage: UByteArray): UByteArray { override fun digest(inputMessage: UByteArray): UByteArray {
val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES) val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES)
Initializer.sodium.crypto_hash_sha512(hashed, inputMessage.toByteArray(), inputMessage.size.toLong()) Initializer.sodium.crypto_hash_sha512(hashed, inputMessage.toByteArray(), inputMessage.size.toLong())
return hashed.toUByteArray() return hashed.asUByteArray()
} }
} }

View File

@ -78,7 +78,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
pointer[i] = testState[i] pointer[i] = testState[i]
} }
println("state after setting-----------") println("state after setting-----------")
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).toUByteArray().hexColumsPrint() state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).asUByteArray().hexColumsPrint()
println("state after setting-----------") println("state after setting-----------")
println("header after setting-----------") println("header after setting-----------")
testHeader.copyInto(header) testHeader.copyInto(header)
@ -92,7 +92,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
val pinnedHeader = header.pin() val pinnedHeader = header.pin()
crypto_secretstream_xchacha20poly1305_init_push(state.ptr, pinnedHeader.addressOf(0), key.toCValues()) crypto_secretstream_xchacha20poly1305_init_push(state.ptr, pinnedHeader.addressOf(0), key.toCValues())
println("state-----------") println("state-----------")
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).toUByteArray().hexColumsPrint() state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).asUByteArray().hexColumsPrint()
println("state-----------") println("state-----------")
println("--------header-----------") println("--------header-----------")
header.hexColumsPrint() header.hexColumsPrint()

View File

@ -20,7 +20,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
requestedHashLength = hashLength requestedHashLength = hashLength
val allocated = malloc(crypto_generichash_state.size.convert())!! val allocated = malloc(crypto_generichash_state.size.convert())!!
state = allocated.reinterpret<crypto_generichash_state>().pointed state = allocated.reinterpret<crypto_generichash_state>().pointed
crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert()) crypto_generichash_init(state.ptr, key?.run { this.toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert())
} }
override fun update(data: UByteArray) { override fun update(data: UByteArray) {

View File

@ -19,6 +19,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.bignum.integer.BigInteger import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.integer.toBigInteger import com.ionspin.kotlin.bignum.integer.toBigInteger
import com.ionspin.kotlin.crypto.* import com.ionspin.kotlin.crypto.*
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.rotateRight import com.ionspin.kotlin.crypto.util.rotateRight
/** /**
@ -146,9 +147,9 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { fun digest(inputString: String, key: String?, hashLength: Int): UByteArray {
val array = inputString.encodeToByteArray().toUByteArray() val array = inputString.encodeToUByteArray()
val keyBytes = key?.run { val keyBytes = key?.run {
encodeToByteArray().toUByteArray() encodeToUByteArray()
} ?: ubyteArrayOf() } ?: ubyteArrayOf()
return digest(inputMessage = array, key = keyBytes, hashLength = hashLength) return digest(inputMessage = array, key = keyBytes, hashLength = hashLength)
@ -248,7 +249,7 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
key: String?, key: String?,
requestedHashLenght: Int = 64 requestedHashLenght: Int = 64
) : this( ) : this(
(key?.encodeToByteArray()?.toUByteArray() ?: ubyteArrayOf()), (key?.encodeToUByteArray() ?: ubyteArrayOf()),
requestedHashLenght requestedHashLenght
) )
@ -310,7 +311,7 @@ class Blake2bPure(val key: UByteArray? = null, val hashLength: Int = 64) : Blake
} }
fun update(data: String) { fun update(data: String) {
update(data.encodeToByteArray().toUByteArray()) update(data.encodeToUByteArray())
} }
private fun appendToBuffer(array: UByteArray, start: Int) { private fun appendToBuffer(array: UByteArray, start: Int) {

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.rotateRight import com.ionspin.kotlin.crypto.util.rotateRight
@ -236,7 +237,7 @@ class Sha256Pure : Sha256 {
fun update(data: String) { fun update(data: String) {
return update(data.encodeToByteArray().toUByteArray()) return update(data.encodeToUByteArray())
} }
override fun update(data: UByteArray) { override fun update(data: UByteArray) {

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.rotateRight import com.ionspin.kotlin.crypto.util.rotateRight
/** /**
@ -310,7 +311,7 @@ class Sha512Pure : Sha512Multipart {
fun update(data: String) { fun update(data: String) {
return update(data.encodeToByteArray().toUByteArray()) return update(data.encodeToUByteArray())
} }
override fun update(data: UByteArray) { override fun update(data: UByteArray) {

View File

@ -22,6 +22,7 @@ import com.ionspin.kotlin.bignum.integer.toBigInteger
import com.ionspin.kotlin.crypto.SRNG import com.ionspin.kotlin.crypto.SRNG
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.keyderivation.ArgonResult import com.ionspin.kotlin.crypto.keyderivation.ArgonResult
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bArbitraryLenghtHash import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bArbitraryLenghtHash
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG
@ -74,14 +75,14 @@ class Argon2Pure(
): ArgonResult { ): ArgonResult {
val salt = SRNG.getRandomBytes(64) val salt = SRNG.getRandomBytes(64)
val argon = Argon2Pure( val argon = Argon2Pure(
password.encodeToByteArray().toUByteArray(), password.encodeToUByteArray(),
salt, salt,
parallelism, parallelism,
tagLength.toUInt(), tagLength.toUInt(),
memory.toUInt(), memory.toUInt(),
numberOfIterations, numberOfIterations,
key.encodeToByteArray().toUByteArray(), key.encodeToUByteArray(),
associatedData.encodeToByteArray().toUByteArray(), associatedData.encodeToUByteArray(),
ArgonType.Argon2id ArgonType.Argon2id
) )
val resultArray = argon.derive() val resultArray = argon.derive()
@ -100,14 +101,14 @@ class Argon2Pure(
associatedData: String = "", associatedData: String = "",
argonType: ArgonType = ArgonType.Argon2id argonType: ArgonType = ArgonType.Argon2id
) : this( ) : this(
password.encodeToByteArray().toUByteArray(), password.encodeToUByteArray(),
salt.encodeToByteArray().toUByteArray(), salt.encodeToUByteArray(),
parallelism, parallelism,
tagLength, tagLength,
requestedMemorySize, requestedMemorySize,
numberOfIterations, numberOfIterations,
key.encodeToByteArray().toUByteArray(), key.encodeToUByteArray(),
associatedData.encodeToByteArray().toUByteArray(), associatedData.encodeToUByteArray(),
argonType argonType
) )

View File

@ -1,12 +1,14 @@
package com.ionspin.kotlin.crypto.symmetric package com.ionspin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
* on 13-Jun-2020 * on 13-Jun-2020
*/ */
internal sealed class InternalAesKey(val key: String, val keyLength: Int) { internal sealed class InternalAesKey(val key: String, val keyLength: Int) {
val keyArray: UByteArray = key.chunked(2).map { it.toUByte(16) }.toUByteArray() val keyArray: UByteArray = key.hexStringToUByteArray()
val numberOf32BitWords = keyLength / 32 val numberOf32BitWords = keyLength / 32
class Aes128Key(key: String) : InternalAesKey(key, 128) class Aes128Key(key: String) : InternalAesKey(key, 128)

View File

@ -161,7 +161,7 @@ internal class AesCtrPure internal constructor(val aesKey: InternalAesKey, val m
} }
private fun consumeBlock(data: UByteArray, blockCount: ModularBigInteger): UByteArray { private fun consumeBlock(data: UByteArray, blockCount: ModularBigInteger): UByteArray {
val blockCountAsByteArray = blockCount.toUByteArray(Endianness.BIG).toUByteArray().expandCounterTo16Bytes() val blockCountAsByteArray = blockCount.toUByteArray(Endianness.BIG).expandCounterTo16Bytes()
return when (mode) { return when (mode) {
Mode.ENCRYPT -> { Mode.ENCRYPT -> {
AesPure.encrypt(aesKey, blockCountAsByteArray) xor data AesPure.encrypt(aesKey, blockCountAsByteArray) xor data

View File

@ -22,6 +22,7 @@ import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Pure
import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType import com.ionspin.kotlin.crypto.keyderivation.argon2.ArgonType
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
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 kotlin.test.Test import kotlin.test.Test
@ -70,7 +71,7 @@ class ReadmeTest {
} }
val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" + val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" +
"6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1") "6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1")
.chunked(2).map { it.toUByte(16) }.toUByteArray() .hexStringToUByteArray()
assertTrue { assertTrue {
result.contentEquals(expectedResult) result.contentEquals(expectedResult)
@ -84,7 +85,7 @@ class ReadmeTest {
val result = Sha256Pure.digest(input.encodeToUByteArray()) val result = Sha256Pure.digest(input.encodeToUByteArray())
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
@ -94,12 +95,12 @@ class ReadmeTest {
@Test @Test
fun sha512Example() { fun sha512Example() {
val input = "abc" val input = "abc"
val result = Sha512Pure.digest(inputMessage = input.encodeToByteArray().map { it.toUByte() }.toUByteArray()) val result = Sha512Pure.digest(inputMessage = input.encodeToUByteArray())
println(result.map { it.toString(16) }) println(result.map { it.toString(16) })
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
@ -113,7 +114,7 @@ class ReadmeTest {
val result = sha256.digest() val result = sha256.digest()
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
} }
@ -126,7 +127,7 @@ class ReadmeTest {
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }

View File

@ -17,6 +17,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -82,7 +83,7 @@ class Blake2BTest {
val expectedResultString = "800bb78cd4da18995c8074713bb674" + val expectedResultString = "800bb78cd4da18995c8074713bb674" +
"3cd94b2b6490a693fe4000ed00833b88b7b474d94af9cfed246b1b" + "3cd94b2b6490a693fe4000ed00833b88b7b474d94af9cfed246b1b" +
"4ce1935a76154d7ea7c410493557741d18ec3a08da75" "4ce1935a76154d7ea7c410493557741d18ec3a08da75"
val expectedResult = expectedResultString.chunked(2).map { it.toUByte(16) }.toUByteArray() val expectedResult = expectedResultString.hexStringToUByteArray()
assertTrue { assertTrue {
result.contentEquals(expectedResult) result.contentEquals(expectedResult)
@ -123,7 +124,7 @@ class Blake2BTest {
} }
val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" + val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" +
"6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1") "6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1")
.chunked(2).map { it.toUByte(16) }.toUByteArray() .hexStringToUByteArray()
assertTrue { assertTrue {
result.contentEquals(expectedResult) result.contentEquals(expectedResult)

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -84,7 +85,7 @@ class Blake2bInstanceTest {
} }
val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" + val expectedResult = ("5c6a9a4ae911c02fb7e71a991eb9aea371ae993d4842d206e" +
"6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1") "6020d46f5e41358c6d5c277c110ef86c959ed63e6ecaaaceaaff38019a43264ae06acf73b9550b1")
.chunked(2).map { it.toUByte(16) }.toUByteArray() .hexStringToUByteArray()
assertTrue { assertTrue {
result.contentEquals(expectedResult) result.contentEquals(expectedResult)

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -36,13 +37,13 @@ class Blake2bKnowAnswerTests {
@Test @Test
fun knownAnswerTest() { fun knownAnswerTest() {
kat.forEach { kat.forEach {
val parsedInput = it.input.chunked(2).map { it.toUByte(16) }.toUByteArray() val parsedInput = it.input.hexStringToUByteArray()
val result = Blake2bPure.digest( val result = Blake2bPure.digest(
inputMessage = parsedInput, inputMessage = parsedInput,
key = it.key.chunked(2).map { it.toUByte(16) }.toUByteArray() key = it.key.hexStringToUByteArray()
) )
assertTrue { assertTrue {
result.contentEquals(it.hash.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(it.hash.hexStringToUByteArray())
} }
} }
} }
@ -51,13 +52,13 @@ class Blake2bKnowAnswerTests {
fun knownAnswerTestInstance() { fun knownAnswerTestInstance() {
kat.forEach { kat -> kat.forEach { kat ->
val parsedInput = kat.input.chunked(2).map { it.toUByte(16) }.toUByteArray() val parsedInput = kat.input.hexStringToUByteArray()
val chunkedInput = parsedInput.toList().chunked(128).map { it.toUByteArray() } val chunkedInput = parsedInput.toList().chunked(128).map { it.toUByteArray() }
val blake2b = Blake2bPure(key = kat.key.chunked(2).map { it.toUByte(16) }.toUByteArray()) val blake2b = Blake2bPure(key = kat.key.hexStringToUByteArray())
chunkedInput.forEach { blake2b.update(it) } chunkedInput.forEach { blake2b.update(it) }
val result = blake2b.digest() val result = blake2b.digest()
assertTrue("KAT ${kat.input} \nkey: ${kat.key} \nexpected: {${kat.hash}") { assertTrue("KAT ${kat.input} \nkey: ${kat.key} \nexpected: {${kat.hash}") {
result.contentEquals(kat.hash.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(kat.hash.hexStringToUByteArray())
} }
} }
} }

View File

@ -17,6 +17,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -35,7 +36,7 @@ class Sha256Test {
val result = Sha256Pure.digest("abc".encodeToUByteArray()) val result = Sha256Pure.digest("abc".encodeToUByteArray())
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
@ -48,7 +49,7 @@ class Sha256Test {
val resultDoubleBlock = Sha256Pure.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToUByteArray()) val resultDoubleBlock = Sha256Pure.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToUByteArray())
val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -61,7 +62,7 @@ class Sha256Test {
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = "")) println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -75,7 +76,7 @@ class Sha256Test {
val resultDoubleBlock = Sha256Pure.digest(inputMessage = (inputBuilder.toString()).encodeToByteArray().map { it.toUByte() }.toUByteArray()) val resultDoubleBlock = Sha256Pure.digest(inputMessage = (inputBuilder.toString()).encodeToByteArray().map { it.toUByte() }.toUByteArray())
val expectedResultForDoubleBlock = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" val expectedResultForDoubleBlock = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
} }

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Ignore import kotlin.test.Ignore
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -36,7 +37,7 @@ class Sha256UpdatableTest {
val result = sha256.digest() val result = sha256.digest()
val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" val expectedResult = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
} }
@ -49,7 +50,7 @@ class Sha256UpdatableTest {
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = "")) println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" val expectedResultForDoubleBlock = "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -62,7 +63,7 @@ class Sha256UpdatableTest {
println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = "")) println(resultDoubleBlock.map{ it.toString(16)}.joinToString(separator = ""))
val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" val expectedResultForDoubleBlock = "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -76,7 +77,7 @@ class Sha256UpdatableTest {
val resultDoubleBlock = sha256.digest() val resultDoubleBlock = sha256.digest()
val expectedResultForDoubleBlock = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" val expectedResultForDoubleBlock = "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -94,7 +95,7 @@ class Sha256UpdatableTest {
val resultDoubleBlock = sha256.digest() val resultDoubleBlock = sha256.digest()
val expectedResultForDoubleBlock = "50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e" val expectedResultForDoubleBlock = "50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
} }

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -35,7 +36,7 @@ class Sha512Test {
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
@ -51,7 +52,7 @@ class Sha512Test {
val expectedResultForDoubleBlock = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + val expectedResultForDoubleBlock = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -65,7 +66,7 @@ class Sha512Test {
val resultDoubleBlock = Sha512Pure.digest(inputMessage = (inputBuilder.toString()).encodeToByteArray().map { it.toUByte() }.toUByteArray()) val resultDoubleBlock = Sha512Pure.digest(inputMessage = (inputBuilder.toString()).encodeToByteArray().map { it.toUByte() }.toUByteArray())
val expectedResultForDoubleBlock = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b" val expectedResultForDoubleBlock = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
} }

View File

@ -16,6 +16,7 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Ignore import kotlin.test.Ignore
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -35,7 +36,7 @@ class Sha512UpdatableTest {
val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + val expectedResult = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" +
"2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toUByte(16) }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
@ -51,7 +52,7 @@ class Sha512UpdatableTest {
val expectedResultForDoubleBlock = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + val expectedResultForDoubleBlock = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" +
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -65,7 +66,7 @@ class Sha512UpdatableTest {
val resultDoubleBlock = sha512.digest() val resultDoubleBlock = sha512.digest()
val expectedResultForDoubleBlock = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b" val expectedResultForDoubleBlock = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
@ -83,7 +84,7 @@ class Sha512UpdatableTest {
val resultDoubleBlock = sha512.digest() val resultDoubleBlock = sha512.digest()
val expectedResultForDoubleBlock = "b47c933421ea2db149ad6e10fce6c7f93d0752380180ffd7f4629a712134831d77be6091b819ed352c2967a2e2d4fa5050723c9630691f1a05a7281dbe6c1086" val expectedResultForDoubleBlock = "b47c933421ea2db149ad6e10fce6c7f93d0752380180ffd7f4629a712134831d77be6091b819ed352c2967a2e2d4fa5050723c9630691f1a05a7281dbe6c1086"
assertTrue { assertTrue {
resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.chunked(2).map { it.toUByte(16) }.toUByteArray()) resultDoubleBlock.contentEquals(expectedResultForDoubleBlock.hexStringToUByteArray())
} }
} }
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.symmetric package com.ionspin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue
@ -183,10 +184,10 @@ class AesTest {
val key = "2b7e151628aed2a6abf7158809cf4f3c" val key = "2b7e151628aed2a6abf7158809cf4f3c"
val expectedResult = "3925841d02dc09fbdc118597196a0b32" val expectedResult = "3925841d02dc09fbdc118597196a0b32"
val aes = AesPure(InternalAesKey.Aes128Key(key), input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) val aes = AesPure(InternalAesKey.Aes128Key(key), input.hexStringToUByteArray())
val result = aes.encrypt() val result = aes.encrypt()
assertTrue { assertTrue {
result.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) result.contentEquals(expectedResult.hexStringToUByteArray())
} }
} }
@ -196,11 +197,11 @@ class AesTest {
val input = "3243f6a8885a308d313198a2e0370734" val input = "3243f6a8885a308d313198a2e0370734"
val key = "2b7e151628aed2a6abf7158809cf4f3c" val key = "2b7e151628aed2a6abf7158809cf4f3c"
val expectedResult = "3925841d02dc09fbdc118597196a0b32" val expectedResult = "3925841d02dc09fbdc118597196a0b32"
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray() val original = input.hexStringToUByteArray()
val aes = AesPure(InternalAesKey.Aes128Key(key), original) val aes = AesPure(InternalAesKey.Aes128Key(key), original)
val encrypted = aes.encrypt() val encrypted = aes.encrypt()
assertTrue { assertTrue {
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) encrypted.contentEquals(expectedResult.hexStringToUByteArray())
} }
val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted) val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted)
@ -210,11 +211,11 @@ class AesTest {
val input = "00112233445566778899aabbccddeeff" val input = "00112233445566778899aabbccddeeff"
val key = "000102030405060708090a0b0c0d0e0f" val key = "000102030405060708090a0b0c0d0e0f"
val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a" val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a"
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray() val original = input.hexStringToUByteArray()
val aes = AesPure(InternalAesKey.Aes128Key(key), original) val aes = AesPure(InternalAesKey.Aes128Key(key), original)
val encrypted = aes.encrypt() val encrypted = aes.encrypt()
assertTrue { assertTrue {
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) encrypted.contentEquals(expectedResult.hexStringToUByteArray())
} }
val aesDec = AesPure(InternalAesKey.Aes128Key(key), encrypted) val aesDec = AesPure(InternalAesKey.Aes128Key(key), encrypted)
val decrypted = aesDec.decrypt() val decrypted = aesDec.decrypt()
@ -228,10 +229,10 @@ class AesTest {
val input = "00112233445566778899aabbccddeeff" val input = "00112233445566778899aabbccddeeff"
val key = "000102030405060708090a0b0c0d0e0f" val key = "000102030405060708090a0b0c0d0e0f"
val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a" val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a"
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray() val original = input.hexStringToUByteArray()
val encrypted = AesPure.encrypt(InternalAesKey.Aes128Key(key), original) val encrypted = AesPure.encrypt(InternalAesKey.Aes128Key(key), original)
assertTrue { assertTrue {
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) encrypted.contentEquals(expectedResult.hexStringToUByteArray())
} }
val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted) val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted)
decrypted.contentEquals(original) decrypted.contentEquals(original)
@ -241,10 +242,10 @@ class AesTest {
val input = "00112233445566778899aabbccddeeff" val input = "00112233445566778899aabbccddeeff"
val key = "000102030405060708090a0b0c0d0e0f1011121314151617" val key = "000102030405060708090a0b0c0d0e0f1011121314151617"
val expectedResult = "dda97ca4864cdfe06eaf70a0ec0d7191" val expectedResult = "dda97ca4864cdfe06eaf70a0ec0d7191"
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray() val original = input.hexStringToUByteArray()
val encrypted = AesPure.encrypt(InternalAesKey.Aes192Key(key), original) val encrypted = AesPure.encrypt(InternalAesKey.Aes192Key(key), original)
assertTrue { assertTrue {
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) encrypted.contentEquals(expectedResult.hexStringToUByteArray())
} }
val decrypted = AesPure.decrypt(InternalAesKey.Aes192Key(key), encrypted) val decrypted = AesPure.decrypt(InternalAesKey.Aes192Key(key), encrypted)
decrypted.contentEquals(original) decrypted.contentEquals(original)
@ -254,10 +255,10 @@ class AesTest {
val input = "00112233445566778899aabbccddeeff" val input = "00112233445566778899aabbccddeeff"
val key = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" val key = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
val expectedResult = "8ea2b7ca516745bfeafc49904b496089" val expectedResult = "8ea2b7ca516745bfeafc49904b496089"
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray() val original = input.hexStringToUByteArray()
val encrypted = AesPure.encrypt(InternalAesKey.Aes256Key(key), original) val encrypted = AesPure.encrypt(InternalAesKey.Aes256Key(key), original)
assertTrue { assertTrue {
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()) encrypted.contentEquals(expectedResult.hexStringToUByteArray())
} }
val decrypted = AesPure.decrypt(InternalAesKey.Aes256Key(key), encrypted) val decrypted = AesPure.decrypt(InternalAesKey.Aes256Key(key), encrypted)
decrypted.contentEquals(original) decrypted.contentEquals(original)

View File

@ -29,6 +29,6 @@ actual object SRNG {
actual fun getRandomBytes(amount: Int): UByteArray { actual fun getRandomBytes(amount: Int): UByteArray {
val byteArray = ByteArray(amount) val byteArray = ByteArray(amount)
secureRandom.nextBytes(byteArray) secureRandom.nextBytes(byteArray)
return byteArray.toUByteArray() return byteArray.asUByteArray()
} }
} }

View File

@ -22,7 +22,7 @@ object Sample {
blake2bUpdateable.update("test".encodeToUByteArray()) blake2bUpdateable.update("test".encodeToUByteArray())
println(blake2bUpdateable.digest().toHexString()) println(blake2bUpdateable.digest().toHexString())
println("Blake2b stateless") println("Blake2b stateless")
val statelessResult = CryptoPrimitives.Blake2b.stateless("test".encodeToByteArray().toUByteArray()) val statelessResult = CryptoPrimitives.Blake2b.stateless("test".encodeToUByteArray())
println("Blake2b stateless: ${statelessResult.toHexString()}") println("Blake2b stateless: ${statelessResult.toHexString()}")
} }
} }