From edf95cbeb177394d664ed474ad7f5f1e46832be2 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 17:04:55 +0100 Subject: [PATCH 1/6] Added crypto_sign_seed_keypair/cryptoSignSeedKeyPair Added crypto_sign_seed_keypair and cryptoSignSeedKeyPair funcs --- .../muquit/libsodiumjna/SodiumLibrary.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index a5adf31..d89756b 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -272,6 +272,7 @@ int crypto_box_open_easy(byte[] decrypted, byte[] cipher_text, long crypto_sign_secretkeybytes(); long crypto_sign_publickeybytes(); int crypto_sign_keypair(byte[] pk, byte[] sk); + int crypto_sign_seed_keypair(byte[] pk, byte[] sk, byte[] seed); int crypto_sign_ed25519_bytes(); int crypto_sign_bytes(); @@ -372,6 +373,25 @@ public static SodiumKeyPair cryptoSignKeyPair() throws SodiumLibraryException return kp; } + public static SodiumKeyPair cryptoSignSeedKeyPair(byte[] seed32) throws SodiumLibraryException + { + SodiumKeyPair kp = new SodiumKeyPair(); + byte[] publicKey = new byte[(int) sodium().crypto_sign_publickeybytes()]; + byte[] privateKey = new byte[(int) sodium().crypto_sign_secretkeybytes()]; + int rc = sodium().crypto_sign_seed_keypair(publicKey, privateKey, seed32); + if (rc != 0) + { + throw new SodiumLibraryException("libsodium crypto_sign_seed_keypair() failed, returned " + rc + ", expected 0"); + } + kp.setPublicKey(publicKey); + kp.setPrivateKey(privateKey); + if (logger.isDebugEnabled()) { + logger.debug("pk len: " + publicKey.length); + logger.debug("sk len: " + privateKey.length); + } + return kp; + } + // libsodium's generichash (blake2b), this function will only return 'length' number of bytes of the hash // this implementation does not expect key/key.length which blake does need, so we are setting them to null and 0 public static byte[] cryptoGenerichash(byte[] input, int length) throws SodiumLibraryException From c4d9c7bce0202b8d85e4e040e54c112b6c0fc139 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 18:06:27 +0100 Subject: [PATCH 2/6] Fixed some typos --- src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index d89756b..0bffc8d 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -1032,17 +1032,17 @@ public static NativeLong cryptoBoxNonceBytes() return sodium().crypto_box_noncebytes(); } - public static NativeLong crytoBoxSeedBytes() + public static NativeLong cryptoBoxSeedBytes() { return sodium().crypto_box_seedbytes(); } - public static NativeLong crytoBoxPublicKeyBytes() + public static NativeLong cryptoBoxPublicKeyBytes() { return sodium().crypto_box_publickeybytes(); } - public static NativeLong crytoBoxSecretKeyBytes() + public static NativeLong cryptoBoxSecretKeyBytes() { return sodium().crypto_box_secretkeybytes(); } From 4e9e3f8ec3f85375081e72402517e783b2269dd7 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 18:16:17 +0100 Subject: [PATCH 3/6] Added cryptoPwhash with variable output length --- .../com/muquit/libsodiumjna/SodiumLibrary.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index 0bffc8d..b1184b8 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -486,19 +486,25 @@ int crypto_pwhash(byte[] key, long keylen, int alg); */ - public static byte[] cryptoPwhash(byte[] passwd, byte[] salt, long opsLimit, NativeLong memLimit, int algorithm) throws SodiumLibraryException - { - byte[] key = new byte[sodium().crypto_box_seedbytes().intValue()]; - + public static byte[] cryptoPwhash(byte[] passwd, byte[] salt16, long opsLimit, NativeLong memLimit, int algorithm) + throws SodiumLibraryException { + int outBytesLength = cryptoBoxSeedBytes().intValue(); + return cryptoPwhash(passwd, salt16, outBytesLength, opsLimit, memLimit, algorithm); + } + + public static byte[] cryptoPwhash(byte[] passwd, byte[] salt16, int outBytesLength, long opsLimit, + NativeLong memLimit, int algorithm) throws SodiumLibraryException { + byte[] key = new byte[outBytesLength]; + int rc = sodium().crypto_pwhash(key, key.length, passwd, passwd.length, - salt, + salt16, opsLimit, memLimit, algorithm); if (logger.isDebugEnabled()) { - logger.debug(">>> NavtiveLong size: " + NativeLong.SIZE * 8 + " bits"); + logger.debug(">>> NativeLong size: " + NativeLong.SIZE * 8 + " bits"); logger.debug("crypto_pwhash returned: " + rc); } From 17e145c8ad2f4fb4f3152941cd936f32c400eec6 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 18:18:20 +0100 Subject: [PATCH 4/6] Added cryptoPwhashArgon2idInteractive --- .../java/com/muquit/libsodiumjna/SodiumLibrary.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index b1184b8..db33012 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -486,6 +486,19 @@ int crypto_pwhash(byte[] key, long keylen, int alg); */ + public static byte[] cryptoPwhashArgon2idInteractive(byte[] passwd, byte[] salt16) + throws SodiumLibraryException { + int outBytesLength = cryptoBoxSeedBytes().intValue(); + return cryptoPwhashArgon2idInteractive(passwd, salt16, outBytesLength); + } + + public static byte[] cryptoPwhashArgon2idInteractive(byte[] passwd, byte[] salt16, int outBytesLength) + throws SodiumLibraryException { + long opsLimit = cryptoPwHashOpsLimitInteractive(); + NativeLong memLimit = cryptoPwHashMemLimitInterative(); + return cryptoPwhash(passwd, salt16, outBytesLength, opsLimit, memLimit, cryptoPwhashAlgArgon2id13()); + } + public static byte[] cryptoPwhash(byte[] passwd, byte[] salt16, long opsLimit, NativeLong memLimit, int algorithm) throws SodiumLibraryException { int outBytesLength = cryptoBoxSeedBytes().intValue(); From 8fb55b150ad504a46b6157153ba2cacca946d2e6 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 18:39:18 +0100 Subject: [PATCH 5/6] Some comments with values --- .../muquit/libsodiumjna/SodiumLibrary.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index db33012..8e4c223 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -1046,31 +1046,49 @@ public static byte[] cryptoPublicKey(byte[] privateKey) throws SodiumLibraryExce return publicKey; } + /** + * 24 bytes + */ public static NativeLong cryptoBoxNonceBytes() { return sodium().crypto_box_noncebytes(); } + /** + * 32 bytes + */ public static NativeLong cryptoBoxSeedBytes() { return sodium().crypto_box_seedbytes(); } - + + /** + * 32 bytes + */ public static NativeLong cryptoBoxPublicKeyBytes() { return sodium().crypto_box_publickeybytes(); } + /** + * 64 bytes + */ public static NativeLong cryptoBoxSecretKeyBytes() { return sodium().crypto_box_secretkeybytes(); } + /** + * 16 bytes + */ public static NativeLong cryptoBoxMacBytes() { return sodium().crypto_box_macbytes(); } + /** + * 48 bytes + */ public static NativeLong cryptoBoxSealBytes() { return sodium().crypto_box_sealbytes(); @@ -1115,6 +1133,9 @@ public static int cryptoPwhashAlgDefault() return sodium().crypto_pwhash_alg_default(); } + /** + * 16 bytes + */ public static int cryptoPwhashSaltBytes() { return sodium().crypto_pwhash_saltbytes(); From c97865b8a487243553f8b4e7304f7250e75e1d71 Mon Sep 17 00:00:00 2001 From: Lilianne-Blaze <39486911+Lilianne-Blaze@users.noreply.github.com> Date: Mon, 3 Feb 2025 18:43:56 +0100 Subject: [PATCH 6/6] Keep typo vers as deprecated --- .../muquit/libsodiumjna/SodiumLibrary.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java index 8e4c223..31cfb7b 100755 --- a/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java +++ b/src/main/java/com/muquit/libsodiumjna/SodiumLibrary.java @@ -1061,6 +1061,13 @@ public static NativeLong cryptoBoxSeedBytes() { return sodium().crypto_box_seedbytes(); } + + // typo version, keep both correct and typo for compatibility + @Deprecated + public static NativeLong crytoBoxSeedBytes() + { + return sodium().crypto_box_seedbytes(); + } /** * 32 bytes @@ -1069,6 +1076,13 @@ public static NativeLong cryptoBoxPublicKeyBytes() { return sodium().crypto_box_publickeybytes(); } + + // typo version, keep both correct and typo for compatibility + @Deprecated + public static NativeLong crytoBoxPublicKeyBytes() + { + return sodium().crypto_box_publickeybytes(); + } /** * 64 bytes @@ -1077,6 +1091,13 @@ public static NativeLong cryptoBoxSecretKeyBytes() { return sodium().crypto_box_secretkeybytes(); } + + // typo version, keep both correct and typo for compatibility + @Deprecated + public static NativeLong crytoBoxSecretKeyBytes() + { + return sodium().crypto_box_secretkeybytes(); + } /** * 16 bytes