Comments (10)
You should consider crypto_secretbox_easy()
(and the _box
counterpart): http://doc.libsodium.org/secret-key_cryptography/authenticated_encryption.html
http://doc.libsodium.org/public-key_cryptography/authenticated_encryption.html
It is way easier than juggling with zero padding and memcpy()
.
from node-sodium.
I'm still digging, but so far I've learned that the input message inside sodium.cc's bind_crypto_secretbox() is getting corrupted. If I check the input plaintext here:
//Copy the message to the new buffer
memcpy((void*) (pmb_ptr + crypto_secretbox_ZEROBYTES), (void *) message, message_size);
message_size += crypto_secretbox_ZEROBYTES;
NEW_BUFFER_AND_PTR(ctxt, message_size);
// <-- ADD CHECK HERE
if( crypto_secretbox(ctxt_ptr, pmb_ptr, message_size, nonce, key) == 0) {
Then on specific iterations of my script above (always i=2285, 12915, 16956, 20981, 41321, for some reason), I see the buffer being passed into crypto_secretbox() change from:
000000000000000000000000000000000000000000000000000000000000000064617461
to:
000000000000005000000000000000500300000000000000000000000000000064617461
On other runs, the corruption happens on the same iterations, but has different bits flipped.
The glitch in the first 16 bytes causes the poly1305 key to be wrong (it flips a couple of bits in the auth key), so the emitted tag is wrong.
from node-sodium.
Looks like the corruption is happening during the NEW_BUFFER_AND_PTR()
macro: things look ok just before that call, but are sometimes corrupted just afterwards. This call uses Buffer::New()
to allocate memory. The consistent-but-sorta-random recurrence of the corruption (always on those specific iterations) makes me think that the memory allocator is behaving differently during those calls, maybe it runs out of freelist entries and has to request an extra page, or GC is taking place. Memory allocators aren't supposed to corrupt random memory, so there's something deeply weird taking place here.
from node-sodium.
If I move the NEW_BUFFER_AND_PTR(ctxt, message_size)
call to earlier in the function (just after the other NEW_BUFFER_AND_PTR), then I get either a segfault or a malloc warning:
node(11795,0x7fff76d9b180) malloc: *** error for object 0x100812388: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
which looks like a dead giveaway. I don't know enough about how Node (or webkit, or C++) does memory management to see what's wrong with bind_crypto_secretbox()
, but obviously there's some use-after-free problem going on. Do Buffer
objects get destructed automatically when they go out of scope?
from node-sodium.
@warner are you sure you're getting the right size when you move that NEW_BUFFER_AND_PTR
up? if you do that you have to NEW_BUFFER_AND_PTR(ctxt, message_size + crypto_secretbox_ZEROBYTES)
to get the same behaviour
from node-sodium.
Oh, good catch. I completely forgot that message_size
changes.
With your fix, I still get the same malloc error.
from node-sodium.
fixed in #20, see explanation there
from node-sodium.
I'd also recommend moving to nan asap for 0.11+ support
from node-sodium.
@rvagg GOOOOOOOOOOOOOOOOOOOOOOOAL!
from node-sodium.
sweet! thanks!
from node-sodium.
Related Issues (20)
- Fails to compile on openSUSE LEAP 15 (../deps/build/lib/libsodium.a: No such file or directory ) HOT 8
- Cant install sodium on my Pi HOT 2
- Compiling sodium on TravisCI fails HOT 1
- Console error 'msvsversiion not defined' when invalid Visual Studio Version is found
- Invalid ELF Header HOT 1
- Error installing on Windows
- make libsodium exited with code 2, pwhash_argon2i fails HOT 3
- NPM install fails with node-gyp error: cannot use a string pattern on a bytes-like object
- package install errors HOT 3
- How to pre-build Node Sodium?
- install.js support vs2019 please HOT 1
- Installing on Windows Failed HOT 1
- Way to use exists keypair which generated in Java HOT 1
- libbtool/libsodium not installing on Petrodctyle panel
- ed25519 decode / decompress HOT 1
- Cannot install with yarn berry
- Can't install sodium, please help me (Windows) HOT 1
- Does not compile on Linux ARM. HOT 6
- Is this library still maintained?
- Can't Install (Windows) HOT 1
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 node-sodium.