Skip to content

Commit 28457b8

Browse files
committed
[CryptoKit] Change package for swift-interop and rename swift classes to have Dwc prefix
1 parent 782c8fd commit 28457b8

File tree

18 files changed

+237
-227
lines changed

18 files changed

+237
-227
lines changed

cryptography-providers/cryptokit/build.gradle.kts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ kotlin {
3535

3636
targets.withType<KotlinNativeTarget>().configureEach {
3737
swiftInterop("DwcCryptoKitInterop") {
38-
packageName("dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop")
39-
// TODO: migrate to new package and rename Swift classes to Dwc*
40-
// packageName("dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop")
38+
packageName("dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop")
4139
}
4240
}
4341
}

cryptography-providers/cryptokit/src/commonMain/kotlin/CryptoKitCryptographyProvider.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package dev.whyoleg.cryptography.providers.cryptokit
77
import dev.whyoleg.cryptography.*
88
import dev.whyoleg.cryptography.algorithms.*
99
import dev.whyoleg.cryptography.providers.cryptokit.algorithms.*
10-
import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.*
10+
import dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop.*
1111
import kotlinx.cinterop.*
1212

1313
private val defaultProvider = lazy { CryptoKitCryptographyProvider }
@@ -20,11 +20,11 @@ internal object CryptoKitCryptographyProvider : CryptographyProvider() {
2020

2121
@Suppress("UNCHECKED_CAST")
2222
override fun <A : CryptographyAlgorithm> getOrNull(identifier: CryptographyAlgorithmId<A>): A? = when (identifier) {
23-
MD5 -> CryptoKitDigest(MD5, SwiftHashAlgorithmMd5)
24-
SHA1 -> CryptoKitDigest(SHA1, SwiftHashAlgorithmSha1)
25-
SHA256 -> CryptoKitDigest(SHA256, SwiftHashAlgorithmSha256)
26-
SHA384 -> CryptoKitDigest(SHA384, SwiftHashAlgorithmSha384)
27-
SHA512 -> CryptoKitDigest(SHA512, SwiftHashAlgorithmSha512)
23+
MD5 -> CryptoKitDigest(MD5, DwcHashAlgorithmMd5)
24+
SHA1 -> CryptoKitDigest(SHA1, DwcHashAlgorithmSha1)
25+
SHA256 -> CryptoKitDigest(SHA256, DwcHashAlgorithmSha256)
26+
SHA384 -> CryptoKitDigest(SHA384, DwcHashAlgorithmSha384)
27+
SHA512 -> CryptoKitDigest(SHA512, DwcHashAlgorithmSha512)
2828
HMAC -> CryptoKitHmac
2929
HKDF -> CryptoKitHkdf
3030
AES.GCM -> CryptoKitAesGcm

cryptography-providers/cryptokit/src/commonMain/kotlin/algorithms/CryptoKitAesGcm.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import dev.whyoleg.cryptography.providers.base.*
1212
import dev.whyoleg.cryptography.providers.base.algorithms.*
1313
import dev.whyoleg.cryptography.providers.base.operations.*
1414
import dev.whyoleg.cryptography.providers.cryptokit.internal.*
15-
import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.*
15+
import dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop.*
1616

1717
internal object CryptoKitAesGcm : AES.GCM {
1818
override fun keyDecoder(): KeyDecoder<AES.Key.Format, AES.GCM.Key> = AesKeyDecoder()
@@ -78,7 +78,7 @@ private class AesGcmCipher(
7878
key.useNSData { keyData ->
7979
(associatedData ?: EmptyByteArray).useNSData { adData ->
8080
swiftTry { error ->
81-
SwiftAesGcm.encryptWithKey(
81+
DwcAesGcm.encryptWithKey(
8282
key = keyData,
8383
nonce = ivData,
8484
plaintext = plaintextData,
@@ -111,7 +111,7 @@ private class AesGcmCipher(
111111
key.useNSData { keyData ->
112112
(associatedData ?: EmptyByteArray).useNSData { adData ->
113113
swiftTry { error ->
114-
SwiftAesGcm.decryptWithKey(
114+
DwcAesGcm.decryptWithKey(
115115
key = keyData,
116116
nonce = ivData,
117117
ciphertext = ciphertextData,

cryptography-providers/cryptokit/src/commonMain/kotlin/algorithms/CryptoKitDigest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ import dev.whyoleg.cryptography.*
88
import dev.whyoleg.cryptography.algorithms.*
99
import dev.whyoleg.cryptography.operations.*
1010
import dev.whyoleg.cryptography.providers.base.*
11-
import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.*
11+
import dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop.*
1212
import dev.whyoleg.cryptography.providers.cryptokit.operations.*
1313
import kotlinx.cinterop.*
1414
import platform.Foundation.*
1515

1616
@OptIn(UnsafeNumber::class)
1717
internal class CryptoKitDigest(
1818
override val id: CryptographyAlgorithmId<Digest>,
19-
private val algorithm: SwiftHashAlgorithm,
19+
private val algorithm: DwcHashAlgorithm,
2020
) : Digest, Hasher {
2121
override fun hasher(): Hasher = this
2222
override fun createHashFunction(): HashFunction = CryptoKitHashFunction(algorithm)
2323
}
2424

2525
@OptIn(UnsafeNumber::class)
2626
private class CryptoKitHashFunction(
27-
algorithm: SwiftHashAlgorithm,
27+
algorithm: DwcHashAlgorithm,
2828
) : HashBasedFunction(algorithm), HashFunction {
2929
private fun hashToNSData(): NSData = function.doFinal().also { reset() }
3030
override fun hashIntoByteArray(destination: ByteArray, destinationOffset: Int): Int {

cryptography-providers/cryptokit/src/commonMain/kotlin/algorithms/CryptoKitEcdh.kt

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dev.whyoleg.cryptography.providers.base.*
1111
import dev.whyoleg.cryptography.providers.base.algorithms.*
1212
import dev.whyoleg.cryptography.providers.base.materials.*
1313
import dev.whyoleg.cryptography.providers.cryptokit.internal.*
14-
import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.*
14+
import dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop.*
1515
import dev.whyoleg.cryptography.serialization.pem.*
1616
import kotlinx.cinterop.*
1717

@@ -30,10 +30,10 @@ internal object CryptoKitEcdh : ECDH {
3030
}
3131

3232
private class KeyPairGenerator(
33-
private val curve: SwiftEcCurve,
33+
private val curve: DwcEcCurve,
3434
) : KeyGenerator<ECDH.KeyPair> {
3535
override fun generateKeyBlocking(): ECDH.KeyPair {
36-
val privateKey = SwiftEcdhPrivateKey.generateWithCurve(curve)
36+
val privateKey = DwcEcdhPrivateKey.generateWithCurve(curve)
3737
return EcdhKeyPair(
3838
privateKey = EcdhPrivateKey(privateKey),
3939
publicKey = EcdhPublicKey(privateKey.publicKey())
@@ -42,40 +42,46 @@ internal object CryptoKitEcdh : ECDH {
4242
}
4343

4444
private class PublicKeyDecoder(
45-
private val curve: SwiftEcCurve,
45+
private val curve: DwcEcCurve,
4646
) : KeyDecoder<EC.PublicKey.Format, ECDH.PublicKey> {
4747
override fun decodeFromByteArrayBlocking(format: EC.PublicKey.Format, bytes: ByteArray): ECDH.PublicKey {
4848
return EcdhPublicKey(swiftTry { error ->
4949
when (format) {
5050
EC.PublicKey.Format.JWK -> error("JWK is not supported")
51-
EC.PublicKey.Format.RAW -> bytes.useNSData { SwiftEcdhPublicKey.decodeRawWithCurve(curve, it, error) }
52-
EC.PublicKey.Format.RAW.Compressed -> bytes.useNSData { SwiftEcdhPublicKey.decodeRawCompressedWithCurve(curve, it, error) }
53-
EC.PublicKey.Format.DER -> bytes.useNSData { SwiftEcdhPublicKey.decodeDerWithCurve(curve, it, error) }
54-
EC.PublicKey.Format.PEM -> SwiftEcdhPublicKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
51+
EC.PublicKey.Format.RAW -> bytes.useNSData { DwcEcdhPublicKey.decodeRawWithCurve(curve, it, error) }
52+
EC.PublicKey.Format.RAW.Compressed -> bytes.useNSData {
53+
DwcEcdhPublicKey.decodeRawCompressedWithCurve(
54+
curve,
55+
it,
56+
error
57+
)
58+
}
59+
EC.PublicKey.Format.DER -> bytes.useNSData { DwcEcdhPublicKey.decodeDerWithCurve(curve, it, error) }
60+
EC.PublicKey.Format.PEM -> DwcEcdhPublicKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
5561
}
5662
})
5763
}
5864
}
5965

6066
private class PrivateKeyDecoder(
61-
private val curve: SwiftEcCurve,
67+
private val curve: DwcEcCurve,
6268
) : KeyDecoder<EC.PrivateKey.Format, ECDH.PrivateKey> {
6369
override fun decodeFromByteArrayBlocking(format: EC.PrivateKey.Format, bytes: ByteArray): ECDH.PrivateKey {
6470
return EcdhPrivateKey(swiftTry { error ->
6571
when (format) {
6672
EC.PrivateKey.Format.JWK -> error("JWK is not supported")
67-
EC.PrivateKey.Format.RAW -> bytes.useNSData { SwiftEcdhPrivateKey.decodeRawWithCurve(curve, it, error) }
73+
EC.PrivateKey.Format.RAW -> bytes.useNSData { DwcEcdhPrivateKey.decodeRawWithCurve(curve, it, error) }
6874
EC.PrivateKey.Format.DER -> decodeFromDer(bytes, error)
6975
EC.PrivateKey.Format.DER.SEC1 -> decodeFromDer(convertEcPrivateKeyFromSec1ToPkcs8(bytes), error)
7076
EC.PrivateKey.Format.PEM,
7177
EC.PrivateKey.Format.PEM.SEC1,
72-
-> SwiftEcdhPrivateKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
78+
-> DwcEcdhPrivateKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
7379
}
7480
})
7581
}
7682

77-
private fun decodeFromDer(bytes: ByteArray, error: SwiftErrorPointer): SwiftEcdhPrivateKey? {
78-
return bytes.useNSData { SwiftEcdhPrivateKey.decodeDerWithCurve(curve, it, error) }
83+
private fun decodeFromDer(bytes: ByteArray, error: DwcErrorPointer): DwcEcdhPrivateKey? {
84+
return bytes.useNSData { DwcEcdhPrivateKey.decodeDerWithCurve(curve, it, error) }
7985
}
8086
}
8187
}
@@ -87,7 +93,7 @@ private class EcdhKeyPair(
8793

8894
@OptIn(UnsafeNumber::class)
8995
private class EcdhPublicKey(
90-
val publicKey: SwiftEcdhPublicKey,
96+
val publicKey: DwcEcdhPublicKey,
9197
) : ECDH.PublicKey, SharedSecretGenerator<ECDH.PrivateKey> {
9298
override fun encodeToByteArrayBlocking(format: EC.PublicKey.Format): ByteArray = when (format) {
9399
EC.PublicKey.Format.JWK -> error("JWK is not supported")
@@ -107,7 +113,7 @@ private class EcdhPublicKey(
107113

108114
@OptIn(UnsafeNumber::class)
109115
private class EcdhPrivateKey(
110-
val privateKey: SwiftEcdhPrivateKey,
116+
val privateKey: DwcEcdhPrivateKey,
111117
) : ECDH.PrivateKey, SharedSecretGenerator<ECDH.PublicKey> {
112118
override fun encodeToByteArrayBlocking(format: EC.PrivateKey.Format): ByteArray = when (format) {
113119
EC.PrivateKey.Format.JWK -> error("JWK is not supported")
@@ -131,8 +137,8 @@ private class EcdhPrivateKey(
131137

132138
@OptIn(UnsafeNumber::class)
133139
private fun deriveSecret(
134-
privateKey: SwiftEcdhPrivateKey,
135-
publicKey: SwiftEcdhPublicKey,
140+
privateKey: DwcEcdhPrivateKey,
141+
publicKey: DwcEcdhPublicKey,
136142
): ByteArray {
137143
require(privateKey.curveType() == publicKey.curveType()) { "Can not derive shared secret: different curves" }
138144
return swiftTry { error ->

cryptography-providers/cryptokit/src/commonMain/kotlin/algorithms/CryptoKitEcdsa.kt

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import dev.whyoleg.cryptography.providers.base.*
1212
import dev.whyoleg.cryptography.providers.base.algorithms.*
1313
import dev.whyoleg.cryptography.providers.base.materials.*
1414
import dev.whyoleg.cryptography.providers.cryptokit.internal.*
15-
import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.*
15+
import dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop.*
1616
import dev.whyoleg.cryptography.providers.cryptokit.operations.*
1717
import dev.whyoleg.cryptography.serialization.pem.*
1818
import kotlinx.cinterop.*
@@ -33,10 +33,10 @@ internal object CryptoKitEcdsa : ECDSA {
3333
}
3434

3535
private class KeyPairGenerator(
36-
private val curve: SwiftEcCurve,
36+
private val curve: DwcEcCurve,
3737
) : KeyGenerator<ECDSA.KeyPair> {
3838
override fun generateKeyBlocking(): ECDSA.KeyPair {
39-
val privateKey = SwiftEcdsaPrivateKey.generateWithCurve(curve)
39+
val privateKey = DwcEcdsaPrivateKey.generateWithCurve(curve)
4040
return EcdsaKeyPair(
4141
privateKey = EcdsaPrivateKey(privateKey),
4242
publicKey = EcdsaPublicKey(privateKey.publicKey())
@@ -45,40 +45,46 @@ internal object CryptoKitEcdsa : ECDSA {
4545
}
4646

4747
private class PublicKeyDecoder(
48-
private val curve: SwiftEcCurve,
48+
private val curve: DwcEcCurve,
4949
) : KeyDecoder<EC.PublicKey.Format, ECDSA.PublicKey> {
5050
override fun decodeFromByteArrayBlocking(format: EC.PublicKey.Format, bytes: ByteArray): ECDSA.PublicKey {
5151
return EcdsaPublicKey(swiftTry { error ->
5252
when (format) {
5353
EC.PublicKey.Format.JWK -> error("JWK is not supported")
54-
EC.PublicKey.Format.RAW -> bytes.useNSData { SwiftEcdsaPublicKey.decodeRawWithCurve(curve, it, error) }
55-
EC.PublicKey.Format.RAW.Compressed -> bytes.useNSData { SwiftEcdsaPublicKey.decodeRawCompressedWithCurve(curve, it, error) }
56-
EC.PublicKey.Format.DER -> bytes.useNSData { SwiftEcdsaPublicKey.decodeDerWithCurve(curve, it, error) }
57-
EC.PublicKey.Format.PEM -> SwiftEcdsaPublicKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
54+
EC.PublicKey.Format.RAW -> bytes.useNSData { DwcEcdsaPublicKey.decodeRawWithCurve(curve, it, error) }
55+
EC.PublicKey.Format.RAW.Compressed -> bytes.useNSData {
56+
DwcEcdsaPublicKey.decodeRawCompressedWithCurve(
57+
curve,
58+
it,
59+
error
60+
)
61+
}
62+
EC.PublicKey.Format.DER -> bytes.useNSData { DwcEcdsaPublicKey.decodeDerWithCurve(curve, it, error) }
63+
EC.PublicKey.Format.PEM -> DwcEcdsaPublicKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
5864
}
5965
})
6066
}
6167
}
6268

6369
private class PrivateKeyDecoder(
64-
private val curve: SwiftEcCurve,
70+
private val curve: DwcEcCurve,
6571
) : KeyDecoder<EC.PrivateKey.Format, ECDSA.PrivateKey> {
6672
override fun decodeFromByteArrayBlocking(format: EC.PrivateKey.Format, bytes: ByteArray): ECDSA.PrivateKey {
6773
return EcdsaPrivateKey(swiftTry { error ->
6874
when (format) {
6975
EC.PrivateKey.Format.JWK -> error("JWK is not supported")
70-
EC.PrivateKey.Format.RAW -> bytes.useNSData { SwiftEcdsaPrivateKey.decodeRawWithCurve(curve, it, error) }
76+
EC.PrivateKey.Format.RAW -> bytes.useNSData { DwcEcdsaPrivateKey.decodeRawWithCurve(curve, it, error) }
7177
EC.PrivateKey.Format.DER -> decodeFromDer(bytes, error)
7278
EC.PrivateKey.Format.DER.SEC1 -> decodeFromDer(convertEcPrivateKeyFromSec1ToPkcs8(bytes), error)
7379
EC.PrivateKey.Format.PEM,
7480
EC.PrivateKey.Format.PEM.SEC1,
75-
-> SwiftEcdsaPrivateKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
81+
-> DwcEcdsaPrivateKey.decodePemWithCurve(curve, bytes.decodeToString(), error)
7682
}
7783
})
7884
}
7985

80-
private fun decodeFromDer(bytes: ByteArray, error: SwiftErrorPointer): SwiftEcdsaPrivateKey? {
81-
return bytes.useNSData { SwiftEcdsaPrivateKey.decodeDerWithCurve(curve, it, error) }
86+
private fun decodeFromDer(bytes: ByteArray, error: DwcErrorPointer): DwcEcdsaPrivateKey? {
87+
return bytes.useNSData { DwcEcdsaPrivateKey.decodeDerWithCurve(curve, it, error) }
8288
}
8389
}
8490
}
@@ -90,7 +96,7 @@ private class EcdsaKeyPair(
9096

9197
@OptIn(UnsafeNumber::class)
9298
private class EcdsaPublicKey(
93-
private val publicKey: SwiftEcdsaPublicKey,
99+
private val publicKey: DwcEcdsaPublicKey,
94100
) : ECDSA.PublicKey {
95101
override fun encodeToByteArrayBlocking(format: EC.PublicKey.Format): ByteArray = when (format) {
96102
EC.PublicKey.Format.JWK -> error("JWK is not supported")
@@ -112,7 +118,7 @@ private class EcdsaPublicKey(
112118

113119
@OptIn(UnsafeNumber::class)
114120
private class EcdsaPrivateKey(
115-
private val privateKey: SwiftEcdsaPrivateKey,
121+
private val privateKey: DwcEcdsaPrivateKey,
116122
) : ECDSA.PrivateKey {
117123
override fun encodeToByteArrayBlocking(format: EC.PrivateKey.Format): ByteArray = when (format) {
118124
EC.PrivateKey.Format.JWK -> error("JWK is not supported")
@@ -138,26 +144,26 @@ private class EcdsaPrivateKey(
138144

139145
@OptIn(UnsafeNumber::class)
140146
private class EcdsaSignatureGenerator(
141-
private val algorithm: SwiftHashAlgorithm,
142-
private val privateKey: SwiftEcdsaPrivateKey,
147+
private val algorithm: DwcHashAlgorithm,
148+
private val privateKey: DwcEcdsaPrivateKey,
143149
private val format: ECDSA.SignatureFormat,
144150
) : SignatureGenerator {
145151
override fun createSignFunction(): SignFunction = EcdsaSignFunction(algorithm, privateKey, format)
146152
}
147153

148154
@OptIn(UnsafeNumber::class)
149155
private class EcdsaSignatureVerifier(
150-
private val algorithm: SwiftHashAlgorithm,
151-
private val publicKey: SwiftEcdsaPublicKey,
156+
private val algorithm: DwcHashAlgorithm,
157+
private val publicKey: DwcEcdsaPublicKey,
152158
private val format: ECDSA.SignatureFormat,
153159
) : SignatureVerifier {
154160
override fun createVerifyFunction(): VerifyFunction = EcdsaVerifyFunction(algorithm, publicKey, format)
155161
}
156162

157163
@OptIn(UnsafeNumber::class)
158164
private class EcdsaSignFunction(
159-
algorithm: SwiftHashAlgorithm,
160-
private val privateKey: SwiftEcdsaPrivateKey,
165+
algorithm: DwcHashAlgorithm,
166+
private val privateKey: DwcEcdsaPrivateKey,
161167
private val format: ECDSA.SignatureFormat,
162168
) : HashBasedFunction(algorithm), SignFunction {
163169
override fun signIntoByteArray(destination: ByteArray, destinationOffset: Int): Int {
@@ -182,8 +188,8 @@ private class EcdsaSignFunction(
182188

183189
@OptIn(UnsafeNumber::class)
184190
private class EcdsaVerifyFunction(
185-
algorithm: SwiftHashAlgorithm,
186-
private val publicKey: SwiftEcdsaPublicKey,
191+
algorithm: DwcHashAlgorithm,
192+
private val publicKey: DwcEcdsaPublicKey,
187193
private val format: ECDSA.SignatureFormat,
188194
) : HashBasedFunction(algorithm), VerifyFunction {
189195
override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean {

cryptography-providers/cryptokit/src/commonMain/kotlin/algorithms/CryptoKitHkdf.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dev.whyoleg.cryptography.algorithms.*
99
import dev.whyoleg.cryptography.operations.*
1010
import dev.whyoleg.cryptography.providers.base.*
1111
import dev.whyoleg.cryptography.providers.cryptokit.internal.*
12-
import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.*
12+
import dev.whyoleg.cryptography.providers.cryptokit.internal.swift.DwcCryptoKitInterop.*
1313
import kotlinx.cinterop.*
1414

1515
@OptIn(UnsafeNumber::class)
@@ -29,7 +29,7 @@ internal object CryptoKitHkdf : HKDF {
2929

3030
@OptIn(UnsafeNumber::class)
3131
private class HkdfSecretDerivation(
32-
private val algorithm: SwiftHashAlgorithm,
32+
private val algorithm: DwcHashAlgorithm,
3333
private val outputSize: BinarySize,
3434
private val salt: ByteArray,
3535
private val info: ByteArray,
@@ -38,7 +38,7 @@ private class HkdfSecretDerivation(
3838
return input.useNSData { ikm ->
3939
salt.useNSData { salt ->
4040
info.useNSData { info ->
41-
SwiftHkdf.deriveWithAlgorithm(
41+
DwcHkdf.deriveWithAlgorithm(
4242
algorithm = algorithm,
4343
inputKey = ikm,
4444
salt = salt,

0 commit comments

Comments
 (0)