Skip to content

Commit

Permalink
feat: AES & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hamada147 committed Dec 20, 2022
1 parent 69dfbd1 commit d60cafb
Show file tree
Hide file tree
Showing 36 changed files with 432 additions and 343 deletions.
2 changes: 1 addition & 1 deletion UUID/UUID.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Pod::Spec.new do |spec|
spec.osx.deployment_target = '12.0'
spec.tvos.deployment_target = '13.0'
spec.watchos.deployment_target = '8.0'
spec.dependency 'IOHKSecureRandomGeneration', '1.0.0'

spec.pod_target_xcconfig = {
'KOTLIN_PROJECT_PATH' => ':uuid',
Expand Down
6 changes: 6 additions & 0 deletions UUID/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ kotlin {
framework {
this.baseName = currentModuleName
}
// workaround for KMM bug
pod("IOHKSecureRandomGeneration") {
version = "1.0.0"
packageName = "IOHKSecureRandomGeneration1"
source = path(project.file("../iOSLibs/IOHKSecureRandomGeneration"))
}
}
}

Expand Down
1 change: 1 addition & 0 deletions aes/aes.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Pod::Spec.new do |spec|
spec.tvos.deployment_target = '13.0'
spec.watchos.deployment_target = '8.0'
spec.dependency 'IOHKAES', '1.0.0'
spec.dependency 'IOHKSecureRandomGeneration', '1.0.0'

spec.pod_target_xcconfig = {
'KOTLIN_PROJECT_PATH' => ':aes',
Expand Down
10 changes: 10 additions & 0 deletions aes/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ kotlin {
framework {
this.baseName = currentModuleName
}
// workaround for KMM bug
pod("IOHKSecureRandomGeneration") {
version = "1.0.0"
packageName = "IOHKSecureRandomGeneration1"
source = path(project.file("../iOSLibs/IOHKSecureRandomGeneration"))
}

pod("IOHKAES") {
version = "1.0.0"
Expand All @@ -101,6 +107,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
api(project(":utils"))
api(project(":base-symmetric-encryption"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}
Expand All @@ -121,6 +128,9 @@ kotlin {
}
val jsMain by getting {
dependencies {
implementation("org.jetbrains.kotlin-wrappers:kotlin-web:1.0.0-pre.461")
implementation("org.jetbrains.kotlin-wrappers:kotlin-node:18.11.13-pre.461")

// Polyfill dependencies
implementation(npm("crypto-browserify", "3.12.0"))
implementation(npm("stream-browserify", "3.0.0"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ actual final class AES actual constructor(
actual companion object : AESKeyGeneration {
private const val AUTH_TAG_SIZE = 128

override suspend fun createRandomAESKey(algorithm: KAESAlgorithm): KMMSymmetricKey {
override suspend fun createRandomAESKey(algorithm: KAESAlgorithm, blockMode: KAESBlockMode): KMMSymmetricKey {
val keygen = KeyGenerator.getInstance("AES")
keygen.init(algorithm.keySize())
return KMMSymmetricKey(keygen.generateKey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface AESKeyGeneration {
*
* @param algorithm AES Key algorithm
*/
suspend fun createRandomAESKey(algorithm: KAESAlgorithm): KMMSymmetricKey
suspend fun createRandomAESKey(algorithm: KAESAlgorithm, blockMode: KAESBlockMode = KAESBlockMode.GCM): KMMSymmetricKey
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.iohk.atala.prism.apollo.aes

import io.iohk.atala.prism.apollo.utils.NativeTypeInterface
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

expect enum class KAESAlgorithm : NativeTypeInterface<KAESAlgorithmNativeType> {
AES_128,
AES_192,
AES_256;
}

@OptIn(ExperimentalJsExport::class)
@JsExport
fun KAESAlgorithm.keySize(): Int {
return when (this) {
KAESAlgorithm.AES_128 -> 128
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AESTests {
val aes = AES(
algo,
KAESBlockMode.CBC,
KAESPadding.PKCS7PADDING,
KAESPadding.PKCS5PADDING,
key,
KMMSymmetricKey.createRandomIV(16)
)
Expand Down
58 changes: 33 additions & 25 deletions aes/src/iosMain/kotlin/io.iohk.atala.prism.apollo.aes/AES.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.iohk.atala.prism.apollo.aes

import cocoapods.IOHKAES.AESOptionsNone
import cocoapods.IOHKAES.KMMFunctions
import cocoapods.IOHKAES.AESOptionsPkcs7Padding
import cocoapods.IOHKAES.IOHKAES
import io.iohk.atala.prism.apollo.utils.KMMSymmetricKey
import io.iohk.atala.prism.apollo.utils.toByteArray
import io.iohk.atala.prism.apollo.utils.toNSData
import kotlinx.cinterop.autoreleasepool
import platform.Foundation.NSData

actual typealias KAESAlgorithmNativeType = Long
Expand All @@ -19,38 +21,44 @@ actual final class AES actual constructor(
actual val iv: ByteArray?
) : AESEncryptor, AESDecryptor {
override suspend fun encrypt(data: ByteArray): ByteArray {
val encryptedData = KMMFunctions.aesDecryptionWithAlgorithm(
algorithm.nativeValue(),
AESOptionsNone,
blockMode.nativeValue().toUInt(),
padding.nativeValue(),
data.toNSData(),
key.nativeValue,
iv?.toNSData() ?: NSData()
)?.toByteArray()
autoreleasepool {
val encryptedData = IOHKAES.aesEncryptionWithAlgorithm(
algorithm.nativeValue(),
AESOptionsPkcs7Padding,
blockMode.nativeValue().toUInt(),
padding.nativeValue(),
data.toNSData(),
key.nativeValue,
iv?.toNSData() ?: NSData()
)?.toByteArray()

return encryptedData ?: ByteArray(0)
return encryptedData ?: ByteArray(0)
}
}

override suspend fun decrypt(data: ByteArray): ByteArray {
val decryptedData = KMMFunctions.aesDecryptionWithAlgorithm(
algorithm.nativeValue(),
AESOptionsNone,
blockMode.nativeValue().toUInt(),
padding.nativeValue(),
data.toNSData(),
key.nativeValue,
iv?.toNSData() ?: NSData()
)?.toByteArray()
autoreleasepool {
val decryptedData = IOHKAES.aesDecryptionWithAlgorithm(
algorithm.nativeValue(),
AESOptionsNone,
blockMode.nativeValue().toUInt(),
padding.nativeValue(),
data.toNSData(),
key.nativeValue,
iv?.toNSData() ?: NSData()
)?.toByteArray()

return decryptedData ?: ByteArray(0)
return decryptedData ?: ByteArray(0)
}
}

actual companion object : AESKeyGeneration {
override suspend fun createRandomAESKey(algorithm: KAESAlgorithm): KMMSymmetricKey {
return KMMSymmetricKey(
KMMFunctions.generateAESKeyWithAlgorithm(algorithm.nativeValue())
)
override suspend fun createRandomAESKey(algorithm: KAESAlgorithm, blockMode: KAESBlockMode): KMMSymmetricKey {
autoreleasepool {
return KMMSymmetricKey(
IOHKAES.generateAESKeyWithAlgorithm(algorithm.nativeValue())
)
}
}
}
}
Loading

0 comments on commit d60cafb

Please sign in to comment.