Argon 2 working with new matrix structure

This commit is contained in:
Ugljesa Jovanovic 2020-05-21 23:23:48 +02:00 committed by Ugljesa Jovanovic
parent 13b60a5eee
commit 7e78bf6fa5
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
2 changed files with 11 additions and 20 deletions

View File

@ -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.compressionFunctionG
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.validateArgonParameters import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.validateArgonParameters
import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToUInt 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.toLittleEndianUByteArray
import com.ionspin.kotlin.crypto.util.xor import com.ionspin.kotlin.crypto.util.xor
@ -202,6 +203,7 @@ class Argon2(
} }
val first32Bit = matrix.sliceArray(previousBlockStart until previousBlockStart + 4).fromLittleEndianArrayToUInt() val first32Bit = matrix.sliceArray(previousBlockStart until previousBlockStart + 4).fromLittleEndianArrayToUInt()
val second32Bit = matrix.sliceArray(previousBlockStart + 4 until previousBlockStart + 8).fromLittleEndianArrayToUInt() val second32Bit = matrix.sliceArray(previousBlockStart + 4 until previousBlockStart + 8).fromLittleEndianArrayToUInt()
Pair(first32Bit, second32Bit) Pair(first32Bit, second32Bit)
} }
ArgonType.Argon2i -> { ArgonType.Argon2i -> {
@ -324,20 +326,14 @@ class Argon2(
//Run all iterations over all lanes and all segments //Run all iterations over all lanes and all segments
executeArgonWithSingleThread() 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 //Temporary fold
val acc = matrix.getBlockAt(0, columnCount - 1).copyOf() val acc = matrix.getBlockAt(0, columnCount - 1).copyOf()
for (i in 1 until parallelism) { 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 //Hash the xored last blocks
val hash = argonBlake2bArbitraryLenghtHash(acc, tagLength) val hash = argonBlake2bArbitraryLenghtHash(acc, tagLength)
matrix.clearMatrix() matrix.clearMatrix()
@ -400,6 +396,7 @@ class Argon2(
column, column,
addressBlock addressBlock
) )
matrix.setBlockAt(lane, column, matrix.setBlockAt(lane, column,
compressionFunctionG( compressionFunctionG(
matrix.getBlockAt(lane, previousColumn), matrix.getBlockAt(lane, previousColumn),

View File

@ -68,17 +68,11 @@ class Argon2MatrixTest {
fun blockRetrievalTest() { fun blockRetrievalTest() {
val argon2Matrix = Argon2Matrix(2, 2) val argon2Matrix = Argon2Matrix(2, 2)
(zeroesBlock + onesBlock + twosBlock + threesBlock).copyInto(argon2Matrix.storage) (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 { assertTrue {
argon2Matrix[0, 0, 0] == 0U.toUByte() && zeroesBlock.contentEquals(argon2Matrix.getBlockAt(0, 0)) &&
argon2Matrix[1, 0, 0] == 1U.toUByte() && onesBlock.contentEquals(argon2Matrix.getBlockAt(0, 1)) &&
argon2Matrix[2, 0, 0] == 2U.toUByte() && twosBlock.contentEquals(argon2Matrix.getBlockAt(1, 0)) &&
argon2Matrix[3, 0, 0] == 3U.toUByte() threesBlock.contentEquals(argon2Matrix.getBlockAt(1, 1))
} }
} }
} }