Skip to content

Commit

Permalink
Adds SaltedCipher mixin to XChaCha20, XSalsa20, and all AEAD ciphers
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Sep 11, 2024
1 parent bc0a363 commit 96b1f56
Show file tree
Hide file tree
Showing 31 changed files with 965 additions and 327 deletions.
2 changes: 1 addition & 1 deletion benchmark/chacha20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CipherlibBenchmark extends Benchmark {

@override
void run() {
cipher.chacha20(input, key, nonce: nonce);
cipher.ChaCha20(key, nonce).convert(input);
}
}

Expand Down
2 changes: 1 addition & 1 deletion benchmark/chacha20_poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CipherlibBenchmark extends Benchmark {

@override
void run() {
cipher.ChaCha20Poly1305(key: key, nonce: nonce).convert(input);
cipher.ChaCha20Poly1305(key, nonce: nonce).convert(input);
}
}

Expand Down
2 changes: 1 addition & 1 deletion benchmark/salsa20_poly1305.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CipherlibBenchmark extends Benchmark {

@override
void run() {
cipher.Salsa20Poly1305(key: key, nonce: nonce).convert(input);
cipher.Salsa20Poly1305(key, nonce: nonce).convert(input);
}
}

Expand Down
9 changes: 6 additions & 3 deletions lib/src/algorithms/aead_cipher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ abstract class AEADCipher<C extends Cipher, M extends MACHashBase>

/// Transforms the [message]. Alias for [sign].
@pragma('vm:prefer-inline')
AEADResult convert(List<int> message) => sign(message);
Uint8List convert(List<int> message, [bool verifyMode = false]) =>
createSink(verifyMode).add(message, true);

