terl / lazysodium-java

A Java implementation of the Libsodium crypto library. For the lazy dev.
https://github.com/terl/lazysodium-java/wiki
Mozilla Public License 2.0
135 stars 48 forks source link

Expose buffer sizes for native interfaces #17

Closed keeferrourke closed 6 years ago

keeferrourke commented 6 years ago

Hey there! LazySodium looks like it's shaping up to be probably the best Libsodium Java binding library, but it doesn't expose libsodium's pre-calculated authenticated encryption interface properly.

In particular, in the Box interface, the value of BEFORENMBYTES etc is not publicly accessible. The code I'm writing needs to fill a shared secret k using the LazySodium.cryptoBoxEasyBeforeNm() method, but I had to dig around to figure out that the buffer size should be 32 bytes long.

I'm doing something like

        byte[] sharedSecret = new byte[32];
        if (!sodium.cryptoBoxBeforeNm(sharedSecret, bobsPubKey, alicesSecKey)) {
            return false;
        };

But it would be much more clear if I could do something like:

        byte[] sharedSecret = new byte[LazySodium.BEFORENMBYTES];

Could this buffer size (and others) be exposed publicly?

gurpreet- commented 6 years ago

Hello!

Glad you're enjoying Lazysodium! 🙌

Sure, of course I can add them for you and expose those sizes publicly.

It seems I missed all those pre-calculated functions out of the Lazy interface too (whoops!). Give me a sec and I'll add some in.

keeferrourke commented 6 years ago

@gurpreet- much appreciated, thanks! :smile:

gurpreet- commented 6 years ago

Hello again.

I just checked and it seems I did add those sizes into the Box interface.

// The byte sizes are available in their relevant interfaces like so...
byte[] sharedSecret = new byte[Box.BEFORENMBYTES];

You can find similar array sizes at the start of their respective interfaces. For example the KeyExchange interface has these byte sizes available for you:

public interface KeyExchange {

    int PUBLICKEYBYTES = 32;
    int SECRETKEYBYTES = 32;
    int SESSIONKEYBYTES = 32;
    int SEEDBYTES = 32;
    String PRIMITIVE = "x25519blake2b";

    // ... rest of interface
}

I've made a note to make that clearer in the documentation 👍

keeferrourke commented 6 years ago

@gurpreet- whoops, my bad. Thanks a bunch!

gurpreet- commented 6 years ago

No worries @keeferrourke.

I've added lazier functions for the precalculation interface a.k.a Box.java (and fixed a minor typo in one of the functions). It's now available as release 1.1.1 on Bintray and GitHub.

If you have any more problems/questions, please let me know.

Until then, have fun! 🎆

gurpreet- commented 6 years ago

Hey @keeferrourke,

If Lazysodium has helped you please consider donating. Even a dollar goes a long way.

Thanks for using Lazysodium :)

keeferrourke commented 6 years ago

Hey @gurpreet- are you folks on Liberapay? I'd be happy throw a couple dollars your way, since this library is being developed so quickly and with extremely high quality. My usage is more one-off (but maybe I'd use it more in the future), but you saved me from having to write bindings myself :wink:

gurpreet- commented 6 years ago

Hey @keeferrourke.

Thanks for the compliments! I got tired of using low quality libs a while ago 😪. I think I'm going to expand Lazysodium to the languages that run iOS, Mac and Windows so then devs can use it to build high quality cryptography applications. I personally would like to see a GPG rival but with a better interface so everyone can use it 😄

One off donations are totally fine too! Just created a Liberapay account https://liberapay.com/terlacious/

Thanks so much!