-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #744 from atsign-foundation/at_chops_faster_aes
feat: at_chops faster aes impl
- Loading branch information
Showing
18 changed files
with
539 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'package:at_chops/at_chops.dart'; | ||
import 'package:at_commons/at_commons.dart'; | ||
import 'package:better_cryptography/better_cryptography.dart'; | ||
|
||
/// A factory class to create AES-CTR encryption algorithms based on the key length. | ||
/// | ||
/// The `AesCtrFactory` class provides a static method to create an instance of | ||
/// the `AesCtr` encryption algorithm. The key length of the provided `AESKey` | ||
/// determines the specific variant of AES-CTR to be instantiated. | ||
class AesCtrFactory { | ||
/// Creates an `AesCtr` encryption algorithm based on the key length of the given [aesKey]. | ||
/// | ||
/// The `aesKey` must have a length of 16, 24, or 32 bytes to correspond to AES-128, AES-192, | ||
/// or AES-256 respectively. A `MacAlgorithm.empty` is used for each variant. | ||
/// | ||
/// Throws an [AtEncryptionException] if the provided key length is invalid. | ||
/// | ||
/// Example usage: | ||
/// ```dart | ||
/// AESKey aesKey =AESKey.generate(32);//pass the length in bytes | ||
/// AesCtr encryptionAlgo = AesCtrFactory.createEncryptionAlgo(aesKey); | ||
/// ``` | ||
/// | ||
/// - [aesKey]: An instance of `AESKey` containing the encryption key. | ||
/// - Returns: An instance of `AesCtr` configured for the appropriate key length. | ||
/// | ||
/// Supported key lengths: | ||
/// - 16 bytes for AES-128 | ||
/// - 24 bytes for AES-192 | ||
/// - 32 bytes for AES-256 | ||
static AesCtr createEncryptionAlgo(AESKey aesKey) { | ||
switch (aesKey.getLength()) { | ||
case 16: | ||
return AesCtr.with128bits(macAlgorithm: MacAlgorithm.empty); | ||
case 24: | ||
return AesCtr.with192bits(macAlgorithm: MacAlgorithm.empty); | ||
case 32: | ||
return AesCtr.with256bits(macAlgorithm: MacAlgorithm.empty); | ||
default: | ||
throw AtEncryptionException( | ||
'Invalid AES key length. Valid lengths are 16/24/32 bytes'); | ||
} | ||
} | ||
} |
47 changes: 34 additions & 13 deletions
47
packages/at_chops/lib/src/algorithm/aes_encryption_algo.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/// An abstract class that defines a padding algorithm for AES encryption and decryption. | ||
/// | ||
/// The `PaddingAlgorithm` class provides methods to add padding bytes during encryption | ||
/// and to remove padding bytes during decryption. Padding ensures that the input data | ||
/// size is compatible with the block size required by AES encryption algorithms. | ||
abstract class PaddingAlgorithm { | ||
/// Adds padding bytes to the given [data] to make its length a multiple of the AES block size. | ||
/// | ||
/// This method appends padding bytes to the input data so that its length becomes | ||
/// a multiple of the AES block size (16 bytes). The exact padding scheme is | ||
/// implementation-dependent. | ||
/// | ||
/// - [data]: A list of bytes representing the input data to be padded. | ||
/// - Returns: A new list of bytes containing the padded data. | ||
/// | ||
/// Example usage: | ||
/// ```dart | ||
/// PaddingAlgorithm paddingAlgorithm = PKCS7Padding(); | ||
/// List<int> paddedData = paddingAlgorithm.addPadding([0x01, 0x02, 0x03]); | ||
/// ``` | ||
List<int> addPadding(List<int> data); | ||
|
||
/// Removes padding bytes from the given [data], restoring it to its original unpadded form. | ||
/// | ||
/// This method removes any padding bytes that were added during encryption to return | ||
/// the data to its original state. The exact removal logic depends on the padding | ||
/// scheme used during encryption. | ||
/// | ||
/// - [data]: A list of bytes representing the padded input data. | ||
/// - Returns: A new list of bytes containing the original unpadded data. | ||
/// | ||
/// Example usage: | ||
/// ```dart | ||
/// PaddingAlgorithm paddingAlgorithm = PKCS7Padding(); | ||
/// List<int> unpaddedData = paddingAlgorithm.removePadding([0x01, 0x02, 0x03, 0x05, 0x05, 0x05]); | ||
/// ``` | ||
List<int> removePadding(List<int> data); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/at_chops/lib/src/algorithm/padding/padding_params.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// 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; | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/at_chops/lib/src/algorithm/padding/pkcs7padding.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:at_chops/src/algorithm/padding/padding.dart'; | ||
import 'package:at_chops/src/algorithm/padding/padding_params.dart'; | ||
import 'package:at_commons/at_commons.dart'; | ||
|
||
class PKCS7Padding implements PaddingAlgorithm { | ||
final PaddingParams _paddingParams; | ||
PKCS7Padding(this._paddingParams); | ||
@override | ||
List<int> addPadding(List<int> data) { | ||
if (_paddingParams.blockSize <= 0 || _paddingParams.blockSize > 255) { | ||
throw AtEncryptionException('Block size must be between 1 and 255.'); | ||
} | ||
|
||
// Calculate the number of padding bytes needed | ||
int padding = | ||
_paddingParams.blockSize - (data.length % _paddingParams.blockSize); | ||
|
||
// Add padding bytes to the data | ||
List<int> paddedData = List.from(data); | ||
paddedData.addAll(List.filled(padding, padding)); | ||
|
||
return paddedData; | ||
} | ||
|
||
@override | ||
List<int> removePadding(List<int> data) { | ||
if (data.isEmpty) { | ||
throw AtDecryptionException('Encrypted data cannot be empty'); | ||
} | ||
|
||
// Get the value of the last byte (padding length) | ||
int paddingLength = data.last; | ||
|
||
// Validate padding length | ||
if (paddingLength <= 0 || paddingLength > data.length) { | ||
throw AtDecryptionException('Invalid padding length'); | ||
} | ||
|
||
// Check if all padding bytes are valid | ||
for (int i = data.length - paddingLength; i < data.length; i++) { | ||
if (data[i] != paddingLength) { | ||
throw AtDecryptionException('Invalid PKCS7 padding'); | ||
} | ||
} | ||
|
||
// Return the data without padding | ||
return data.sublist(0, data.length - paddingLength); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.