Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am taking the liberty of making a post on this site because I am encountering a problem using the LibSodium library, in JavaScript.
The goal is to encrypt a message, so I execute:

JavaScript
var i=sodium.crypto_secretbox_NONCEBYTES;
var a=sodium.crypto_kx_keypair(), b=sodium.crypto_kx_keypair(); // To get two pairs of keys, a and b
var p=sodium.crypto_kx_client_session_keys(a.publicKey,a.privateKey,b.publicKey); // To calculate the exchange keys between a and b
var n=sodium.randombytes_buf(i); //To get a nonce
var e=sodium.crypto_secretbox_easy('my message',p.sharedRx,n) // To encrypt "my message" using one of the exchange keys and the nonce


And I get the error

JavaScript
"Uncaught TypeError: invalid nonce length
      at p (sodium.js:1:644283)
      at Object.Wg [as crypto_secretbox_easy] (sodium.js:1:705164)
      at <anonymous>:1:222"


Do you know where the error comes from?

Thanking you in advance for your answers.

What I have tried:

This happens regardless of the value of i, I tried with sodium.crypto_secretbox_NONCEBYTES, with 24, and with all integers between 0 and 7500.

Edit : I modified the library line indicated to me in the error, and it turns out that the length of the nonce is always evaluated at 32 whatever its value.
Posted
Updated 14-Apr-24 6:46am
v3

You are generating the nonce incorrectly. Your 'sodium.randombytes_buf(i)' function expects the argument of 'i' to be the length of the buffer (in bytes) to be generated, not the actual value of the nonce length as per your code.

The 'crypto_secretbox_NONCEBYTES' constant is equal to 24, which is the required length for the nonce when using the 'crypto_secretbox_easy' function as per teh documentation - Authenticated encryption[^]

As per the documentation, your code should be -

JavaScript
var noncebytes = sodium.crypto_secretbox_NONCEBYTES; //Bites size should be 24...
var a = sodium.crypto_kx_keypair(), b = sodium.crypto_kx_keypair();
var p = sodium.crypto_kx_client_session_keys(a.publicKey, a.privateKey, b.publicKey);
var nonce = sodium.randombytes_buf(noncebytes);
var e = sodium.crypto_secretbox_easy('my message', p.sharedRx, nonce); 
 
Share this answer
 
Comments
jv067i 14-Apr-24 12:17pm    
Thank you for your response, but it does not solve the problem. I made a mistake on a variable name when copying my code into the question, and I apologize, but the fact is that I put all the possible and imaginable values for the size of the nonce, including the value of sodium.crypto_secretbox_NONCEBYTES, and I get the error "invalid nonce length"
It's a 3rd-party library. Best place is to ask in their issues on github: Issues · jedisct1/libsodium.js · GitHub[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900