From 9160338e7ac38e48ad5596a8eab2fb8d413c65f5 Mon Sep 17 00:00:00 2001 From: Pointcheval Hugo Date: Sat, 8 May 2021 20:10:19 +0200 Subject: [PATCH] Remove some AES modes --- .../fr/pointcheval/native_crypto/Cipher.kt | 8 ----- lib/src/cipher.dart | 4 +-- lib/src/key.dart | 31 +++++++------------ 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/android/src/main/kotlin/fr/pointcheval/native_crypto/Cipher.kt b/android/src/main/kotlin/fr/pointcheval/native_crypto/Cipher.kt index fece17c..5302993 100644 --- a/android/src/main/kotlin/fr/pointcheval/native_crypto/Cipher.kt +++ b/android/src/main/kotlin/fr/pointcheval/native_crypto/Cipher.kt @@ -8,15 +8,11 @@ import javax.crypto.spec.SecretKeySpec enum class CipherAlgorithm(val spec: String) { AES("AES"), - BlowFish("BLOWFISH") } enum class BlockCipherMode(val instance: String) { - ECB("ECB"), CBC("CBC"), - CFB("CFB"), GCM("GCM"), - CGM("CGM"), } enum class Padding(val instance: String) { @@ -35,18 +31,14 @@ class Cipher { fun getCipherAlgorithm(dartAlgorithm: String) : CipherAlgorithm { return when (dartAlgorithm) { "aes" -> CipherAlgorithm.AES - "blowfish" -> CipherAlgorithm.BlowFish else -> CipherAlgorithm.AES } } fun getInstance(mode : String, padding : String) : CipherParameters { val m = when (mode) { - "ecb" -> BlockCipherMode.ECB "cbc" -> BlockCipherMode.CBC - "cfb" -> BlockCipherMode.CFB "gcm" -> BlockCipherMode.GCM - "cgm" -> BlockCipherMode.CGM else -> throw Exception() } val p = when (padding) { diff --git a/lib/src/cipher.dart b/lib/src/cipher.dart index 17e3f5a..b1e34bf 100644 --- a/lib/src/cipher.dart +++ b/lib/src/cipher.dart @@ -6,10 +6,10 @@ import 'dart:typed_data'; import 'key.dart'; /// Represents different cipher algorithms -enum CipherAlgorithm { AES, BlowFish, None } +enum CipherAlgorithm { AES, None } /// Represents different block cipher modes -enum BlockCipherMode { ECB, CBC, CFB, GCM, CGM } +enum BlockCipherMode { CBC, GCM } /// Represents different padding enum PlainTextPadding { PKCS5, None } diff --git a/lib/src/key.dart b/lib/src/key.dart index 3ee2db8..16ba29f 100644 --- a/lib/src/key.dart +++ b/lib/src/key.dart @@ -48,13 +48,6 @@ class SecretKey extends Key { if (!_supportedSizes.contains(size)) { throw KeyException("AES must be 128, 192 or 256 bits long."); } - } else if (algorithm == CipherAlgorithm.BlowFish) { - List supportedSizes = - List.generate(52, (int index) => (index + 4) * 8); - if (!supportedSizes.contains(size)) { - throw KeyException( - "Blowfish must be between 4 and 56 bytes (32 and 448 bits) long."); - } } try { @@ -69,8 +62,6 @@ class SecretKey extends Key { /// This represents a keypair, usefull in /// algorithms like RSA. class KeyPair extends Key { - List _supportedAlgorithms = ["RSA"]; - PublicKey _publicKey; PrivateKey _privateKey; @@ -84,27 +75,25 @@ class KeyPair extends Key { bool get isComplete => (_publicKey != null && _privateKey != null); /// Creates a key pair from public and private keys. - KeyPair.from(PublicKey publicKey, PrivateKey privateKey) { + KeyPair.from(PublicKey publicKey, PrivateKey privateKey, {String algorithm}) { _publicKey = publicKey; _privateKey = privateKey; + _algo = algorithm; } /// Creates a key pair from a specific size. - KeyPair.generate(KeySpec keySpec) { + static Future generate(KeySpec keySpec) async { + List _supportedAlgorithms = ["RSA"]; if (!_supportedAlgorithms.contains(keySpec.algorithm)) { throw KeyException(keySpec.algorithm + " not supported!"); } - _generate(keySpec); - } - - Future _generate(KeySpec keySpec) async { if (keySpec.algorithm == "RSA") { RSAKeySpec spec = keySpec; try { List kp = await Platform().rsaKeypairGen(spec.size); - _publicKey = PublicKey.fromBytes(kp.first); - _privateKey = PrivateKey.fromBytes(kp.last); - _algo = "RSA"; + PublicKey _publicKey = PublicKey.fromBytes(kp.first, "RSA"); + PrivateKey _privateKey = PrivateKey.fromBytes(kp.last, "RSA"); + return KeyPair.from(_publicKey, _privateKey, algorithm: "RSA"); } on PlatformException catch (e) { throw KeyException(e); } @@ -117,11 +106,13 @@ class KeyPair extends Key { /// This represents a public key class PublicKey extends Key { /// Creates a public key from raw byte array - PublicKey.fromBytes(Uint8List bytes) : super(bytes: bytes); + PublicKey.fromBytes(Uint8List bytes, String algorithm) + : super(bytes: bytes, algorithm: algorithm); } /// This represents a private key class PrivateKey extends Key { /// Creates a private key from raw byte array - PrivateKey.fromBytes(Uint8List bytes) : super(bytes: bytes); + PrivateKey.fromBytes(Uint8List bytes, String algorithm) + : super(bytes: bytes, algorithm: algorithm); }