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) {
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) {

View File

@ -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 }

View File

@ -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<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 {
@ -69,8 +62,6 @@ class SecretKey extends Key {
/// This represents a keypair, usefull in
/// algorithms like RSA.
class KeyPair extends Key {
List<String> _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<KeyPair> generate(KeySpec keySpec) async {
List<String> _supportedAlgorithms = ["RSA"];
if (!_supportedAlgorithms.contains(keySpec.algorithm)) {
throw KeyException(keySpec.algorithm + " not supported!");
}
_generate(keySpec);
}
Future<void> _generate(KeySpec keySpec) async {
if (keySpec.algorithm == "RSA") {
RSAKeySpec spec = keySpec;
try {
List<Uint8List> 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);
}