/// Signs the [message] with an authentication tag.
AEADResult sign(List<int> message) {
Expand All @@ -193,8 +194,9 @@ abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
Stream<Uint8List> bind(
Stream<List<int>> stream, [
Function(HashDigest tag)? onDigest,
bool verifyMode = false,
]) async* {
var sink = createSink();
var sink = createSink(verifyMode);
List<int>? cache;
await for (var data in stream) {
if (cache != null) {
Expand All @@ -212,9 +214,10 @@ abstract class AEADCipher<C extends Cipher, M extends MACHashBase>
Stream<int> stream(
Stream<int> stream, [
Function(HashDigest tag)? onDigest,
bool verifyMode = false,
]) async* {
int p = 0;
var sink = createSink();
var sink = createSink(verifyMode);
var chunk = Uint8List(1024);
await for (var x in stream) {
chunk[p++] = x;
Expand Down
28 changes: 19 additions & 9 deletions lib/src/algorithms/aes/cbc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import 'dart:typed_data';

import 'package:cipherlib/src/algorithms/padding.dart';
import 'package:cipherlib/src/core/cipher.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:cipherlib/src/core/salted_cipher.dart';
import 'package:cipherlib/src/core/collate_cipher.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '../padding.dart';
import '_core.dart';

/// The sink used for encryption by the [AESInCBCModeEncrypt] algorithm.
Expand Down Expand Up @@ -215,7 +216,7 @@ class AESInCBCModeDecryptSink implements CipherSink {
}

/// Provides encryption for AES cipher in CBC mode.
class AESInCBCModeEncrypt extends SaltedCipher {
class AESInCBCModeEncrypt extends Cipher with SaltedCipher {
@override
String get name => "AES#encrypt/CBC/${padding.name}";

Expand All @@ -225,11 +226,14 @@ class AESInCBCModeEncrypt extends SaltedCipher {
/// Padding scheme for the input message
final Padding padding;

@override
final Uint8List iv;

const AESInCBCModeEncrypt(
this.key,
Uint8List iv, [
this.iv, [
this.padding = Padding.pkcs7,
]) : super(iv);
]);

@override
@pragma('vm:prefer-inline')
Expand All @@ -238,7 +242,7 @@ class AESInCBCModeEncrypt extends SaltedCipher {
}

/// Provides decryption for AES cipher in CBC mode.
class AESInCBCModeDecrypt extends SaltedCipher {
class AESInCBCModeDecrypt extends Cipher with SaltedCipher {
@override
String get name => "AES#decrypt/CBC/${padding.name}";

Expand All @@ -248,11 +252,14 @@ class AESInCBCModeDecrypt extends SaltedCipher {
/// Padding scheme for the output message
final Padding padding;

@override
final Uint8List iv;

const AESInCBCModeDecrypt(
this.key,
Uint8List iv, [
this.iv, [
this.padding = Padding.pkcs7,
]) : super(iv);
]);

@override
@pragma('vm:prefer-inline')
Expand All @@ -261,7 +268,7 @@ class AESInCBCModeDecrypt extends SaltedCipher {
}

/// Provides encryption and decryption for AES cipher in CBC mode.
class AESInCBCMode extends SaltedCollateCipher {
class AESInCBCMode extends CollateCipher with SaltedCipher {
@override
String get name => "AES/CBC/${padding.name}";

Expand All @@ -276,6 +283,9 @@ class AESInCBCMode extends SaltedCollateCipher {
required this.decryptor,
});

@override
Uint8List get iv => encryptor.iv;

/// Creates AES cipher in CBC mode.
///
/// Parameters:
Expand Down
30 changes: 22 additions & 8 deletions lib/src/algorithms/aes/cfb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import 'dart:typed_data';

import 'package:cipherlib/src/algorithms/padding.dart';
import 'package:cipherlib/src/core/cipher.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:cipherlib/src/core/salted_cipher.dart';
import 'package:cipherlib/src/core/collate_cipher.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '../padding.dart';
import '_core.dart';

/// The sink used for encryption by the [AESInCFBModeEncrypt] algorithm.
Expand Down Expand Up @@ -164,7 +165,7 @@ class AESInCFBModeDecryptSink implements CipherSink {
}

/// Provides encryption for AES cipher in CFB mode.
class AESInCFBModeEncrypt extends SaltedCipher {
class AESInCFBModeEncrypt extends Cipher with SaltedCipher {
@override
String get name => "AES#encrypt/CFB/${Padding.none.name}";

Expand All @@ -174,11 +175,14 @@ class AESInCFBModeEncrypt extends SaltedCipher {
/// Number of bytes to use per block
final int sbyte;

@override
final Uint8List iv;

const AESInCFBModeEncrypt(
this.key,
Uint8List iv,
this.iv,
this.sbyte,
) : super(iv);
);

@override
@pragma('vm:prefer-inline')
Expand All @@ -187,7 +191,7 @@ class AESInCFBModeEncrypt extends SaltedCipher {
}

/// Provides decryption for AES cipher in CFB mode.
class AESInCFBModeDecrypt extends SaltedCipher {
class AESInCFBModeDecrypt extends Cipher with SaltedCipher {
@override
String get name => "AES#decrypt/CFB/${Padding.none.name}";

Expand All @@ -197,7 +201,14 @@ class AESInCFBModeDecrypt extends SaltedCipher {
/// Number of bytes to use per block
final int sbyte;

const AESInCFBModeDecrypt(this.key, Uint8List iv, this.sbyte) : super(iv);
@override
final Uint8List iv;

const AESInCFBModeDecrypt(
this.key,
this.iv,
this.sbyte,
);

@override
@pragma('vm:prefer-inline')
Expand All @@ -206,7 +217,7 @@ class AESInCFBModeDecrypt extends SaltedCipher {
}

/// Provides encryption and decryption for AES cipher in CFB mode.
class AESInCFBMode extends SaltedCollateCipher {
class AESInCFBMode extends CollateCipher with SaltedCipher {
@override
String get name => "AES/CFB/${Padding.none.name}";

Expand All @@ -221,6 +232,9 @@ class AESInCFBMode extends SaltedCollateCipher {
required this.decryptor,
});

@override
Uint8List get iv => encryptor.iv;

/// Creates AES cipher in CFB mode.
///
/// Parameters:
Expand Down
17 changes: 12 additions & 5 deletions lib/src/algorithms/aes/ctr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import 'dart:typed_data';

import 'package:cipherlib/src/algorithms/padding.dart';
import 'package:cipherlib/src/core/cipher.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:cipherlib/src/core/salted_cipher.dart';
import 'package:cipherlib/src/core/collate_cipher.dart';
import 'package:cipherlib/src/utils/nonce.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '../padding.dart';
import '_core.dart';

const int _mask32 = 0xFFFFFFFF;
Expand Down Expand Up @@ -99,22 +100,25 @@ class AESInCTRModeSink implements CipherSink {
}

/// Provides AES cipher in CTR mode.
class AESInCTRModeCipher extends SaltedCipher {
class AESInCTRModeCipher extends Cipher with SaltedCipher {
@override
String get name => "AES#cipher/CTR/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;

const AESInCTRModeCipher(this.key, Uint8List iv) : super(iv);
@override
final Uint8List iv;

const AESInCTRModeCipher(this.key, this.iv);

@override
@pragma('vm:prefer-inline')
AESInCTRModeSink createSink() => AESInCTRModeSink(key, iv);
}

/// Provides encryption and decryption for AES cipher in CTR mode.
class AESInCTRMode extends SaltedCollateCipher {
class AESInCTRMode extends CollateCipher with SaltedCipher {
@override
String get name => "AES/CTR/${Padding.none.name}";

Expand All @@ -129,6 +133,9 @@ class AESInCTRMode extends SaltedCollateCipher {
required this.decryptor,
});

@override
Uint8List get iv => encryptor.iv;

/// Creates AES cipher in CTR mode.
///
/// The [iv] parameter combines the 64-bit nonce, and 64-bit counter
Expand Down
28 changes: 19 additions & 9 deletions lib/src/algorithms/aes/gcm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import 'dart:typed_data';

import 'package:cipherlib/src/algorithms/padding.dart';
import 'package:cipherlib/src/core/cipher.dart';
import 'package:cipherlib/src/core/cipher_sink.dart';
import 'package:cipherlib/src/core/salted_cipher.dart';
import 'package:cipherlib/src/core/collate_cipher.dart';
import 'package:hashlib/hashlib.dart' show randomBytes;

import '../padding.dart';
import '_core.dart';

const List<int> _pow2 = <int>[
Expand Down Expand Up @@ -400,13 +401,16 @@ class AESInGCMModeDecryptSink extends _AESInGCMModeSinkBase {
}

/// Provides AES cipher in GCM mode for encryption.
class AESInGCMModeEncrypt extends SaltedCipher {
class AESInGCMModeEncrypt extends Cipher with SaltedCipher {
@override
String get name => "AES#encrypt/GCM/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;

@override
final Uint8List iv;

/// The length of the message authentication tag in bytes
final int tagSize;

Expand All @@ -415,10 +419,10 @@ class AESInGCMModeEncrypt extends SaltedCipher {

const AESInGCMModeEncrypt(
this.key,
Uint8List iv, {
this.iv, {
this.aad,
this.tagSize = 16,
}) : super(iv);
});

@override
@pragma('vm:prefer-inline')
Expand All @@ -427,13 +431,16 @@ class AESInGCMModeEncrypt extends SaltedCipher {
}

/// Provides AES cipher in GCM mode for decryption.
class AESInGCMModeDecrypt extends SaltedCipher {
class AESInGCMModeDecrypt extends Cipher with SaltedCipher {
@override
String get name => "AES#decrypt/GCM/${Padding.none.name}";

/// Key for the cipher
final Uint8List key;

@override
final Uint8List iv;

/// The length of the message authentication tag in bytes
final int tagSize;

Expand All @@ -442,10 +449,10 @@ class AESInGCMModeDecrypt extends SaltedCipher {

const AESInGCMModeDecrypt(
this.key,
Uint8List iv, {
this.iv, {
this.aad,
this.tagSize = 16,
}) : super(iv);
});

@override
@pragma('vm:prefer-inline')
Expand All @@ -454,7 +461,7 @@ class AESInGCMModeDecrypt extends SaltedCipher {
}

/// Provides encryption and decryption for AES cipher in GCM mode.
class AESInGCMMode extends SaltedCollateCipher {
class AESInGCMMode extends CollateCipher with SaltedCipher {
@override
String get name => "AES/GCM/${Padding.none.name}";

Expand All @@ -469,6 +476,9 @@ class AESInGCMMode extends SaltedCollateCipher {
required this.decryptor,
});

@override
Uint8List get iv => encryptor.iv;

/// Creates AES cipher in GCM mode.
///
/// Parameters:
Expand Down
Loading

0 comments on commit 96b1f56

Please sign in to comment.