Comments (7)
Right OK so this seemingly benign function has bested me. At first I tried to pass in byte[] buf
as you suggested, but the array does not get resized on the Java side. So then I tried to use a Pointer
like so:
String test = "test";
byte[] testArr = Native.toByteArray(test, StandardCharsets.UTF_8);
Pointer arrPtr = new Memory(testArr.length);
arrPtr.write(0, testArr, 0, testArr.length);
if (!lazySodium.sodiumPad(ref, arrPtr, testArr.length, 12, 30)) {
throw new IllegalArgumentException("Failed to pad");
}
byte[] resultArr = new byte[ref.getValue()]; // ref.getValue contains new size of array
Pointer ptr2 = new Memory(ref.getValue());
ptr2.setPointer(0, arrPtr);
ptr2.read(0, resultArr, 0, ref.getValue());
// resultArr is an array of garbled nonsense instead of "test "
I'm going slightly crazy here, do you have any insight as to how to pass in a pointer and then retrieve the memory block (that has a new length) pointed to that pointer?
from lazysodium-java.
You definitely need to pass an input array to libsodium that's large enough to hold the message and the padding. Here's what I would do: Take the input data with length len and the block size b and allocate an array that has length len + b. This array will always be able to hold the padded data, as the padding will never be longer than one full block:
1, When len is not divisible by b, the padding will be len % b bytes long which is less than b
2. When len is divisible by b, a full padding block will be appended, i.e. the padding length is b
You could even go a little further by using a temporary buffer that already has the correct length ((floor(len / b) + 1) * b
or, even simpler, len - len % b + b
), which saves one array copy operation.
from lazysodium-java.
If we allocate the size of the array that we know is going to be the final size, then padding it would be redundant because Java already allocates 0'd bytes to the end of that array.
As far as I understand, the C function pads the end of the array with 0
s. So my question is, what's the point in padding it with 0
s if the Java array would already have 0
d its bytes at the end of the array?
Therefore that's why I was trying to pass in a Pointer
in the first place because on the C side it seems to be doing more magic to the array passed in (and also we don't have to predetermine the size of the array in Java land).
These sodiumPad
functions seem ever so redundant in Java land and would be better off if I removed them to avoid confusion.
from lazysodium-java.
As far as I understand, the C function pads the end of the array with 0s. So my question is, what's the point in padding it with 0s if the Java array would already have 0d its bytes at the end of the array?
The function is applying the ISO/IEC 7816-4 padding which is not all zeroes, but 0x80 0x00 ... 0x00
. That's what the barrier_mask
variable is for in the C function, I believe. This is still relatively easy to achieve in Java, but at least not by simply allocating a fresh array. I would not remove the function, even though it does not do much, for the sake of completeness.
The sodium_unpad
function, however, will also perform checking on the padding which is easy to get wrong, so I would also leave it there.
from lazysodium-java.
After a lot of head-scratching, I got this to work and will be available in the next release
from lazysodium-java.
Available in release 5.1.0
from lazysodium-java.
Cool, thanks a lot!
from lazysodium-java.
Related Issues (20)
- Bnd vs osgi HOT 1
- Upgrade JNA to latest HOT 1
- Problems with resolving resource-loader dependency through OSGI HOT 2
- Support passing byte arrays to lazy methods instead of strings HOT 2
- Lazy Sodium on an M1 mac is not able to load libsodium properly HOT 13
- Lazysodium 5.1.1 doesn't load libsodium for Rocky Linux 8 HOT 1
- libsodium.so can't be loaded on Raspberry Pi OS due to missing libssp.so.0 library HOT 1
- 5.1.2 is not released to maven HOT 3
- ShortHash 'in' expected in hex format?
- Bundled sodium.dll cannot be loaded when path to JAR contains spaces HOT 1
- Provide module information HOT 1
- classes that support shortened number of parameters
- LazySodiumAndroid.cryptoKdfDeriveFromKey() throws an exception if key length exceeds 32 bytes
- help using sealed box for GitHub secret HOT 1
- LazySodium creates folders "resource-loader*" under /tmp folder HOT 1
- An error occurred with java. lang. NoClassDefFoundError
- sodiumHex2Bin silently accepts garbage input
- Decrypting Signed Message HOT 1
- Some sort of memory issue using cryptoBoxKeyPair() HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lazysodium-java.