From 7e78bf6fa56c62cb372fffb6d26158bdf04c246b Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Thu, 21 May 2020 23:23:48 +0200 Subject: [PATCH] Argon 2 working with new matrix structure --- .../crypto/keyderivation/argon2/Argon2.kt | 17 +++++++---------- .../crypto/hash/argon/Argon2MatrixTest.kt | 14 ++++---------- 2 files changed, 11 insertions(+), 20 deletions(-) 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 0b053f0..85d92cb 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 @@ -26,6 +26,7 @@ import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bAr import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.validateArgonParameters import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToUInt +import com.ionspin.kotlin.crypto.util.hexColumsPrint import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray import com.ionspin.kotlin.crypto.util.xor @@ -202,6 +203,7 @@ class Argon2( } val first32Bit = matrix.sliceArray(previousBlockStart until previousBlockStart + 4).fromLittleEndianArrayToUInt() val second32Bit = matrix.sliceArray(previousBlockStart + 4 until previousBlockStart + 8).fromLittleEndianArrayToUInt() + Pair(first32Bit, second32Bit) } ArgonType.Argon2i -> { @@ -324,20 +326,14 @@ class Argon2( //Run all iterations over all lanes and all segments executeArgonWithSingleThread() -// 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] }.toUByteArray() -// -// } -// } + //Temporary fold val acc = matrix.getBlockAt(0, columnCount - 1).copyOf() for (i in 1 until parallelism) { - acc.xor(matrix.getBlockAt(i, columnCount -1)) + acc.hexColumsPrint(1024) + (acc xor matrix.getBlockAt(i, columnCount -1)).copyInto(acc) } + acc.hexColumsPrint(1024) //Hash the xored last blocks val hash = argonBlake2bArbitraryLenghtHash(acc, tagLength) matrix.clearMatrix() @@ -400,6 +396,7 @@ class Argon2( column, addressBlock ) + matrix.setBlockAt(lane, column, compressionFunctionG( matrix.getBlockAt(lane, previousColumn), diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt index 9c156e9..b8af5a4 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/hash/argon/Argon2MatrixTest.kt @@ -68,17 +68,11 @@ class Argon2MatrixTest { fun blockRetrievalTest() { val argon2Matrix = Argon2Matrix(2, 2) (zeroesBlock + onesBlock + twosBlock + threesBlock).copyInto(argon2Matrix.storage) - println(argon2Matrix[0, 0, 0]) - println(argon2Matrix[1, 0, 0]) - println(argon2Matrix[2, 0, 0]) - println(argon2Matrix[3, 0, 0]) - argon2Matrix.storage.hexColumsPrint(1024) assertTrue { - argon2Matrix[0, 0, 0] == 0U.toUByte() && - argon2Matrix[1, 0, 0] == 1U.toUByte() && - argon2Matrix[2, 0, 0] == 2U.toUByte() && - argon2Matrix[3, 0, 0] == 3U.toUByte() - + zeroesBlock.contentEquals(argon2Matrix.getBlockAt(0, 0)) && + onesBlock.contentEquals(argon2Matrix.getBlockAt(0, 1)) && + twosBlock.contentEquals(argon2Matrix.getBlockAt(1, 0)) && + threesBlock.contentEquals(argon2Matrix.getBlockAt(1, 1)) } } } \ No newline at end of file