Skip to content

Commit

Permalink
Fixes various issues in 'cryptography' and 'cryptography_flutter'.
Browse files Browse the repository at this point in the history
  • Loading branch information
terrier989 committed Jun 8, 2020
1 parent 6400726 commit d541e80
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 64 deletions.
16 changes: 8 additions & 8 deletions cryptography/lib/src/cryptography/algorithms/aes_gcm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ int _uint32ChangeEndian(int v) {
(0xFF & (v >> 24));
}

/// {@nodoc}
@visibleForTesting
/// _AES-GCM_ implementation for subclassing.
/// For documentation, see [aesGcm].
class AesGcm extends AesCipher {
static final _r = () {
final result = Uint32List(4);
Expand All @@ -93,8 +93,8 @@ class AesGcm extends AesCipher {
@override
Future<Uint8List> decrypt(
List<int> cipherText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) async {
Expand Down Expand Up @@ -131,8 +131,8 @@ class AesGcm extends AesCipher {
@override
Future<Uint8List> encrypt(
List<int> cipherText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) async {
Expand All @@ -150,8 +150,8 @@ class AesGcm extends AesCipher {
@override
Uint8List encryptSync(
List<int> plainText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) {
Expand Down
16 changes: 9 additions & 7 deletions cryptography/lib/src/cryptography/algorithms/chacha20_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,13 @@ class ChaCha extends Cipher {
Set<int> get secretKeyValidLengths => const <int>{32};

@override
Uint8List decryptSync(List<int> cipherText,
{SecretKey secretKey,
Nonce nonce,
List<int> aad,
int keyStreamIndex = 0}) {
Uint8List decryptSync(
List<int> cipherText, {
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) {
return encryptSync(
cipherText,
secretKey: secretKey,
Expand All @@ -284,8 +286,8 @@ class ChaCha extends Cipher {
@override
Uint8List encryptSync(
List<int> plainText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ const CipherWithAppendedMac xchacha20Poly1305Aead = Chacha20Poly1305Aead(
cipher: xchacha20,
);

/// {@nodoc}
@visibleForTesting
/// _AEAD_CHACHA20_POLY1305_ implementation for subclassing.
/// For documentation, see [chacha20Poly1305Aead].
///
/// This is used by [chacha20Poly1305Aead] and [xchacha20Poly1305Aead].
class Chacha20Poly1305Aead extends CipherWithAppendedMac {
static final _tmpByteData = ByteData(16);
static final _tmpUint8List = Uint8List.view(_tmpByteData.buffer);
Expand All @@ -91,8 +93,9 @@ class Chacha20Poly1305Aead extends CipherWithAppendedMac {
const Chacha20Poly1305Aead({
@required this.name,
@required Cipher cipher,
MacAlgorithm macAlgorithm = poly1305,
}) : super(cipher, macAlgorithm);
}) : assert(name != null),
assert(cipher != null),
super(cipher, poly1305);

@override
bool get supportsAad => true;
Expand Down Expand Up @@ -198,8 +201,8 @@ class Chacha20Poly1305Aead extends CipherWithAppendedMac {
@override
Future<Uint8List> decrypt(
List<int> cipherText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) async {
Expand Down Expand Up @@ -227,8 +230,8 @@ class Chacha20Poly1305Aead extends CipherWithAppendedMac {
@override
Uint8List decryptSync(
List<int> cipherText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) {
Expand Down Expand Up @@ -256,8 +259,8 @@ class Chacha20Poly1305Aead extends CipherWithAppendedMac {
@override
Future<Uint8List> encrypt(
List<int> plainText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) async {
Expand All @@ -284,8 +287,8 @@ class Chacha20Poly1305Aead extends CipherWithAppendedMac {
@override
Uint8List encryptSync(
List<int> plainText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// For specification, see the License for the specific language governing permissions and
// limitations under the License.

import 'dart:math';

import 'package:cryptography/cryptography.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -218,4 +216,4 @@ abstract class _EcdsaNist extends SignatureAlgorithm {
bool verifySync(List<int> input, Signature signature) {
throw UnimplementedError();
}
}
}
15 changes: 14 additions & 1 deletion cryptography_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ Then just use:
import 'package:cryptography_flutter/cryptography.dart';
```

For more instructions, read documentation for [cryptography](https://pub.dev/packages/cryptography).
For more instructions, read documentation for [cryptography](https://pub.dev/packages/cryptography).

# Contributing?
## Testing
Run "no plugin available" tests:
```
flutter test
```

Run e2e tests:
```
cd example
flutter driver test/cryptography_flutter_e2e.dart
```
1 change: 1 addition & 0 deletions cryptography_flutter/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:pedantic/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
Nonce nonce;
List<int> cipherText;

setUpAll(() {
setUp(() {
secretKey = algo.newSecretKeySync();
nonce = algo.newNonce();
cipherText = algo.encryptSync(
Expand Down Expand Up @@ -48,7 +48,7 @@ void main() {
Nonce nonce;
List<int> cipherText;

setUpAll(() {
setUp(() {
secretKey = algo.newSecretKeySync();
nonce = algo.newNonce();
cipherText = algo.encryptSync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class SwiftCryptographyFlutterPlugin: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "ping" {
result("ok")
return
}

let args = call.arguments as! [String: Any];
if #available(iOS 13.0, OSX 15.0, tvOS 13.0, watchOS 6.0, *) {
switch call.method {
Expand Down
9 changes: 1 addition & 8 deletions cryptography_flutter/lib/cryptography.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,5 @@
/// An optimized version of [package:cryptography](https://pub.dev/packages/cryptography).
library cryptography_flutter;

export 'package:cryptography/cryptography.dart'
hide
aesGcm,
AesGcm,
chacha20Poly1305Aead,
Chacha20Poly1305Aead;

export 'src/all_overrides.dart'
export 'src/all_exports.dart'
if (dart.library.js) 'package:cryptography/cryptography.dart';
21 changes: 13 additions & 8 deletions cryptography_flutter/lib/src/aes_gcm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import 'dart:typed_data';

import 'package:cryptography/cryptography.dart' hide aesGcm, AesGcm;
import 'package:cryptography/cryptography.dart' as cryptography;
import 'package:flutter/foundation.dart';
import 'package:meta/meta.dart';

import 'plugin.dart';

const Cipher aesGcm = cryptography.aesGcm;
/// Optimized _AES-GCM_ implementation.
///
/// See [cryptography.aesGcm].
const Cipher aesGcm = AesGcm();

/// {@nodoc}
@visibleForTesting
// ignore: invalid_use_of_visible_for_testing_member
/// Optimized _AES-GCM_ implementation for subclassing.
/// For documentation, see [aesGcm].
class AesGcm extends cryptography.AesGcm {
const AesGcm();

Expand All @@ -39,7 +39,9 @@ class AesGcm extends cryptography.AesGcm {
int keyStreamIndex = 0,
}) async {
aad ??= const <int>[];
if (aad.isEmpty &&
final isPluginAvailable = await getIsPluginAvailable();
if (isPluginAvailable &&
aad.isEmpty &&
keyStreamIndex == 0 &&
cipherText.length > 128 &&
(Platform.isIOS || Platform.isMacOS)) {
Expand Down Expand Up @@ -77,8 +79,10 @@ class AesGcm extends cryptography.AesGcm {
List<int> aad,
int keyStreamIndex = 0,
}) async {
final isPluginAvailable = await getIsPluginAvailable();
aad ??= const <int>[];
if (aad.isEmpty &&
if (isPluginAvailable &&
aad.isEmpty &&
keyStreamIndex == 0 &&
plainText.length > 128 &&
(Platform.isIOS || Platform.isMacOS)) {
Expand All @@ -105,6 +109,7 @@ class AesGcm extends cryptography.AesGcm {
}
return super.encrypt(
plainText,
secretKey: secretKey,
nonce: nonce,
aad: aad,
keyStreamIndex: keyStreamIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

export 'package:cryptography/cryptography.dart'
hide aesGcm, AesGcm, chacha20Poly1305Aead, Chacha20Poly1305Aead;
export 'aes_gcm.dart';
export 'chacha20.dart';
42 changes: 28 additions & 14 deletions cryptography_flutter/lib/src/chacha20.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,39 @@ import 'plugin.dart';
import 'dart:typed_data';
import 'dart:io';

const Cipher chacha20Poly1305Aead = Chacha20Poly1305Aead();
/// Optimized _AEAD_CHACHA20_POLY1305_ implementation.
///
/// See [cryptography.chacha20Poly1305Aead].
const Cipher chacha20Poly1305Aead = Chacha20Poly1305Aead(
name: 'chacha20Poly1305Aead',
cipher: chacha20,
);

/// {@nodoc}
@visibleForTesting
// ignore: invalid_use_of_visible_for_testing_member
/// Optimized _AEAD_CHACHA20_POLY1305_ implementation for subclassing.
/// For documentation, see [chacha20Poly1305Aead].
class Chacha20Poly1305Aead extends cryptography.Chacha20Poly1305Aead {
const Chacha20Poly1305Aead()
: super(
name: 'chacha20Poly1305Aead',
cipher: chacha20,
const Chacha20Poly1305Aead({
@required String name,
@required Cipher cipher,
}) : assert(name != null),
assert(cipher != null),
super(
name: name,
cipher: cipher,
);

@override
Future<Uint8List> decrypt(
List<int> cipherText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) async {
aad ??= const <int>[];
if (aad.isEmpty &&
final isPluginAvailable = await getIsPluginAvailable();
if (isPluginAvailable &&
aad.isEmpty &&
keyStreamIndex == 0 &&
cipherText.length > 128 &&
(Platform.isIOS || Platform.isMacOS)) {
Expand Down Expand Up @@ -74,13 +85,15 @@ class Chacha20Poly1305Aead extends cryptography.Chacha20Poly1305Aead {
@override
Future<Uint8List> encrypt(
List<int> plainText, {
SecretKey secretKey,
Nonce nonce,
@required SecretKey secretKey,
@required Nonce nonce,
List<int> aad,
int keyStreamIndex = 0,
}) async {
aad ??= const <int>[];
if (aad.isEmpty &&
final isPluginAvailable = await getIsPluginAvailable();
if (isPluginAvailable &&
aad.isEmpty &&
keyStreamIndex == 0 &&
plainText.length > 128 &&
(Platform.isIOS || Platform.isMacOS)) {
Expand All @@ -107,6 +120,7 @@ class Chacha20Poly1305Aead extends cryptography.Chacha20Poly1305Aead {
}
return super.encrypt(
plainText,
secretKey: secretKey,
nonce: nonce,
aad: aad,
keyStreamIndex: keyStreamIndex,
Expand Down
17 changes: 16 additions & 1 deletion cryptography_flutter/lib/src/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@

import 'package:flutter/services.dart';

const MethodChannel channel = const MethodChannel('cryptography_flutter');
const MethodChannel channel = MethodChannel('cryptography_flutter');

Future<bool> _isPluginAvailable;

Future<bool> getIsPluginAvailable() {
return _isPluginAvailable ??= _getIsPluginAvailable();
}

Future<bool> _getIsPluginAvailable() async {
try {
await channel.invokeMethod('ping').timeout(const Duration(seconds: 1));
return true;
} on MissingPluginException {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public class CryptographyFlutterPlugin: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "ping" {
result("ok")
return
}
let args = call.arguments as! [String: Any];
if #available(iOS 13.0, OSX 15.0, tvOS 13.0, watchOS 6.0, *) {
switch call.method {
Expand Down
Loading

0 comments on commit d541e80

Please sign in to comment.