-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: at_chops faster aes impl #744
Open
murali-shris
wants to merge
13
commits into
trunk
Choose a base branch
from
at_chops_faster_aes
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cf74276
feat: better crypto AES impl
murali-shris 009e7ee
fix: at_auth added await to encrypt methods
murali-shris 836af9f
fix: at_auth unit tests
murali-shris f8975b5
fix: move at_auth changes to new PR
murali-shris 7aad63e
fix: at_auth revert changes
murali-shris aa635f3
fix: analyzer issue
murali-shris fbec869
fix: analyzer issue
murali-shris e32fed4
git: add dependency overrides for at_auth in func test
murali-shris 6812b7c
fix: changelog and pubspec
murali-shris a9d2c02
fix: onboarding func test failure
murali-shris 9ffae1a
fix: pubspec
murali-shris 632985c
fix: added dart docs
murali-shris 8ac9637
fix: analyzer issue
murali-shris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, relevant RFC: RFC 5652