summaryrefslogtreecommitdiff
path: root/wallet/src/nativeMain/kotlin/net/taler/lib/wallet/crypto/CryptoFactory.kt
blob: 61646a09076dc42595581a66d3176846e16c3635 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * 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.wallet.crypto

import kotlinx.cinterop.CValuesRef
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.alloc
import kotlinx.cinterop.free
import kotlinx.cinterop.nativeHeap
import kotlinx.cinterop.ptr
import kotlinx.cinterop.refTo
import org.libsodium.crypto_hash_sha256
import org.libsodium.crypto_hash_sha256_bytes
import org.libsodium.crypto_hash_sha512
import org.libsodium.crypto_hash_sha512_bytes
import org.libsodium.crypto_hash_sha512_final
import org.libsodium.crypto_hash_sha512_init
import org.libsodium.crypto_hash_sha512_state
import org.libsodium.crypto_hash_sha512_update
import org.libsodium.crypto_scalarmult
import org.libsodium.crypto_scalarmult_BYTES
import org.libsodium.crypto_scalarmult_base
import org.libsodium.crypto_scalarmult_curve25519_BYTES
import org.libsodium.crypto_sign_BYTES
import org.libsodium.crypto_sign_PUBLICKEYBYTES
import org.libsodium.crypto_sign_SECRETKEYBYTES
import org.libsodium.crypto_sign_detached
import org.libsodium.crypto_sign_ed25519_pk_to_curve25519
import org.libsodium.crypto_sign_seed_keypair
import org.libsodium.crypto_sign_verify_detached
import org.libsodium.randombytes

internal actual object CryptoFactory {
    internal actual fun getCrypto(): Crypto = CryptoNativeImpl
}

@OptIn(ExperimentalUnsignedTypes::class)
internal object CryptoNativeImpl : CryptoImpl() {

    override fun sha256(input: ByteArray): ByteArray {
        val output = ByteArray(crypto_hash_sha256_bytes().toInt())
        val cInput = if (input.isEmpty()) null else input.toCValuesRef()
        crypto_hash_sha256(output.toCValuesRef(), cInput, input.size.toULong())
        return output
    }

    override fun sha512(input: ByteArray): ByteArray {
        val output = ByteArray(crypto_hash_sha512_bytes().toInt())
        val cInput = if (input.isEmpty()) null else input.toCValuesRef()
        crypto_hash_sha512(output.toCValuesRef(), cInput, input.size.toULong())
        return output
    }

    override fun getHashSha512State(): HashSha512State {
        return NativeHashSha512State()
    }

    override fun getRandomBytes(num: Int): ByteArray {
        val bytes = ByteArray(num)
        randombytes(bytes.toCValuesRef(), num.toULong())
        return bytes
    }

    override fun eddsaGetPublic(eddsaPrivateKey: ByteArray): ByteArray {
        val publicKey = ByteArray(crypto_sign_PUBLICKEYBYTES.toInt())
        val privateKey = ByteArray(crypto_sign_SECRETKEYBYTES.toInt())
        crypto_sign_seed_keypair(publicKey.toCValuesRef(), privateKey.toCValuesRef(), eddsaPrivateKey.toCValuesRef())
        return publicKey
    }

    override fun ecdheGetPublic(ecdhePrivateKey: ByteArray): ByteArray {
        val publicKey = ByteArray(crypto_scalarmult_BYTES.toInt())
        crypto_scalarmult_base(publicKey.toCValuesRef(), ecdhePrivateKey.toCValuesRef())
        return publicKey
    }

    override fun createEddsaKeyPair(): EddsaKeyPair {
        val privateKey = ByteArray(crypto_sign_SECRETKEYBYTES.toInt())
        randombytes(privateKey.toCValuesRef(), crypto_sign_SECRETKEYBYTES.toULong())
        val publicKey = eddsaGetPublic(privateKey)
        return EddsaKeyPair(privateKey, publicKey)
    }

