bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
237 stars 123 forks source link

'(x >= 0) && (x <= _MASK_32)': is not true #225

Open hsynksahin opened 7 months ago

hsynksahin commented 7 months ago

After updating Flutter version pointy castle started to give me errors with the same code.

I am using the encrypt package which uses this package. The same encryption code was working a few days ago, I have upgraded the flutter now its gives me 'package:pointycastle/src/ufixnum.dart': Failed assertion: line 198 pos 10: '(x >= 0) && (x <= _MASK_32)': is not true. error when I try to encrypt something with it. Can anyone explain why this could be happen?

When I downgraded the flutter to 3.19.0 and there is no problem, but after upgrading to 3.19.1 the exception occurs.

hsynksahin commented 7 months ago

Now it happens even in flutter 3.19.0. Can we at least have a error message for this one ?

Problem occurres when I encrypt a dummy password like 123456 Using version 3.7.4 (latest) // Also tested at 3.7.3 Encryption (5.0.3)

Here is my flutter doctor at any case:

[✓] Flutter (Channel stable, 3.19.0, on macOS 14.2.1 23C71 darwin-arm64, locale en-TR)
    • Flutter version 3.19.0 on channel stable at /Applications/Development/tools/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (13 days ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Applications/Development/tools/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Development/apps/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Applications/Development/apps/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /Applications/Development/apps/Android Studio.app
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Development/apps/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (3 available)
    • iPhone X (mobile) • DF1A73AF-23A9-40FE-B341-E23944D7524D • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-0 (simulator)
    • macOS (desktop)   • macos                                • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
    • Chrome (web)      • chrome                               • web-javascript • Google Chrome 121.0.6167.184

[✓] Network resources
    • All expected network resources are available.

• No issues found!
hsynksahin commented 7 months ago

The code that trying to encrypt the data:

(IStorageRepository is basicly a SharedPrefferences)

class TwoWayAES extends ITwoWayEncryption {
  static const AESMode _mode = AESMode.ofb64Gctr;

  // secure storage
  final IStorageRepository secureStorage;

  const TwoWayAES({required this.secureStorage});

  @override
  Future<String> encrypt(String plainText) async {
    final key = Key.fromUtf8(EncryptionEnv.privateKey);
    final encrypter = Encrypter(AES(key, mode: _mode));

    final encrypted = encrypter.encrypt(plainText, iv: await getIv);

    return encrypted.base64;
  }

  @override
  Future<String> decrypt(String base64) async {
    final key = Key.fromUtf8(EncryptionEnv.privateKey);
    final encrypter = Encrypter(AES(key, mode: _mode));

    var encrypted = Encrypted.fromBase64(base64);
    final decrypted = encrypter.decrypt(encrypted, iv: await getIv);

    return decrypted;
  }

  @override
  Future<bool> isValid(String encryptedText, String test) async {
    final key = Key.fromUtf8(EncryptionEnv.privateKey);
    final encrypter = Encrypter(AES(key, mode: _mode));

    final encryptedTest = encrypter.encrypt(test, iv: await getIv);
    var result = encryptedText.compareTo(encryptedTest.base64) == 0;

    return result;
  }

  Future<IV> get getIv async {
    var iv = await secureStorage.read(EncryptionEnv.publicKey);

    if (iv == null) {
      var newIv = IV.fromSecureRandom(16);

      await secureStorage.write(EncryptionEnv.publicKey, newIv.base64);
      return newIv;
    } else {
      return IV.fromBase64(iv);
    }
  }
}

Note: It seems like problem is with the ofb64Gctr. I switched mode to ctr now I'm not taking any errors.

Also oppened an issue at encryption repository to direct here.