From 1904e2b9f2c8667f4f627db84e83d4624bcaf998 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Fri, 15 May 2020 23:50:18 +0200 Subject: [PATCH] Working for all segments 2d variant --- .../kotlin/crypto/keyderivation/Argon2.kt | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 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 e5a7948..d20f246 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 @@ -98,12 +98,12 @@ class Argon2 internal constructor( fun compressionFunctionG( - x: Array, - y: Array, + previousBlock: Array, + referenceBlock: Array, currentBlock: Array, xorWithCurrentBlock: Boolean ): Array { - val r = x xor y + val r = referenceBlock xor previousBlock // println("R = X xor Y") // r.hexColumsPrint(16) // val r = Array(1024) { 0U } // view as 8x8 matrix of 16 byte registers @@ -138,7 +138,7 @@ class Argon2 internal constructor( // z.hexColumsPrint(16) val final = if (xorWithCurrentBlock) { // println("Z xor R xor CURRENT") - (z xor r) xor ((x xor y) xor currentBlock) + (z xor r) xor currentBlock } else { // println("Z xor R") z xor r @@ -329,10 +329,11 @@ class Argon2 internal constructor( // blocks in the last SL - 1 = 3 segments computed and finished in // lane l. If B[i][j] is the first block of a segment, then the // very last index from W is excluded. + val segmentIndex = column - (slice * (columnCount / 4)) val referenceAreaSize = if (iteration == 0) { if (slice == 0) { //All indices except the previous - column - 1 + (column % (columnCount / 4)) - 1 } else { if (lane == l) { //Same lane @@ -347,9 +348,9 @@ class Argon2 internal constructor( } } else { if (lane == l) { - columnCount - (columnCount / 4) + column - 1 + columnCount - (columnCount / 4) + (column % (columnCount / 4) - 1) } else { - columnCount - (columnCount / 4) + if (column == 0) { + columnCount - (columnCount / 4) + if (column % (columnCount / 4) == 0) { -1 } else { 0 @@ -575,16 +576,27 @@ class Argon2 internal constructor( } } } else { - val (l, z) = computeIndexNew(matrix, lane, 0, columnCount, parallelism.toInt(), iteration, slice, type) - matrix[lane][0] = compressionFunctionG(matrix[lane][columnCount - 1], matrix[l][z], matrix[lane][0], true) - for (column in 1..(slice * segmentLength)) { - val (l, z) = computeIndexNew(matrix, lane, column, columnCount, parallelism.toInt(), iteration, slice, type) - println("Calling compress for I: $iteration S: $slice Lane: $lane Column: $column with l: $l z: $z") - matrix[lane][column] = - compressionFunctionG(matrix[lane][column - 1], matrix[l][z], matrix[lane][column], true) + if (slice == 0) { + val (l, z) = computeIndexNew(matrix, lane, 0, columnCount, parallelism.toInt(), iteration, slice, type) + matrix[lane][0] = compressionFunctionG(matrix[lane][columnCount - 1], matrix[l][z], matrix[lane][0], true) + for (column in 1 until segmentLength) { + val (l, z) = computeIndexNew(matrix, lane, column, columnCount, parallelism.toInt(), iteration, slice, type) + println("Calling compress for I: $iteration S: $slice Lane: $lane Column: $column with l: $l z: $z") + matrix[lane][column] = + compressionFunctionG(matrix[lane][column - 1], matrix[l][z], matrix[lane][column], true) // matrix[lane][column].hexColumsPrint(16) + } + } else { + for (column in slice * segmentLength until (slice + 1) * segmentLength) { + val (l, z) = computeIndexNew(matrix, lane, column, columnCount, parallelism.toInt(), iteration, slice, type) + println("Calling compress for I: $iteration S: $slice Lane: $lane Column: $column with l: $l z: $z") + matrix[lane][column] = + compressionFunctionG(matrix[lane][column - 1], matrix[l][z], matrix[lane][column], true) +// matrix[lane][column].hexColumsPrint(16) + } } + }