    override fun createEcdheKeyPair(): EcdheKeyPair {
        val privateKey = ByteArray(crypto_scalarmult_BYTES.toInt())
        randombytes(privateKey.toCValuesRef(), crypto_scalarmult_BYTES.toULong())
        val publicKey = ecdheGetPublic(privateKey)
        return EcdheKeyPair(privateKey, publicKey)
    }

    override fun eddsaSign(msg: ByteArray, eddsaPrivateKey: ByteArray): ByteArray {
        val publicKey = ByteArray(crypto_sign_PUBLICKEYBYTES.toInt())
        val privateKey = ByteArray(crypto_sign_SECRETKEYBYTES.toInt())
        crypto_sign_seed_keypair(publicKey.toCValuesRef(), privateKey.toCValuesRef(), eddsaPrivateKey.toCValuesRef())

        val signatureBytes = ByteArray(crypto_sign_BYTES.toInt())
        crypto_sign_detached(
            signatureBytes.toCValuesRef(),
            null,
            msg.toCValuesRef(),
            msg.size.toULong(),
            privateKey.toCValuesRef()
        )
        return signatureBytes
    }

    override fun eddsaVerify(msg: ByteArray, sig: ByteArray, eddsaPub: ByteArray): Boolean {
        return crypto_sign_verify_detached(
            sig.toCValuesRef(),
            msg.toCValuesRef(),
            msg.size.toULong(),
            eddsaPub.toCValuesRef()
        ) == 0
    }

    override fun keyExchangeEddsaEcdhe(eddsaPrivateKey: ByteArray, ecdhePublicKey: ByteArray): ByteArray {
        val privateKey = sha512(eddsaPrivateKey).copyOfRange(0, 32)
        val sharedKey = ByteArray(crypto_scalarmult_BYTES.toInt())
        crypto_scalarmult(sharedKey.toCValuesRef(), privateKey.toCValuesRef(), ecdhePublicKey.toCValuesRef())
        return sha512(sharedKey)
    }

    override fun keyExchangeEcdheEddsa(ecdhePrivateKey: ByteArray, eddsaPublicKey: ByteArray): ByteArray {
        val curve25519Pub = ByteArray(crypto_scalarmult_curve25519_BYTES.toInt())
        val cCurve25519Pub = curve25519Pub.toCValuesRef()
        crypto_sign_ed25519_pk_to_curve25519(cCurve25519Pub, eddsaPublicKey.toCValuesRef())

        val sharedKey = ByteArray(crypto_scalarmult_BYTES.toInt())
        crypto_scalarmult(sharedKey.toCValuesRef(), ecdhePrivateKey.toCValuesRef(), cCurve25519Pub)
        return sha512(sharedKey)
    }

    override fun rsaBlind(hm: ByteArray, bks: ByteArray, rsaPubEnc: ByteArray): ByteArray {
        TODO("Not yet implemented")
    }

    override fun rsaUnblind(sig: ByteArray, rsaPubEnc: ByteArray, bks: ByteArray): ByteArray {
        TODO("Not yet implemented")
    }

    override fun rsaVerify(hm: ByteArray, rsaSig: ByteArray, rsaPubEnc: ByteArray): Boolean {
        TODO("Not yet implemented")
    }

    private class NativeHashSha512State : HashSha512State {
        private val state = nativeHeap.alloc<crypto_hash_sha512_state>()
        private val statePointer = state.ptr

        init {
            check(crypto_hash_sha512_init(statePointer) == 0) { "Error doing crypto_hash_sha512_init" }
        }

        override fun update(data: ByteArray): HashSha512State {
            val cInput = if (data.isEmpty()) null else data.toCValuesRef()
            crypto_hash_sha512_update(statePointer, cInput, data.size.toULong())
            return this
        }

        override fun final(): ByteArray {
            val output = ByteArray(crypto_hash_sha512_bytes().toInt())
            crypto_hash_sha512_final(statePointer, output.toCValuesRef())
            nativeHeap.free(statePointer)
            return output
        }

    }

    private fun ByteArray.toCValuesRef(): CValuesRef<UByteVar> {
        @Suppress("UNCHECKED_CAST")
        return this.refTo(0) as CValuesRef<UByteVar>
    }

}