From bc1c50f26834dd22de60573484a1395a8e614515 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Tue, 19 May 2020 23:33:10 +0200 Subject: [PATCH] Initial switch to byte array --- jvmSample/out/test/classes/JvmTest.class | Bin 4074 -> 3617 bytes .../kotlin/com/ionspin/kotlin/crypto/SRNG.kt | 2 +- .../keyderivation/KeyDerivationFunction.kt | 2 +- .../crypto/keyderivation/argon2/Argon2.kt | 71 +++++++++--------- .../keyderivation/argon2/Argon2Utils.kt | 26 ++----- .../ionspin/kotlin/crypto/symmetric/AesCbc.kt | 2 +- .../ionspin/kotlin/crypto/symmetric/AesCtr.kt | 2 +- .../com/ionspin/kotlin/crypto/util/Util.kt | 12 +++ .../kotlin/crypto/hash/argon/Argon2Test.kt | 30 ++++---- .../kotlin/com/ionspin/kotlin/crypto/SRNG.kt | 4 +- .../kotlin/com/ionspin/kotlin/crypto/SRNG.kt | 4 +- .../kotlin/com/ionspin/kotlin/crypto/SRNG.kt | 4 +- .../kotlin/com/ionspin/kotlin/crypto/SRNG.kt | 4 +- 13 files changed, 82 insertions(+), 81 deletions(-) diff --git a/jvmSample/out/test/classes/JvmTest.class b/jvmSample/out/test/classes/JvmTest.class index 660406d84fe366d15ca2ff29dd9471be0f5f1f04..f609dc8686d06b6e0a3e50490001bbd211ee4904 100644 GIT binary patch delta 1405 zcmZuwOH>?KyzfqquOR$CVp5>V`#+YuBxJQYw0m=j8tXea-#v{qC1P%Py44 zKi>HITLA6&^0NFF8)lFOjZEffDjk(j%^(jM$!KaKY$PlRcQWK0PFaa~GO$0Cv_#Z0 z_(oI7jFp}mwNmNE_{5O}gR3S~8}gza4KnJ`$N&iee%8^=TKLb7Ua1Z39<*W$FLUZ_ zD?jA?VN)k`86D_ia7WE_eA=|D)JB3{bYr_6xr-0DN}@X%@^3c~oUzQE>9jE;qKCm7 zGp%s^xVhSbP!kE-J?z468F%BJTTLDCq7xw*LG&^djvLcPV9JUo0zIij!W^~YsicTL z{=I8G8{ogWnkolzpNxAk#GtIcYMIBZK-fyhld;TR0$?9+5;~*q{8PSRiDXX=5Z|ig<*fXwBa0VUHxrm_U+2*k{D_atTNHd9h1MBV+q5{)bq#aT-T$ z;}|V$?WOQc#xf^F9Oo5MdBx*+!h^?ff}tRr5>EvNX;6#CH4Vx-$w#Cr=_x!d<4K(2 zb5akV^mI8Lks$xZ(_cFG-_lu(M7%1)h2+(0ImXyLKa*26`!TUC4^BEO;YJnw*o;re z@`8wh0&F5baFN7K9*ORJ$`awgJm3&1uN+_=|7L%U=-c5_yYpSxt`%wpx?j(CYOc}^ zV@qh#6g6MPBUQQmU+1?od^=L@ic}7O}0b zegQlB8+4ab6I7S(zJ}ni>RQ5{MeJQhKR@9uo^4k>N=}>RRYk?CaUPmXBur0gCJyhZne0T=WB9}6A@f@C~jbBFvUcig=&m~59}X~^<`kW$#;HSQCqk%O#A`M_j_gP_Ux2cIu}N61Bv40yOmT&7 zpah@M>A*+$obFzJHLojG*C)4BwlrvMdW&4QOU^QIfWCO0c=$#H&Jf-V;jIwP;qBYj rxe(sLc?#g&5Z=T45wHwRbRhyUgUSp78C$8!D((zEBs6ZZS^)kA`;Z@T delta 1806 zcmZuyYj6`)6#i~ryWOU3nzC&diVZYC-);KXhAqWD08JZOqy;QhOG-kUHce`>Eud(8 zfTH-qE4~$ZBZ~5nwoqmI#~H?P#u@#gIi)TYyIu6;~d(S=h+jH*u&bfC_ z6#Q8z{qXONn*i!?^Ro008?=&4zcmg9$8caGP!$P82djF9;-NrLz#0Z?A`%LXF<6S* zOSaolf>H^^C}R+}1QLn8v3O8K1%tFT5Dmsgy8{tjL={8&NKB7}qgC5{QC)zCSC|U2 zYf&em2K5XO(ZJy42h3ixfVKRXsXe_B>(Zd1iJ_kKb(R_d5Av1f?|WO&Dxn!|47Ok> z9-as>WEYq8`R6^f?$<+&c5K8Z1NIO@PAZUbJshcOiN^!`{ow@d!DqzaGh%2#hlF)z=Hil8JGP=*!XxNm$en}hp?!MQwr)Khjt(X| z$isGi!ctlED0(IAz)l8vF>n#?G5)v3oB0I#B|MH@C;3QrsQ1N@ZW7N14Zm^{aS5@hxQ|3h$z2XQD3FXAPhA?CBge6{En zU&Rp#ui!PlRcuR2VLdeJ4o}dZ!RfzOI9&t7q1}2TWyq}$g%j@4z*tXg9+=a2Ut}

