|
| 1 | +/* |
| 2 | + * Copyright (c) 2024-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.* |
| 8 | +import dev.whyoleg.cryptography.BinarySize.Companion.bytes |
| 9 | +import dev.whyoleg.cryptography.algorithms.* |
| 10 | +import dev.whyoleg.cryptography.materials.key.* |
| 11 | +import dev.whyoleg.cryptography.providers.base.* |
| 12 | +import dev.whyoleg.cryptography.providers.base.algorithms.* |
| 13 | +import dev.whyoleg.cryptography.providers.base.operations.* |
| 14 | +import dev.whyoleg.cryptography.providers.cryptokit.internal.swiftinterop.* |
| 15 | +import dev.whyoleg.cryptography.random.* |
| 16 | +import kotlinx.cinterop.* |
| 17 | +import platform.Foundation.* |
| 18 | + |
| 19 | +internal object CryptoKitAesGcm : AES.GCM { |
| 20 | + override fun keyDecoder(): KeyDecoder<AES.Key.Format, AES.GCM.Key> = AesKeyDecoder() |
| 21 | + |
| 22 | + override fun keyGenerator(keySize: BinarySize): KeyGenerator<AES.GCM.Key> = AesGcmKeyGenerator(keySize.inBytes) |
| 23 | +} |
| 24 | + |
| 25 | +private class AesKeyDecoder : KeyDecoder<AES.Key.Format, AES.GCM.Key> { |
| 26 | + override fun decodeFromByteArrayBlocking(format: AES.Key.Format, bytes: ByteArray): AES.GCM.Key = when (format) { |
| 27 | + AES.Key.Format.RAW -> { |
| 28 | + require(bytes.size == 16 || bytes.size == 24 || bytes.size == 32) { |
| 29 | + "AES key size must be 128, 192 or 256 bits" |
| 30 | + } |
| 31 | + AesGcmKey(bytes.copyOf()) |
| 32 | + } |
| 33 | + AES.Key.Format.JWK -> error("JWK is not supported") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +private class AesGcmKeyGenerator(private val keySizeBytes: Int) : KeyGenerator<AES.GCM.Key> { |
| 38 | + override fun generateKeyBlocking(): AES.GCM.Key { |
| 39 | + val key = CryptographyRandom.nextBytes(keySizeBytes) |
| 40 | + return AesGcmKey(key) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +private class AesGcmKey(private val key: ByteArray) : AES.GCM.Key { |
| 45 | + override fun cipher(tagSize: BinarySize): AES.IvAuthenticatedCipher { |
| 46 | + require(tagSize == 16.bytes) { "GCM tag size must be 16 bytes, but was $tagSize" } |
| 47 | + return AesGcmCipher(key, tagSize.inBytes) |
| 48 | + } |
| 49 | + |
| 50 | + override fun encodeToByteArrayBlocking(format: AES.Key.Format): ByteArray = when (format) { |
| 51 | + AES.Key.Format.RAW -> key.copyOf() |
| 52 | + AES.Key.Format.JWK -> error("JWK is not supported") |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +private class AesGcmCipher( |
| 57 | + private val key: ByteArray, |
| 58 | + private val tagSize: Int, |
| 59 | +) : BaseAesIvAuthenticatedCipher { |
| 60 | + private val ivSize: Int get() = 12 |
| 61 | + |
| 62 | + override fun createEncryptFunction(associatedData: ByteArray?): CipherFunction { |
| 63 | + val iv = CryptographyRandom.nextBytes(ivSize) |
| 64 | + return BaseAesImplicitIvEncryptFunction(iv, createEncryptFunctionWithIv(iv, associatedData)) |
| 65 | + } |
| 66 | + |
| 67 | + override fun createDecryptFunction(associatedData: ByteArray?): CipherFunction { |
| 68 | + return BaseAesImplicitIvDecryptFunction(ivSize) { iv, startIndex -> |
| 69 | + createDecryptFunctionWithIv(iv, startIndex, associatedData) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + override fun createEncryptFunctionWithIv(iv: ByteArray, associatedData: ByteArray?): CipherFunction { |
| 74 | + require(iv.size == ivSize) { "IV size is wrong" } |
| 75 | + |
| 76 | + return AccumulatingCipherFunction { plaintext -> |
| 77 | + plaintext.useNSData { plaintextData -> |
| 78 | + iv.useNSData { ivData -> |
| 79 | + key.useNSData { keyData -> |
| 80 | + (associatedData ?: EmptyByteArray).useNSData { adData -> |
| 81 | + swiftTry { error -> |
| 82 | + SwiftAesGcm.encryptWithKey( |
| 83 | + key = keyData, |
| 84 | + nonce = ivData, |
| 85 | + plaintext = plaintextData, |
| 86 | + authenticatedData = adData, |
| 87 | + error = error |
| 88 | + ) |
| 89 | + }.toByteArray().let { |
| 90 | + it.copyOfRange(ivSize, it.size) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private fun createDecryptFunctionWithIv(iv: ByteArray, startIndex: Int, associatedData: ByteArray?): CipherFunction { |
| 100 | + require(iv.size - startIndex >= ivSize) { "IV size is wrong" } |
| 101 | + |
| 102 | + return AccumulatingCipherFunction { ciphertext -> |
| 103 | + ciphertext.useNSData(endIndex = ciphertext.size - tagSize) { ciphertextData -> |
| 104 | + ciphertext.useNSData(startIndex = ciphertext.size - tagSize) { tagData -> |
| 105 | + iv.useNSData(startIndex, startIndex + ivSize) { ivData -> |
| 106 | + key.useNSData { keyData -> |
| 107 | + (associatedData ?: EmptyByteArray).useNSData { adData -> |
| 108 | + swiftTry { error -> |
| 109 | + SwiftAesGcm.decryptWithKey( |
| 110 | + key = keyData, |
| 111 | + nonce = ivData, |
| 112 | + ciphertext = ciphertextData, |
| 113 | + tag = tagData, |
| 114 | + authenticatedData = adData, |
| 115 | + error = error |
| 116 | + ) |
| 117 | + }.toByteArray() |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + override fun createDecryptFunctionWithIv(iv: ByteArray, associatedData: ByteArray?): CipherFunction { |
| 127 | + return createDecryptFunctionWithIv(iv, 0, associatedData) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +@OptIn(BetaInteropApi::class) |
| 132 | +private fun <T : Any> swiftTry( |
| 133 | + block: (error: CPointer<ObjCObjectVar<NSError?>>) -> T?, |
| 134 | +): T = memScoped { |
| 135 | + val errorH = alloc<ObjCObjectVar<NSError?>>() |
| 136 | + when (val result = block(errorH.ptr)) { |
| 137 | + null -> error("Swift call failed: ${errorH.value?.localizedDescription ?: "unknown error"}") |
| 138 | + else -> result |
| 139 | + } |
| 140 | +} |
0 commit comments