From 3519d2240fc01d26567ca021cb43be068a88befa Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Thu, 14 May 2020 22:48:19 +0200 Subject: [PATCH] Fixed row mixing --- .../kotlin/crypto/keyderivation/Argon2.kt | 16 +++++++++------- .../com/ionspin/kotlin/crypto/util/Util.kt | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/Argon2.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/Argon2.kt index a99a6cb..8c943f7 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/Argon2.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/Argon2.kt @@ -113,13 +113,9 @@ class Argon2 internal constructor( val z = Array(1024) { 0U } // Do the argon/blake2b mixing on rows for (i in 0..7) { - println("Q round $i") - q.hexColumsPrint(16) val startOfRow = (i * 8 * 16) val endOfRow = startOfRow + (8 * 16) val rowToMix = r.copyOfRange(startOfRow, endOfRow) - println("Mixing row:") - rowToMix.hexColumsPrint(16) mixRound(rowToMix) .map { it.toLittleEndianUByteArray() } .flatMap { it.asIterable() } @@ -130,6 +126,7 @@ class Argon2 internal constructor( q.hexColumsPrint(16) // Do the argon/blake2b mixing on columns for (i in 0..7) { + println("Z round ${i}") copyIntoGBlockColumn( z, i, @@ -142,7 +139,7 @@ class Argon2 internal constructor( println("---- Z -----") z.hexColumsPrint(16) val final = if (xorWithCurrentBlock) { - println("Z xor R xoe CURRENT") + println("Z xor R xor CURRENT") (z xor r) xor ((x xor y) xor currentBlock) } else { println("Z xor R") @@ -156,14 +153,18 @@ class Argon2 internal constructor( private fun extractColumnFromGBlock(gBlock: Array, columnPosition: Int): Array { val result = Array(128) { 0U } for (i in 0..7) { - result[i] = gBlock[i * 8 + columnPosition] + gBlock.copyOfRange(i * 128 + (columnPosition * 16), i * 128 + (columnPosition * 16) + 16).copyInto(result, i * 16) } return result } private fun copyIntoGBlockColumn(gBlock: Array, columnPosition: Int, columnData: Array) { for (i in 0..7) { - gBlock[i * 8 + columnPosition] = columnData[i] + println("Mixed column data ${i}") + val column = columnData.copyOfRange(i * 16, i * 16 + 16) + column.hexColumsPrint(16) + column.copyInto(gBlock, i * 128 + columnPosition * 16) +// gBlock[i * 8 + columnPosition] = columnData[i] } } @@ -180,6 +181,7 @@ class Argon2 internal constructor( v = mix(v, 1, 6, 11, 12) v = mix(v, 2, 7, 8, 13) v = mix(v, 3, 4, 9, 14) + v.hexColumsPrint(2) return v } 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 5c148ef..129eef0 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 @@ -31,8 +31,8 @@ fun Array.hexColumsPrint(chunk : Int = 16) { printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } } -fun Array.hexColumsPrint() { - val printout = this.map { it.toString(16) }.chunked(3) +fun Array.hexColumsPrint(chunk: Int = 3) { + val printout = this.map { it.toString(16) }.chunked(chunk) printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) } }