From 19a75e46c5163bdb5cedf5c782a7408d327c943b Mon Sep 17 00:00:00 2001 From: XavierChanth Date: Tue, 18 Feb 2025 08:01:38 -0500 Subject: [PATCH] chore: reorganize at_chops --- .../at_chops/example/at_chops_example.dart | 18 +++--- .../zariot/at_chops_secure_element.dart | 1 + packages/at_chops/lib/at_chops.dart | 44 ++++--------- .../at_chops/lib/src/algorithm/algo_type.dart | 17 ----- .../src/algorithm/default_hashing_algo.dart | 18 ------ .../src/algorithm/padding/padding_params.dart | 13 ---- .../lib/src/algorithm/pkam_signing_algo.dart | 56 ---------------- .../lib/src/{util => }/at_chops_util.dart | 29 ++++----- packages/at_chops/lib/src/at_keys_crypto.dart | 9 +-- .../at_chops.dart} | 14 ++-- .../src/{ => at_platform}/at_chops_impl.dart | 64 ++++++++----------- .../impl => at_platform}/at_chops_keys.dart | 16 ++--- .../lib/src/at_platform/at_platform.dart | 4 ++ .../at_platform/standard_at_keys_names.dart | 9 +++ .../aes_ctr_factory.dart | 2 +- .../aes_encryption_algo.dart | 16 ++--- .../at_encryption_algorithm.dart} | 30 ++------- .../lib/src/encryption/encryption.dart | 5 ++ .../initalisation_vector.dart} | 0 .../rsa_encryption_algo.dart | 15 ++--- .../argon2id_hashing_algo.dart | 37 ++++++++++- .../at_hashing_algo_factory.dart | 10 +-- .../lib/src/hashing/at_hashing_algorithm.dart | 27 ++++++++ .../at_chops/lib/src/hashing/hashing.dart | 5 ++ .../lib/src/hashing/md5_hashing_algo.dart | 9 +++ .../lib/src/hashing/sha512_hashing_algo.dart | 10 +++ .../lib/src/key/asymmetric_key_pair.dart | 31 +++++++++ .../{impl/aes_key.dart => at_aes_key.dart} | 24 ++++--- .../at_chops/lib/src/key/at_key_pair.dart | 23 ------- .../at_chops/lib/src/key/at_private_key.dart | 6 -- .../at_chops/lib/src/key/at_public_key.dart | 6 -- .../at_chops/lib/src/key/at_rsa_key_pair.dart | 31 +++++++++ .../src/key/impl/at_encryption_key_pair.dart | 6 -- .../lib/src/key/impl/at_pkam_key_pair.dart | 5 -- .../lib/src/key/impl/at_signing_key_pair.dart | 5 -- .../at_chops/lib/src/key/impl/cram_key.dart | 6 -- packages/at_chops/lib/src/key/key.dart | 4 ++ packages/at_chops/lib/src/key/key_names.dart | 6 -- packages/at_chops/lib/src/key/key_type.dart | 12 +++- .../at_chops/lib/src/key/symmetric_key.dart | 10 +++ .../at_chops/lib/src/model/at_encrypted.dart | 1 + .../{metadata => model}/at_signing_input.dart | 14 ++-- .../encryption_metadata.dart | 2 +- .../encryption_result.dart | 2 +- .../at_chops/lib/src/model/hash_params.dart | 36 ----------- packages/at_chops/lib/src/model/model.dart | 4 ++ .../{metadata => model}/signing_metadata.dart | 3 +- .../{metadata => model}/signing_result.dart | 2 +- .../at_chops/lib/src/padding/padding.dart | 2 + .../padding_algorithm.dart} | 0 .../pkcs7_padding.dart} | 20 +++++- .../lib/src/signing/at_signing_algorithm.dart | 22 +++++++ .../ecc_signing_algo.dart | 2 +- .../ed25519_signing_algo.dart | 2 +- .../lib/src/signing/pkam_signing_algo.dart | 8 +++ .../rsa_signing_algo.dart} | 15 +++-- .../at_chops/lib/src/signing/signing.dart | 5 ++ packages/at_chops/lib/types.dart | 29 ++++----- .../at_chops/test/aes_encrption_old_impl.dart | 8 +-- .../test/aes_encryption_algo_test.dart | 10 +-- packages/at_chops/test/aes_key_test.dart | 32 +++++----- packages/at_chops/test/at_chops_test.dart | 55 ++++++++-------- .../at_chops/test/at_chops_util_test.dart | 5 +- .../test/default_signing_algo_test.dart | 26 ++++---- .../at_chops/test/ecc_signing_algo_test.dart | 2 +- .../test/ed25519_signing_algo_test.dart | 2 +- .../at_chops/test/pkcs7_padding_test.dart | 17 +++-- .../test/rsa_encryption_algo_test.dart | 5 +- .../apkam_examples/enroll_app_listen.dart | 7 +- .../onboard/at_onboarding_service_impl.dart | 2 +- 70 files changed, 493 insertions(+), 500 deletions(-) delete mode 100644 packages/at_chops/lib/src/algorithm/algo_type.dart delete mode 100644 packages/at_chops/lib/src/algorithm/default_hashing_algo.dart delete mode 100644 packages/at_chops/lib/src/algorithm/padding/padding_params.dart delete mode 100644 packages/at_chops/lib/src/algorithm/pkam_signing_algo.dart rename packages/at_chops/lib/src/{util => }/at_chops_util.dart (70%) rename packages/at_chops/lib/src/{at_chops_base.dart => at_platform/at_chops.dart} (93%) rename packages/at_chops/lib/src/{ => at_platform}/at_chops_impl.dart (83%) rename packages/at_chops/lib/src/{key/impl => at_platform}/at_chops_keys.dart (52%) create mode 100644 packages/at_chops/lib/src/at_platform/at_platform.dart create mode 100644 packages/at_chops/lib/src/at_platform/standard_at_keys_names.dart rename packages/at_chops/lib/src/{algorithm => encryption}/aes_ctr_factory.dart (96%) rename packages/at_chops/lib/src/{algorithm => encryption}/aes_encryption_algo.dart (89%) rename packages/at_chops/lib/src/{algorithm/at_algorithm.dart => encryption/at_encryption_algorithm.dart} (54%) create mode 100644 packages/at_chops/lib/src/encryption/encryption.dart rename packages/at_chops/lib/src/{algorithm/at_iv.dart => encryption/initalisation_vector.dart} (100%) rename packages/at_chops/lib/src/{algorithm => encryption}/rsa_encryption_algo.dart (76%) rename packages/at_chops/lib/src/{algorithm => hashing}/argon2id_hashing_algo.dart (59%) rename packages/at_chops/lib/src/{factory => hashing}/at_hashing_algo_factory.dart (76%) create mode 100644 packages/at_chops/lib/src/hashing/at_hashing_algorithm.dart create mode 100644 packages/at_chops/lib/src/hashing/hashing.dart create mode 100644 packages/at_chops/lib/src/hashing/md5_hashing_algo.dart create mode 100644 packages/at_chops/lib/src/hashing/sha512_hashing_algo.dart create mode 100644 packages/at_chops/lib/src/key/asymmetric_key_pair.dart rename packages/at_chops/lib/src/key/{impl/aes_key.dart => at_aes_key.dart} (61%) delete mode 100644 packages/at_chops/lib/src/key/at_key_pair.dart delete mode 100644 packages/at_chops/lib/src/key/at_private_key.dart delete mode 100644 packages/at_chops/lib/src/key/at_public_key.dart create mode 100644 packages/at_chops/lib/src/key/at_rsa_key_pair.dart delete mode 100644 packages/at_chops/lib/src/key/impl/at_encryption_key_pair.dart delete mode 100644 packages/at_chops/lib/src/key/impl/at_pkam_key_pair.dart delete mode 100644 packages/at_chops/lib/src/key/impl/at_signing_key_pair.dart delete mode 100644 packages/at_chops/lib/src/key/impl/cram_key.dart create mode 100644 packages/at_chops/lib/src/key/key.dart delete mode 100644 packages/at_chops/lib/src/key/key_names.dart create mode 100644 packages/at_chops/lib/src/key/symmetric_key.dart rename packages/at_chops/lib/src/{metadata => model}/at_signing_input.dart (86%) rename packages/at_chops/lib/src/{metadata => model}/encryption_metadata.dart (83%) rename packages/at_chops/lib/src/{metadata => model}/encryption_result.dart (83%) delete mode 100644 packages/at_chops/lib/src/model/hash_params.dart create mode 100644 packages/at_chops/lib/src/model/model.dart rename packages/at_chops/lib/src/{metadata => model}/signing_metadata.dart (81%) rename packages/at_chops/lib/src/{metadata => model}/signing_result.dart (89%) create mode 100644 packages/at_chops/lib/src/padding/padding.dart rename packages/at_chops/lib/src/{algorithm/padding/padding.dart => padding/padding_algorithm.dart} (100%) rename packages/at_chops/lib/src/{algorithm/padding/pkcs7padding.dart => padding/pkcs7_padding.dart} (64%) create mode 100644 packages/at_chops/lib/src/signing/at_signing_algorithm.dart rename packages/at_chops/lib/src/{algorithm => signing}/ecc_signing_algo.dart (96%) rename packages/at_chops/lib/src/{algorithm => signing}/ed25519_signing_algo.dart (95%) create mode 100644 packages/at_chops/lib/src/signing/pkam_signing_algo.dart rename packages/at_chops/lib/src/{algorithm/default_signing_algo.dart => signing/rsa_signing_algo.dart} (78%) create mode 100644 packages/at_chops/lib/src/signing/signing.dart diff --git a/packages/at_chops/example/at_chops_example.dart b/packages/at_chops/example/at_chops_example.dart index cba53060..85572855 100644 --- a/packages/at_chops/example/at_chops_example.dart +++ b/packages/at_chops/example/at_chops_example.dart @@ -3,7 +3,8 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; +import 'package:at_chops/types.dart'; import 'package:encrypt/encrypt.dart'; /// Usage: @@ -11,7 +12,7 @@ import 'package:encrypt/encrypt.dart'; /// dart run at_chops_example.dart /// or /// Using key pairs from atKeys file -/// dart run at_chops_example.dart +/// dart run at_chops_example.dart (path_to_atkeys_file) void main(List args) async { AtChops atChops; if (args.isNotEmpty) { @@ -36,7 +37,8 @@ void main(List args) async { atChops = AtChopsImpl(atChopsKeys); } - var atEncryptionKeyPair = atChops.atChopsKeys.atEncryptionKeyPair; + var atEncryptionKeyPair = + atChops.atChopsKeys.atEncryptionKeyPair as AtRSAKeyPair?; // 1 - Encryption and decryption using asymmetric key pair final data = 'Hello World'; //1.1 encrypt the data using [atEncryptionKeyPair.publicKey] @@ -56,7 +58,7 @@ void main(List args) async { signingInput.signingAlgoType = SigningAlgoType.rsa2048; signingInput.hashingAlgoType = HashingAlgoType.sha512; AtSigningAlgorithm signingAlgorithm = - DefaultSigningAlgo(atEncryptionKeyPair, signingInput.hashingAlgoType); + RSASigningAlgo(atEncryptionKeyPair, signingInput.hashingAlgoType); signingInput.signingAlgorithm = signingAlgorithm; // 2.2 sign the data final dataSigningResult = atChops.sign(signingInput); @@ -68,8 +70,8 @@ void main(List args) async { atEncryptionKeyPair!.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha512; - AtSigningAlgorithm verifyAlgorithm = DefaultSigningAlgo( - atEncryptionKeyPair, verificationInput.hashingAlgoType); + AtSigningAlgorithm verifyAlgorithm = + RSASigningAlgo(atEncryptionKeyPair, verificationInput.hashingAlgoType); verificationInput.signingAlgorithm = verifyAlgorithm; // 2.4 verify the signature AtSigningResult dataVerificationResult = atChops.verify(verificationInput); @@ -78,12 +80,12 @@ void main(List args) async { } AtChops _createAtChops(Map atKeysDataMap) { - final atEncryptionKeyPair = AtEncryptionKeyPair.create( + final atEncryptionKeyPair = AtRSAKeyPair.create( _decryptValue(atKeysDataMap[AuthKeyType.encryptionPublicKey]!, atKeysDataMap[AuthKeyType.selfEncryptionKey]!)!, _decryptValue(atKeysDataMap[AuthKeyType.encryptionPrivateKey]!, atKeysDataMap[AuthKeyType.selfEncryptionKey]!)!); - final atPkamKeyPair = AtPkamKeyPair.create( + final atPkamKeyPair = AtRSAKeyPair.create( _decryptValue(atKeysDataMap[AuthKeyType.pkamPublicKey]!, atKeysDataMap[AuthKeyType.selfEncryptionKey]!)!, _decryptValue(atKeysDataMap[AuthKeyType.pkamPrivateKey]!, diff --git a/packages/at_chops/example/zariot/at_chops_secure_element.dart b/packages/at_chops/example/zariot/at_chops_secure_element.dart index 38a8a175..2b5ed439 100644 --- a/packages/at_chops/example/zariot/at_chops_secure_element.dart +++ b/packages/at_chops/example/zariot/at_chops_secure_element.dart @@ -1,5 +1,6 @@ import 'dart:convert'; import 'dart:typed_data'; +import 'package:at_chops/types.dart'; import 'package:crypto/crypto.dart'; import 'package:at_chops/at_chops.dart'; diff --git a/packages/at_chops/lib/at_chops.dart b/packages/at_chops/lib/at_chops.dart index 9f985cc8..07fad1db 100644 --- a/packages/at_chops/lib/at_chops.dart +++ b/packages/at_chops/lib/at_chops.dart @@ -1,34 +1,16 @@ -library at_chops; +// export cryptography wrappers +export 'src/hashing/hashing.dart'; +export 'src/encryption/encryption.dart'; +export 'src/signing/signing.dart'; +export 'src/padding/padding.dart'; -export 'src/algorithm/aes_encryption_algo.dart'; -export 'src/algorithm/algo_type.dart'; -export 'src/algorithm/at_iv.dart'; -export 'src/algorithm/default_signing_algo.dart'; -export 'src/algorithm/ecc_signing_algo.dart'; -export 'src/algorithm/pkam_signing_algo.dart'; -export 'src/algorithm/rsa_encryption_algo.dart'; -export 'src/at_chops_base.dart'; -export 'src/at_chops_impl.dart'; +// export inteface types +export 'src/key/key.dart'; +export 'src/model/model.dart'; -// Class to encrypt/decrypt atKeys file based on the password specified. -export 'src/at_keys_crypto.dart'; -export 'src/key/at_key_pair.dart'; -export 'src/key/at_private_key.dart'; -export 'src/key/at_public_key.dart'; -export 'src/key/impl/aes_key.dart'; -export 'src/key/impl/at_chops_keys.dart'; -export 'src/key/impl/at_encryption_key_pair.dart'; -export 'src/key/impl/at_pkam_key_pair.dart'; -export 'src/key/key_type.dart'; -export 'src/metadata/at_signing_input.dart'; -export 'src/metadata/encryption_metadata.dart'; -export 'src/metadata/encryption_result.dart'; -export 'src/metadata/signing_metadata.dart'; -export 'src/metadata/signing_result.dart'; - -// A model class which represents the encrypted AtKeys with a passphrase. -export 'src/model/at_encrypted.dart'; +// export at_platform cryptography wrappers +export 'src/at_platform/at_platform.dart'; -// Class representing the hashing parameters to pass to an hashing algorithm. -export 'src/model/hash_params.dart' hide HashParams; -export 'src/util/at_chops_util.dart'; +// misc +export 'src/at_chops_util.dart'; +export 'src/at_keys_crypto.dart'; diff --git a/packages/at_chops/lib/src/algorithm/algo_type.dart b/packages/at_chops/lib/src/algorithm/algo_type.dart deleted file mode 100644 index 2e109180..00000000 --- a/packages/at_chops/lib/src/algorithm/algo_type.dart +++ /dev/null @@ -1,17 +0,0 @@ -// ignore: constant_identifier_names -import 'package:at_commons/at_commons.dart'; - -enum SigningAlgoType { ecc_secp256r1, rsa2048, rsa4096 } - -enum HashingAlgoType { - sha256, - sha512, - md5, - argon2id; - - static HashingAlgoType fromString(String name) { - return HashingAlgoType.values.firstWhere( - (algo) => algo.name == name.toLowerCase(), - orElse: () => throw AtException('Invalid hashing algo type')); - } -} diff --git a/packages/at_chops/lib/src/algorithm/default_hashing_algo.dart b/packages/at_chops/lib/src/algorithm/default_hashing_algo.dart deleted file mode 100644 index 236a2e45..00000000 --- a/packages/at_chops/lib/src/algorithm/default_hashing_algo.dart +++ /dev/null @@ -1,18 +0,0 @@ -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/model/hash_params.dart'; -import 'package:crypto/crypto.dart'; - -class DefaultHash implements AtHashingAlgorithm, String> { - @override - String hash(List data, {HashParams? hashParams}) { - return md5.convert(data).toString(); - } -} - -class SHA512HashingAlgo implements AtHashingAlgorithm, String> { - @override - String hash(List data, {covariant HashParams? hashParams}) { - Digest digest = sha512.convert(data); - return digest.toString(); - } -} diff --git a/packages/at_chops/lib/src/algorithm/padding/padding_params.dart b/packages/at_chops/lib/src/algorithm/padding/padding_params.dart deleted file mode 100644 index ca7a4617..00000000 --- a/packages/at_chops/lib/src/algorithm/padding/padding_params.dart +++ /dev/null @@ -1,13 +0,0 @@ -/// A class that defines parameters for padding algorithms used in AES encryption. -/// -/// The `PaddingParams` class provides configurable parameters required for -/// padding algorithms, such as the block size. These parameters are used to -/// ensure that data conforms to the block size required by AES encryption. -class PaddingParams { - /// The block size (in bytes) used for padding. - /// - /// The default value is `16`, which corresponds to the block size of AES encryption. - /// This value determines the size to which input data will be padded to ensure - /// compatibility with the encryption algorithm. - int blockSize = 16; -} diff --git a/packages/at_chops/lib/src/algorithm/pkam_signing_algo.dart b/packages/at_chops/lib/src/algorithm/pkam_signing_algo.dart deleted file mode 100644 index 1dad2407..00000000 --- a/packages/at_chops/lib/src/algorithm/pkam_signing_algo.dart +++ /dev/null @@ -1,56 +0,0 @@ -import 'dart:typed_data'; - -import 'package:at_chops/src/algorithm/algo_type.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/key/impl/at_pkam_key_pair.dart'; -import 'package:at_commons/at_commons.dart'; -import 'package:crypton/crypton.dart'; - -/// Data signing and verification for Public Key Authentication Mechanism - Pkam -class PkamSigningAlgo implements AtSigningAlgorithm { - final AtPkamKeyPair? _pkamKeyPair; - final HashingAlgoType _hashingAlgoType; - PkamSigningAlgo(this._pkamKeyPair, this._hashingAlgoType); - - @override - Uint8List sign(Uint8List data) { - if (_pkamKeyPair == null) { - throw AtSigningException('pkam key pair is null. cannot sign data'); - } - final rsaPrivateKey = - RSAPrivateKey.fromString(_pkamKeyPair!.atPrivateKey.privateKey); - switch (_hashingAlgoType) { - case HashingAlgoType.sha256: - return rsaPrivateKey.createSHA256Signature(data); - case HashingAlgoType.sha512: - return rsaPrivateKey.createSHA512Signature(data); - default: - throw AtSigningException( - 'Hashing algo $_hashingAlgoType is invalid/not supported'); - } - } - - @override - bool verify(Uint8List signedData, Uint8List signature, {String? publicKey}) { - RSAPublicKey rsaPublicKey; - if (publicKey != null) { - rsaPublicKey = RSAPublicKey.fromString(publicKey); - } else if (_pkamKeyPair != null) { - rsaPublicKey = - RSAPublicKey.fromString(_pkamKeyPair!.atPublicKey.publicKey); - } else { - throw AtSigningVerificationException( - 'Pkam key pair or public key not set for pkam verification'); - } - - switch (_hashingAlgoType) { - case HashingAlgoType.sha256: - return rsaPublicKey.verifySHA256Signature(signedData, signature); - case HashingAlgoType.sha512: - return rsaPublicKey.verifySHA512Signature(signedData, signature); - default: - throw AtSigningVerificationException( - 'Invalid hashing algo $_hashingAlgoType provided'); - } - } -} diff --git a/packages/at_chops/lib/src/util/at_chops_util.dart b/packages/at_chops/lib/src/at_chops_util.dart similarity index 70% rename from packages/at_chops/lib/src/util/at_chops_util.dart rename to packages/at_chops/lib/src/at_chops_util.dart index 84ec7c4d..3eb46dae 100644 --- a/packages/at_chops/lib/src/util/at_chops_util.dart +++ b/packages/at_chops/lib/src/at_chops_util.dart @@ -1,11 +1,8 @@ import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/at_iv.dart'; -import 'package:at_chops/src/key/at_key_pair.dart'; -import 'package:at_chops/src/key/impl/aes_key.dart'; -import 'package:at_chops/src/key/impl/at_encryption_key_pair.dart'; -import 'package:at_chops/src/key/impl/at_pkam_key_pair.dart'; -import 'package:at_chops/src/key/key_type.dart'; +import 'package:at_chops/src/encryption/initalisation_vector.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; +import 'package:at_chops/src/key/key.dart'; import 'package:better_cryptography/better_cryptography.dart'; import 'package:crypton/crypton.dart'; import 'package:encrypt/encrypt.dart'; @@ -18,11 +15,13 @@ class AtChopsUtil { return InitialisationVector(iv.bytes); } + /// DO NOT USE THIS IF YOU ARE IMPLEMENTING NEW FEATURES + @Deprecated("Preserved for backwards compatibility") static InitialisationVector generateIVLegacy() { return InitialisationVector(IV(Uint8List(16)).bytes); } - static InitialisationVector generateIVFromBase64String(String ivBase64) { + static InitialisationVector ivFromBase64(String ivBase64) { final iv = IV.fromBase64(ivBase64); return InitialisationVector(iv.bytes); } @@ -33,16 +32,16 @@ class AtChopsUtil { } /// Generates AtEncryption asymmetric keypair with default size 2048 bits - static AtEncryptionKeyPair generateAtEncryptionKeyPair({int keySize = 2048}) { + static AtRSAKeyPair generateAtEncryptionKeyPair({int keySize = 2048}) { final rsaKeyPair = RSAKeypair.fromRandom(keySize: keySize); - return AtEncryptionKeyPair.create( + return AtRSAKeyPair.create( rsaKeyPair.publicKey.toString(), rsaKeyPair.privateKey.toString()); } /// Generates AtEncryption asymmetric keypair with default size 2048 bits - static AtPkamKeyPair generateAtPkamKeyPair({int keySize = 2048}) { + static AtRSAKeyPair generateAtPkamKeyPair({int keySize = 2048}) { final rsaKeyPair = RSAKeypair.fromRandom(keySize: keySize); - return AtPkamKeyPair.create( + return AtRSAKeyPair.create( rsaKeyPair.publicKey.toString(), rsaKeyPair.privateKey.toString()); } @@ -60,13 +59,13 @@ class AtChopsUtil { static SymmetricKey generateSymmetricKey(EncryptionKeyType keyType) { switch (keyType) { case EncryptionKeyType.aes128: - return AESKey.generate(16); + return AtAESKey.generate(16); case EncryptionKeyType.aes192: - return AESKey.generate(24); + return AtAESKey.generate(24); case EncryptionKeyType.aes256: - return AESKey.generate(32); + return AtAESKey.generate(32); default: - return AESKey.generate(32); + return AtAESKey.generate(32); } } } diff --git a/packages/at_chops/lib/src/at_keys_crypto.dart b/packages/at_chops/lib/src/at_keys_crypto.dart index 51627f8a..51a1aa2e 100644 --- a/packages/at_chops/lib/src/at_keys_crypto.dart +++ b/packages/at_chops/lib/src/at_keys_crypto.dart @@ -3,8 +3,9 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/factory/at_hashing_algo_factory.dart'; -import 'package:at_chops/src/model/hash_params.dart'; +import 'package:at_chops/src/encryption/encryption.dart'; +import 'package:at_chops/src/hashing/hashing.dart'; +import 'package:at_chops/src/at_chops_util.dart'; import 'package:at_commons/at_commons.dart'; /// An abstract class that provides cryptographic operations for AtKeys using @@ -65,7 +66,7 @@ class _AtKeysCryptoImpl implements AtKeysCrypto { String hashKey = await _getHashKey(passPhrase, _hashingAlgoType, hashParams: hashParams); - AESKey aesKey = AESKey(hashKey); + AtAESKey aesKey = AtAESKey(hashKey); StringAESEncryptor atEncryptionAlgorithm = StringAESEncryptor(aesKey); InitialisationVector iv = AtChopsUtil.generateRandomIV(16); @@ -92,7 +93,7 @@ class _AtKeysCryptoImpl implements AtKeysCrypto { // 1. Generate hash key based on the hashing algo type: String hashKey = await _getHashKey(passPhrase, _hashingAlgoType, hashParams: hashParams); - AESKey aesKey = AESKey(hashKey); + AtAESKey aesKey = AtAESKey(hashKey); StringAESEncryptor atEncryptionAlgorithm = StringAESEncryptor(aesKey); Uint8List iv = base64Decode(atEncrypted.iv!); diff --git a/packages/at_chops/lib/src/at_chops_base.dart b/packages/at_chops/lib/src/at_platform/at_chops.dart similarity index 93% rename from packages/at_chops/lib/src/at_chops_base.dart rename to packages/at_chops/lib/src/at_platform/at_chops.dart index 19430ed4..16167ac7 100644 --- a/packages/at_chops/lib/src/at_chops_base.dart +++ b/packages/at_chops/lib/src/at_platform/at_chops.dart @@ -2,18 +2,18 @@ import 'dart:async'; import 'dart:typed_data'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/algorithm/default_hashing_algo.dart'; -import 'package:at_chops/src/factory/at_hashing_algo_factory.dart'; +import 'package:at_chops/src/model/signing_result.dart'; /// Base class for all Cryptographic and Hashing Operations. Callers have to either implement /// specific encryption, signing or hashing algorithms. Otherwise default implementation of specific algorithms will be used. abstract class AtChops { - final AtChopsKeys _atChopsKeys; + AtChopsKeys get atChopsKeys; - AtChopsKeys get atChopsKeys => _atChopsKeys; + const AtChops.init(); - AtChops(this._atChopsKeys); + factory AtChops(AtChopsKeys keys) { + return AtChopsImpl(keys); + } /// Returns an instance of [AtHashingAlgorithm] based on the provided [hashingAlgoType]. /// @@ -87,7 +87,7 @@ abstract class AtChops { AtSigningResult verify(AtSigningVerificationInput verifyInput); /// Create a hash of input [signedData] using a [hashingAlgorithm]. - /// Refer to [DefaultHash] for default implementation of hashing. + /// Refer to [Md5HashingAlgo] for default implementation of hashing. String hash(Uint8List signedData, AtHashingAlgorithm hashingAlgorithm); /// Reads a public key from a secure element or any other source diff --git a/packages/at_chops/lib/src/at_chops_impl.dart b/packages/at_chops/lib/src/at_platform/at_chops_impl.dart similarity index 83% rename from packages/at_chops/lib/src/at_chops_impl.dart rename to packages/at_chops/lib/src/at_platform/at_chops_impl.dart index d4e9483c..38fd816e 100644 --- a/packages/at_chops/lib/src/at_chops_impl.dart +++ b/packages/at_chops/lib/src/at_platform/at_chops_impl.dart @@ -4,32 +4,16 @@ import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/aes_encryption_algo.dart'; -import 'package:at_chops/src/algorithm/algo_type.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/algorithm/at_iv.dart'; -import 'package:at_chops/src/algorithm/default_signing_algo.dart'; -import 'package:at_chops/src/algorithm/ecc_signing_algo.dart'; -import 'package:at_chops/src/algorithm/pkam_signing_algo.dart'; -import 'package:at_chops/src/algorithm/rsa_encryption_algo.dart'; -import 'package:at_chops/src/at_chops_base.dart'; -import 'package:at_chops/src/key/at_key_pair.dart'; -import 'package:at_chops/src/key/impl/aes_key.dart'; -import 'package:at_chops/src/key/impl/at_encryption_key_pair.dart'; -import 'package:at_chops/src/key/key_names.dart'; -import 'package:at_chops/src/key/key_type.dart'; -import 'package:at_chops/src/metadata/at_signing_input.dart'; -import 'package:at_chops/src/metadata/encryption_metadata.dart'; -import 'package:at_chops/src/metadata/encryption_result.dart'; -import 'package:at_chops/src/metadata/signing_metadata.dart'; -import 'package:at_chops/src/metadata/signing_result.dart'; +import 'package:at_chops/at_chops.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; +import 'package:at_chops/types.dart'; import 'package:at_commons/at_commons.dart'; import 'package:at_utils/at_logger.dart'; -import 'algorithm/default_hashing_algo.dart'; - class AtChopsImpl extends AtChops { - AtChopsImpl(super.atChopsKeys); + @override + final AtChopsKeys atChopsKeys; + AtChopsImpl(this.atChopsKeys) : super.init(); final AtSignLogger _logger = AtSignLogger('AtChopsImpl'); @@ -148,8 +132,8 @@ class AtChopsImpl extends AtChops { @override String hash(Uint8List signedData, AtHashingAlgorithm hashingAlgorithm) { - if (hashingAlgorithm.runtimeType == DefaultHash) { - return DefaultHash().hash(signedData); + if (hashingAlgorithm.runtimeType == DefaultHashingAlgo) { + return DefaultHashingAlgo().hash(signedData); } throw AtException('$hashingAlgorithm is not supported'); } @@ -215,20 +199,21 @@ class AtChopsImpl extends AtChops { switch (encryptionKeyType) { case EncryptionKeyType.rsa2048: case EncryptionKeyType.rsa4096: - return RsaEncryptionAlgo.fromKeyPair(_getEncryptionKeyPair(keyName)!); - case EncryptionKeyType.ecc: - throw AtEncryptionException('EncryptionKeyType.ecc not supported'); + return RsaEncryptionAlgo.fromKeyPair( + _getEncryptionKeyPair(keyName) as AtRSAKeyPair); + case EncryptionKeyType.ed25519: + throw AtEncryptionException('EncryptionKeyType.ed25519 not supported'); case EncryptionKeyType.aes128: - return AESEncryptionAlgo(_getSymmetricKey(keyName)! as AESKey); + return AESEncryptionAlgo(_getSymmetricKey(keyName)! as AtAESKey); case EncryptionKeyType.aes256: - return AESEncryptionAlgo(_getSymmetricKey(keyName)! as AESKey); + return AESEncryptionAlgo(_getSymmetricKey(keyName)! as AtAESKey); default: throw AtEncryptionException( 'Cannot find encryption algorithm for encryption key type $encryptionKeyType'); } } - AtEncryptionKeyPair? _getEncryptionKeyPair(String? keyName) { + AsymmetricKeyPair? _getEncryptionKeyPair(String? keyName) { if (keyName == null) { return atChopsKeys.atEncryptionKeyPair!; } @@ -238,9 +223,9 @@ class AtChopsImpl extends AtChops { } SymmetricKey? _getSymmetricKey(String? keyName) { - if (keyName == null || keyName == KeyNames.selfEncryptionKey) { + if (keyName == null || keyName == StandardAtKeysNames.selfEncryptionKey) { return atChopsKeys.selfEncryptionKey!; - } else if (keyName == KeyNames.apkamSymmetricKey) { + } else if (keyName == StandardAtKeysNames.apkamSymmetricKey) { return atChopsKeys.apkamSymmetricKey!; } return null; @@ -251,12 +236,13 @@ class AtChopsImpl extends AtChops { return signingInput.signingAlgorithm; } else if (signingInput.signingMode != null && signingInput.signingMode == AtSigningMode.pkam) { - return PkamSigningAlgo( - atChopsKeys.atPkamKeyPair!, signingInput.hashingAlgoType); + return PkamSigningAlgo(atChopsKeys.atPkamKeyPair! as AtRSAKeyPair, + signingInput.hashingAlgoType); } else if (signingInput.signingMode != null && signingInput.signingMode == AtSigningMode.data) { return DefaultSigningAlgo( - atChopsKeys.atEncryptionKeyPair!, signingInput.hashingAlgoType); + atChopsKeys.atEncryptionKeyPair! as AtRSAKeyPair, + signingInput.hashingAlgoType); } else { throw AtSigningException( 'Cannot find signing algorithm for signing input $signingInput'); @@ -273,16 +259,16 @@ class AtChopsImpl extends AtChops { } else if (verificationInput.signingMode != null && verificationInput.signingMode == AtSigningMode.pkam) { if (atChopsKeys.atPkamKeyPair != null) { - return PkamSigningAlgo( - atChopsKeys.atPkamKeyPair, verificationInput.hashingAlgoType); + return PkamSigningAlgo(atChopsKeys.atPkamKeyPair as AtRSAKeyPair, + verificationInput.hashingAlgoType); } else { return PkamSigningAlgo(null, verificationInput.hashingAlgoType); } } else if (verificationInput.signingMode != null && verificationInput.signingMode == AtSigningMode.data && atChopsKeys.atEncryptionKeyPair != null) { - return DefaultSigningAlgo( - atChopsKeys.atEncryptionKeyPair, verificationInput.hashingAlgoType); + return DefaultSigningAlgo(atChopsKeys.atEncryptionKeyPair as AtRSAKeyPair, + verificationInput.hashingAlgoType); } else { throw AtSigningVerificationException( 'Cannot find signing algorithm for signing input $verificationInput'); diff --git a/packages/at_chops/lib/src/key/impl/at_chops_keys.dart b/packages/at_chops/lib/src/at_platform/at_chops_keys.dart similarity index 52% rename from packages/at_chops/lib/src/key/impl/at_chops_keys.dart rename to packages/at_chops/lib/src/at_platform/at_chops_keys.dart index 2ad83ec6..5e091a0a 100644 --- a/packages/at_chops/lib/src/key/impl/at_chops_keys.dart +++ b/packages/at_chops/lib/src/at_platform/at_chops_keys.dart @@ -1,17 +1,14 @@ -import 'package:at_chops/src/key/at_key_pair.dart'; -import 'package:at_chops/src/key/impl/at_encryption_key_pair.dart'; -import 'package:at_chops/src/key/impl/at_pkam_key_pair.dart'; -import 'package:at_chops/src/key/impl/at_signing_key_pair.dart'; +import 'package:at_chops/src/key/key.dart'; class AtChopsKeys { /// Default encryption key pair - AtEncryptionKeyPair? atEncryptionKeyPair; + AsymmetricKeyPair? atEncryptionKeyPair; /// Key pair for pkam authentication. Can be legacy pkam keypair or apkam keypair for new enrollment - AtPkamKeyPair? _atPkamKeyPair; + AsymmetricKeyPair? _atPkamKeyPair; /// Key pair for data signing and verification - AtSigningKeyPair? atSigningKeyPair; + AsymmetricKeyPair? atSigningKeyPair; /// Default self encryption key SymmetricKey? selfEncryptionKey; @@ -19,9 +16,12 @@ class AtChopsKeys { /// APKAM symmetric key created during new enrollment SymmetricKey? apkamSymmetricKey; + /// EnrollmentId associated with pkam keys + String? enrollmentId; + AtChopsKeys.create(this.atEncryptionKeyPair, this._atPkamKeyPair); AtChopsKeys(); - AtPkamKeyPair? get atPkamKeyPair => _atPkamKeyPair; + AsymmetricKeyPair? get atPkamKeyPair => _atPkamKeyPair; } diff --git a/packages/at_chops/lib/src/at_platform/at_platform.dart b/packages/at_chops/lib/src/at_platform/at_platform.dart new file mode 100644 index 00000000..69216377 --- /dev/null +++ b/packages/at_chops/lib/src/at_platform/at_platform.dart @@ -0,0 +1,4 @@ +export 'at_chops.dart'; +export 'at_chops_impl.dart'; +export 'at_chops_keys.dart'; +export 'standard_at_keys_names.dart'; diff --git a/packages/at_chops/lib/src/at_platform/standard_at_keys_names.dart b/packages/at_chops/lib/src/at_platform/standard_at_keys_names.dart new file mode 100644 index 00000000..9324dd73 --- /dev/null +++ b/packages/at_chops/lib/src/at_platform/standard_at_keys_names.dart @@ -0,0 +1,9 @@ +class StandardAtKeysNames { + static const String apkamPublicKey = 'aesPkamPublicKey'; + static const String apkamPrivateKey = 'aesPkamPrivateKey'; + static const String encryptionPublicKey = 'aesEncryptPublicKey'; + static const String encryptionPrivateKey = 'aesEncryptPrivateKey'; + static const String selfEncryptionKey = 'selfEncryptionKey'; + static const String apkamSymmetricKey = 'apkamSymmetricKey'; + static const String apkamEnrollmentId = 'enrollmentId'; +} diff --git a/packages/at_chops/lib/src/algorithm/aes_ctr_factory.dart b/packages/at_chops/lib/src/encryption/aes_ctr_factory.dart similarity index 96% rename from packages/at_chops/lib/src/algorithm/aes_ctr_factory.dart rename to packages/at_chops/lib/src/encryption/aes_ctr_factory.dart index 712d145d..fe1ad014 100644 --- a/packages/at_chops/lib/src/algorithm/aes_ctr_factory.dart +++ b/packages/at_chops/lib/src/encryption/aes_ctr_factory.dart @@ -28,7 +28,7 @@ class AesCtrFactory { /// - 16 bytes for AES-128 /// - 24 bytes for AES-192 /// - 32 bytes for AES-256 - static AesCtr createEncryptionAlgo(AESKey aesKey) { + static AesCtr createEncryptionAlgo(AtAESKey aesKey) { switch (aesKey.getLength()) { case 16: return AesCtr.with128bits(macAlgorithm: MacAlgorithm.empty); diff --git a/packages/at_chops/lib/src/algorithm/aes_encryption_algo.dart b/packages/at_chops/lib/src/encryption/aes_encryption_algo.dart similarity index 89% rename from packages/at_chops/lib/src/algorithm/aes_encryption_algo.dart rename to packages/at_chops/lib/src/encryption/aes_encryption_algo.dart index 8524e189..466b500b 100644 --- a/packages/at_chops/lib/src/algorithm/aes_encryption_algo.dart +++ b/packages/at_chops/lib/src/encryption/aes_encryption_algo.dart @@ -1,12 +1,10 @@ import 'dart:async'; import 'dart:typed_data'; import 'dart:convert'; -import 'package:at_chops/src/algorithm/aes_ctr_factory.dart'; -import 'package:at_chops/src/algorithm/padding/padding.dart'; +import 'package:at_chops/src/encryption/aes_ctr_factory.dart'; +import 'package:at_chops/src/padding/padding.dart'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/algorithm/padding/padding_params.dart'; -import 'package:at_chops/src/algorithm/padding/pkcs7padding.dart'; +import 'package:at_chops/types.dart'; import 'package:at_commons/at_commons.dart'; import 'package:encrypt/encrypt.dart'; import 'package:better_cryptography/better_cryptography.dart'; @@ -15,10 +13,10 @@ import 'package:better_cryptography/better_cryptography.dart'; /// implementing the [SymmetricEncryptionAlgorithm] interface. class AESEncryptionAlgo implements SymmetricEncryptionAlgorithm { - final AESKey _aesKey; + final AtAESKey _aesKey; PaddingAlgorithm? paddingAlgo; AESEncryptionAlgo(this._aesKey) { - paddingAlgo ??= PKCS7Padding(PaddingParams()..blockSize = 16); + paddingAlgo ??= PKCS7Padding(PKCS7PaddingParams()..blockSize = 16); } @override @@ -62,13 +60,13 @@ class AESEncryptionAlgo /// A class that provides AES encryption and decryption for strings, /// implementing the [SymmetricEncryptionAlgorithm] interface. /// -/// This class uses an [AESKey] to perform encryption and decryption of strings. +/// This class uses an [AtAESKey] to perform encryption and decryption of strings. /// The key and an [InitialisationVector] (IV) are used for encryption, and the /// same key must be used for decryption. class StringAESEncryptor implements SymmetricEncryptionAlgorithm { /// The AES key used for encryption and decryption. - final AESKey _aesKey; + final AtAESKey _aesKey; /// Constructs an instance of [StringAESEncryptor] with the provided [_aesKey]. /// diff --git a/packages/at_chops/lib/src/algorithm/at_algorithm.dart b/packages/at_chops/lib/src/encryption/at_encryption_algorithm.dart similarity index 54% rename from packages/at_chops/lib/src/algorithm/at_algorithm.dart rename to packages/at_chops/lib/src/encryption/at_encryption_algorithm.dart index 8c9dc117..b1de9a6d 100644 --- a/packages/at_chops/lib/src/algorithm/at_algorithm.dart +++ b/packages/at_chops/lib/src/encryption/at_encryption_algorithm.dart @@ -2,10 +2,8 @@ import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/at_iv.dart'; -import 'package:at_chops/src/key/at_private_key.dart'; -import 'package:at_chops/src/key/at_public_key.dart'; -import 'package:at_chops/src/model/hash_params.dart'; +import 'package:at_chops/src/encryption/initalisation_vector.dart'; +import 'package:at_chops/src/key/key.dart'; /// Interface for encrypting and decrypting data. Check [DefaultEncryptionAlgo] for sample implementation. abstract class AtEncryptionAlgorithm { @@ -27,10 +25,11 @@ abstract class SymmetricEncryptionAlgorithm } /// Interface for asymmetric encryption algorithms. Check [DefaultEncryptionAlgo] for sample implementation. -abstract class ASymmetricEncryptionAlgorithm +abstract class AsymmetricEncryptionAlgorithm extends AtEncryptionAlgorithm { - AtPublicKey? atPublicKey; - AtPrivateKey? atPrivateKey; + Pub? atPublicKey; + Priv? atPrivateKey; /// Encrypt [plainData] with [atPublicKey.publicKey] @override @@ -40,20 +39,3 @@ abstract class ASymmetricEncryptionAlgorithm @override Uint8List decrypt(Uint8List encryptedData); } - -/// Interface for data signing. Data is signed using private key from a key pair -/// Signed data signature is verified with public key of the key pair. -abstract class AtSigningAlgorithm { - /// Signs the data using private key of asymmetric key pair - FutureOr sign(Uint8List data); - - /// Verifies the data signature using public key of asymmetric key pair or the passed [publicKey] - FutureOr verify(Uint8List signedData, Uint8List signature, - {String? publicKey}); -} - -/// Interface for hashing data. Refer [DefaultHash] for sample implementation. -abstract class AtHashingAlgorithm { - /// Hashes the passed data - FutureOr hash(K data, {covariant HashParams? hashParams}); -} diff --git a/packages/at_chops/lib/src/encryption/encryption.dart b/packages/at_chops/lib/src/encryption/encryption.dart new file mode 100644 index 00000000..1329a6b8 --- /dev/null +++ b/packages/at_chops/lib/src/encryption/encryption.dart @@ -0,0 +1,5 @@ +export 'aes_ctr_factory.dart'; +export 'aes_encryption_algo.dart'; +export 'at_encryption_algorithm.dart'; +export 'initalisation_vector.dart'; +export 'rsa_encryption_algo.dart'; diff --git a/packages/at_chops/lib/src/algorithm/at_iv.dart b/packages/at_chops/lib/src/encryption/initalisation_vector.dart similarity index 100% rename from packages/at_chops/lib/src/algorithm/at_iv.dart rename to packages/at_chops/lib/src/encryption/initalisation_vector.dart diff --git a/packages/at_chops/lib/src/algorithm/rsa_encryption_algo.dart b/packages/at_chops/lib/src/encryption/rsa_encryption_algo.dart similarity index 76% rename from packages/at_chops/lib/src/algorithm/rsa_encryption_algo.dart rename to packages/at_chops/lib/src/encryption/rsa_encryption_algo.dart index ca6bc8c2..d3cb16a2 100644 --- a/packages/at_chops/lib/src/algorithm/rsa_encryption_algo.dart +++ b/packages/at_chops/lib/src/encryption/rsa_encryption_algo.dart @@ -1,14 +1,13 @@ import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/key/at_private_key.dart'; -import 'package:at_chops/src/key/at_public_key.dart'; -import 'package:at_chops/src/key/impl/at_encryption_key_pair.dart'; +import 'package:at_chops/src/encryption/at_encryption_algorithm.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; import 'package:at_commons/at_commons.dart'; import 'package:crypton/crypton.dart'; -class RsaEncryptionAlgo implements ASymmetricEncryptionAlgorithm { - AtEncryptionKeyPair? _encryptionKeypair; +class RsaEncryptionAlgo + implements AsymmetricEncryptionAlgorithm { + AtRSAKeyPair? _encryptionKeypair; RsaEncryptionAlgo.fromKeyPair(this._encryptionKeypair); RsaEncryptionAlgo(); @override @@ -38,8 +37,8 @@ class RsaEncryptionAlgo implements ASymmetricEncryptionAlgorithm { } @override - AtPrivateKey? atPrivateKey; + AtRSAPrivateKey? atPrivateKey; @override - AtPublicKey? atPublicKey; + AtRSAPublicKey? atPublicKey; } diff --git a/packages/at_chops/lib/src/algorithm/argon2id_hashing_algo.dart b/packages/at_chops/lib/src/hashing/argon2id_hashing_algo.dart similarity index 59% rename from packages/at_chops/lib/src/algorithm/argon2id_hashing_algo.dart rename to packages/at_chops/lib/src/hashing/argon2id_hashing_algo.dart index 567ab995..ff461dde 100644 --- a/packages/at_chops/lib/src/algorithm/argon2id_hashing_algo.dart +++ b/packages/at_chops/lib/src/hashing/argon2id_hashing_algo.dart @@ -1,8 +1,7 @@ import 'dart:async'; import 'dart:convert'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/model/hash_params.dart'; +import 'package:at_chops/src/hashing/at_hashing_algorithm.dart'; import 'package:cryptography/cryptography.dart'; /// A class that implements the Argon2id hashing algorithm for password hashing. @@ -50,3 +49,37 @@ class Argon2idHashingAlgo implements AtHashingAlgorithm { return Base64Encoder().convert(await secretKey.extractBytes()); } } + +/// A class that holds the parameters for configuring a hashing algorithm. +/// +/// This class is used to customize the behavior of a hashing algorithm by +/// providing control over key parameters such as parallelism, memory usage, +/// iteration count, and the length of the resulting hash. +/// +/// These parameters are particularly useful when working with algorithms +/// like Argon2id, which can be adjusted for performance and security needs. + +class ArgonHashParams extends HashParams { + /// The degree of parallelism, representing the number of threads used during hashing. + /// + /// The default value is 2, meaning the hashing algorithm will use 2 threads. + int parallelism = 2; + + /// The amount of memory (in KB) to be used during the hashing process. + /// + /// The default value is 10,000 KB (10 MB). Increasing the memory value + /// can make the hashing process more resistant to brute-force attacks. + int memory = 10000; + + /// The number of iterations (time cost) applied during the hashing process. + /// + /// The default value is 2. A higher iteration count increases the time + /// required to compute the hash, providing greater security. + int iterations = 2; + + /// The length of the resulting hash in bytes. + /// + /// The default value is 32 bytes. This value controls the size of the + /// derived hash or key. + int hashLength = 32; +} diff --git a/packages/at_chops/lib/src/factory/at_hashing_algo_factory.dart b/packages/at_chops/lib/src/hashing/at_hashing_algo_factory.dart similarity index 76% rename from packages/at_chops/lib/src/factory/at_hashing_algo_factory.dart rename to packages/at_chops/lib/src/hashing/at_hashing_algo_factory.dart index de3a148c..998930bf 100644 --- a/packages/at_chops/lib/src/factory/at_hashing_algo_factory.dart +++ b/packages/at_chops/lib/src/hashing/at_hashing_algo_factory.dart @@ -1,7 +1,7 @@ -import 'package:at_chops/src/algorithm/algo_type.dart'; -import 'package:at_chops/src/algorithm/argon2id_hashing_algo.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/algorithm/default_hashing_algo.dart'; +import 'package:at_chops/src/hashing/argon2id_hashing_algo.dart'; +import 'package:at_chops/src/hashing/at_hashing_algorithm.dart'; +import 'package:at_chops/src/hashing/md5_hashing_algo.dart'; +import 'package:at_chops/src/hashing/sha512_hashing_algo.dart'; import 'package:at_commons/at_commons.dart'; /// A factory class for creating instances of different hashing algorithms @@ -14,7 +14,7 @@ class AtHashingAlgorithmFactory { /// Returns an instance of [AtHashingAlgorithm] based on the provided [HashingAlgoType]. /// /// The method supports the following hashing algorithms: - /// - [HashingAlgoType.md5]: returns an instance of [DefaultHash] (MD5 hashing). + /// - [HashingAlgoType.md5]: returns an instance of [Md5HashingAlgo] (MD5 hashing). /// - [HashingAlgoType.argon2id]: returns an instance of [Argon2idHashingAlgo] (Argon2id hashing). /// /// Throws an [AtException] if an unsupported hashing algorithm is passed. diff --git a/packages/at_chops/lib/src/hashing/at_hashing_algorithm.dart b/packages/at_chops/lib/src/hashing/at_hashing_algorithm.dart new file mode 100644 index 00000000..615aed7a --- /dev/null +++ b/packages/at_chops/lib/src/hashing/at_hashing_algorithm.dart @@ -0,0 +1,27 @@ +import 'dart:async'; + +import 'package:at_chops/src/hashing/md5_hashing_algo.dart'; +import 'package:at_commons/at_commons.dart'; + +/// Interface for hashing data. Refer [DefaultHash] for sample implementation. +abstract class AtHashingAlgorithm { + /// Hashes the passed data + FutureOr hash(K data, {covariant HashParams? hashParams}); +} + +class HashParams {} + +class DefaultHashingAlgo extends Md5HashingAlgo {} + +enum HashingAlgoType { + sha256, + sha512, + md5, + argon2id; + + static HashingAlgoType fromString(String name) { + return HashingAlgoType.values.firstWhere( + (algo) => algo.name == name.toLowerCase(), + orElse: () => throw AtException('Invalid hashing algo type')); + } +} diff --git a/packages/at_chops/lib/src/hashing/hashing.dart b/packages/at_chops/lib/src/hashing/hashing.dart new file mode 100644 index 00000000..5c8b6d7c --- /dev/null +++ b/packages/at_chops/lib/src/hashing/hashing.dart @@ -0,0 +1,5 @@ +export 'argon2id_hashing_algo.dart'; +export 'at_hashing_algo_factory.dart'; +export 'at_hashing_algorithm.dart'; +export 'md5_hashing_algo.dart'; +export 'sha512_hashing_algo.dart'; diff --git a/packages/at_chops/lib/src/hashing/md5_hashing_algo.dart b/packages/at_chops/lib/src/hashing/md5_hashing_algo.dart new file mode 100644 index 00000000..ea8acff2 --- /dev/null +++ b/packages/at_chops/lib/src/hashing/md5_hashing_algo.dart @@ -0,0 +1,9 @@ +import 'package:at_chops/src/hashing/at_hashing_algorithm.dart'; +import 'package:crypto/crypto.dart'; + +class Md5HashingAlgo implements AtHashingAlgorithm, String> { + @override + String hash(List data, {HashParams? hashParams}) { + return md5.convert(data).toString(); + } +} diff --git a/packages/at_chops/lib/src/hashing/sha512_hashing_algo.dart b/packages/at_chops/lib/src/hashing/sha512_hashing_algo.dart new file mode 100644 index 00000000..c86186d4 --- /dev/null +++ b/packages/at_chops/lib/src/hashing/sha512_hashing_algo.dart @@ -0,0 +1,10 @@ +import 'package:at_chops/src/hashing/at_hashing_algorithm.dart'; +import 'package:crypto/crypto.dart'; + +class SHA512HashingAlgo implements AtHashingAlgorithm, String> { + @override + String hash(List data, {covariant HashParams? hashParams}) { + Digest digest = sha512.convert(data); + return digest.toString(); + } +} diff --git a/packages/at_chops/lib/src/key/asymmetric_key_pair.dart b/packages/at_chops/lib/src/key/asymmetric_key_pair.dart new file mode 100644 index 00000000..5f6381a2 --- /dev/null +++ b/packages/at_chops/lib/src/key/asymmetric_key_pair.dart @@ -0,0 +1,31 @@ +import 'dart:typed_data'; + +/// Represents a key pair for asymmetric public-private key encryption/decryption +abstract class AsymmetricKeyPair { + final Priv _atPrivateKey; + final Pub _atPublicKey; + + AsymmetricKeyPair(this._atPublicKey, this._atPrivateKey); + + Pub get atPublicKey => _atPublicKey; + Priv get atPrivateKey => _atPrivateKey; +} + +/// Represents a private key from [AtKeyPair] +abstract class AtPrivateKey { + String get privateKey; + Uint8List get raw; + + @override + String toString() => privateKey; +} + +/// Represents a public key from [AtKeyPair] +abstract class AtPublicKey { + String get publicKey; + Uint8List get raw; + + @override + String toString() => publicKey; +} diff --git a/packages/at_chops/lib/src/key/impl/aes_key.dart b/packages/at_chops/lib/src/key/at_aes_key.dart similarity index 61% rename from packages/at_chops/lib/src/key/impl/aes_key.dart rename to packages/at_chops/lib/src/key/at_aes_key.dart index cc6c2ec0..0ea49e72 100644 --- a/packages/at_chops/lib/src/key/impl/aes_key.dart +++ b/packages/at_chops/lib/src/key/at_aes_key.dart @@ -1,19 +1,25 @@ -import 'package:at_chops/src/key/at_key_pair.dart'; +import 'dart:typed_data'; + +import 'package:at_chops/src/key/symmetric_key.dart'; import 'package:encrypt/encrypt.dart'; import 'dart:convert'; /// Represents an AES key for symmetric encryption. -class AESKey extends SymmetricKey { - final String _aesKey; +class AtAESKey extends SymmetricKey { + @override + final Uint8List raw; + @override - String get key => _aesKey; - AESKey(this._aesKey) : super(_aesKey); + String get key => base64Encode(raw); + + AtAESKey(String base64) : raw = base64Decode(base64); + AtAESKey.raw(this.raw); /// Generates an AES key for symmetric encryption with a given length. /// Key is created with a list of [length] with non negative values randomly generated from >=0 and < 256 and converted to base64 string - static AESKey generate(int length) { + static AtAESKey generate(int length) { var aesKey = AES(Key.fromSecureRandom(length)); - return AESKey(aesKey.key.base64); + return AtAESKey(aesKey.key.base64); } /// Returns the key length in bytes. @@ -21,11 +27,11 @@ class AESKey extends SymmetricKey { /// for 192 bit key length will be 24 /// for 256 bit key length will be 32 int getLength() { - return base64.decode(_aesKey).length; + return raw.length; } @override String toString() { - return _aesKey; + return key; } } diff --git a/packages/at_chops/lib/src/key/at_key_pair.dart b/packages/at_chops/lib/src/key/at_key_pair.dart deleted file mode 100644 index 737d6f0d..00000000 --- a/packages/at_chops/lib/src/key/at_key_pair.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'package:at_chops/src/key/at_private_key.dart'; -import 'package:at_chops/src/key/at_public_key.dart'; - -/// Represents a key pair for asymmetric public-private key encryption/decryption -abstract class AsymmetricKeyPair { - late AtPrivateKey _atPrivateKey; - - late AtPublicKey _atPublicKey; - - AsymmetricKeyPair.create(String publicKey, String privateKey) { - _atPublicKey = AtPublicKey.fromString(publicKey); - _atPrivateKey = AtPrivateKey.fromString(privateKey); - } - - AtPublicKey get atPublicKey => _atPublicKey; - AtPrivateKey get atPrivateKey => _atPrivateKey; -} - -/// Represent a key for symmetric key encryption/decryption -abstract class SymmetricKey { - late String key; - SymmetricKey(this.key); -} diff --git a/packages/at_chops/lib/src/key/at_private_key.dart b/packages/at_chops/lib/src/key/at_private_key.dart deleted file mode 100644 index 41a1b435..00000000 --- a/packages/at_chops/lib/src/key/at_private_key.dart +++ /dev/null @@ -1,6 +0,0 @@ -/// Represents a private key from [AtKeyPair] -class AtPrivateKey { - final String _privateKey; - AtPrivateKey.fromString(this._privateKey); - String get privateKey => _privateKey; -} diff --git a/packages/at_chops/lib/src/key/at_public_key.dart b/packages/at_chops/lib/src/key/at_public_key.dart deleted file mode 100644 index 5be20894..00000000 --- a/packages/at_chops/lib/src/key/at_public_key.dart +++ /dev/null @@ -1,6 +0,0 @@ -/// Represents a public key from [AtKeyPair] -class AtPublicKey { - final String _publicKey; - AtPublicKey.fromString(this._publicKey); - String get publicKey => _publicKey; -} diff --git a/packages/at_chops/lib/src/key/at_rsa_key_pair.dart b/packages/at_chops/lib/src/key/at_rsa_key_pair.dart new file mode 100644 index 00000000..4dc1d854 --- /dev/null +++ b/packages/at_chops/lib/src/key/at_rsa_key_pair.dart @@ -0,0 +1,31 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:at_chops/src/key/asymmetric_key_pair.dart'; + +class AtRSAKeyPair extends AsymmetricKeyPair { + AtRSAKeyPair(super.publicKey, super.privateKey); + AtRSAKeyPair.create(String publicKey, String privateKey) + : super(AtRSAPublicKey(publicKey), AtRSAPrivateKey(privateKey)); +} + +class AtRSAPublicKey extends AtPublicKey { + @override + final Uint8List raw; + + AtRSAPublicKey(String base64) : raw = base64Decode(base64); + AtRSAPublicKey.raw(this.raw); + + @override + String get publicKey => base64Encode(raw); +} + +class AtRSAPrivateKey extends AtPrivateKey { + @override + final Uint8List raw; + AtRSAPrivateKey(String base64) : raw = base64Decode(base64); + AtRSAPrivateKey.raw(this.raw); + + @override + String get privateKey => base64Encode(raw); +} diff --git a/packages/at_chops/lib/src/key/impl/at_encryption_key_pair.dart b/packages/at_chops/lib/src/key/impl/at_encryption_key_pair.dart deleted file mode 100644 index e653aebf..00000000 --- a/packages/at_chops/lib/src/key/impl/at_encryption_key_pair.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:at_chops/src/key/at_key_pair.dart'; - -class AtEncryptionKeyPair extends AsymmetricKeyPair { - AtEncryptionKeyPair.create(super.publicKey, super.privateKey) - : super.create(); -} diff --git a/packages/at_chops/lib/src/key/impl/at_pkam_key_pair.dart b/packages/at_chops/lib/src/key/impl/at_pkam_key_pair.dart deleted file mode 100644 index 03ce8b1c..00000000 --- a/packages/at_chops/lib/src/key/impl/at_pkam_key_pair.dart +++ /dev/null @@ -1,5 +0,0 @@ -import 'package:at_chops/src/key/at_key_pair.dart'; - -class AtPkamKeyPair extends AsymmetricKeyPair { - AtPkamKeyPair.create(super.publicKey, super.privateKey) : super.create(); -} diff --git a/packages/at_chops/lib/src/key/impl/at_signing_key_pair.dart b/packages/at_chops/lib/src/key/impl/at_signing_key_pair.dart deleted file mode 100644 index abd0b5a2..00000000 --- a/packages/at_chops/lib/src/key/impl/at_signing_key_pair.dart +++ /dev/null @@ -1,5 +0,0 @@ -import 'package:at_chops/src/key/at_key_pair.dart'; - -class AtSigningKeyPair extends AsymmetricKeyPair { - AtSigningKeyPair.create(super.publicKey, super.privateKey) : super.create(); -} diff --git a/packages/at_chops/lib/src/key/impl/cram_key.dart b/packages/at_chops/lib/src/key/impl/cram_key.dart deleted file mode 100644 index 946dc1a1..00000000 --- a/packages/at_chops/lib/src/key/impl/cram_key.dart +++ /dev/null @@ -1,6 +0,0 @@ -/// Represents a key for Challenge Response Authentication -class CramKey { - final String _cramSecret; - CramKey(this._cramSecret); - String get secret => _cramSecret; -} diff --git a/packages/at_chops/lib/src/key/key.dart b/packages/at_chops/lib/src/key/key.dart new file mode 100644 index 00000000..9debe900 --- /dev/null +++ b/packages/at_chops/lib/src/key/key.dart @@ -0,0 +1,4 @@ +export 'at_aes_key.dart'; +export 'asymmetric_key_pair.dart'; +export 'symmetric_key.dart'; +export 'key_type.dart'; diff --git a/packages/at_chops/lib/src/key/key_names.dart b/packages/at_chops/lib/src/key/key_names.dart deleted file mode 100644 index dbec6349..00000000 --- a/packages/at_chops/lib/src/key/key_names.dart +++ /dev/null @@ -1,6 +0,0 @@ -class KeyNames { - static const String selfEncryptionKey = 'selfEncryptionKey'; - static const String apkamSymmetricKey = 'apkamSymmetricKey'; - static const String rsa2048EncKey = 'rsa2048EncKey'; - static const String rsa4096EncKey = 'rsa4096EncKey'; -} diff --git a/packages/at_chops/lib/src/key/key_type.dart b/packages/at_chops/lib/src/key/key_type.dart index d04ac213..405828bb 100644 --- a/packages/at_chops/lib/src/key/key_type.dart +++ b/packages/at_chops/lib/src/key/key_type.dart @@ -1 +1,11 @@ -enum EncryptionKeyType { rsa2048, rsa4096, ecc, aes128, aes192, aes256 } +enum EncryptionKeyType { +// symmetric + aes128, + aes192, + aes256, + +// asymmetric + rsa2048, + rsa4096, + ed25519 +} diff --git a/packages/at_chops/lib/src/key/symmetric_key.dart b/packages/at_chops/lib/src/key/symmetric_key.dart new file mode 100644 index 00000000..af439d37 --- /dev/null +++ b/packages/at_chops/lib/src/key/symmetric_key.dart @@ -0,0 +1,10 @@ +import 'dart:typed_data'; + +/// Represent a key for symmetric key encryption/decryption +abstract class SymmetricKey { + String get key; + Uint8List get raw; + + @override + String toString() => key; +} diff --git a/packages/at_chops/lib/src/model/at_encrypted.dart b/packages/at_chops/lib/src/model/at_encrypted.dart index 52815d04..bc40bd7f 100644 --- a/packages/at_chops/lib/src/model/at_encrypted.dart +++ b/packages/at_chops/lib/src/model/at_encrypted.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:at_chops/at_chops.dart'; +import 'package:at_chops/src/hashing/hashing.dart'; /// A class that represents encrypted content, along with metadata such as /// initialization vector (IV) and the hashing algorithm used. diff --git a/packages/at_chops/lib/src/metadata/at_signing_input.dart b/packages/at_chops/lib/src/model/at_signing_input.dart similarity index 86% rename from packages/at_chops/lib/src/metadata/at_signing_input.dart rename to packages/at_chops/lib/src/model/at_signing_input.dart index 3184e314..920c572b 100644 --- a/packages/at_chops/lib/src/metadata/at_signing_input.dart +++ b/packages/at_chops/lib/src/model/at_signing_input.dart @@ -1,10 +1,10 @@ import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/algo_type.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/algorithm/default_signing_algo.dart'; -import 'package:at_chops/src/algorithm/ecc_signing_algo.dart'; -import 'package:at_chops/src/algorithm/pkam_signing_algo.dart'; +import 'package:at_chops/src/hashing/hashing.dart'; +import 'package:at_chops/src/signing/at_signing_algorithm.dart'; +import 'package:at_chops/src/signing/rsa_signing_algo.dart'; +import 'package:at_chops/src/signing/ecc_signing_algo.dart'; +import 'package:at_chops/src/signing/pkam_signing_algo.dart'; /// Represents input attributes required for data signing. /// @@ -31,7 +31,7 @@ class AtSigningInput { /// SigningAlgorithm that will be used to sign/verify data /// - /// Available implementations are [DefaultSigningAlgo], [PkamSigningAlgo], [EccSigningAlgo]. Callers can set their own signing algorithm by implementing [AtSigningAlgorithm] + /// Available implementations are [RSASigningAlgo], [PkamSigningAlgo], [EccSigningAlgo]. Callers can set their own signing algorithm by implementing [AtSigningAlgorithm] AtSigningAlgorithm? signingAlgorithm; /// Select signingMode from [AtSigningMode] @@ -92,7 +92,7 @@ class AtSigningVerificationInput { /// SigningAlgorithm that will be used to sign/verify data /// - /// Available options are [DefaultSigningAlgo], [PkamSigningAlgo], [EccSigningAlgo] + /// Available options are [RSASigningAlgo], [PkamSigningAlgo], [EccSigningAlgo] AtSigningAlgorithm? signingAlgorithm; AtSigningVerificationInput(this._data, this._signature, this._publicKey); diff --git a/packages/at_chops/lib/src/metadata/encryption_metadata.dart b/packages/at_chops/lib/src/model/encryption_metadata.dart similarity index 83% rename from packages/at_chops/lib/src/metadata/encryption_metadata.dart rename to packages/at_chops/lib/src/model/encryption_metadata.dart index 216f76ab..89426e44 100644 --- a/packages/at_chops/lib/src/metadata/encryption_metadata.dart +++ b/packages/at_chops/lib/src/model/encryption_metadata.dart @@ -1,4 +1,4 @@ -import 'package:at_chops/src/algorithm/at_iv.dart'; +import 'package:at_chops/src/encryption/initalisation_vector.dart'; import 'package:at_chops/src/key/key_type.dart'; /// Class which represents metadata for encryption/decryption. diff --git a/packages/at_chops/lib/src/metadata/encryption_result.dart b/packages/at_chops/lib/src/model/encryption_result.dart similarity index 83% rename from packages/at_chops/lib/src/metadata/encryption_result.dart rename to packages/at_chops/lib/src/model/encryption_result.dart index c440a9ed..f02efbeb 100644 --- a/packages/at_chops/lib/src/metadata/encryption_result.dart +++ b/packages/at_chops/lib/src/model/encryption_result.dart @@ -1,4 +1,4 @@ -import 'package:at_chops/src/metadata/encryption_metadata.dart'; +import 'package:at_chops/src/model/encryption_metadata.dart'; // Class that contains the encryption/decryption result with data type [AtEncryptionResultType] and metadata [AtEncryptionMetaData] class AtEncryptionResult { diff --git a/packages/at_chops/lib/src/model/hash_params.dart b/packages/at_chops/lib/src/model/hash_params.dart deleted file mode 100644 index 70def9a3..00000000 --- a/packages/at_chops/lib/src/model/hash_params.dart +++ /dev/null @@ -1,36 +0,0 @@ -/// A class that holds the parameters for configuring a hashing algorithm. -/// -/// This class is used to customize the behavior of a hashing algorithm by -/// providing control over key parameters such as parallelism, memory usage, -/// iteration count, and the length of the resulting hash. -/// -/// These parameters are particularly useful when working with algorithms -/// like Argon2id, which can be adjusted for performance and security needs. -abstract class HashParams {} - -class ArgonHashParams extends HashParams { - /// The degree of parallelism, representing the number of threads used during hashing. - /// - /// The default value is 2, meaning the hashing algorithm will use 2 threads. - int parallelism = 2; - - /// The amount of memory (in KB) to be used during the hashing process. - /// - /// The default value is 10,000 KB (10 MB). Increasing the memory value - /// can make the hashing process more resistant to brute-force attacks. - int memory = 10000; - - /// The number of iterations (time cost) applied during the hashing process. - /// - /// The default value is 2. A higher iteration count increases the time - /// required to compute the hash, providing greater security. - int iterations = 2; - - /// The length of the resulting hash in bytes. - /// - /// The default value is 32 bytes. This value controls the size of the - /// derived hash or key. - int hashLength = 32; -} - -class DefaultHashParams extends HashParams {} diff --git a/packages/at_chops/lib/src/model/model.dart b/packages/at_chops/lib/src/model/model.dart new file mode 100644 index 00000000..9a885749 --- /dev/null +++ b/packages/at_chops/lib/src/model/model.dart @@ -0,0 +1,4 @@ +export 'at_encrypted.dart'; +export 'at_signing_input.dart'; +export 'encryption_metadata.dart'; +export 'encryption_result.dart'; diff --git a/packages/at_chops/lib/src/metadata/signing_metadata.dart b/packages/at_chops/lib/src/model/signing_metadata.dart similarity index 81% rename from packages/at_chops/lib/src/metadata/signing_metadata.dart rename to packages/at_chops/lib/src/model/signing_metadata.dart index 5456c771..e83cecbd 100644 --- a/packages/at_chops/lib/src/metadata/signing_metadata.dart +++ b/packages/at_chops/lib/src/model/signing_metadata.dart @@ -1,4 +1,5 @@ -import 'package:at_chops/src/algorithm/algo_type.dart'; +import 'package:at_chops/src/hashing/hashing.dart'; +import 'package:at_chops/src/signing/at_signing_algorithm.dart'; /// Class which represents metadata for data signing. class AtSigningMetaData { diff --git a/packages/at_chops/lib/src/metadata/signing_result.dart b/packages/at_chops/lib/src/model/signing_result.dart similarity index 89% rename from packages/at_chops/lib/src/metadata/signing_result.dart rename to packages/at_chops/lib/src/model/signing_result.dart index df5da325..6c25fd8f 100644 --- a/packages/at_chops/lib/src/metadata/signing_result.dart +++ b/packages/at_chops/lib/src/model/signing_result.dart @@ -1,4 +1,4 @@ -import 'package:at_chops/src/metadata/signing_metadata.dart'; +import 'package:at_chops/src/model/signing_metadata.dart'; /// Class that contains the signing/verification result with data type [AtSigningResultType] and metadata [AtSigningMetaData] /// [result] should be base64Encoded string diff --git a/packages/at_chops/lib/src/padding/padding.dart b/packages/at_chops/lib/src/padding/padding.dart new file mode 100644 index 00000000..25c7f092 --- /dev/null +++ b/packages/at_chops/lib/src/padding/padding.dart @@ -0,0 +1,2 @@ +export 'padding_algorithm.dart'; +export 'pkcs7_padding.dart'; diff --git a/packages/at_chops/lib/src/algorithm/padding/padding.dart b/packages/at_chops/lib/src/padding/padding_algorithm.dart similarity index 100% rename from packages/at_chops/lib/src/algorithm/padding/padding.dart rename to packages/at_chops/lib/src/padding/padding_algorithm.dart diff --git a/packages/at_chops/lib/src/algorithm/padding/pkcs7padding.dart b/packages/at_chops/lib/src/padding/pkcs7_padding.dart similarity index 64% rename from packages/at_chops/lib/src/algorithm/padding/pkcs7padding.dart rename to packages/at_chops/lib/src/padding/pkcs7_padding.dart index b2f9172c..713966d1 100644 --- a/packages/at_chops/lib/src/algorithm/padding/pkcs7padding.dart +++ b/packages/at_chops/lib/src/padding/pkcs7_padding.dart @@ -1,9 +1,8 @@ -import 'package:at_chops/src/algorithm/padding/padding.dart'; -import 'package:at_chops/src/algorithm/padding/padding_params.dart'; +import 'package:at_chops/src/padding/padding.dart'; import 'package:at_commons/at_commons.dart'; class PKCS7Padding implements PaddingAlgorithm { - final PaddingParams _paddingParams; + final PKCS7PaddingParams _paddingParams; PKCS7Padding(this._paddingParams); @override List addPadding(List data) { @@ -47,3 +46,18 @@ class PKCS7Padding implements PaddingAlgorithm { return data.sublist(0, data.length - paddingLength); } } + +/// A class that defines parameters for padding algorithms used in AES encryption. +/// +/// The `PaddingParams` class provides configurable parameters required for +/// padding algorithms, such as the block size. These parameters are used to +/// ensure that data conforms to the block size required by AES encryption. +class PKCS7PaddingParams { + /// The block size (in bytes) used for padding. + /// + /// The default value is `16`, which corresponds to the block size of AES encryption. + /// This value determines the size to which input data will be padded to ensure + /// compatibility with the encryption algorithm. + int blockSize; + PKCS7PaddingParams({this.blockSize = 16}); +} diff --git a/packages/at_chops/lib/src/signing/at_signing_algorithm.dart b/packages/at_chops/lib/src/signing/at_signing_algorithm.dart new file mode 100644 index 00000000..08e8322d --- /dev/null +++ b/packages/at_chops/lib/src/signing/at_signing_algorithm.dart @@ -0,0 +1,22 @@ +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:at_chops/src/signing/rsa_signing_algo.dart'; + +/// Interface for data signing. Data is signed using private key from a key pair +/// Signed data signature is verified with public key of the key pair. +abstract class AtSigningAlgorithm { + /// Signs the data using private key of asymmetric key pair + FutureOr sign(Uint8List data); + + /// Verifies the data signature using public key of asymmetric key pair or the passed [publicKey] + FutureOr verify(Uint8List signedData, Uint8List signature, + {String? publicKey}); +} + +class DefaultSigningAlgo extends RSASigningAlgo { + DefaultSigningAlgo(super.encryptionKeyPair, super.hashingAlgoType); +} + +// ignore: constant_identifier_names +enum SigningAlgoType { ecc_secp256r1, rsa2048, rsa4096 } diff --git a/packages/at_chops/lib/src/algorithm/ecc_signing_algo.dart b/packages/at_chops/lib/src/signing/ecc_signing_algo.dart similarity index 96% rename from packages/at_chops/lib/src/algorithm/ecc_signing_algo.dart rename to packages/at_chops/lib/src/signing/ecc_signing_algo.dart index 555406dc..7255d41f 100644 --- a/packages/at_chops/lib/src/algorithm/ecc_signing_algo.dart +++ b/packages/at_chops/lib/src/signing/ecc_signing_algo.dart @@ -1,6 +1,6 @@ import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; +import 'package:at_chops/src/signing/at_signing_algorithm.dart'; import 'package:at_commons/at_commons.dart'; import 'package:crypto/crypto.dart'; import 'package:ecdsa/ecdsa.dart' as ecdsa; diff --git a/packages/at_chops/lib/src/algorithm/ed25519_signing_algo.dart b/packages/at_chops/lib/src/signing/ed25519_signing_algo.dart similarity index 95% rename from packages/at_chops/lib/src/algorithm/ed25519_signing_algo.dart rename to packages/at_chops/lib/src/signing/ed25519_signing_algo.dart index e4c4daf4..e39a85f7 100644 --- a/packages/at_chops/lib/src/algorithm/ed25519_signing_algo.dart +++ b/packages/at_chops/lib/src/signing/ed25519_signing_algo.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; +import 'package:at_chops/src/signing/at_signing_algorithm.dart'; import 'package:at_commons/at_commons.dart'; import 'package:better_cryptography/better_cryptography.dart'; diff --git a/packages/at_chops/lib/src/signing/pkam_signing_algo.dart b/packages/at_chops/lib/src/signing/pkam_signing_algo.dart new file mode 100644 index 00000000..d4d71d7e --- /dev/null +++ b/packages/at_chops/lib/src/signing/pkam_signing_algo.dart @@ -0,0 +1,8 @@ +import 'package:at_chops/src/signing/rsa_signing_algo.dart'; + +/// Data signing and verification for Public Key Authentication Mechanism - Pkam +class PkamSigningAlgo extends RSASigningAlgo { + @override + String get errorMessageKeyname => "Pkam"; + PkamSigningAlgo(super.encryptionKeyPair, super.hashingAlgoType); +} diff --git a/packages/at_chops/lib/src/algorithm/default_signing_algo.dart b/packages/at_chops/lib/src/signing/rsa_signing_algo.dart similarity index 78% rename from packages/at_chops/lib/src/algorithm/default_signing_algo.dart rename to packages/at_chops/lib/src/signing/rsa_signing_algo.dart index 5380e2b2..098605e7 100644 --- a/packages/at_chops/lib/src/algorithm/default_signing_algo.dart +++ b/packages/at_chops/lib/src/signing/rsa_signing_algo.dart @@ -1,18 +1,19 @@ import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/algo_type.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; -import 'package:at_chops/src/key/impl/at_encryption_key_pair.dart'; +import 'package:at_chops/src/hashing/hashing.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; +import 'package:at_chops/src/signing/at_signing_algorithm.dart'; import 'package:at_commons/at_commons.dart'; import 'package:crypton/crypton.dart'; /// Data signing and verification using atsign encryption keypair /// Allowed algorithms are listed in [SigningAlgoType] and [HashingAlgoType] -class DefaultSigningAlgo implements AtSigningAlgorithm { - final AtEncryptionKeyPair? _encryptionKeyPair; +class RSASigningAlgo implements AtSigningAlgorithm { + final AtRSAKeyPair? _encryptionKeyPair; final HashingAlgoType _hashingAlgoType; + final String errorMessageKeyname = "Encryption"; - DefaultSigningAlgo(this._encryptionKeyPair, this._hashingAlgoType); + RSASigningAlgo(this._encryptionKeyPair, this._hashingAlgoType); @override Uint8List sign(Uint8List data) { @@ -43,7 +44,7 @@ class DefaultSigningAlgo implements AtSigningAlgorithm { RSAPublicKey.fromString(_encryptionKeyPair!.atPublicKey.publicKey); } else { throw AtSigningVerificationException( - 'Encryption key pair or public key not set for default signing algo'); + '$errorMessageKeyname key pair or public key not set for default signing algo'); } switch (_hashingAlgoType) { case HashingAlgoType.sha256: diff --git a/packages/at_chops/lib/src/signing/signing.dart b/packages/at_chops/lib/src/signing/signing.dart new file mode 100644 index 00000000..b1933a31 --- /dev/null +++ b/packages/at_chops/lib/src/signing/signing.dart @@ -0,0 +1,5 @@ +export 'at_signing_algorithm.dart'; +export 'ecc_signing_algo.dart'; +export 'ed25519_signing_algo.dart'; +export 'pkam_signing_algo.dart'; +export 'rsa_signing_algo.dart'; diff --git a/packages/at_chops/lib/types.dart b/packages/at_chops/lib/types.dart index cdd3cc84..b67e4c8e 100644 --- a/packages/at_chops/lib/types.dart +++ b/packages/at_chops/lib/types.dart @@ -1,23 +1,18 @@ -library at_chops_types; - -/// library at_chops/types.dart provides all of the types for implementing -/// your own algorithms for use with at_chops. - // Algorithm interfaces -export 'src/algorithm/at_algorithm.dart'; -export 'src/algorithm/at_iv.dart'; +export 'src/encryption/at_encryption_algorithm.dart'; +export 'src/encryption/initalisation_vector.dart'; + // Key interfaces -export 'src/key/at_key_pair.dart'; -export 'src/key/at_private_key.dart'; -export 'src/key/at_public_key.dart'; -export 'src/key/key_names.dart'; +export 'src/key/asymmetric_key_pair.dart'; +export 'src/key/symmetric_key.dart'; export 'src/key/key_type.dart'; + // Metadata Interfaces +export 'src/model/at_signing_input.dart'; +export 'src/model/encryption_metadata.dart'; +export 'src/model/encryption_result.dart'; +export 'src/model/signing_metadata.dart'; +export 'src/model/signing_result.dart'; -export 'src/metadata/at_signing_input.dart'; -export 'src/metadata/encryption_metadata.dart'; -export 'src/metadata/encryption_result.dart'; -export 'src/metadata/signing_metadata.dart'; -export 'src/metadata/signing_result.dart'; // Util -export 'src/util/at_chops_util.dart'; +export 'src/at_chops_util.dart'; diff --git a/packages/at_chops/test/aes_encrption_old_impl.dart b/packages/at_chops/test/aes_encrption_old_impl.dart index 6ad7b0ef..70c9b55b 100644 --- a/packages/at_chops/test/aes_encrption_old_impl.dart +++ b/packages/at_chops/test/aes_encrption_old_impl.dart @@ -1,14 +1,14 @@ import 'dart:typed_data'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; +import 'package:at_chops/src/encryption/at_encryption_algorithm.dart'; import 'package:at_commons/at_commons.dart'; import 'package:encrypt/encrypt.dart'; /// Old AES implementation. Used for testing backward compatibility. class AESEncryptionAlgoV1 implements SymmetricEncryptionAlgorithm { - final AESKey _aesKey; + final AtAESKey _aesKey; AESEncryptionAlgoV1(this._aesKey); @@ -40,13 +40,13 @@ class AESEncryptionAlgoV1 /// A class that provides AES encryption and decryption for strings, /// implementing the [SymmetricEncryptionAlgorithm] interface. /// -/// This class uses an [AESKey] to perform encryption and decryption of strings. +/// This class uses an [AtAESKey] to perform encryption and decryption of strings. /// The key and an [InitialisationVector] (IV) are used for encryption, and the /// same key must be used for decryption. class StringAESEncryptor implements SymmetricEncryptionAlgorithm { /// The AES key used for encryption and decryption. - final AESKey _aesKey; + final AtAESKey _aesKey; /// Constructs an instance of [StringAESEncryptor] with the provided [_aesKey]. /// diff --git a/packages/at_chops/test/aes_encryption_algo_test.dart b/packages/at_chops/test/aes_encryption_algo_test.dart index b5138829..d795daf4 100644 --- a/packages/at_chops/test/aes_encryption_algo_test.dart +++ b/packages/at_chops/test/aes_encryption_algo_test.dart @@ -9,7 +9,7 @@ void main() { () { test('Encrypt with old algo and decrypt with better crypto', () async { var data = 'Hello World'; - var aesKey = AESKey.generate(32); + var aesKey = AtAESKey.generate(32); var iv = AtChopsUtil.generateRandomIV(16); final encryptionAlgo = AESEncryptionAlgoV1(aesKey); var encryptedBytes = encryptionAlgo.encrypt(utf8.encode(data), iv: iv); @@ -21,7 +21,7 @@ void main() { test('Encrypt with better crypto AES algo and decrypt with old algo', () async { var data = 'Hello World12345'; - var aesKey = AESKey.generate(32); + var aesKey = AtAESKey.generate(32); var iv = AtChopsUtil.generateRandomIV(16); final betterCryptoAESAlgo = AESEncryptionAlgo(aesKey); var encryptedBytes = @@ -36,7 +36,7 @@ void main() { () { test('Test encryption and decryption for 128 bit AES key', () async { var data = 'Hello World🛠'; - var aesKey = AESKey.generate(16); + var aesKey = AtAESKey.generate(16); var iv = AtChopsUtil.generateRandomIV(16); final betterCryptoAESAlgo = AESEncryptionAlgo(aesKey); var encryptedBytes = @@ -47,7 +47,7 @@ void main() { }); test('Test encryption and decryption for 192 bit AES key', () async { var data = 'Hello\nWorld🛠\n123asdasd!@&^'; - var aesKey = AESKey.generate(24); + var aesKey = AtAESKey.generate(24); var iv = AtChopsUtil.generateRandomIV(16); final betterCryptoAESAlgo = AESEncryptionAlgo(aesKey); var encryptedBytes = @@ -58,7 +58,7 @@ void main() { }); test('Test encryption and decryption for 256 bit AES key', () async { var data = '🛠Hello\nWorld🛠\n123asdasd!@&^\'🛠'; - var aesKey = AESKey.generate(32); + var aesKey = AtAESKey.generate(32); var iv = AtChopsUtil.generateRandomIV(16); final betterCryptoAESAlgo = AESEncryptionAlgo(aesKey); var encryptedBytes = diff --git a/packages/at_chops/test/aes_key_test.dart b/packages/at_chops/test/aes_key_test.dart index 76e58d11..1fdaf441 100644 --- a/packages/at_chops/test/aes_key_test.dart +++ b/packages/at_chops/test/aes_key_test.dart @@ -1,52 +1,52 @@ -import 'package:at_chops/src/key/impl/aes_key.dart'; +import 'package:at_chops/src/key/at_aes_key.dart'; import 'package:encrypt/encrypt.dart'; import 'package:test/test.dart'; void main() { group('A group of tests for AES Key generation', () { test('Test generate AESKey - 128 bit', () { - final aesKey = AESKey.generate(16); + final aesKey = AtAESKey.generate(16); expect(Key.fromBase64(aesKey.key).length, 16); }); test('Test generate AESKey - 128 bit random generation', () { - final aesKey_1 = AESKey.generate(16); - final aesKey_2 = AESKey.generate(16); + final aesKey_1 = AtAESKey.generate(16); + final aesKey_2 = AtAESKey.generate(16); expect(aesKey_1, isNot(aesKey_2)); }); test('Test generate AESKey - 256 bit', () { - final aesKey = AESKey.generate(32); + final aesKey = AtAESKey.generate(32); expect(Key.fromBase64(aesKey.key).length, 32); }); test('Test generate AESKey - 256 bit random generation', () { - final aesKey_1 = AESKey.generate(32); - final aesKey_2 = AESKey.generate(32); + final aesKey_1 = AtAESKey.generate(32); + final aesKey_2 = AtAESKey.generate(32); expect(aesKey_1, isNot(aesKey_2)); }); test('check random key generated length for 128 bit key', () { - final aesKey = AESKey.generate(16); + final aesKey = AtAESKey.generate(16); expect(aesKey.getLength(), 16); }); test('check random key generated length for 192 bit key', () { - final aesKey = AESKey.generate(24); + final aesKey = AtAESKey.generate(24); expect(aesKey.getLength(), 24); }); test('check random key generated length for 256 bit key', () { - final aesKey = AESKey.generate(32); + final aesKey = AtAESKey.generate(32); expect(aesKey.getLength(), 32); }); test('verify key length for 256 bit key constructed from string', () { - final aesKey_1 = AESKey.generate(32); - final aesKey = AESKey(aesKey_1.key); + final aesKey_1 = AtAESKey.generate(32); + final aesKey = AtAESKey(aesKey_1.key); expect(aesKey.getLength(), 32); }); test('verify key length for 192 bit key constructed from string', () { - final aesKey_1 = AESKey.generate(24); - final aesKey = AESKey(aesKey_1.key); + final aesKey_1 = AtAESKey.generate(24); + final aesKey = AtAESKey(aesKey_1.key); expect(aesKey.getLength(), 24); }); test('verify key length for 128 bit key constructed from string', () { - final aesKey_1 = AESKey.generate(16); - final aesKey = AESKey(aesKey_1.key); + final aesKey_1 = AtAESKey.generate(16); + final aesKey = AtAESKey(aesKey_1.key); expect(aesKey.getLength(), 16); }); }); diff --git a/packages/at_chops/test/at_chops_test.dart b/packages/at_chops/test/at_chops_test.dart index f799be1d..c12a7416 100644 --- a/packages/at_chops/test/at_chops_test.dart +++ b/packages/at_chops/test/at_chops_test.dart @@ -2,7 +2,8 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/algorithm/at_algorithm.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; +import 'package:at_chops/types.dart'; import 'package:at_commons/at_commons.dart'; import 'package:at_utils/at_logger.dart'; import 'package:crypton/crypton.dart'; @@ -253,7 +254,7 @@ void main() { AtSigningInput signingInput = AtSigningInput(data); signingInput.signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); final signingResult = atChops.sign(signingInput); expect(signingResult.atSigningMetaData, isNotNull); expect( @@ -270,8 +271,8 @@ void main() { AtSigningVerificationInput? verificationInput = AtSigningVerificationInput(data, base64Decode(signingResult.result), encryptionKeypair.atPublicKey.publicKey); - verificationInput.signingAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + verificationInput.signingAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); AtSigningResult verificationResult = atChops.verify(verificationInput); expect(verificationResult.atSigningMetaData, isNotNull); expect(verificationResult.atSigningResultType, AtSigningResultType.bool); @@ -296,7 +297,7 @@ void main() { signingInput.signingAlgoType = SigningAlgoType.rsa2048; signingInput.hashingAlgoType = HashingAlgoType.sha256; AtSigningAlgorithm signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); signingInput.signingAlgorithm = signingAlgorithm; final signingResult = atChops.sign(signingInput); expect(signingResult.atSigningMetaData, isNotNull); @@ -316,8 +317,8 @@ void main() { encryptionKeypair.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha256; - AtSigningAlgorithm verifyAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + AtSigningAlgorithm verifyAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); verificationInput.signingAlgorithm = verifyAlgorithm; AtSigningResult verificationResult = atChops.verify(verificationInput); expect(verificationResult.atSigningMetaData, isNotNull); @@ -343,7 +344,7 @@ void main() { signingInput.signingAlgoType = SigningAlgoType.rsa2048; signingInput.hashingAlgoType = HashingAlgoType.sha512; AtSigningAlgorithm signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); signingInput.signingAlgorithm = signingAlgorithm; final signingResult = atChops.sign(signingInput); expect(signingResult.atSigningMetaData, isNotNull); @@ -363,8 +364,8 @@ void main() { encryptionKeypair.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha512; - AtSigningAlgorithm verifyAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + AtSigningAlgorithm verifyAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); verificationInput.signingAlgorithm = verifyAlgorithm; AtSigningResult verificationResult = atChops.verify(verificationInput); expect(verificationResult.atSigningMetaData, isNotNull); @@ -388,7 +389,7 @@ void main() { signingInput.signingAlgoType = SigningAlgoType.rsa2048; signingInput.hashingAlgoType = HashingAlgoType.sha256; signingInput.signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); final signingResult = atChops.sign(signingInput); AtSigningVerificationInput? verificationInput = @@ -396,8 +397,8 @@ void main() { encryptionKeypair.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha512; - verificationInput.signingAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + verificationInput.signingAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); AtSigningResult verificationResult = atChops.verify(verificationInput); expect(verificationResult.atSigningMetaData, isNotNull); @@ -421,7 +422,7 @@ void main() { signingInput.signingAlgoType = SigningAlgoType.rsa2048; signingInput.hashingAlgoType = HashingAlgoType.sha512; signingInput.signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); final signingResult = atChops.sign(signingInput); AtSigningVerificationInput? verificationInput = @@ -429,8 +430,8 @@ void main() { encryptionKeypair.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha256; - verificationInput.signingAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + verificationInput.signingAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); AtSigningResult verificationResult = atChops.verify(verificationInput); expect(verificationResult.atSigningMetaData, isNotNull); @@ -453,8 +454,8 @@ void main() { data, 'dummysignature', encryptionKeypair.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha256; - AtSigningAlgorithm verifyAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + AtSigningAlgorithm verifyAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); verificationInput.signingAlgorithm = verifyAlgorithm; AtSigningResult verificationResult = atChops.verify(verificationInput); @@ -478,8 +479,8 @@ void main() { encryptionKeypair.atPublicKey.publicKey); verificationInput.signingAlgoType = SigningAlgoType.rsa2048; verificationInput.hashingAlgoType = HashingAlgoType.sha512; - AtSigningAlgorithm verifyAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + AtSigningAlgorithm verifyAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); verificationInput.signingAlgorithm = verifyAlgorithm; AtSigningResult verificationResult = atChops.verify(verificationInput); @@ -497,21 +498,21 @@ void main() { final encryptionKeypair = AtChopsUtil.generateAtEncryptionKeyPair(); final anotherEncryptionKeypair = AtChopsUtil.generateAtEncryptionKeyPair(); - AtEncryptionKeyPair dummyKeyPair = AtEncryptionKeyPair.create( - encryptionKeypair.atPrivateKey.privateKey, ''); + AtRSAKeyPair dummyKeyPair = + AtRSAKeyPair.create(encryptionKeypair.atPrivateKey.privateKey, ''); final atChopsKeys = AtChopsKeys.create(dummyKeyPair, null); final atChops = AtChopsImpl(atChopsKeys); AtSigningInput signingInput = AtSigningInput(data); signingInput.signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); final signingResult = atChops.sign(signingInput); AtSigningVerificationInput? verificationInput = AtSigningVerificationInput(data, base64Decode(signingResult.result), anotherEncryptionKeypair.atPublicKey.publicKey); - verificationInput.signingAlgorithm = DefaultSigningAlgo( - encryptionKeypair, verificationInput.hashingAlgoType); + verificationInput.signingAlgorithm = + RSASigningAlgo(encryptionKeypair, verificationInput.hashingAlgoType); AtSigningResult verificationResult = atChops.verify(verificationInput); expect(verificationResult.atSigningMetaData, isNotNull); @@ -529,7 +530,7 @@ void main() { AtSigningInput signingInput = AtSigningInput('abcde'); signingInput.signingAlgorithm = - DefaultSigningAlgo(null, signingInput.hashingAlgoType); + RSASigningAlgo(null, signingInput.hashingAlgoType); try { atChops.sign(signingInput); } catch (e, _) { @@ -546,7 +547,7 @@ void main() { AtSigningInput signingInput = AtSigningInput(213456777); signingInput.signingAlgorithm = - DefaultSigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); + RSASigningAlgo(encryptionKeypair, signingInput.hashingAlgoType); try { atChops.sign(signingInput); } catch (e, _) { diff --git a/packages/at_chops/test/at_chops_util_test.dart b/packages/at_chops/test/at_chops_util_test.dart index 4e2070ce..de486fe4 100644 --- a/packages/at_chops/test/at_chops_util_test.dart +++ b/packages/at_chops/test/at_chops_util_test.dart @@ -2,7 +2,7 @@ import 'dart:convert'; import 'dart:math'; import 'package:collection/collection.dart'; -import 'package:at_chops/src/util/at_chops_util.dart'; +import 'package:at_chops/src/at_chops_util.dart'; import 'package:encrypt/encrypt.dart'; import 'package:test/test.dart'; @@ -37,8 +37,7 @@ void main() { var random = Random(); List randomBytes = List.generate(16, (i) => random.nextInt(256)); - var iv = - AtChopsUtil.generateIVFromBase64String(base64.encode(randomBytes)); + var iv = AtChopsUtil.ivFromBase64(base64.encode(randomBytes)); expect(ListEquality().equals(iv.ivBytes, randomBytes), true); }); }); diff --git a/packages/at_chops/test/default_signing_algo_test.dart b/packages/at_chops/test/default_signing_algo_test.dart index f23218fb..a31938e2 100644 --- a/packages/at_chops/test/default_signing_algo_test.dart +++ b/packages/at_chops/test/default_signing_algo_test.dart @@ -11,7 +11,7 @@ void main() { () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -25,7 +25,7 @@ void main() { () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(keySize: 4096); final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -37,7 +37,7 @@ void main() { test('Test default signing and verification - set sha256 hashing algo', () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -49,7 +49,7 @@ void main() { test('Test default signing and verification - set sha512 hashing algo', () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha512); + RSASigningAlgo(keyPair, HashingAlgoType.sha512); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -63,9 +63,9 @@ void main() { () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultsigningalgoSha256 = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final defaultsigningalgoSha512 = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha512); + RSASigningAlgo(keyPair, HashingAlgoType.sha512); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -80,9 +80,9 @@ void main() { () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultsigningalgoSha256 = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final defaultsigningalgoSha512 = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha512); + RSASigningAlgo(keyPair, HashingAlgoType.sha512); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -95,8 +95,7 @@ void main() { 'Test default signing and verification - set md5 hashing algo - not supported', () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); - final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.md5); + final defaultSigningAlgo = RSASigningAlgo(keyPair, HashingAlgoType.md5); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -108,8 +107,7 @@ void main() { 'Hashing algo HashingAlgoType.md5 is invalid/not supported')))); }); test('Test default signing - key pair not set', () { - final defaultSigningAlgo = - DefaultSigningAlgo(null, HashingAlgoType.sha256); + final defaultSigningAlgo = RSASigningAlgo(null, HashingAlgoType.sha256); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -123,7 +121,7 @@ void main() { test('Test default verification - passing public key', () { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); @@ -138,7 +136,7 @@ void main() { var keyPair = AtChopsUtil.generateAtEncryptionKeyPair(); var keyPair2 = AtChopsUtil.generateAtEncryptionKeyPair(); final defaultSigningAlgo = - DefaultSigningAlgo(keyPair, HashingAlgoType.sha256); + RSASigningAlgo(keyPair, HashingAlgoType.sha256); final dataToSign = '_a7028ce7-aaa8-4c52-9cf4-b94ca3bdf971@alice:c2834cd4-bb16-4801-8abc-efe79cdceb8f'; final dataInBytes = Uint8List.fromList(dataToSign.codeUnits); diff --git a/packages/at_chops/test/ecc_signing_algo_test.dart b/packages/at_chops/test/ecc_signing_algo_test.dart index f5aad3b5..e41d17c9 100644 --- a/packages/at_chops/test/ecc_signing_algo_test.dart +++ b/packages/at_chops/test/ecc_signing_algo_test.dart @@ -1,6 +1,6 @@ import 'dart:typed_data'; -import 'package:at_chops/src/algorithm/ecc_signing_algo.dart'; +import 'package:at_chops/src/signing/ecc_signing_algo.dart'; import 'package:elliptic/elliptic.dart'; import 'package:at_commons/at_commons.dart'; import 'package:test/test.dart'; diff --git a/packages/at_chops/test/ed25519_signing_algo_test.dart b/packages/at_chops/test/ed25519_signing_algo_test.dart index 198aae93..7e40e97d 100644 --- a/packages/at_chops/test/ed25519_signing_algo_test.dart +++ b/packages/at_chops/test/ed25519_signing_algo_test.dart @@ -2,7 +2,7 @@ import 'dart:math'; import 'dart:typed_data'; import 'package:at_chops/at_chops.dart'; -import 'package:at_chops/src/algorithm/ed25519_signing_algo.dart'; +import 'package:at_chops/src/signing/ed25519_signing_algo.dart'; import 'package:test/test.dart'; void main() { diff --git a/packages/at_chops/test/pkcs7_padding_test.dart b/packages/at_chops/test/pkcs7_padding_test.dart index bee04e5c..8bc696e9 100644 --- a/packages/at_chops/test/pkcs7_padding_test.dart +++ b/packages/at_chops/test/pkcs7_padding_test.dart @@ -1,5 +1,4 @@ -import 'package:at_chops/src/algorithm/padding/padding_params.dart'; -import 'package:at_chops/src/algorithm/padding/pkcs7padding.dart'; +import 'package:at_chops/src/padding/pkcs7_padding.dart'; import 'package:at_commons/at_commons.dart'; import 'package:test/test.dart'; @@ -7,7 +6,7 @@ void main() { group('A group of tests to verify pkcs7 padding', () { test('A test to verify padding when data length is less than block size', () { - final paddingAlgo = PKCS7Padding(PaddingParams()..blockSize = 16); + final paddingAlgo = PKCS7Padding(PKCS7PaddingParams()..blockSize = 16); var dataString = 'Hello World'; var unPaddedData = dataString.codeUnits; var paddedData = paddingAlgo.addPadding(dataString.codeUnits); @@ -21,7 +20,7 @@ void main() { }); test('A test to verify padding when data length is equal to block size', () { - final paddingAlgo = PKCS7Padding(PaddingParams()..blockSize = 16); + final paddingAlgo = PKCS7Padding(PKCS7PaddingParams()..blockSize = 16); var dataString = 'Hello World12345'; var unPaddedData = dataString.codeUnits; var paddedData = paddingAlgo.addPadding(dataString.codeUnits); @@ -36,7 +35,7 @@ void main() { test( 'A test to verify padding when data length is one less than block size', () { - final paddingAlgo = PKCS7Padding(PaddingParams()..blockSize = 16); + final paddingAlgo = PKCS7Padding(PKCS7PaddingParams()..blockSize = 16); var dataString = 'Hello World1234'; var unPaddedData = dataString.codeUnits; var paddedData = paddingAlgo.addPadding(dataString.codeUnits); @@ -49,20 +48,20 @@ void main() { expect(dataAfterRemovingPadding, unPaddedData); }); test('A test to verify invalid block size', () { - var paddingAlgo = PKCS7Padding(PaddingParams()..blockSize = -10); + var paddingAlgo = PKCS7Padding(PKCS7PaddingParams()..blockSize = -10); var dataString = 'Hello World1234'; expect( () => paddingAlgo.addPadding(dataString.codeUnits), throwsA(predicate((e) => e is AtEncryptionException && e.toString().contains('Block size must be between 1 and 255.')))); - paddingAlgo = PKCS7Padding(PaddingParams()..blockSize = 0); + paddingAlgo = PKCS7Padding(PKCS7PaddingParams()..blockSize = 0); expect( () => paddingAlgo.addPadding(dataString.codeUnits), throwsA(predicate((e) => e is AtEncryptionException && e.toString().contains('Block size must be between 1 and 255.')))); - paddingAlgo = PKCS7Padding(PaddingParams()..blockSize = 300); + paddingAlgo = PKCS7Padding(PKCS7PaddingParams()..blockSize = 300); expect( () => paddingAlgo.addPadding(dataString.codeUnits), throwsA(predicate((e) => @@ -70,7 +69,7 @@ void main() { e.toString().contains('Block size must be between 1 and 255.')))); }); test('A test to verify invalid input data to remove padding', () { - var paddingAlgo = PKCS7Padding(PaddingParams()); + var paddingAlgo = PKCS7Padding(PKCS7PaddingParams()); List invalidInput = []; expect( () => paddingAlgo.removePadding(invalidInput), diff --git a/packages/at_chops/test/rsa_encryption_algo_test.dart b/packages/at_chops/test/rsa_encryption_algo_test.dart index b9b45ae4..9f6c62ad 100644 --- a/packages/at_chops/test/rsa_encryption_algo_test.dart +++ b/packages/at_chops/test/rsa_encryption_algo_test.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:at_chops/at_chops.dart'; +import 'package:at_chops/src/key/at_rsa_key_pair.dart'; import 'package:at_commons/at_commons.dart'; import 'package:test/test.dart'; @@ -38,7 +39,7 @@ void main() { test('Test encrypt throws exception when passed public key is null', () { var defaultEncryptionAlgo = RsaEncryptionAlgo(); var dataToEncrypt = 'Hello World12!@'; - AtPublicKey? publicKey; + AtRSAPublicKey? publicKey; defaultEncryptionAlgo.atPublicKey = publicKey; expect( () => defaultEncryptionAlgo.encrypt(utf8.encode(dataToEncrypt)), @@ -49,7 +50,7 @@ void main() { test('Test decrypt throws exception when passed private key is null', () { var defaultEncryptionAlgo = RsaEncryptionAlgo(); var encryptedData = 'random data'; - AtPrivateKey? privateKey; + AtRSAPrivateKey? privateKey; defaultEncryptionAlgo.atPrivateKey = privateKey; expect( () => defaultEncryptionAlgo.decrypt(utf8.encode(encryptedData)), diff --git a/packages/at_onboarding_cli/example/apkam_examples/enroll_app_listen.dart b/packages/at_onboarding_cli/example/apkam_examples/enroll_app_listen.dart index 107b1899..d943cbd3 100644 --- a/packages/at_onboarding_cli/example/apkam_examples/enroll_app_listen.dart +++ b/packages/at_onboarding_cli/example/apkam_examples/enroll_app_listen.dart @@ -93,7 +93,7 @@ AtAuthKeys _decryptAtKeysFile(Map jsonData) { var securityKeys = AtAuthKeys(); String decryptionKey = jsonData[auth_constants.defaultSelfEncryptionKey]!; var atChops = - AtChopsImpl(AtChopsKeys()..selfEncryptionKey = AESKey(decryptionKey)); + AtChopsImpl(AtChopsKeys()..selfEncryptionKey = AtAESKey(decryptionKey)); securityKeys.defaultEncryptionPublicKey = atChops .decryptString(jsonData[auth_constants.defaultEncryptionPublicKey]!, EncryptionKeyType.aes256, @@ -145,9 +145,10 @@ AtChops _createAtChops(AtAuthKeys atKeysFile) { atKeysFile.apkamPublicKey!, atKeysFile.apkamPrivateKey!); final atChopsKeys = AtChopsKeys.create(atEncryptionKeyPair, atPkamKeyPair); if (atKeysFile.apkamSymmetricKey != null) { - atChopsKeys.apkamSymmetricKey = AESKey(atKeysFile.apkamSymmetricKey!); + atChopsKeys.apkamSymmetricKey = AtAESKey(atKeysFile.apkamSymmetricKey!); } - atChopsKeys.selfEncryptionKey = AESKey(atKeysFile.defaultSelfEncryptionKey!); + atChopsKeys.selfEncryptionKey = + AtAESKey(atKeysFile.defaultSelfEncryptionKey!); return AtChopsImpl(atChopsKeys); } diff --git a/packages/at_onboarding_cli/lib/src/onboard/at_onboarding_service_impl.dart b/packages/at_onboarding_cli/lib/src/onboard/at_onboarding_service_impl.dart index 1a748ec7..283973cf 100644 --- a/packages/at_onboarding_cli/lib/src/onboard/at_onboarding_service_impl.dart +++ b/packages/at_onboarding_cli/lib/src/onboard/at_onboarding_service_impl.dart @@ -234,7 +234,7 @@ class AtOnboardingServiceImpl implements AtOnboardingService { AtPkamKeyPair.create(enrollmentResponse.atAuthKeys!.apkamPublicKey!, enrollmentResponse.atAuthKeys!.apkamPrivateKey!)); atChopsKeys.apkamSymmetricKey = - AESKey(enrollmentResponse.atAuthKeys!.apkamSymmetricKey!); + AtAESKey(enrollmentResponse.atAuthKeys!.apkamSymmetricKey!); AtLookupImpl atLookUpImpl = AtLookupImpl(_atSign, atOnboardingPreference.rootDomain, atOnboardingPreference.rootPort);