$UbzRyZW7qg;$(@{}Gmr^0^+mA34JTG(ie`3Du_6a6Ne@g@vyn!m zF`G~VOt=P^b`s77Fvk+Cnnt;^(pfDynjK10wgv0eC2Ef5)UwU0rC|BcG*neqvK6PC zBQKF#*J_?_h2@Zv9MEDKGkWFI7Rm*Bt~c$1{7)ZW5TDvLAZ z{B3Yb@?rS#4vx__4C!5pP$26*yiYh0afI*zty;+IKoTF4(n=0i;3IrY%G+3lPe?M; z#!lc<9H;6T##wwuGophz?o(yVIKs3_RYuV=0|#sA6wVl2n^N=`1ql=`Dp#vvXr+O~ z4eBKV$TWg@oT1)?Z}2URzw*5qjma`sjjKO(O(rTwbZPF&+P!e-5_j)Vy5Rcz=Wrdm zRO**JMV@lCPV-1>YiqqlHJVy4tyLR5MK$HMnyN~r8>P~2S4ZkAupu-sJ}8xLlFD2j zSNp_h51n|!8HxO2hB#hdU*s{~H^zmxQ>U$y_I{S=KGz4c59fXO+=mPJVt#hPhc9uF o7Vwo1U*l3ASOS-Er4K>^MF}({48ffeYza!~cNDil7=8Bs1BNGxq5uE@ diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index 174801d..80e6f05 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -22,5 +22,5 @@ package com.ionspin.kotlin.crypto * on 21-Sep-2019 */ expect object SRNG { - fun getRandomBytes(amount : Int) : Array + fun getRandomBytes(amount : Int) : UByteArray } \ No newline at end of file diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/KeyDerivationFunction.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/KeyDerivationFunction.kt index bf8b6ed..3a6463b 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/KeyDerivationFunction.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/KeyDerivationFunction.kt @@ -22,5 +22,5 @@ package com.ionspin.kotlin.crypto.keyderivation * on 16-May-2020 */ interface KeyDerivationFunction { - fun derive() : Array + fun derive() : UByteArray } \ No newline at end of file diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt index 78851bc..06aa497 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt @@ -27,6 +27,7 @@ import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFun import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.validateArgonParameters import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToUInt import com.ionspin.kotlin.crypto.util.toLittleEndianTypedUByteArray +import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray /** * Created by Ugljesa Jovanovic @@ -45,8 +46,8 @@ data class SegmentPosition( ) data class ArgonResult( - val hashBytes: Array, - val salt: Array + val hashBytes: UByteArray, + val salt: UByteArray ) { val hashString by lazy { hashBytes.map { it.toString(16).padStart(2, '0') }.joinToString(separator = "") } val saltString by lazy { salt.map { it.toString(16).padStart(2, '0') }.joinToString(separator = "") } @@ -55,14 +56,14 @@ data class ArgonResult( @ExperimentalStdlibApi class Argon2( - private val password: Array, - private val salt: Array = emptyArray(), + private val password: UByteArray, + private val salt: UByteArray = ubyteArrayOf(), private val parallelism: Int = 1, private val tagLength: UInt = 64U, requestedMemorySize: UInt = 0U, private val numberOfIterations: Int = 1, - private val key: Array = emptyArray(), - private val associatedData: Array = emptyArray(), + private val key: UByteArray = ubyteArrayOf(), + private val associatedData: UByteArray = ubyteArrayOf(), private val argonType: ArgonType = ArgonType.Argon2id ) : KeyDerivationFunction { @@ -79,14 +80,14 @@ class Argon2( ): ArgonResult { val salt = SRNG.getRandomBytes(64) val argon = Argon2( - password.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), + password.encodeToByteArray().toUByteArray(), salt, parallelism, tagLength.toUInt(), memory.toUInt(), numberOfIterations, - key.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), - associatedData.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), + key.encodeToByteArray().toUByteArray(), + associatedData.encodeToByteArray().toUByteArray(), ArgonType.Argon2id ) val resultArray = argon.derive() @@ -105,14 +106,14 @@ class Argon2( associatedData: String = "", argonType: ArgonType = ArgonType.Argon2id ) : this( - password.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), - salt.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), + password.encodeToByteArray().toUByteArray(), + salt.encodeToByteArray().toUByteArray(), parallelism, tagLength, requestedMemorySize, numberOfIterations, - key.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), - associatedData.encodeToByteArray().map { it.toUByte() }.toList().toTypedArray(), + key.encodeToByteArray().toUByteArray(), + associatedData.encodeToByteArray().toUByteArray(), argonType ) @@ -168,25 +169,25 @@ class Argon2( iteration: Int, slice: Int, lane: Int, - addressBlock: Array, + addressBlock: UByteArray, addressCounter: ULong - ): Array { + ): UByteArray { //Calculate first pass val firstPass = compressionFunctionG( - Array(1024) { 0U }, - iteration.toULong().toLittleEndianTypedUByteArray() + - lane.toULong().toLittleEndianTypedUByteArray() + - slice.toULong().toLittleEndianTypedUByteArray() + - blockCount.toULong().toLittleEndianTypedUByteArray() + - numberOfIterations.toULong().toLittleEndianTypedUByteArray() + - argonType.typeId.toULong().toLittleEndianTypedUByteArray() + - addressCounter.toLittleEndianTypedUByteArray() + - Array(968) { 0U }, + UByteArray(1024) { 0U }, + iteration.toULong().toLittleEndianUByteArray() + + lane.toULong().toLittleEndianUByteArray() + + slice.toULong().toLittleEndianUByteArray() + + blockCount.toULong().toLittleEndianUByteArray() + + numberOfIterations.toULong().toLittleEndianUByteArray() + + argonType.typeId.toULong().toLittleEndianUByteArray() + + addressCounter.toLittleEndianUByteArray() + + UByteArray(968) { 0U }, addressBlock, false ) val secondPass = compressionFunctionG( - Array(1024) { 0U }, + UByteArray(1024) { 0U }, firstPass, firstPass, false @@ -200,7 +201,7 @@ class Argon2( slice: Int, lane: Int, column: Int, - addressBlock: Array? + addressBlock: UByteArray? ): Pair { val segmentIndex = (column % segmentLength) val independentIndex = segmentIndex % 128 // 128 is the number of addresses in address block @@ -298,7 +299,7 @@ class Argon2( return Pair(l, absolutePosition) } - override fun derive(): Array { + override fun derive(): UByteArray { val h0 = Blake2b.digest( parallelism.toUInt() .toLittleEndianTypedUByteArray() + tagLength.toLittleEndianTypedUByteArray() + memorySize.toLittleEndianTypedUByteArray() + @@ -309,13 +310,13 @@ class Argon2( salt.size.toUInt().toLittleEndianTypedUByteArray() + salt + key.size.toUInt().toLittleEndianTypedUByteArray() + key + associatedData.size.toUInt().toLittleEndianTypedUByteArray() + associatedData - ) + ).toUByteArray() //Compute B[i][0] for (i in 0 until parallelism) { matrix[i][0] = argonBlake2bArbitraryLenghtHash( - h0 + 0.toUInt().toLittleEndianTypedUByteArray() + i.toUInt().toLittleEndianTypedUByteArray(), + (h0 + 0.toUInt().toLittleEndianUByteArray() + i.toUInt().toLittleEndianUByteArray()).toUByteArray(), 1024U ).toUByteArray() } @@ -324,19 +325,19 @@ class Argon2( for (i in 0 until parallelism) { matrix[i][1] = argonBlake2bArbitraryLenghtHash( - h0 + 1.toUInt().toLittleEndianTypedUByteArray() + i.toUInt().toLittleEndianTypedUByteArray(), + (h0 + 1.toUInt().toLittleEndianUByteArray() + i.toUInt().toLittleEndianUByteArray()).toUByteArray(), 1024U ).toUByteArray() } executeArgonWithSingleThread() - val result = matrix.foldIndexed(emptyArray()) { lane, acc, laneArray -> + val result = matrix.foldIndexed(ubyteArrayOf()) { lane, acc, laneArray -> if (acc.size == 0) { acc + laneArray[columnCount - 1] // add last element in first lane to the accumulator } else { // For each element in our accumulator, xor it with an appropriate element from the last column in current lane (from 1 to `parallelism`) - acc.mapIndexed { index, it -> it xor laneArray[columnCount - 1][index] } - .toTypedArray() + acc.mapIndexed { index, it -> it xor laneArray[columnCount - 1][index] }.toUByteArray() + } } //Hash the xored last blocks @@ -363,12 +364,12 @@ class Argon2( val slice = segmentPosition.slice val lane = segmentPosition.lane - var addressBlock: Array? = null + var addressBlock: UByteArray? = null var addressCounter = 1UL //Starts from 1 in each segment as defined by the spec //Generate initial segment address block if (useIndependentAddressing) { - addressBlock = Array(1024) { + addressBlock = UByteArray(1024) { 0U } addressBlock = populateAddressBlock(iteration, slice, lane, addressBlock, addressCounter) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt index 3963015..72b59cd 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt @@ -33,19 +33,7 @@ object Argon2Utils { const val R3 = 16 const val R4 = 63 - private fun mixInPlace(input: UByteArray, startPosition: Int) { - var v = input.arrayChunked(8).map { it.fromLittleEndianArrayToULong() }.toTypedArray() - v = mix(v, 0, 4, 8, 12) - v = mix(v, 1, 5, 9, 13) - v = mix(v, 2, 6, 10, 14) - v = mix(v, 3, 7, 11, 15) - v = mix(v, 0, 5, 10, 15) - v = mix(v, 1, 6, 11, 12) - v = mix(v, 2, 7, 8, 13) - v = mix(v, 3, 4, 9, 14) - } - - //based on Blake2b mixRound //TODO rework so it's in place mix + //based on Blake2b mixRound private fun mixRound(input: UByteArray): Array { var v = input.arrayChunked(8).map { it.fromLittleEndianArrayToULong() }.toTypedArray() v = mix(v, 0, 4, 8, 12) @@ -130,21 +118,21 @@ object Argon2Utils { internal fun argonBlake2bArbitraryLenghtHash(input: UByteArray, length: UInt): UByteArray { if (length <= 64U) { - return Blake2b.digest(inputMessage = length + input, hashLength = length.toInt()) + return Blake2b.digest(inputMessage = length + input.toTypedArray(), hashLength = length.toInt()).toUByteArray() } //We can cast to int because UInt even if MAX_VALUE divided by 32 is guaranteed not to overflow val numberOf64ByteBlocks = (1U + ((length - 1U) / 32U) - 2U).toInt() // equivalent to ceil(length/32) - 2 - val v = Array(numberOf64ByteBlocks) { emptyArray() } - v[0] = Blake2b.digest(length + input) + val v = Array(numberOf64ByteBlocks) { ubyteArrayOf() } + v[0] = Blake2b.digest(length + input.toTypedArray()).toUByteArray() for (i in 1 until numberOf64ByteBlocks) { - v[i] = Blake2b.digest(v[i - 1]) + v[i] = Blake2b.digest(v[i - 1].toTypedArray()).toUByteArray() } val remainingPartOfInput = length.toInt() - numberOf64ByteBlocks * 32 - val vLast = Blake2b.digest(v[numberOf64ByteBlocks - 1], hashLength = remainingPartOfInput) + val vLast = Blake2b.digest(v[numberOf64ByteBlocks - 1].toTypedArray(), hashLength = remainingPartOfInput).toUByteArray() val concat = (v.map { it.copyOfRange(0, 32) }) .plus(listOf(vLast)) - .foldRight(emptyUByteArray()) { arrayOfUBytes, acc -> arrayOfUBytes + acc } + .foldRight(ubyteArrayOf()) { arrayOfUBytes, acc -> arrayOfUBytes + acc } return concat } diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCbc.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCbc.kt index 3d6d7e0..6a5fd2f 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCbc.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCbc.kt @@ -98,7 +98,7 @@ class AesCbc internal constructor(val aesKey: AesKey, val mode: Mode, initializa var currentOutput: Array = arrayOf() var previousEncrypted: Array = arrayOf() val initVector = if (initializationVector.isNullOrEmpty()) { - SRNG.getRandomBytes(16) + SRNG.getRandomBytes(16).toTypedArray() } else { initializationVector } diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCtr.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCtr.kt index 66b7423..c96508c 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCtr.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/AesCtr.kt @@ -79,7 +79,7 @@ class AesCtr internal constructor(val aesKey: AesKey, val mode: Mode, initialCou var currentOutput: Array = arrayOf() var previousEncrypted: Array = arrayOf() val counterStart = if (initialCounter.isNullOrEmpty()) { - SRNG.getRandomBytes(16) + SRNG.getRandomBytes(16).toTypedArray() } else { initialCounter } diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt index 6580f82..b6d6c73 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/util/Util.kt @@ -33,6 +33,11 @@ fun Array.hexColumsPrint(chunk : Int = 16) { printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } } +fun UByteArray.hexColumsPrint(chunk : Int = 16) { + val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk) + printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } +} + fun Array.hexColumsPrint(chunk: Int = 3) { val printout = this.map { it.toString(16) }.chunked(chunk) printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } @@ -113,6 +118,13 @@ fun UInt.toLittleEndianTypedUByteArray() : Array { } } +@ExperimentalUnsignedTypes +fun UInt.toLittleEndianUByteArray() : UByteArray { + return UByteArray (4) { + ((this shr (it * 8)) and 0xFFU).toUByte() + } +} + // UInt / Array utils @ExperimentalUnsignedTypes fun ULong.toBigEndianUByteArray() : Array { diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2Test.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2Test.kt index 477ba69..7fe4a75 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2Test.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2Test.kt @@ -34,7 +34,7 @@ class Argon2Test { @Test fun argon2dKATTest() { - val expected : Array = arrayOf( + val expected : UByteArray = ubyteArrayOf( 0x51U, 0x2BU, 0x39U, 0x1BU, 0x6FU, 0x11U, 0x62U, 0x97U, 0x53U, 0x71U, 0xD3U, 0x09U, 0x19U, 0x73U, 0x42U, 0x94U, 0xF8U, 0x68U, 0xE3U, 0xBEU, 0x39U, 0x84U, 0xF3U, 0xC1U, @@ -46,15 +46,15 @@ class Argon2Test { val iterations = 3 val parallelism = 4U val tagLength = 32U - val password: Array = arrayOf( + val password: UByteArray = ubyteArrayOf( 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U ) - val salt: Array = arrayOf(0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U) - val secret: Array = arrayOf(0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U) - val associatedData: Array = arrayOf(0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U) + val salt: UByteArray = ubyteArrayOf(0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U) + val secret: UByteArray = ubyteArrayOf(0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U) + val associatedData: UByteArray = ubyteArrayOf(0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U) val digest = Argon2( password, @@ -75,7 +75,7 @@ class Argon2Test { @Test fun argon2iKATTest() { - val expected : Array = arrayOf( + val expected : UByteArray = ubyteArrayOf( 0xc8U, 0x14U, 0xd9U, 0xd1U, 0xdcU, 0x7fU, 0x37U, 0xaaU, 0x13U, 0xf0U, 0xd7U, 0x7fU, 0x24U, 0x94U, 0xbdU, 0xa1U, 0xc8U, 0xdeU, 0x6bU, 0x01U, 0x6dU, 0xd3U, 0x88U, 0xd2U, @@ -87,15 +87,15 @@ class Argon2Test { val iterations = 3 val parallelism = 4U val tagLength = 32U - val password: Array = arrayOf( + val password: UByteArray = ubyteArrayOf( 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U ) - val salt: Array = arrayOf(0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U) - val secret: Array = arrayOf(0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U) - val associatedData: Array = arrayOf(0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U) + val salt: UByteArray = ubyteArrayOf(0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U) + val secret: UByteArray = ubyteArrayOf(0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U) + val associatedData: UByteArray = ubyteArrayOf(0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U) val digest = Argon2( password, @@ -116,7 +116,7 @@ class Argon2Test { @Test fun argon2idKATTest() { - val expected : Array = arrayOf( + val expected : UByteArray = ubyteArrayOf( 0x0dU, 0x64U, 0x0dU, 0xf5U, 0x8dU, 0x78U, 0x76U, 0x6cU, 0x08U, 0xc0U, 0x37U, 0xa3U, 0x4aU, 0x8bU, 0x53U, 0xc9U, 0xd0U, 0x1eU, 0xf0U, 0x45U, 0x2dU, 0x75U, 0xb6U, 0x5eU, @@ -128,15 +128,15 @@ class Argon2Test { val iterations = 3 val parallelism = 4U val tagLength = 32U - val password: Array = arrayOf( + val password: UByteArray = ubyteArrayOf( 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U, 0x01U ) - val salt: Array = arrayOf(0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U) - val secret: Array = arrayOf(0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U) - val associatedData: Array = arrayOf(0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U) + val salt: UByteArray = ubyteArrayOf(0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U, 0x02U) + val secret: UByteArray = ubyteArrayOf(0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U, 0x03U) + val associatedData: UByteArray = ubyteArrayOf(0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U, 0x04U) val digest = Argon2( password, diff --git a/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index 5d6330e..7bc4879 100644 --- a/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto/src/jsMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -26,7 +26,7 @@ import kotlin.browser.window actual object SRNG { var counter = 0 @ExperimentalUnsignedTypes - actual fun getRandomBytes(amount: Int): Array { + actual fun getRandomBytes(amount: Int): UByteArray { val runningOnNode = jsTypeOf(window) == "undefined" val randomBytes = if (runningOnNode) { js("require('crypto')").randomBytes(amount).toJSON().data @@ -42,6 +42,6 @@ actual object SRNG { randomArrayResult } - return randomBytes as Array + return randomBytes as UByteArray } } \ No newline at end of file diff --git a/multiplatform-crypto/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index 8bfdaa3..05b6419 100644 --- a/multiplatform-crypto/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -26,9 +26,9 @@ import java.security.SecureRandom @ExperimentalUnsignedTypes actual object SRNG { val secureRandom = SecureRandom() - actual fun getRandomBytes(amount: Int): Array { + actual fun getRandomBytes(amount: Int): UByteArray { val byteArray = ByteArray(amount) secureRandom.nextBytes(byteArray) - return byteArray.toUByteArray().toTypedArray() + return byteArray.toUByteArray() } } \ No newline at end of file diff --git a/multiplatform-crypto/src/mingwX64Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto/src/mingwX64Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index f082a4b..c9172fd 100644 --- a/multiplatform-crypto/src/mingwX64Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto/src/mingwX64Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -32,12 +32,12 @@ actual object SRNG { } @Suppress("EXPERIMENTAL_UNSIGNED_LITERALS") - actual fun getRandomBytes(amount: Int): Array { + actual fun getRandomBytes(amount: Int): UByteArray { memScoped { val randArray = allocArray(amount) val pointer = randArray.getPointer(this) val status = advapiRandom(pointer.reinterpret(), amount.convert()) - return Array(amount) { pointer[it].toUByte() } + return UByteArray(amount) { pointer[it].toUByte() } } } } \ No newline at end of file diff --git a/multiplatform-crypto/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index ea8b0fc..e76cf80 100644 --- a/multiplatform-crypto/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -26,14 +26,14 @@ import platform.posix.* */ actual object SRNG { @Suppress("EXPERIMENTAL_UNSIGNED_LITERALS") - actual fun getRandomBytes(amount: Int): Array { + actual fun getRandomBytes(amount: Int): UByteArray { memScoped { val array = allocArray(amount) val urandomFile = fopen("/dev/urandom", "rb") if (urandomFile != null) { fread(array, 1, amount.convert(), urandomFile) } - return Array(amount) { + return UByteArray(amount) { array[it] } }