summaryrefslogtreecommitdiff
path: root/wallet/src/commonTest/kotlin/net/taler/lib/crypto
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-08-17 09:29:54 -0300
committerTorsten Grote <t@grobox.de>2020-08-17 09:29:54 -0300
commitdade0470c7e378c72ac2f2fd2a623416dadbff10 (patch)
tree083449814b1ad1320677e115f42f9a76125ccd76 /wallet/src/commonTest/kotlin/net/taler/lib/crypto
parenta307a498dc8a42df129e8eaff591e9144ed96298 (diff)
downloadwallet-kotlin-dade0470c7e378c72ac2f2fd2a623416dadbff10.tar.gz
wallet-kotlin-dade0470c7e378c72ac2f2fd2a623416dadbff10.tar.bz2
wallet-kotlin-dade0470c7e378c72ac2f2fd2a623416dadbff10.zip
Provide a blocking API for iOS (until Kotlin 1.4 is out)
and put base crypto into dedicated package (to be split out later).
Diffstat (limited to 'wallet/src/commonTest/kotlin/net/taler/lib/crypto')
-rw-r--r--wallet/src/commonTest/kotlin/net/taler/lib/crypto/Base32CrockfordTest.kt123
-rw-r--r--wallet/src/commonTest/kotlin/net/taler/lib/crypto/EllipticCurveTest.kt155
-rw-r--r--wallet/src/commonTest/kotlin/net/taler/lib/crypto/KdfTest.kt212
-rw-r--r--wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha256Test.kt69
-rw-r--r--wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha512Test.kt115
5 files changed, 674 insertions, 0 deletions
diff --git a/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Base32CrockfordTest.kt b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Base32CrockfordTest.kt
new file mode 100644
index 0000000..f1c5be1
--- /dev/null
+++ b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Base32CrockfordTest.kt
@@ -0,0 +1,123 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.lib.crypto
+
+import kotlin.test.Ignore
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+
+@ExperimentalStdlibApi
+class Base32CrockfordTest {
+
+ private class TestVector(val value: ByteArray, val encoding: List<String>)
+
+ private val vectors = listOf(
+ TestVector(byteArrayOf(0), listOf("00", "0O", "0o")),
+ TestVector(byteArrayOf(0), listOf("00", "0O", "0o")),
+ TestVector(byteArrayOf(1), listOf("04")),
+ TestVector(byteArrayOf(2), listOf("08")),
+ TestVector(byteArrayOf(3), listOf("0C")),
+ TestVector(byteArrayOf(4), listOf("0G")),
+ TestVector(byteArrayOf(5), listOf("0M")),
+ TestVector(byteArrayOf(6), listOf("0R")),
+ TestVector(byteArrayOf(7), listOf("0W")),
+ TestVector(byteArrayOf(8), listOf("10")),
+ TestVector(byteArrayOf(9), listOf("14")),
+ TestVector(byteArrayOf(10), listOf("18")),
+ TestVector(byteArrayOf(11), listOf("1C")),
+ TestVector(byteArrayOf(12), listOf("1G")),
+ TestVector(byteArrayOf(13), listOf("1M")),
+ TestVector(byteArrayOf(14), listOf("1R")),
+ TestVector(byteArrayOf(15), listOf("1W")),
+ TestVector(byteArrayOf(16), listOf("20")),
+ TestVector(byteArrayOf(17), listOf("24")),
+ TestVector(byteArrayOf(18), listOf("28")),
+ TestVector(byteArrayOf(19), listOf("2C")),
+ TestVector(byteArrayOf(20), listOf("2G")),
+ TestVector(byteArrayOf(21), listOf("2M")),
+ TestVector(byteArrayOf(22), listOf("2R")),
+ TestVector(byteArrayOf(23), listOf("2W")),
+ TestVector(byteArrayOf(24), listOf("30")),
+ TestVector(byteArrayOf(25), listOf("34")),
+ TestVector(byteArrayOf(26), listOf("38")),
+ TestVector(byteArrayOf(27), listOf("3C")),
+ TestVector(byteArrayOf(28), listOf("3G")),
+ TestVector(byteArrayOf(29), listOf("3M")),
+ TestVector(byteArrayOf(30), listOf("3R")),
+ TestVector(byteArrayOf(31), listOf("3W")),
+ TestVector(byteArrayOf(0, 0), listOf("0000", "oooo", "OOOO", "0oO0")),
+ TestVector(byteArrayOf(1, 0), listOf("0400", "o4oo", "O4OO", "04oO")),
+ TestVector(byteArrayOf(0, 1), listOf("000G", "ooog", "OOOG", "0oOg")),
+ TestVector(byteArrayOf(1, 1), listOf("040G", "o4og", "O4og", "04Og")),
+ TestVector(byteArrayOf(136.toByte(), 64), listOf("H100", "hio0", "HLOo")),
+ TestVector(byteArrayOf(139.toByte(), 188.toByte()), listOf("HEY0", "heyo", "HeYO")),
+ TestVector(byteArrayOf(54, 31, 127), listOf("6RFQY", "6rfqy")),
+ TestVector(
+ byteArrayOf(72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33),
+ listOf("91JPRV3F41BPYWKCCGGG", "91jprv3f41bpywkccggg", "9Ljprv3f4ibpywkccggg")
+ ),
+ TestVector(
+ byteArrayOf(139.toByte(), 130.toByte(), 16, 112, 24, 11, 64),
+ listOf("HE110W0R1D00", "helloworld00", "heiiOw0RidoO")
+ ),
+ TestVector(
+ byteArrayOf(139.toByte(), 130.toByte(), 16, 112, 24, 11),
+ listOf("HE110W0R1C", "helloworlc", "heiiOw0RiC")
+ ),
+ TestVector(
+ byteArrayOf(139.toByte(), 130.toByte(), 16, 112, 24, 11, 0),
+ listOf("HE110W0R1C00", "helloworlc00", "heiiOw0RiC00")
+ )
+ )
+
+ @Test
+ fun testEncode() {
+ for (vector in vectors) {
+ assertEquals(vector.encoding[0], Base32Crockford.encode(vector.value))
+ }
+ }
+
+ @Test
+ fun testDecode() {
+ for (vector in vectors) {
+ for (encoding in vector.encoding) {
+ assertTrue(vector.value contentEquals Base32Crockford.decode(encoding))
+ }
+ }
+ }
+
+ @Ignore // TODO
+ @Test
+ fun testDecodeFuck() {
+ val bytes = byteArrayOf(0x7e, 0xd9.toByte())
+ assertTrue(bytes contentEquals Base32Crockford.decode("FUCK"))
+ assertTrue(bytes contentEquals Base32Crockford.decode("FuCk"))
+ assertTrue(bytes contentEquals Base32Crockford.decode("fUcK"))
+ assertTrue(bytes contentEquals Base32Crockford.decode("FVCK"))
+ }
+
+ @Test
+ fun testEncodingDecoding() {
+ val input = "Hello, World"
+ val encoded = Base32Crockford.encode(input.encodeToByteArray())
+ val decoded = Base32Crockford.decode(encoded)
+ val output = decoded.decodeToString()
+ assertEquals(input, output)
+ }
+
+}
diff --git a/wallet/src/commonTest/kotlin/net/taler/lib/crypto/EllipticCurveTest.kt b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/EllipticCurveTest.kt
new file mode 100644
index 0000000..4cdd5b9
--- /dev/null
+++ b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/EllipticCurveTest.kt
@@ -0,0 +1,155 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.lib.crypto
+
+import kotlin.random.Random
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
+
+class EllipticCurveTest {
+
+ private val crypto = CryptoFactory.getCrypto()
+
+ @Test
+ fun testExchangeTvgEddsaKey() {
+ val pri = "9TM70AKDTS57AWY9JK2J4TMBTMW6K62WHHGZWYDG0VM5ABPZKD40"
+ val pub = "8GSJZ649T2PXMKZC01Y4ANNBE7MF14QVK9SQEC4E46ZHKCVG8AS0"
+ val pubBytes = crypto.eddsaGetPublic(Base32Crockford.decode(pri))
+ assertEquals(pub, Base32Crockford.encode(pubBytes))
+
+ val pri2 = "C9C5X5JS2SWRTSX5AQBPFA4S2DFD6V9Q04XTTEZDQ5XSSQK2P39G"
+ val pub2 = "6WC3MPYM5XKPKRA2Z6SYB81CPFV3E7EC6S2GVE095X8XH63QTZCG"
+ val pub2Bytes = crypto.eddsaGetPublic(Base32Crockford.decode(pri2))
+ assertEquals(pub2, Base32Crockford.encode(pub2Bytes))
+ }
+
+ @Test
+ fun testExchangeTvgEcdheKey() {
+ val pri = "X4T4N0M8PVQXQEBW2BA7049KFSM7J437NSDFC6GDNM3N5J9367A0"
+ val pub = "M997P494MS6A95G1P0QYWW2VNPSHSX5Q6JBY5B9YMNYWP0B50X3G"
+ val pubBytes = crypto.ecdheGetPublic(Base32Crockford.decode(pri))
+ assertEquals(pub, Base32Crockford.encode(pubBytes))
+
+ val pri2 = "MB8ZSQFTVPX4V0MGT3BA1PQS12NJ7KH33DA8D22NCWTNA5BHH2YG"
+ val pub2 = "8T2CN1W8G3XZE9C9158A5ASS74117GK1XQ1XAX5SGBFDGHS8H5W0"
+ val pub2Bytes = crypto.ecdheGetPublic(Base32Crockford.decode(pri2))
+ assertEquals(pub2, Base32Crockford.encode(pub2Bytes))
+ }
+
+ @Test
+ fun testCreateEddsaKeyPair() {
+ val pair1 = crypto.createEddsaKeyPair()
+ val pair2 = crypto.createEddsaKeyPair()
+ assertFalse(pair1.privateKey contentEquals pair2.privateKey)
+ assertFalse(pair1.publicKey contentEquals pair2.publicKey)
+ }
+
+ @Test
+ fun testCreateEcdheKeyPair() {
+ val pair1 = crypto.createEcdheKeyPair()
+ val pair2 = crypto.createEcdheKeyPair()
+ assertFalse(pair1.privateKey contentEquals pair2.privateKey)
+ assertFalse(pair1.publicKey contentEquals pair2.publicKey)
+ }
+
+ @Test
+ @ExperimentalStdlibApi
+ fun testEddsaSignAndVerify() {
+ val msg = "Hallo world!".encodeToByteArray()
+ val pri = "9TM70AKDTS57AWY9JK2J4TMBTMW6K62WHHGZWYDG0VM5ABPZKD40"
+ val expectedSig =
+ "Z6H76JXPJFP3JBGSF54XBF0BVXDJ0CJBK4YT9GVR1AT916ZD57KP53YZN5G67A4YN95WGMZKQW7744483P5JDF06B6S7TMK195QGP20"
+ val sig = crypto.eddsaSign(msg, Base32Crockford.decode(pri))
+ assertEquals(expectedSig, Base32Crockford.encode(sig))
+
+ val pub = crypto.eddsaGetPublic(Base32Crockford.decode(pri))
+ assertTrue(crypto.eddsaVerify(msg, sig, pub))
+
+ val wrongSig = Random.nextBytes(64)
+ assertFalse(crypto.eddsaVerify(msg, wrongSig, pub))
+
+ val wrongPub = Random.nextBytes(32)
+ assertFalse(crypto.eddsaVerify(msg, sig, wrongPub))
+
+ val wrongMsg = Random.nextBytes(16)
+ assertFalse(crypto.eddsaVerify(wrongMsg, sig, pub))
+
+ val pri2 = "T4NK2VVZZ1ZF6EBGEQHRK7KY67PCEVAZC5YHYH612XG5R6NJXSB0"
+ val pub2 = "2X3PSPT7D6TEM97R98C0DHZREFVAVA3XTH11D5A2Z2K7GBKQ7AEG"
+ val data2 =
+ "00000J00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ val expectedSig2 =
+ "RFC59ZWJB88TD03F4GTQD3RWKH3KKRVEY2T1VN7CFEF51SCZ4BX8HGHC708GW1SN7Y4XFEDE3A5PFZEJ7PR7V09YZXX10DM608T1W20"
+ val sig2 = crypto.eddsaSign(Base32Crockford.decode(data2), Base32Crockford.decode(pri2))
+ assertEquals(expectedSig2, Base32Crockford.encode(sig2))
+ assertTrue(crypto.eddsaVerify(Base32Crockford.decode(data2), sig2, Base32Crockford.decode(pub2)))
+ }
+
+ @Test
+ fun testExchangeTvgEddsaEcdh() {
+ val ecdhePrivateKey = "4AFZWMSGTVCHZPQ0R81NWXDCK4N58G7SDBBE5KXE080Y50370JJG"
+ val ecdhePublicKey = "FXFN5GPAFTKVPWJDPVXQ87167S8T82T5ZV8CDYC0NH2AE14X0M30"
+ val ecdhePublicKeyBytes = crypto.ecdheGetPublic(Base32Crockford.decode(ecdhePrivateKey))
+ assertEquals(ecdhePublicKey, Base32Crockford.encode(ecdhePublicKeyBytes))
+
+ val eddsaPrivateKey = "1KG54M8T3X8BSFSZXCR3SQBSR7Y9P53NX61M864S7TEVMJ2XVPF0"
+ val eddsaPublicKey = "7BXWKG6N224C57RTDV8XEAHR108HG78NMA995BE8QAT5GC1S7E80"
+ val eddsaPublicKeyBytes = crypto.eddsaGetPublic(Base32Crockford.decode(eddsaPrivateKey))
+ assertEquals(eddsaPublicKey, Base32Crockford.encode(eddsaPublicKeyBytes))
+
+ val keyMaterial =
+ "PKZ42Z56SVK2796HG1QYBRJ6ZQM2T9QGA3JA4AAZ8G7CWK9FPX175Q9JE5P0ZAX3HWWPHAQV4DPCK10R9X3SAXHRV0WF06BHEC2ZTKR"
+ val keyMaterial1Bytes = crypto.keyExchangeEddsaEcdhe(
+ Base32Crockford.decode(eddsaPrivateKey),
+ Base32Crockford.decode(ecdhePublicKey)
+ )
+ assertEquals(keyMaterial, Base32Crockford.encode(keyMaterial1Bytes))
+
+ val keyMaterial2Bytes = crypto.keyExchangeEcdheEddsa(
+ Base32Crockford.decode(ecdhePrivateKey),
+ Base32Crockford.decode(eddsaPublicKey)
+ )
+ assertEquals(keyMaterial, Base32Crockford.encode(keyMaterial2Bytes))
+
+ val ecdhePrivateKey2 = "3AMRJ1KC87VWX9MVQMW8MB9YVX5DMS6P5V2SYZNCZ44XVMSDVEFG"
+ val ecdhePublicKey2 = "CFFVCRFTNP7701KZC7187BC42MGSVCMBWK38F23E7T9VVK9D41Q0"
+ val ecdhePublicKeyBytes2 = crypto.ecdheGetPublic(Base32Crockford.decode(ecdhePrivateKey2))
+ assertEquals(ecdhePublicKey2, Base32Crockford.encode(ecdhePublicKeyBytes2))
+
+ val eddsaPrivateKey2 = "8AZTVPCXNCVBKJVQ6XS7Z38KB9JB38WJ7Z80F9FTCX1WHZACTXJ0"
+ val eddsaPublicKey2 = "T3F1VYAVZX3DVCNJTB3BNQTRZGXN446QP2VD847CB6N5V75RT2B0"
+ val eddsaPublicKeyBytes2 = crypto.eddsaGetPublic(Base32Crockford.decode(eddsaPrivateKey2))
+ assertEquals(eddsaPublicKey2, Base32Crockford.encode(eddsaPublicKeyBytes2))
+
+ val keyMaterial2 =
+ "FPQSHC6MWWJGEPXXVF76Q8C4PBJFE59RSGRGFQ3Z6ESR67HMG7FAK6JTCQ4ZKHSVNNCF53DX8FY55EA2193N2A6KD510AEV5TBVZRJR"
+ val keyMaterial1Bytes2 = crypto.keyExchangeEddsaEcdhe(
+ Base32Crockford.decode(eddsaPrivateKey2),
+ Base32Crockford.decode(ecdhePublicKey2)
+ )
+ assertEquals(keyMaterial2, Base32Crockford.encode(keyMaterial1Bytes2))
+
+ val keyMaterial2Bytes2 = crypto.keyExchangeEcdheEddsa(
+ Base32Crockford.decode(ecdhePrivateKey2),
+ Base32Crockford.decode(eddsaPublicKey2)
+ )
+ assertEquals(keyMaterial2, Base32Crockford.encode(keyMaterial2Bytes2))
+ }
+
+}
diff --git a/wallet/src/commonTest/kotlin/net/taler/lib/crypto/KdfTest.kt b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/KdfTest.kt
new file mode 100644
index 0000000..c75f484
--- /dev/null
+++ b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/KdfTest.kt
@@ -0,0 +1,212 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.lib.crypto
+
+import net.taler.lib.crypto.Kdf.HMAC_SHA256_BLOCK_SIZE
+import net.taler.lib.crypto.Kdf.HMAC_SHA512_BLOCK_SIZE
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertTrue
+
+class KdfTest {
+
+ private val crypto = CryptoFactory.getCrypto()
+
+ @Test
+ fun testHmacSha256() {
+ // key smaller than block size
+ val key1 = "EDJP6WK5EG"
+ val message1 = "91JPRV3F41BPYWKCCGGG"
+ val expectedOut1 = "DYKV9QN2HVHMHQRGZ6XNJPPSGQZHA2JAVZB1676ACXYSNKQ0FQ30"
+ val out1 = Kdf.hmacSha256(Base32Crockford.decode(key1), Base32Crockford.decode(message1)) { crypto.sha256(it) }
+ assertTrue(Base32Crockford.decode(key1).size < HMAC_SHA256_BLOCK_SIZE)
+ assertEquals(expectedOut1, Base32Crockford.encode(out1))
+
+ // key bigger than block size
+ val key2 =
+ "AHM6JWS089GQ6S9K68G7CRBJD5GPWX10EXGQ6833E9JP2X35CGG64Y908HQQASVCC5SJ0GVJDXHPPSKFE9J20X3F41GQCVV9CGG66VVECSTQ6TBFDRQ0"
+ val message2 = "89P62RVB4166JXK5ECG4TRBMEHJQ488"
+ val expectedOut2 = "QGEK8853DW3GSQ4RHDYXZ0KX9R6E356R5495Z10KDNFNZ98V9FQG"
+ val out2 = Kdf.hmacSha256(Base32Crockford.decode(key2), Base32Crockford.decode(message2)) { crypto.sha256(it) }
+ assertTrue(Base32Crockford.decode(key2).size > HMAC_SHA256_BLOCK_SIZE)
+ assertEquals(expectedOut2, Base32Crockford.encode(out2))
+
+ // key exactly block size
+ val key3 =
+ "AHM6JWS0EDJP6WK5EGG6PSBS41MQ6831ECG6RVVECWG62WS0EHM6A832DHQP6TS0EDMQMS90DXK20J2D851J0MT884S3ADH07MG3CD0"
+ val message3 = "89P62RVB4166JXK5ECG4TRBMEHJQ488"
+ val expectedOut3 = "HAEWRWXEWWPRMT4W6E32GS0P6QMW85MMZZA5CX2BMJW36QZQFJ2G"
+ val out3 = Kdf.hmacSha256(Base32Crockford.decode(key3), Base32Crockford.decode(message3)) { crypto.sha256(it) }
+ assertEquals(HMAC_SHA256_BLOCK_SIZE, Base32Crockford.decode(key3).size)
+ assertEquals(expectedOut3, Base32Crockford.encode(out3))
+ }
+
+ @Test
+ fun testHmacSha512() {
+ // key smaller than block size
+ val key1 = "EDJP6WK5EG"
+ val message1 = "91JPRV3F41BPYWKCCGGG"
+ val expectedOut1 =
+ "ZMVHDTWE341YSE6YQ4S0Y6XESW7RVFG4WK2CE7K4ZESQ10125JJ2VTRBCHPAQTTKW9MT4H350ZXN8GMP2SBFG03SB6YK63QMHQRE2FG"
+ val out1 = Kdf.hmacSha512(Base32Crockford.decode(key1), Base32Crockford.decode(message1)) { crypto.sha512(it) }
+ assertTrue(Base32Crockford.decode(key1).size < HMAC_SHA512_BLOCK_SIZE)
+ assertEquals(expectedOut1, Base32Crockford.encode(out1))
+
+ // key bigger than block size
+ val key2 =
+ "AHM6JWS089GQ6S9K68G7CRBJD5GPWX10EXGQ6833E9JP2X35CGG64Y908HQQASVCC5SJ0GVJDXHPPSKFE9J2W829EGG6AY33DHTP8SBK414JR82C5GG4Y83MDWG62XKFD5J20RVFDSK7AWV9DXQ20XV9EHM20S39CXMQ8WS0C5Q6882N41T6Y83GE9JQCSBEEGG62RV3D5J6AVKMC5P20VV2EDHPAVK9EHWJW"
+ val message2 = "89P62RVB4166JXK5ECG4TRBMEHJQ488"
+ val expectedOut2 =
+ "0N9GF90FES6HXS344WB396CF78SQDRZ7CS9MRAVCVZPJ3PJ97HYSB5A3C7RCXTTYYSS4CV11X3DANKQD71YMTA46R3NS1X8A99YVAE0"
+ val out2 = Kdf.hmacSha512(Base32Crockford.decode(key2), Base32Crockford.decode(message2)) { crypto.sha512(it) }
+ assertTrue(Base32Crockford.decode(key2).size > HMAC_SHA512_BLOCK_SIZE)
+ assertEquals(expectedOut2, Base32Crockford.encode(out2))
+
+ // key exactly block size
+ val key3 =
+ "AHM6JWS0ESJQ4Y90EDJP6WK5EGG6PSBS41MQ6835F1GP6X3CF4G62WS0DHQPWSS0C5SJ0X38CMG64V3FCDNJ0WV9F9JJ0VV641T6GS90916M2GS0AD44281N64S20RBCCXQQ4TBMD1PJ0XV8D5HPG839ECG32CHR41H7JX35ECG62VK441GJ0V3FEGG6YSH0EHJQGX1E5RQ2W"
+ val message3 = "89P62RVB4166JXK5ECG4TRBMEHJQ488"
+ val expectedOut3 =
+ "A3ZAKHA0EADSBP832KBCPDNVB4GP876PMQEEB2F55ERTQFQ7KMYNTJ3SEWJEQRNH3EV2H0HSA740JE4JQH7GM8FC708KHF4A5FG9QNG"
+ val out3 = Kdf.hmacSha512(Base32Crockford.decode(key3), Base32Crockford.decode(message3)) { crypto.sha512(it) }
+ assertEquals(HMAC_SHA512_BLOCK_SIZE, Base32Crockford.decode(key3).size)
+ assertEquals(expectedOut3, Base32Crockford.encode(out3))
+ }
+
+ @Test
+ fun testRfc4231() {
+ val key1 = ByteArray(20) { 0x0b }
+ val data1 = "91MJ0N38CNS6A"
+ assertEquals(
+ "P0T4RRERVCW56Q58NZ7AY2ZH5E41VGG0S61KV9S6X4VPRBHJSZVG",
+ Base32Crockford.encode(Kdf.hmacSha256(key1, Base32Crockford.decode(data1)) { crypto.sha256(it) })
+ )
+ assertEquals(
+ "GYN7SQN5XXGSTKZGPGJ1M7BCP0HQKX72SS7C4Y3TT2SGAHF1FKFDNA1KPZBBH9R20E5JEKNEMFTE9FMXJ57EPRFHE0Q6JV107896GN0",
+ Base32Crockford.encode(Kdf.hmacSha512(key1, Base32Crockford.decode(data1)) { crypto.sha512(it) })
+ )
+
+ val key2 = Base32Crockford.decode("99JPCS8")
+ val data2 = "EXM62X10CHQJ0YB141VP2VKM41K6YWH0DSQQ8T39DSKKY"
+ assertEquals(
+ "BFEC2HNZC1TMWTG44GK0H5BNRXD00FR8KMKKK0WXXHCBJS7C711G",
+ Base32Crockford.encode(Kdf.hmacSha256(key2, Base32Crockford.decode(data2)) { crypto.sha256(it) })
+ )
+ assertEquals(
+ "2S5QMYZWZ0CY5RWNZFKKPNQ0ME3VTS125T1HZNGG4W6DFTH50NA9EP5ZEQ05N6AADM1MYSFRY3KFVJQAP6HMTJKB9DHPW1RA72YEEDR",
+ Base32Crockford.encode(Kdf.hmacSha512(key2, Base32Crockford.decode(data2)) { crypto.sha512(it) })
+ )
+
+ val key3 = ByteArray(20) { 0xaa.toByte() }
+ val data3 = "VQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEXVQEX"
+ assertEquals(
+ "EWZAJ7HPG074D1ADQ3NX14C1MWMNJ2CB7VWC28PSCDAH9KPNCQZ0",
+ Base32Crockford.encode(Kdf.hmacSha256(key3, Base32Crockford.decode(data3)) { crypto.sha256(it) })
+ )
+ assertEquals(
+ "Z9SV024XATH89VXGY1TPS28BX6RVBPYXHVM1MDJNZ0Z37CH7KMWVYFM4G9WTE8P80TT8B93YCZ40FEA6MCVVXT4M4ST2F22SW4S95YR",
+ Base32Crockford.encode(Kdf.hmacSha512(key3, Base32Crockford.decode(data3)) { crypto.sha512(it) })
+ )
+
+ val key4 = Base32Crockford.decode("041061050R3GG28A1C60T3GF208H44RM2MB1E60S")
+ val data4 = "SQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKEDSQ6WVKED"
+ assertEquals(
+ "G9ARME4T8GY0X96CG6C9KWG87A2Z1YN3WNWFG1VT5RZZ8SS9CSDG",
+ Base32Crockford.encode(Kdf.hmacSha256(key4, Base32Crockford.decode(data4)) { crypto.sha256(it) })
+ )
+ assertEquals(
+ "P2X4CNHQ8P66K475N32ZC7AAYZJQDPBZZ55REBF7DY050DGYWFDTJ755R4DA4QNMTSWJEQ65F20679FHJX0H432F5QHAVTZB22H9HQ8",
+ Base32Crockford.encode(Kdf.hmacSha512(key4, Base32Crockford.decode(data4)) { crypto.sha512(it) })
+ )
+
+ val key5 = ByteArray(20) { 0x0c.toByte() }
+ val data5 = "AHJQ6X10AXMQ8T10AHS7AVK3C5T6JVVE"
+ assertEquals(
+ "MEV1CX3K207E0VGCF5P2JNAN5C",
+ Base32Crockford.encode(Kdf.hmacSha256(key5, Base32Crockford.decode(data5)) { crypto.sha256(it) }
+ .copyOfRange(0, 16))
+ )
+ assertEquals(
+ "85FTTRKHB05567A1F6Y8J7C7MR",
+ Base32Crockford.encode(Kdf.hmacSha512(key5, Base32Crockford.decode(data5)) { crypto.sha512(it) }
+ .copyOfRange(0, 16))
+ )
+
+ val key6 = ByteArray(131) { 0xaa.toByte() }
+ val data6 = "AHJQ6X10ANSPJVK741662WK7CNS20N38C5Q20GKCDXHPPBAKD5X6A82BCNWJ0B9091GQ6T109DJQJ826D5S76X0"
+ assertEquals(
+ "C3J32P8YW2V7Y3CA4TNCQXDQFY70QHH16WMCA5058R20Y3Q3FXA0",
+ Base32Crockford.encode(Kdf.hmacSha256(key6, Base32Crockford.decode(data6)) { crypto.sha256(it) })
+ )
+ assertEquals(
+ "G2S44RY7R6HYQDRMJF0XTYZ8PJDMDMFM3D5EXG8J3C0KF0ZRYD96PNPG6ZG5Y9CRQM7X48AXD8F555F69XSZCFRAXJ5S2PMRBNW6B60",
+ Base32Crockford.encode(Kdf.hmacSha512(key6, Base32Crockford.decode(data6)) { crypto.sha512(it) })
+ )
+
+ val key7 = ByteArray(131) { 0xaa.toByte() }
+ val data7 =
+ "AHM6JWS0D5SJ0R90EHJQ6X10ENSPJVK741GJ0V31E9KPAWH0EHM62VH0C9P6YRVB5NSPJYK541NPAY90C5Q6883141P62WK7CNS20X38C5Q20RKCDXHPPBBKD5X6A834C5T62BH0AHM6A83BCNWJ0VK5CNJ7683MDWG64S90D1GQ6T35CGG64SB6DXS6A832CNMPWSS0ENSPAS10C9WJ0X38CMG4GKA18CG62V37DXS6JX38DMQ0"
+ assertEquals(
+ "KC4ZZ9RVJGQWP9V3BYYDBC798JZXRRV49W3H74WAFX8N6Q1T6QH0",
+ Base32Crockford.encode(Kdf.hmacSha256(key7, Base32Crockford.decode(data7)) { crypto.sha256(it) })
+ )
+ assertEquals(
+ "WDXPMXTXS1YVN96ZN7WPWQHZZQFBTWFRGSS8K1JXYPHJT86DS52BC0HCNGY4K0NH1NFEPNE3WKF1A4T6EVXPVR24C1JWJX20ZA66MP0",
+ Base32Crockford.encode(Kdf.hmacSha512(key7, Base32Crockford.decode(data7)) { crypto.sha512(it) })
+ )
+ }
+
+ @Test
+ fun testKdf() {
+ val salt = "94KPT83PCNS7J83KC5P78Y8"
+ val ikm = "94KPT83MD1JJ0WV5CDS6AX10D5Q70XBM41NPAY90DNGQ8SBJD5GPR"
+ val ctx = "94KPT83141HPYVKMCNW78833D1TPWTSC41GPRWVF41NPWVVQDRG62WS04XMPWSKF4WG6JVH0EHM6A82J8S1G"
+ val expectedOut =
+ "GTMR4QT05Z9WF5HKVG0WK9RPXGHSMHJNW377G9GJXCA8B0FEKPF4D27RJMSJZYWSQNTBJ5EYVV7ZW18B48Z0JVJJ80RHB706Y96Q358"
+ val out =
+ crypto.kdf(64, Base32Crockford.decode(ikm), Base32Crockford.decode(salt), Base32Crockford.decode(ctx))
+ assertEquals(expectedOut, Base32Crockford.encode(out))
+
+ val salt2 = "94KPT83141V6AWKS41SP2V3MF4G76VK1CDNG"
+ val ikm2 =
+ "94KPT83MD1JJ0WV5CDS6AX10D5Q70XBM41NPAY90DNGQ8SBJD5GPR83MD1GQ8838C5SJ0RK5CNQ20RV8C5Q6ESB441K6YWH08X75A82MC5P6AWG"
+ val ctx2 =
+ "94KPT83141HPYVKMCNW78833D1TPWTSC41GPRWVF41NPWVVQDRG62WS04XMPWSKF4WG6JVH0EHM6A82J8S1J0WVF41VPGY90DSQQ8833C5P6R83DCMG6JVK6DWZG"
+ val expectedOut2 =
+ "C6TXT6GMVZ8JNWZF27SPD6939FK035Y3FTWXYT0W4EZX9FNJ5KH24MQSK9D0MW3G7T1BBZ4ERVQC1RE24BNDPJQ68ZRQ8S3XN4GNHC8"
+ val out2 =
+ crypto.kdf(64, Base32Crockford.decode(ikm2), Base32Crockford.decode(salt2), Base32Crockford.decode(ctx2))
+ assertEquals(expectedOut2, Base32Crockford.encode(out2))
+
+ val salt3 = "C4G76RBCEG"
+ val ikm3 = "ESJQ4Y90EDJP6WK5EGG6PSBS"
+ val ctx3 = "D5Q6CVR"
+ val expectedOut3 = "ZYMG67TD51XYS0ZM3QZKH8HF8FW7CTVT91NAWD0PGZM2QGTNYKMXAA4ZJBH5V633TJW9E124CRYEY"
+ val out3 =
+ crypto.kdf(48, Base32Crockford.decode(ikm3), Base32Crockford.decode(salt3), Base32Crockford.decode(ctx3))
+ assertEquals(expectedOut3, Base32Crockford.encode(out3))
+
+ val salt4 = "84G76XV5CNT20WV1DHT20EH9"
+ val ikm4 = "9NWJ0SBREHS6AVB5DHWJ0WV5CDS6AX10DDJQJ"
+ val ctx4 = "CDQPWX35F1T20TBECSQKY88"
+ val expectedOut4 = "YM46SN0HYJ0RNGY1V5S7WY1SD2RQ1Y17G5Z37JD46E2Z8KY11PH5EV772RQ5NQ9SBMGG"
+ val out4 =
+ crypto.kdf(42, Base32Crockford.decode(ikm4), Base32Crockford.decode(salt4), Base32Crockford.decode(ctx4))
+ assertEquals(expectedOut4, Base32Crockford.encode(out4))
+ }
+
+}
diff --git a/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha256Test.kt b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha256Test.kt
new file mode 100644
index 0000000..9c47806
--- /dev/null
+++ b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha256Test.kt
@@ -0,0 +1,69 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.lib.crypto
+
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+@kotlin.ExperimentalStdlibApi
+class Sha256Test {
+
+ private val crypto = CryptoFactory.getCrypto()
+
+ @Test
+ fun testAbc() {
+ assertEquals(
+ "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
+ crypto.sha256("abc".encodeToByteArray()).toHexString()
+ )
+ }
+
+ @Test
+ fun testEmptyString() {
+ assertEquals(
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+ crypto.sha256("".encodeToByteArray()).toHexString()
+ )
+ }
+
+ @Test
+ fun testAbc448bits() {
+ assertEquals(
+ "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
+ crypto.sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToByteArray()).toHexString()
+ )
+ }
+
+ @Test
+ fun testAbc896bits() {
+ assertEquals(
+ "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1",
+ crypto.sha256("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".encodeToByteArray())
+ .toHexString()
+ )
+ }
+
+ @Test
+ fun testAMillionAs() {
+ val input = ByteArray(1_000_000) { 0x61 }
+ assertEquals(
+ "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0",
+ crypto.sha256(input).toHexString()
+ )
+ }
+
+}
diff --git a/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha512Test.kt b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha512Test.kt
new file mode 100644
index 0000000..a3e817f
--- /dev/null
+++ b/wallet/src/commonTest/kotlin/net/taler/lib/crypto/Sha512Test.kt
@@ -0,0 +1,115 @@
+/*
+ * This file is part of GNU Taler
+ * (C) 2020 Taler Systems S.A.
+ *
+ * GNU Taler is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation; either version 3, or (at your option) any later version.
+ *
+ * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+ */
+
+package net.taler.lib.crypto
+
+import kotlin.random.Random
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+class Sha512Test {
+
+ private val crypto = CryptoFactory.getCrypto()
+
+ @Test
+ fun testAbc() {
+ assertEquals(
+ "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
+ crypto.sha512("abc".encodeToByteArray()).toHexString()
+ )
+ }
+
+ @Test
+ fun testEmptyString() {
+ assertEquals(
+ "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
+ crypto.sha512("".encodeToByteArray()).toHexString()
+ )
+ }
+
+ @Test
+ fun testAbc448bits() {
+ assertEquals(
+ "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445",
+ crypto.sha512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq".encodeToByteArray()).toHexString()
+ )
+ }
+
+ @Test
+ fun testAbc896bits() {
+ assertEquals(
+ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
+ crypto.sha512("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu".encodeToByteArray())
+ .toHexString()
+ )
+ }
+
+ @Test
+ fun testAMillionAs() {
+ val input = ByteArray(1_000_000) { 0x61 }
+ assertEquals(
+ "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b",
+ crypto.sha512(input).toHexString()
+ )
+ }
+
+ @Test
+ fun testExchangeTvgHashCode() {
+ val input = "91JPRV3F5GG4EKJN41A62V35E8"
+ val output =
+ "CW96WR74JS8T53EC8GKSGD49QKH4ZNFTZXDAWMMV5GJ1E4BM6B8GPN5NVHDJ8ZVXNCW7Q4WBYCV61HCA3PZC2YJD850DT29RHHN7ESR"
+ assertEquals(output, Base32Crockford.encode(crypto.sha512(Base32Crockford.decode(input))))
+ }
+
+ @Test
+ fun testIncrementalHashing() {
+ val n = 1024
+ val d = Random.nextBytes(n)
+
+ val h1 = crypto.sha512(d)
+ val h2 = crypto.getHashSha512State().update(d).final()
+ assertEquals(Base32Crockford.encode(h1), Base32Crockford.encode(h2))
+
+ val s = crypto.getHashSha512State()
+ for (i in 0 until n) {
+ val b = ByteArray(1)
+ b[0] = d[i]
+ s.update(b)
+ }
+ val h3 = s.final()
+ assertEquals(Base32Crockford.encode(h1), Base32Crockford.encode(h3))
+ }
+
+ @Test
+ fun testIncrementalHashing2() {
+ val n = 10
+ val d = Random.nextBytes(n)
+
+ val h1 = crypto.sha512(d)
+ val h2 = crypto.getHashSha512State().update(d).final()
+ assertEquals(Base32Crockford.encode(h1), Base32Crockford.encode(h2))
+
+ val s = crypto.getHashSha512State()
+ for (i in 0 until n) {
+ val b = ByteArray(1)
+ b[0] = d[i]
+ s.update(b)
+ }
+ val h3 = s.final()
+ assertEquals(Base32Crockford.encode(h1), Base32Crockford.encode(h3))
+ }
+
+}