|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package dev.whyoleg.cryptography.providers.cryptokit.algorithms |
| 6 | + |
| 7 | +import dev.whyoleg.cryptography.algorithms.* |
| 8 | +import dev.whyoleg.cryptography.materials.key.* |
| 9 | +import dev.whyoleg.cryptography.operations.* |
| 10 | +import dev.whyoleg.cryptography.providers.cryptokit.internal.* |
| 11 | +import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.* |
| 12 | +import dev.whyoleg.cryptography.providers.base.* |
| 13 | +import dev.whyoleg.cryptography.providers.base.materials.* |
| 14 | +import dev.whyoleg.cryptography.providers.base.operations.* |
| 15 | +import dev.whyoleg.cryptography.serialization.asn1.* |
| 16 | +import dev.whyoleg.cryptography.serialization.asn1.modules.* |
| 17 | +import dev.whyoleg.cryptography.serialization.pem.* |
| 18 | +import kotlinx.cinterop.* |
| 19 | +import platform.Foundation.* |
| 20 | + |
| 21 | +internal object CryptoKitEdDSA : EdDSA { |
| 22 | + override fun publicKeyDecoder(curve: EdDSA.Curve): KeyDecoder<EdDSA.PublicKey.Format, EdDSA.PublicKey> { |
| 23 | + check(curve == EdDSA.Curve.Ed25519) { "CryptoKit supports Ed25519 only" } |
| 24 | + return object : KeyDecoder<EdDSA.PublicKey.Format, EdDSA.PublicKey> { |
| 25 | + override fun decodeFromByteArrayBlocking(format: EdDSA.PublicKey.Format, bytes: ByteArray): EdDSA.PublicKey = when (format) { |
| 26 | + EdDSA.PublicKey.Format.RAW -> EdPublicKey( |
| 27 | + swiftTry<SwiftEdDsaPublicKey> { error -> bytes.useNSData { SwiftEdDsaPublicKey.decodeRawWithRawRepresentation(it, error) } } |
| 28 | + ) |
| 29 | + EdDSA.PublicKey.Format.DER -> { |
| 30 | + val raw = unwrapSubjectPublicKeyInfo(ObjectIdentifier.Ed25519, bytes) |
| 31 | + EdPublicKey(swiftTry { error -> raw.useNSData { SwiftEdDsaPublicKey.decodeRawWithRawRepresentation(it, error) } }) |
| 32 | + } |
| 33 | + EdDSA.PublicKey.Format.PEM -> { |
| 34 | + val der = unwrapPem(PemLabel.PublicKey, bytes) |
| 35 | + val raw = unwrapSubjectPublicKeyInfo(ObjectIdentifier.Ed25519, der) |
| 36 | + EdPublicKey(swiftTry { error -> raw.useNSData { SwiftEdDsaPublicKey.decodeRawWithRawRepresentation(it, error) } }) |
| 37 | + } |
| 38 | + else -> error("$format is not supported by CryptoKit EdDSA") |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + override fun privateKeyDecoder(curve: EdDSA.Curve): KeyDecoder<EdDSA.PrivateKey.Format, EdDSA.PrivateKey> { |
| 44 | + check(curve == EdDSA.Curve.Ed25519) { "CryptoKit supports Ed25519 only" } |
| 45 | + return object : KeyDecoder<EdDSA.PrivateKey.Format, EdDSA.PrivateKey> { |
| 46 | + override fun decodeFromByteArrayBlocking(format: EdDSA.PrivateKey.Format, bytes: ByteArray): EdDSA.PrivateKey = when (format) { |
| 47 | + EdDSA.PrivateKey.Format.RAW -> EdPrivateKey( |
| 48 | + swiftTry<SwiftEdDsaPrivateKey> { error -> bytes.useNSData { SwiftEdDsaPrivateKey.decodeRawWithRawRepresentation(it, error) } } |
| 49 | + ) |
| 50 | + EdDSA.PrivateKey.Format.DER -> { |
| 51 | + val raw = unwrapPrivateKeyInfo(ObjectIdentifier.Ed25519, bytes) |
| 52 | + EdPrivateKey(swiftTry { error -> raw.useNSData { SwiftEdDsaPrivateKey.decodeRawWithRawRepresentation(it, error) } }) |
| 53 | + } |
| 54 | + EdDSA.PrivateKey.Format.PEM -> { |
| 55 | + val der = unwrapPem(PemLabel.PrivateKey, bytes) |
| 56 | + val raw = unwrapPrivateKeyInfo(ObjectIdentifier.Ed25519, der) |
| 57 | + EdPrivateKey(swiftTry { error -> raw.useNSData { SwiftEdDsaPrivateKey.decodeRawWithRawRepresentation(it, error) } }) |
| 58 | + } |
| 59 | + else -> error("$format is not supported by CryptoKit EdDSA") |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + override fun keyPairGenerator(curve: EdDSA.Curve): KeyGenerator<EdDSA.KeyPair> { |
| 65 | + check(curve == EdDSA.Curve.Ed25519) { "CryptoKit supports Ed25519 only" } |
| 66 | + return object : KeyGenerator<EdDSA.KeyPair> { |
| 67 | + override fun generateKeyBlocking(): EdDSA.KeyPair { |
| 68 | + val p = SwiftEdDsaPrivateKey.generate() |
| 69 | + return EdKeyPair(EdPublicKey(p.publicKey()), EdPrivateKey(p)) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private class EdKeyPair( |
| 75 | + override val publicKey: EdDSA.PublicKey, |
| 76 | + override val privateKey: EdDSA.PrivateKey, |
| 77 | + ) : EdDSA.KeyPair |
| 78 | + |
| 79 | + private class EdPublicKey( |
| 80 | + val key: SwiftEdDsaPublicKey, |
| 81 | + ) : EdDSA.PublicKey { |
| 82 | + override fun encodeToByteArrayBlocking(format: EdDSA.PublicKey.Format): ByteArray = when (format) { |
| 83 | + EdDSA.PublicKey.Format.RAW -> { |
| 84 | + val raw = key.rawRepresentation().toByteArray() |
| 85 | + raw |
| 86 | + } |
| 87 | + EdDSA.PublicKey.Format.DER -> { |
| 88 | + val raw = key.rawRepresentation().toByteArray() |
| 89 | + wrapSubjectPublicKeyInfo( |
| 90 | + UnknownKeyAlgorithmIdentifier(ObjectIdentifier.Ed25519), |
| 91 | + raw |
| 92 | + ) |
| 93 | + } |
| 94 | + EdDSA.PublicKey.Format.PEM -> { |
| 95 | + val raw = key.rawRepresentation().toByteArray() |
| 96 | + wrapPem( |
| 97 | + PemLabel.PublicKey, |
| 98 | + wrapSubjectPublicKeyInfo( |
| 99 | + UnknownKeyAlgorithmIdentifier(ObjectIdentifier.Ed25519), |
| 100 | + raw |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + else -> error("$format is not supported by CryptoKit EdDSA") |
| 105 | + } |
| 106 | + |
| 107 | + override fun signatureVerifier(): SignatureVerifier = object : SignatureVerifier { |
| 108 | + override fun createVerifyFunction(): VerifyFunction = |
| 109 | + AccumulatingVerifyFunction { data, signature, startIndex, endIndex -> |
| 110 | + val sig = signature.copyOfRange(startIndex, endIndex) |
| 111 | + val ok = data.useNSData { dataNs -> sig.useNSData { sigNs -> |
| 112 | + key.verifyWithSignature(sigNs, message = dataNs) |
| 113 | + } } as Boolean |
| 114 | + ok |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private class EdPrivateKey( |
| 120 | + val key: SwiftEdDsaPrivateKey, |
| 121 | + ) : EdDSA.PrivateKey { |
| 122 | + override fun encodeToByteArrayBlocking(format: EdDSA.PrivateKey.Format): ByteArray = when (format) { |
| 123 | + EdDSA.PrivateKey.Format.RAW -> { |
| 124 | + val raw = key.rawRepresentation().toByteArray() |
| 125 | + raw |
| 126 | + } |
| 127 | + EdDSA.PrivateKey.Format.DER -> { |
| 128 | + val raw = key.rawRepresentation().toByteArray() |
| 129 | + wrapPrivateKeyInfo( |
| 130 | + 0, |
| 131 | + UnknownKeyAlgorithmIdentifier(ObjectIdentifier.Ed25519), |
| 132 | + raw |
| 133 | + ) |
| 134 | + } |
| 135 | + EdDSA.PrivateKey.Format.PEM -> { |
| 136 | + val raw = key.rawRepresentation().toByteArray() |
| 137 | + wrapPem( |
| 138 | + PemLabel.PrivateKey, |
| 139 | + wrapPrivateKeyInfo( |
| 140 | + 0, |
| 141 | + UnknownKeyAlgorithmIdentifier(ObjectIdentifier.Ed25519), |
| 142 | + raw |
| 143 | + ) |
| 144 | + ) |
| 145 | + } |
| 146 | + else -> error("$format is not supported by CryptoKit EdDSA") |
| 147 | + } |
| 148 | + |
| 149 | + override fun signatureGenerator(): SignatureGenerator = object : SignatureGenerator { |
| 150 | + override fun createSignFunction(): SignFunction = |
| 151 | + AccumulatingSignFunction { data -> |
| 152 | + swiftTry { error -> data.useNSData { key.signWithMessage(it, error) } }.toByteArray() |
| 153 | + } |
| 154 | + override fun generateSignatureBlocking(data: ByteArray): ByteArray = |
| 155 | + swiftTry { error -> data.useNSData { key.signWithMessage(it, error) } }.toByteArray() |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments