derecalliance / cryptography

cryptography primitives (secret sharing, secure channel) for implementing the DeRec protocol
Apache License 2.0
4 stars 0 forks source link

`recover()` method does not return a usable value when insufficient shares are provided #22

Closed diptimahamuni closed 1 month ago

diptimahamuni commented 1 month ago

When the recover() method is called with insufficient number of shares provided (below the threshold), it should return null/falsy value. However, the recover() method currently does not return a reasonable falsy value that can be captured by the application. It also does not throw an appropriate Java Exception. Instead, the rust library crashes.

When run with the following test code:

byte[] recovered = new byte[0];
        DerecCryptoImpl cryptoImpl = new DerecCryptoImpl();

        byte[] id = "some_id".getBytes();
        byte[] secret = "top_secret".getBytes();

        List<byte[]> shares = cryptoImpl.share(id, 0, secret, 5, 3);
        ArrayList<byte[]> shares01 = new ArrayList<>();
        shares01.add(shares.get(0));
        shares01.add(shares.get(1));

        try {
            recovered = cryptoImpl.recover(id, 0, shares01);
        } catch (Exception e) {
            System.out.println("Exception occurred: " + e);
            e.printStackTrace();
        }

I get the below output after doing gradle clean build and gradle run:

> Task :run FAILED
thread '<unnamed>' panicked at <...>/cryptography/src/secret_sharing/utils.rs:45:34:
called `Result::unwrap()` on an `Err` value: Error
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/opt/homebrew/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home/bin/java'' finished with non-zero exit value 134

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.5/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 454ms
6 actionable tasks: 1 executed, 5 up-to-date