From c86f60135a69a3aa84b59a90cd824670a9e832a5 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Tue, 10 Sep 2019 20:48:57 +0200 Subject: [PATCH] Add shiftRows --- .../ionspin/kotlin/crypto/symmetric/Aes.kt | 7 ++++++ .../kotlin/crypto/symmetric/AesTest.kt | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/Aes.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/Aes.kt index c11ff7a..883efda 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/Aes.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/symmetric/Aes.kt @@ -60,6 +60,13 @@ class Aes { } } + fun shiftRows() { + stateMatrix[0] = arrayOf(stateMatrix[0][0], stateMatrix[0][1], stateMatrix[0][2], stateMatrix[0][3]) + stateMatrix[1] = arrayOf(stateMatrix[1][1], stateMatrix[1][2], stateMatrix[1][3], stateMatrix[1][0]) + stateMatrix[2] = arrayOf(stateMatrix[2][2], stateMatrix[2][3], stateMatrix[2][0], stateMatrix[2][1]) + stateMatrix[3] = arrayOf(stateMatrix[3][3], stateMatrix[3][0], stateMatrix[3][1], stateMatrix[3][2]) + } + fun expandKey(key: AesKey) { } diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/symmetric/AesTest.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/symmetric/AesTest.kt index 8e6a06c..54bab3b 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/symmetric/AesTest.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/symmetric/AesTest.kt @@ -27,4 +27,29 @@ class AesTest { aes.stateMatrix[0][0] == 0xEDU.toUByte() } } + + @Test + fun testShiftRows() { + val fakeState = arrayOf( + ubyteArrayOf(0U, 1U, 2U, 3U).toTypedArray(), + ubyteArrayOf(0U, 1U, 2U, 3U).toTypedArray(), + ubyteArrayOf(0U, 1U, 2U, 3U).toTypedArray(), + ubyteArrayOf(0U, 1U, 2U, 3U).toTypedArray() + ) + val expectedState = arrayOf( + ubyteArrayOf(0U, 1U, 2U, 3U).toTypedArray(), + ubyteArrayOf(1U, 2U, 3U, 0U).toTypedArray(), + ubyteArrayOf(2U, 3U, 0U, 1U).toTypedArray(), + ubyteArrayOf(3U, 0U, 1U, 2U).toTypedArray() + ) + val aes = Aes() + fakeState.copyInto(aes.stateMatrix) + aes.shiftRows() + aes.stateMatrix.forEach{ + println(it.joinToString { it.toString(16) }) + } + assertTrue { + aes.stateMatrix.contentDeepEquals(expectedState) + } + } } \ No newline at end of file