From 198cfbfa9bfcab799076f7617776d7c3e6b64540 Mon Sep 17 00:00:00 2001 From: cach30verfl0w Date: Thu, 13 Jun 2024 01:11:48 +0200 Subject: [PATCH] Add purpose validation on initialization of key generator --- .../io/karma/advcrypto/android/providers/PQCryptoProvider.kt | 2 +- .../advcrypto/algorithm/delegates/KeyGeneratorDelegate.kt | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/providers/PQCryptoProvider.kt b/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/providers/PQCryptoProvider.kt index 7c3ce9b..9dcf22b 100644 --- a/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/providers/PQCryptoProvider.kt +++ b/kmp-advcrypto/src/androidMain/kotlin/io/karma/advcrypto/android/providers/PQCryptoProvider.kt @@ -58,7 +58,7 @@ class PQCryptoProvider: AbstractProvider( algorithm("Kyber") { keyGenerator( - Key.PURPOSE_ENCRYPT or Key.PURPOSE_ENCRYPT, + Key.PURPOSE_ENCRYPT or Key.PURPOSE_DECRYPT, arrayOf(512, 768, 1024), 1024 ) { diff --git a/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/algorithm/delegates/KeyGeneratorDelegate.kt b/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/algorithm/delegates/KeyGeneratorDelegate.kt index 70b406b..cc66388 100644 --- a/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/algorithm/delegates/KeyGeneratorDelegate.kt +++ b/kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/algorithm/delegates/KeyGeneratorDelegate.kt @@ -120,7 +120,9 @@ class KeyGeneratorDelegate( */ fun initializer(closure: (KeyGeneratorSpec) -> C) { this.initializer = { keyGenSpec -> - // TODO: Add validation for purposes + if ((keyGenSpec.purposes and this.keyPurposes) != keyGenSpec.purposes) { + throw IllegalArgumentException("Key purposes ${keyGenSpec.purposes} not supported") + } KeyGenContext(keyGenSpec, closure(keyGenSpec)) } }