summaryrefslogtreecommitdiff
path: root/src/commonMain/kotlin/net/taler/wallet/kotlin/crypto/Crypto.kt
blob: 7c5fa2959d9e4617020d6d9a7e8c632e7c93fe1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package net.taler.wallet.kotlin.crypto

internal interface Crypto {
    fun sha256(input: ByteArray): ByteArray
    fun sha512(input: ByteArray): ByteArray
    fun getHashSha512State(): HashSha512State
    fun getRandomBytes(num: Int): ByteArray
    fun eddsaGetPublic(eddsaPrivateKey: ByteArray): ByteArray
    fun ecdheGetPublic(ecdhePrivateKey: ByteArray): ByteArray
    fun createEddsaKeyPair(): EddsaKeyPair
    fun createEcdheKeyPair(): EcdheKeyPair
    fun eddsaSign(msg: ByteArray, eddsaPrivateKey: ByteArray): ByteArray
    fun eddsaVerify(msg: ByteArray, sig: ByteArray, eddsaPub: ByteArray): Boolean
    fun keyExchangeEddsaEcdhe(eddsaPrivateKey: ByteArray, ecdhePublicKey: ByteArray): ByteArray
    fun keyExchangeEcdheEddsa(ecdhePrivateKey: ByteArray, eddsaPublicKey: ByteArray): ByteArray
    fun kdf(outputLength: Int, ikm: ByteArray, salt: ByteArray, info: ByteArray): ByteArray
    fun rsaBlind(hm: ByteArray, bks: ByteArray, rsaPubEnc: ByteArray): ByteArray
    fun rsaUnblind(sig: ByteArray, rsaPubEnc: ByteArray, bks: ByteArray): ByteArray
    fun rsaVerify(hm: ByteArray, rsaSig: ByteArray, rsaPubEnc: ByteArray): Boolean
    fun setupRefreshPlanchet(secretSeed: ByteArray, coinNumber: Int): FreshCoin
}

interface HashSha512State {
    fun update(data: ByteArray): HashSha512State
    fun final(): ByteArray
}
class EddsaKeyPair(val privateKey: ByteArray, val publicKey: ByteArray)
class EcdheKeyPair(val privateKey: ByteArray, val publicKey: ByteArray)
data class FreshCoin(val coinPublicKey: ByteArray, val coinPrivateKey: ByteArray, val bks: ByteArray) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || this::class != other::class) return false
        other as FreshCoin
        if (!coinPublicKey.contentEquals(other.coinPublicKey)) return false
        if (!coinPrivateKey.contentEquals(other.coinPrivateKey)) return false
        if (!bks.contentEquals(other.bks)) return false
        return true
    }

    override fun hashCode(): Int {
        var result = coinPublicKey.contentHashCode()
        result = 31 * result + coinPrivateKey.contentHashCode()
        result = 31 * result + bks.contentHashCode()
        return result
    }
}

internal expect object CryptoFactory {
    internal fun getCrypto(): Crypto
}

private val hexArray = arrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')

fun ByteArray.toHexString(): String {
    val hexChars = CharArray(this.size * 2)
    for (j in this.indices) {
        val v = (this[j].toInt() and 0xFF)
        hexChars[j * 2] = hexArray[v ushr 4]
        hexChars[j * 2 + 1] = hexArray[v and 0x0F]
    }
    return String(hexChars)
}