Remove some AES modes

This commit is contained in:
Hugo Pointcheval 2021-05-08 20:10:19 +02:00
parent 501e5bcf08
commit 9160338e7a
3 changed files with 13 additions and 30 deletions

View File

@ -8,15 +8,11 @@ import javax.crypto.spec.SecretKeySpec
enum class CipherAlgorithm(val spec: String) { enum class CipherAlgorithm(val spec: String) {
AES("AES"), AES("AES"),
BlowFish("BLOWFISH")
} }
enum class BlockCipherMode(val instance: String) { enum class BlockCipherMode(val instance: String) {
ECB("ECB"),
CBC("CBC"), CBC("CBC"),
CFB("CFB"),
GCM("GCM"), GCM("GCM"),
CGM("CGM"),
} }
enum class Padding(val instance: String) { enum class Padding(val instance: String) {
@ -35,18 +31,14 @@ class Cipher {
fun getCipherAlgorithm(dartAlgorithm: String) : CipherAlgorithm { fun getCipherAlgorithm(dartAlgorithm: String) : CipherAlgorithm {
return when (dartAlgorithm) { return when (dartAlgorithm) {
"aes" -> CipherAlgorithm.AES "aes" -> CipherAlgorithm.AES
"blowfish" -> CipherAlgorithm.BlowFish
else -> CipherAlgorithm.AES else -> CipherAlgorithm.AES
} }
} }
fun getInstance(mode : String, padding : String) : CipherParameters { fun getInstance(mode : String, padding : String) : CipherParameters {
val m = when (mode) { val m = when (mode) {
"ecb" -> BlockCipherMode.ECB
"cbc" -> BlockCipherMode.CBC "cbc" -> BlockCipherMode.CBC
"cfb" -> BlockCipherMode.CFB
"gcm" -> BlockCipherMode.GCM "gcm" -> BlockCipherMode.GCM
"cgm" -> BlockCipherMode.CGM
else -> throw Exception() else -> throw Exception()
} }
val p = when (padding) { val p = when (padding) {

View File

@ -6,10 +6,10 @@ import 'dart:typed_data';
import 'key.dart'; import 'key.dart';
/// Represents different cipher algorithms /// Represents different cipher algorithms
enum CipherAlgorithm { AES, BlowFish, None } enum CipherAlgorithm { AES, None }
/// Represents different block cipher modes /// Represents different block cipher modes
enum BlockCipherMode { ECB, CBC, CFB, GCM, CGM } enum BlockCipherMode { CBC, GCM }
/// Represents different padding /// Represents different padding
enum PlainTextPadding { PKCS5, None } enum PlainTextPadding { PKCS5, None }

View File

@ -48,13 +48,6 @@ class SecretKey extends Key {
if (!_supportedSizes.contains(size)) { if (!_supportedSizes.contains(size)) {
throw KeyException("AES must be 128, 192 or 256 bits long."); throw KeyException("AES must be 128, 192 or 256 bits long.");
} }
} else if (algorithm == CipherAlgorithm.BlowFish) {
List<int> supportedSizes =
List<int>.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 { try {
@ -69,8 +62,6 @@ class SecretKey extends Key {
/// This represents a keypair, usefull in /// This represents a keypair, usefull in
/// algorithms like RSA. /// algorithms like RSA.
class KeyPair extends Key { class KeyPair extends Key {
List<String> _supportedAlgorithms = ["RSA"];
PublicKey _publicKey; PublicKey _publicKey;
PrivateKey _privateKey; PrivateKey _privateKey;
@ -84,27 +75,25 @@ class KeyPair extends Key {
bool get isComplete => (_publicKey != null && _privateKey != null); bool get isComplete => (_publicKey != null && _privateKey != null);
/// Creates a key pair from public and private keys. /// 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; _publicKey = publicKey;
_privateKey = privateKey; _privateKey = privateKey;
_algo = algorithm;
} }
/// Creates a key pair from a specific size. /// Creates a key pair from a specific size.
KeyPair.generate(KeySpec keySpec) { static Future<KeyPair> generate(KeySpec keySpec) async {
List<String> _supportedAlgorithms = ["RSA"];
if (!_supportedAlgorithms.contains(keySpec.algorithm)) { if (!_supportedAlgorithms.contains(keySpec.algorithm)) {
throw KeyException(keySpec.algorithm + " not supported!"); throw KeyException(keySpec.algorithm + " not supported!");
} }
_generate(keySpec);
}
Future<void> _generate(KeySpec keySpec) async {
if (keySpec.algorithm == "RSA") { if (keySpec.algorithm == "RSA") {
RSAKeySpec spec = keySpec; RSAKeySpec spec = keySpec;
try { try {
List<Uint8List> kp = await Platform().rsaKeypairGen(spec.size); List<Uint8List> kp = await Platform().rsaKeypairGen(spec.size);
_publicKey = PublicKey.fromBytes(kp.first); PublicKey _publicKey = PublicKey.fromBytes(kp.first, "RSA");
_privateKey = PrivateKey.fromBytes(kp.last); PrivateKey _privateKey = PrivateKey.fromBytes(kp.last, "RSA");
_algo = "RSA"; return KeyPair.from(_publicKey, _privateKey, algorithm: "RSA");
} on PlatformException catch (e) { } on PlatformException catch (e) {
throw KeyException(e); throw KeyException(e);
} }
@ -117,11 +106,13 @@ class KeyPair extends Key {
/// This represents a public key /// This represents a public key
class PublicKey extends Key { class PublicKey extends Key {
/// Creates a public key from raw byte array /// 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 /// This represents a private key
class PrivateKey extends Key { class PrivateKey extends Key {
/// Creates a private key from raw byte array /// 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);
} }