Removing direct aes cbc and ctr, to reduce foot shooting incidents, introducing aes256-gcm
This commit is contained in:
1 parent
0b30215143
commit
5c10d3abf4
15 files changed
+136
-923
No files matched your search
+18
@@ -0,0 +1,18 @@
|
||||
package com.ionspin.kotlin.crypto.authenticated
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 14-Jun-2020
|
||||
*/
|
||||
class Aes256GcmStatelessPure : Aes256GcmStateless {
|
||||
/**
|
||||
* Nonce autogenerated
|
||||
*/
|
||||
override fun encrypt(message: UByteArray, additionalData: UByteArray, rawData : UByteArray, key:) : Aes256GcmEncryptionResult {
|
||||
|
||||
TODO()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.ionspin.kotlin.crypto.symmetric
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 13-Jun-2020
|
||||
*/
|
||||
internal sealed class InternalAesKey(val key: String, val keyLength: Int) {
|
||||
val keyArray: UByteArray = key.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
val numberOf32BitWords = keyLength / 32
|
||||
|
||||
class Aes128Key(key: String) : InternalAesKey(key, 128)
|
||||
class Aes192Key(key: String) : InternalAesKey(key, 192)
|
||||
class Aes256Key(key: String) : InternalAesKey(key, 256)
|
||||
|
||||
init {
|
||||
checkKeyLength(key, keyLength)
|
||||
}
|
||||
|
||||
fun checkKeyLength(key: String, expectedLength: Int) {
|
||||
if ((key.length / 2) != expectedLength / 8) {
|
||||
throw RuntimeException("Invalid key length")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-9
@@ -17,7 +17,6 @@
|
||||
package com.ionspin.kotlin.crypto.symmetric
|
||||
|
||||
import com.ionspin.kotlin.crypto.SRNG
|
||||
import com.ionspin.kotlin.crypto.symmetric.aes.AesKey
|
||||
import com.ionspin.kotlin.crypto.util.xor
|
||||
|
||||
/**
|
||||
@@ -32,7 +31,7 @@ import com.ionspin.kotlin.crypto.util.xor
|
||||
* on 21-Sep-2019
|
||||
*/
|
||||
|
||||
class AesCbcPure internal constructor(val aesKey: AesKey, val mode: Mode, initializationVector: UByteArray? = null) {
|
||||
internal class AesCbcPure internal constructor(val aesKey: InternalAesKey, val mode: Mode, initializationVector: UByteArray? = null) {
|
||||
|
||||
companion object {
|
||||
const val BLOCK_BYTES = 16
|
||||
@@ -40,21 +39,21 @@ class AesCbcPure internal constructor(val aesKey: AesKey, val mode: Mode, initia
|
||||
* Creates and returns AesCbc instance that can be fed data using [addData]. Once you have submitted all
|
||||
* data call [encrypt]
|
||||
*/
|
||||
fun createEncryptor(aesKey: AesKey) : AesCbcPure {
|
||||
fun createEncryptor(aesKey: InternalAesKey) : AesCbcPure {
|
||||
return AesCbcPure(aesKey, Mode.ENCRYPT)
|
||||
}
|
||||
/**
|
||||
* Creates and returns AesCbc instance that can be fed data using [addData]. Once you have submitted all
|
||||
* data call [decrypt]
|
||||
*/
|
||||
fun createDecryptor(aesKey : AesKey) : AesCbcPure {
|
||||
fun createDecryptor(aesKey : InternalAesKey) : AesCbcPure {
|
||||
return AesCbcPure(aesKey, Mode.DECRYPT)
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk encryption, returns encrypted data and a random initialization vector
|
||||
*/
|
||||
fun encrypt(aesKey: AesKey, data: UByteArray): EncryptedDataAndInitializationVector {
|
||||
fun encrypt(aesKey: InternalAesKey, data: UByteArray): EncryptedDataAndInitializationVector {
|
||||
val aesCbc = AesCbcPure(aesKey, Mode.ENCRYPT)
|
||||
aesCbc.addData(data)
|
||||
return aesCbc.encrypt()
|
||||
@@ -63,7 +62,7 @@ class AesCbcPure internal constructor(val aesKey: AesKey, val mode: Mode, initia
|
||||
/**
|
||||
* Bulk decryption, returns decrypted data
|
||||
*/
|
||||
fun decrypt(aesKey: AesKey, data: UByteArray, initialCounter: UByteArray? = null): UByteArray {
|
||||
fun decrypt(aesKey: InternalAesKey, data: UByteArray, initialCounter: UByteArray? = null): UByteArray {
|
||||
val aesCbc = AesCbcPure(aesKey, Mode.DECRYPT, initialCounter)
|
||||
aesCbc.addData(data)
|
||||
return aesCbc.decrypt()
|
||||
@@ -221,7 +220,7 @@ class AesCbcPure internal constructor(val aesKey: AesKey, val mode: Mode, initia
|
||||
}
|
||||
|
||||
|
||||
data class EncryptedDataAndInitializationVector(val encryptedData : UByteArray, val initilizationVector : UByteArray) {
|
||||
data class EncryptedDataAndInitializationVector(val encryptedData : UByteArray, val initializationVector : UByteArray) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
@@ -229,14 +228,14 @@ data class EncryptedDataAndInitializationVector(val encryptedData : UByteArray,
|
||||
other as EncryptedDataAndInitializationVector
|
||||
|
||||
if (!encryptedData.contentEquals(other.encryptedData)) return false
|
||||
if (!initilizationVector.contentEquals(other.initilizationVector)) return false
|
||||
if (!initializationVector.contentEquals(other.initializationVector)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = encryptedData.contentHashCode()
|
||||
result = 31 * result + initilizationVector.contentHashCode()
|
||||
result = 31 * result + initializationVector.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
+24
-7
@@ -21,8 +21,6 @@ import com.ionspin.kotlin.bignum.integer.BigInteger
|
||||
import com.ionspin.kotlin.bignum.modular.ModularBigInteger
|
||||
import com.ionspin.kotlin.crypto.SRNG
|
||||
import com.ionspin.kotlin.crypto.symmetric.AesCtrPure.Companion.encrypt
|
||||
import com.ionspin.kotlin.crypto.symmetric.aes.AesKey
|
||||
import com.ionspin.kotlin.crypto.symmetric.aes.EncryptedDataAndInitialCounter
|
||||
import com.ionspin.kotlin.crypto.util.xor
|
||||
|
||||
/**
|
||||
@@ -38,7 +36,7 @@ import com.ionspin.kotlin.crypto.util.xor
|
||||
* on 22-Sep-2019
|
||||
*/
|
||||
|
||||
class AesCtrPure internal constructor(val aesKey: AesKey, val mode: Mode, initialCounter: UByteArray? = null) {
|
||||
internal class AesCtrPure internal constructor(val aesKey: InternalAesKey, val mode: Mode, initialCounter: UByteArray? = null) {
|
||||
|
||||
companion object {
|
||||
const val BLOCK_BYTES = 16
|
||||
@@ -48,20 +46,20 @@ class AesCtrPure internal constructor(val aesKey: AesKey, val mode: Mode, initia
|
||||
* Creates and returns AesCtr instance that can be fed data using [addData]. Once you have submitted all
|
||||
* data call [encrypt]
|
||||
*/
|
||||
fun createEncryptor(aesKey: AesKey) : AesCtrPure {
|
||||
fun createEncryptor(aesKey: InternalAesKey) : AesCtrPure {
|
||||
return AesCtrPure(aesKey, Mode.ENCRYPT)
|
||||
}
|
||||
/**
|
||||
* Creates and returns AesCtr instance that can be fed data using [addData]. Once you have submitted all
|
||||
* data call [decrypt]
|
||||
*/
|
||||
fun createDecryptor(aesKey : AesKey) : AesCtrPure {
|
||||
fun createDecryptor(aesKey : InternalAesKey) : AesCtrPure {
|
||||
return AesCtrPure(aesKey, Mode.DECRYPT)
|
||||
}
|
||||
/**
|
||||
* Bulk encryption, returns encrypted data and a random initial counter
|
||||
*/
|
||||
fun encrypt(aesKey: AesKey, data: UByteArray): EncryptedDataAndInitialCounter {
|
||||
fun encrypt(aesKey: InternalAesKey, data: UByteArray): EncryptedDataAndInitialCounter {
|
||||
val aesCtr = AesCtrPure(aesKey, Mode.ENCRYPT)
|
||||
aesCtr.addData(data)
|
||||
return aesCtr.encrypt()
|
||||
@@ -69,7 +67,7 @@ class AesCtrPure internal constructor(val aesKey: AesKey, val mode: Mode, initia
|
||||
/**
|
||||
* Bulk decryption, returns decrypted data
|
||||
*/
|
||||
fun decrypt(aesKey: AesKey, data: UByteArray, initialCounter: UByteArray? = null): UByteArray {
|
||||
fun decrypt(aesKey: InternalAesKey, data: UByteArray, initialCounter: UByteArray? = null): UByteArray {
|
||||
val aesCtr = AesCtrPure(aesKey, Mode.DECRYPT, initialCounter)
|
||||
aesCtr.addData(data)
|
||||
return aesCtr.decrypt()
|
||||
@@ -188,4 +186,23 @@ class AesCtrPure internal constructor(val aesKey: AesKey, val mode: Mode, initia
|
||||
|
||||
}
|
||||
|
||||
data class EncryptedDataAndInitialCounter(val encryptedData : UByteArray, val initialCounter : UByteArray) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
|
||||
other as EncryptedDataAndInitialCounter
|
||||
|
||||
if (!encryptedData.contentEquals(other.encryptedData)) return false
|
||||
if (!initialCounter.contentEquals(other.initialCounter)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = encryptedData.contentHashCode()
|
||||
result = 31 * result + initialCounter.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -6,7 +6,7 @@ import com.ionspin.kotlin.crypto.util.flattenToUByteArray
|
||||
* Created by Ugljesa Jovanovic (jovanovic.ugljesa@gmail.com) on 07/Sep/2019
|
||||
*/
|
||||
|
||||
internal class AesPure internal constructor(val aesKey: AesKey, val input: UByteArray) {
|
||||
internal class AesPure internal constructor(val aesKey: InternalAesKey, val input: UByteArray) {
|
||||
companion object {
|
||||
private val debug = false
|
||||
|
||||
@@ -56,11 +56,11 @@ internal class AesPure internal constructor(val aesKey: AesKey, val input: UByte
|
||||
|
||||
val rcon: UByteArray = ubyteArrayOf(0x8DU, 0x01U, 0x02U, 0x04U, 0x08U, 0x10U, 0x20U, 0x40U, 0x80U, 0x1BU, 0x36U)
|
||||
|
||||
fun encrypt(aesKey: AesKey, input: UByteArray): UByteArray {
|
||||
fun encrypt(aesKey: InternalAesKey, input: UByteArray): UByteArray {
|
||||
return AesPure(aesKey, input).encrypt()
|
||||
}
|
||||
|
||||
fun decrypt(aesKey: AesKey, input: UByteArray): UByteArray {
|
||||
fun decrypt(aesKey: InternalAesKey, input: UByteArray): UByteArray {
|
||||
return AesPure(aesKey, input).decrypt()
|
||||
}
|
||||
|
||||
@@ -71,9 +71,9 @@ internal class AesPure internal constructor(val aesKey: AesKey, val input: UByte
|
||||
}.toTypedArray()
|
||||
|
||||
val numberOfRounds = when (aesKey) {
|
||||
is AesKey.Aes128Key -> 10
|
||||
is AesKey.Aes192Key -> 12
|
||||
is AesKey.Aes256Key -> 14
|
||||
is InternalAesKey.Aes128Key -> 10
|
||||
is InternalAesKey.Aes192Key -> 12
|
||||
is InternalAesKey.Aes256Key -> 14
|
||||
}
|
||||
|
||||
val expandedKey: Array<UByteArray> = expandKey()
|
||||
@@ -235,7 +235,7 @@ internal class AesPure internal constructor(val aesKey: AesKey, val input: UByte
|
||||
|
||||
temp[0] = temp[0] xor rcon[i / aesKey.numberOf32BitWords]
|
||||
|
||||
} else if (aesKey is AesKey.Aes256Key && i % aesKey.numberOf32BitWords == 4) {
|
||||
} else if (aesKey is InternalAesKey.Aes256Key && i % aesKey.numberOf32BitWords == 4) {
|
||||
temp[0] = getSBoxValue(temp[0])
|
||||
temp[1] = getSBoxValue(temp[1])
|
||||
temp[2] = getSBoxValue(temp[2])
|
||||
|
||||
+4
-4
@@ -37,7 +37,7 @@ class AesCbcTest {
|
||||
val plaintext = "3c888bbbb1a8eb9f3e9b87acaad986c466e2f7071c83083b8a557971918850e5"
|
||||
val expectedCipherText = "479c89ec14bc98994e62b2c705b5014e175bd7832e7e60a1e92aac568a861eb7"
|
||||
val aesCbc =
|
||||
AesCbcPure(AesKey.Aes128Key(key), mode = Mode.ENCRYPT, initializationVector = iv.hexStringToUByteArray())
|
||||
AesCbcPure(InternalAesKey.Aes128Key(key), mode = Mode.ENCRYPT, initializationVector = iv.hexStringToUByteArray())
|
||||
aesCbc.addData(plaintext.hexStringToUByteArray())
|
||||
val encrypted = aesCbc.encrypt()
|
||||
println("Encrypted: ${encrypted.encryptedData.toHexString()}")
|
||||
@@ -54,7 +54,7 @@ class AesCbcTest {
|
||||
fun testEncryptionApi() {
|
||||
assertTrue {
|
||||
val keyString = "4278b840fb44aaa757c1bf04acbe1a3e"
|
||||
val key = AesKey.Aes128Key(keyString)
|
||||
val key = InternalAesKey.Aes128Key(keyString)
|
||||
|
||||
val plainText = "3c888bbbb1a8eb9f3e9b87acaad986c466e2f7071c83083b8a557971918850e5"
|
||||
|
||||
@@ -76,7 +76,7 @@ class AesCbcTest {
|
||||
val cipherText = "479c89ec14bc98994e62b2c705b5014e175bd7832e7e60a1e92aac568a861eb7"
|
||||
val expectedPlainText = "3c888bbbb1a8eb9f3e9b87acaad986c466e2f7071c83083b8a557971918850e5"
|
||||
val aesCbc =
|
||||
AesCbcPure(AesKey.Aes128Key(key), mode = Mode.DECRYPT, initializationVector = iv.hexStringToUByteArray())
|
||||
AesCbcPure(InternalAesKey.Aes128Key(key), mode = Mode.DECRYPT, initializationVector = iv.hexStringToUByteArray())
|
||||
aesCbc.addData(cipherText.hexStringToUByteArray())
|
||||
val decrypted = aesCbc.decrypt()
|
||||
println("Decrypted: ${decrypted.toHexString()}")
|
||||
@@ -95,7 +95,7 @@ class AesCbcTest {
|
||||
val iv = "57f02a5c5339daeb0a2908a06ac6393f"
|
||||
val cipherText = "479c89ec14bc98994e62b2c705b5014e175bd7832e7e60a1e92aac568a861eb7"
|
||||
val expectedPlainText = "3c888bbbb1a8eb9f3e9b87acaad986c466e2f7071c83083b8a557971918850e5"
|
||||
val decrypted = AesCbcPure.decrypt(AesKey.Aes128Key(key), cipherText.hexStringToUByteArray(), iv.hexStringToUByteArray())
|
||||
val decrypted = AesCbcPure.decrypt(InternalAesKey.Aes128Key(key), cipherText.hexStringToUByteArray(), iv.hexStringToUByteArray())
|
||||
println("Decrypted: ${decrypted.toHexString()}")
|
||||
|
||||
expectedPlainText == decrypted.toHexString()
|
||||
|
||||
+4
-4
@@ -38,7 +38,7 @@ class AesCtrTest {
|
||||
"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"
|
||||
val expectedCipherText =
|
||||
"874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"
|
||||
val aesCtr = AesCtrPure(AesKey.Aes128Key(key), mode = Mode.ENCRYPT, initialCounter = ic.hexStringToUByteArray())
|
||||
val aesCtr = AesCtrPure(InternalAesKey.Aes128Key(key), mode = Mode.ENCRYPT, initialCounter = ic.hexStringToUByteArray())
|
||||
aesCtr.addData(
|
||||
plaintext.hexStringToUByteArray()
|
||||
)
|
||||
@@ -54,7 +54,7 @@ class AesCtrTest {
|
||||
@Test
|
||||
fun testEncryptionApi() {
|
||||
val keyString = "4278b840fb44aaa757c1bf04acbe1a3e"
|
||||
val key = AesKey.Aes128Key(keyString)
|
||||
val key = InternalAesKey.Aes128Key(keyString)
|
||||
val plainText = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"
|
||||
|
||||
val encryptedDataAndInitializationVector = AesCtrPure.encrypt(key, plainText.hexStringToUByteArray())
|
||||
@@ -77,7 +77,7 @@ class AesCtrTest {
|
||||
"874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"
|
||||
val expectedPlainText =
|
||||
"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"
|
||||
val aesCtr = AesCtrPure(AesKey.Aes128Key(key), mode = Mode.DECRYPT, initialCounter = ic.hexStringToUByteArray())
|
||||
val aesCtr = AesCtrPure(InternalAesKey.Aes128Key(key), mode = Mode.DECRYPT, initialCounter = ic.hexStringToUByteArray())
|
||||
aesCtr.addData(cipherText.hexStringToUByteArray())
|
||||
val decrypted = aesCtr.decrypt()
|
||||
println("Decrypted: ${decrypted.toHexString()}")
|
||||
@@ -97,7 +97,7 @@ class AesCtrTest {
|
||||
"874d6191b620e3261bef6864990db6ce9806f66b7970fdff8617187bb9fffdff5ae4df3edbd5d35e5b4f09020db03eab1e031dda2fbe03d1792170a0f3009cee"
|
||||
val expectedPlainText =
|
||||
"6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"
|
||||
val decrypted = AesCtrPure.decrypt(AesKey.Aes128Key(key), cipherText.hexStringToUByteArray(), ic.hexStringToUByteArray())
|
||||
val decrypted = AesCtrPure.decrypt(InternalAesKey.Aes128Key(key), cipherText.hexStringToUByteArray(), ic.hexStringToUByteArray())
|
||||
println("Decrypted: ${decrypted.toHexString()}")
|
||||
expectedPlainText == decrypted.toHexString()
|
||||
}
|
||||
|
||||
+19
-19
@@ -20,7 +20,7 @@ class AesTest {
|
||||
ubyteArrayOf(0U, 0U, 0U, 0U),
|
||||
ubyteArrayOf(0U, 0U, 0U, 0U)
|
||||
)
|
||||
val aes = AesPure(AesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
fakeState.copyInto(aes.state)
|
||||
aes.subBytes()
|
||||
assertTrue {
|
||||
@@ -42,7 +42,7 @@ class AesTest {
|
||||
ubyteArrayOf(2U, 3U, 0U, 1U),
|
||||
ubyteArrayOf(3U, 0U, 1U, 2U)
|
||||
)
|
||||
val aes = AesPure(AesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
fakeState.copyInto(aes.state)
|
||||
aes.shiftRows()
|
||||
assertTrue {
|
||||
@@ -56,7 +56,7 @@ class AesTest {
|
||||
assertTrue {
|
||||
val a = 0x57U
|
||||
val b = 0x83U
|
||||
val aes = AesPure(AesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val c = aes.galoisFieldMultiply(a.toUByte(), b.toUByte())
|
||||
c == 0xC1U.toUByte()
|
||||
}
|
||||
@@ -64,7 +64,7 @@ class AesTest {
|
||||
assertTrue {
|
||||
val a = 0x57U
|
||||
val b = 0x13U
|
||||
val aes = AesPure(AesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val c = aes.galoisFieldMultiply(a.toUByte(), b.toUByte())
|
||||
c == 0xFEU.toUByte()
|
||||
}
|
||||
@@ -89,7 +89,7 @@ class AesTest {
|
||||
ubyteArrayOf(0xbcU, 0x9dU, 0x01U, 0xc6U)
|
||||
)
|
||||
|
||||
val aes = AesPure(AesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(irrelevantKey), irrelevantInput)
|
||||
fakeState.copyInto(aes.state)
|
||||
aes.mixColumns()
|
||||
assertTrue {
|
||||
@@ -116,7 +116,7 @@ class AesTest {
|
||||
).toTypedArray()
|
||||
|
||||
|
||||
val aes = AesPure(AesKey.Aes128Key(key), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), irrelevantInput)
|
||||
val result = aes.expandedKey.map {
|
||||
it.foldIndexed(0U) { index, acc, uByte ->
|
||||
acc + (uByte.toUInt() shl (24 - index * 8))
|
||||
@@ -140,7 +140,7 @@ class AesTest {
|
||||
).toTypedArray()
|
||||
|
||||
|
||||
val aes = AesPure(AesKey.Aes192Key(key), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes192Key(key), irrelevantInput)
|
||||
val result = aes.expandedKey.map {
|
||||
it.foldIndexed(0U) { index, acc, uByte ->
|
||||
acc + (uByte.toUInt() shl (24 - index * 8))
|
||||
@@ -166,7 +166,7 @@ class AesTest {
|
||||
).toTypedArray()
|
||||
|
||||
|
||||
val aes = AesPure(AesKey.Aes256Key(key), irrelevantInput)
|
||||
val aes = AesPure(InternalAesKey.Aes256Key(key), irrelevantInput)
|
||||
val result = aes.expandedKey.map {
|
||||
it.foldIndexed(0U) { index, acc, uByte ->
|
||||
acc + (uByte.toUInt() shl (24 - index * 8))
|
||||
@@ -183,7 +183,7 @@ class AesTest {
|
||||
val key = "2b7e151628aed2a6abf7158809cf4f3c"
|
||||
val expectedResult = "3925841d02dc09fbdc118597196a0b32"
|
||||
|
||||
val aes = AesPure(AesKey.Aes128Key(key), input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
val result = aes.encrypt()
|
||||
assertTrue {
|
||||
result.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
@@ -197,12 +197,12 @@ class AesTest {
|
||||
val key = "2b7e151628aed2a6abf7158809cf4f3c"
|
||||
val expectedResult = "3925841d02dc09fbdc118597196a0b32"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val aes = AesPure(AesKey.Aes128Key(key), original)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), original)
|
||||
val encrypted = aes.encrypt()
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(AesKey.Aes128Key(key), encrypted)
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted)
|
||||
|
||||
decrypted.contentEquals(original)
|
||||
}
|
||||
@@ -211,12 +211,12 @@ class AesTest {
|
||||
val key = "000102030405060708090a0b0c0d0e0f"
|
||||
val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val aes = AesPure(AesKey.Aes128Key(key), original)
|
||||
val aes = AesPure(InternalAesKey.Aes128Key(key), original)
|
||||
val encrypted = aes.encrypt()
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
}
|
||||
val aesDec = AesPure(AesKey.Aes128Key(key), encrypted)
|
||||
val aesDec = AesPure(InternalAesKey.Aes128Key(key), encrypted)
|
||||
val decrypted = aesDec.decrypt()
|
||||
assertTrue {
|
||||
aesDec.expandedKey.contentDeepEquals(aes.expandedKey)
|
||||
@@ -229,11 +229,11 @@ class AesTest {
|
||||
val key = "000102030405060708090a0b0c0d0e0f"
|
||||
val expectedResult = "69c4e0d86a7b0430d8cdb78070b4c55a"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val encrypted = AesPure.encrypt(AesKey.Aes128Key(key), original)
|
||||
val encrypted = AesPure.encrypt(InternalAesKey.Aes128Key(key), original)
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(AesKey.Aes128Key(key), encrypted)
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes128Key(key), encrypted)
|
||||
decrypted.contentEquals(original)
|
||||
}
|
||||
|
||||
@@ -242,11 +242,11 @@ class AesTest {
|
||||
val key = "000102030405060708090a0b0c0d0e0f1011121314151617"
|
||||
val expectedResult = "dda97ca4864cdfe06eaf70a0ec0d7191"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val encrypted = AesPure.encrypt(AesKey.Aes192Key(key), original)
|
||||
val encrypted = AesPure.encrypt(InternalAesKey.Aes192Key(key), original)
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(AesKey.Aes192Key(key), encrypted)
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes192Key(key), encrypted)
|
||||
decrypted.contentEquals(original)
|
||||
}
|
||||
|
||||
@@ -255,11 +255,11 @@ class AesTest {
|
||||
val key = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
|
||||
val expectedResult = "8ea2b7ca516745bfeafc49904b496089"
|
||||
val original = input.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray()
|
||||
val encrypted = AesPure.encrypt(AesKey.Aes256Key(key), original)
|
||||
val encrypted = AesPure.encrypt(InternalAesKey.Aes256Key(key), original)
|
||||
assertTrue {
|
||||
encrypted.contentEquals(expectedResult.chunked(2).map { it.toInt(16).toUByte() }.toUByteArray())
|
||||
}
|
||||
val decrypted = AesPure.decrypt(AesKey.Aes256Key(key), encrypted)
|
||||
val decrypted = AesPure.decrypt(InternalAesKey.Aes256Key(key), encrypted)
|
||||
decrypted.contentEquals(original)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user