Skip to content
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

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

Open
hsynksahin opened this issue Feb 26, 2024 · 4 comments
Open

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

hsynksahin opened this issue Feb 26, 2024 · 4 comments

Comments

@hsynksahin
Copy link

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 hsynksahin changed the title Update broke my code Flutter upgrade broke my encrytion code Feb 26, 2024
@hsynksahin
Copy link
Author

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.gitFramework revision bae5e49bc2 (13 days ago), 2024-02-13 17:46:18 -0800Engine revision 04817c99c9
    • Dart version 3.3.0DevTools 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.0Java 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/DeveloperBuild 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/ContentsFlutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutterDart 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/ContentsFlutter 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 hsynksahin changed the title Flutter upgrade broke my encrytion code '(x >= 0) && (x <= _MASK_32)': is not true Feb 27, 2024
@hsynksahin
Copy link
Author

hsynksahin commented Feb 27, 2024

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.

@dodatw
Copy link

dodatw commented Oct 18, 2024

Hi hsynksahin

Did you find solution? I face same issue when upgrade from 3.13.7 to 3.24.3.
I cannot reproduce this issue, but on firebase crashlytics:

Using pointycastle (3.7.4) / encrypt (5.0.3)

          Crashed: io.flutter.1.ui
0  App                            0x5e3254 pack32 + 197 (ufixnum.dart:197)
1  App                            0x5e3188 pack32 + 197 (ufixnum.dart:197)
2  App                            0xc70abc AESEngine._decryptBlock + 1548 (aes.dart:1548)
3  App                            0xc6ee68 AESEngine.processBlock + 1325 (aes.dart:1325)
4  App                            0xc73cac ECBBlockCipher.processBlock + 40 (ecb.dart:40)
5  App                            0xc83d94 PaddedBlockCipherImpl.processBlock + 79 (padded_block_cipher_impl.dart:79)
6  App                            0xc844a0 PaddedBlockCipherImpl.doFinal + 112 (padded_block_cipher_impl.dart:112)
7  App                            0xc84014 PaddedBlockCipherImpl.process + 74 (padded_block_cipher_impl.dart:74)
8  App                            0xb7dcf0 AES.decrypt + 49 (aes.dart:49)
9  App                            0x5b31d0 Encrypter.decryptBytes + 40 (encrypter.dart:40)
10 App                            0x5b3164 Encrypter.decrypt + 49 (encrypter.dart:49)
11 App                            0x5b308c FileEncryptUtils.decryptUrlFromServer + 25 (file_encrypt_utils.dart:25)
12 App                            0x5b2db4 UploadUtils.downloadVideo + 777 (upload_utils.dart:777)
13 App                            0xca785c SuspendState._createAsyncStarCallback.<anonymous closure> + 384 (async_patch.dart:384)
14 App                            0x23e04 FutureListener.handleValue + 1662 (zone.dart:1662)
15 App                            0x23ca0 Future._propagateToListeners.handleValueCallback + 863 (future_impl.dart:863)
16 App                            0x1480c Future._propagateToListeners + 894 (future_impl.dart:894)
17 App                            0x14490 Future._completeWithValue + 661 (future_impl.dart:661)
18 App                            0xca7c30 SuspendState._returnAsyncNotFuture + 359 (async_patch.dart:359)
19 App                            0x3dc38 stub _iso_stub_ReturnAsyncNotFutureStub + 193336
20 App                            0xca785c SuspendState._createAsyncStarCallback.<anonymous closure> + 384 (async_patch.dart:384)
21 App                            0x23e04 FutureListener.handleValue + 1662 (zone.dart:1662)
22 App                            0x23ca0 Future._propagateToListeners.handleValueCallback + 863 (future_impl.dart:863)
23 App                            0x1480c Future._propagateToListeners + 894 (future_impl.dart:894)
24 App                            0x14490 Future._completeWithValue + 661 (future_impl.dart:661)
25 App                            0xca7c30 SuspendState._returnAsyncNotFuture + 359 (async_patch.dart:359)
26 App                            0x3dc38 stub _iso_stub_ReturnAsyncNotFutureStub + 193336
27 App                            0xca785c SuspendState._createAsyncStarCallback.<anonymous closure> + 384 (async_patch.dart:384)
28 App                            0x23e04 FutureListener.handleValue + 1662 (zone.dart:1662)
29 App                            0x23ca0 Future._propagateToListeners.handleValueCallback + 863 (future_impl.dart:863)
30 App                            0x1480c Future._propagateToListeners + 894 (future_impl.dart:894)
31 App                            0x14490 Future._completeWithValue + 661 (future_impl.dart:661)
32 App                            0x14404 Future._asyncCompleteWithValue.<anonymous closure> + 735 (future_impl.dart:735)
33 App                            0x14290 microtaskLoop + 34 (schedule_microtask.dart:34)
34 App                            0x14190 startMicrotaskLoop (#2) + 54 (schedule_microtask.dart:54)
35 App                            0x14154 startMicrotaskLoop + 44 (schedule_microtask.dart:44)
36 App                            0xbbac stub InvokeDartCode + 31660
37 Flutter                        0x62b748 dart::DartEntry::InvokeFunction(dart::Function const&, dart::Array const&, dart::Array const&) + 33 (allocation.h:33)
38 Flutter                        0x732e9c Dart_InvokeClosure + 4671 (dart_api_impl.cc:4671)
39 Flutter                        0x59adb4 tonic::DartMicrotaskQueue::RunMicrotasks() + 77 (dart_microtask_queue.cc:77)
40 Flutter                        0x7eaec fml::MessageLoopImpl::FlushTasks(fml::FlushType) + 100 (wrap_iter.h:100)
41 Flutter                        0x81e5c fml::MessageLoopDarwin::OnTimerFire(__CFRunLoopTimer*, fml::MessageLoopDarwin*) + 86 (message_loop_darwin.mm:86)
42 CoreFoundation                 0xb7a2c __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32
43 CoreFoundation                 0xb76d0 __CFRunLoopDoTimer + 1004
44 CoreFoundation                 0xb722c __CFRunLoopDoTimers + 288
45 CoreFoundation                 0x53888 __CFRunLoopRun + 1856
46 CoreFoundation                 0x52cd8 CFRunLoopRunSpecific + 608
47 Flutter                        0x81f48 fml::MessageLoopDarwin::Run() + 52 (message_loop_darwin.mm:52)
48 Flutter                        0x81b90 std::_fl::__function::__func<fml::Thread::Thread(std::_fl::function<void (fml::Thread::ThreadConfig const&)> const&, fml::Thread::ThreadConfig const&)::$_0, std::_fl::allocator<fml::Thread::Thread(std::_fl::function<void (fml::Thread::ThreadConfig const&)> const&, fml::Thread::ThreadConfig const&)::$_0>, void ()>::operator()() + 94 (message_loop_impl.cc:94)
49 Flutter                        0x81820 fml::ThreadHandle::ThreadHandle(std::_fl::function<void ()>&&)::$_0::__invoke(void*) + 470 (function.h:470)
50 libsystem_pthread.dylib        0x606c _pthread_start + 136
51 libsystem_pthread.dylib        0x10d8 thread_start + 8
        

code

  static String decryptUrlFromServer(String encUrl, String aesKey) {
    final encrypter = Encrypter(AES(Key.fromBase64(aesKey), mode: AESMode.ecb));
    final encrypted = Encrypted.fromBase64(encUrl);
    return encrypter.decrypt(encrypted, iv: IV.fromLength(16));
  }

Thanks.

@hsynksahin
Copy link
Author

Unfortunately I did not! But staying in specific version does it. Just initialize a seperate package with seperate pubspec.yaml so the flutter pub upgrade wont update it. And for some reason if its gonna updated put some description. This is how I solved. Here is my versions:

encrypt: 5.0.3 # last working: 5.0.3
pointycastle: 3.9.1 # last working: 3.9.1

And Im using static const AESMode _mode = AESMode.ofb64; as mode now. (Dont really know what changes)

Hope this will work for you too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants