Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Salsa20: the nonce and the 64-bit counter #11

Closed
R2F opened this issue Apr 24, 2019 · 3 comments
Closed

Salsa20: the nonce and the 64-bit counter #11

R2F opened this issue Apr 24, 2019 · 3 comments

Comments

@R2F
Copy link

R2F commented Apr 24, 2019

Hi, I went through the implementation of the Salsa20 algorithm and I found out, that the table nonce (16 bytes long) is written to the matrix on positions z6, z7, z8 and z9 (in cf_salsa20_core() ):

           x6 = z6 = read32_le(nonce + 0),
           x7 = z7 = read32_le(nonce + 4),
           x8 = z8 = read32_le(nonce + 8),
           x9 = z9 = read32_le(nonce + 12),

The first two parts (z6 and z7) are set to zero during the initialization and z8 and z9 contain the nonce from the input:

         memset(ctx->nonce, 0, sizeof ctx->nonce);
         memcpy(ctx->nonce + 8, nonce, 8);

Later in the algorithm positions z6 and z7 are incremented every 64-byte block of the encrypted message:

         incr_le(ctx->nonce, ctx->ncounter);

So as I understand z6 and z7 contain the 64-bit counter and z8 and z9 contain the actual nonce.

The issue I found is that the documentation of Salsa20 says that the 64-bit counter is supposed to be on positions z8 and z9, and the 8-byte nonce on positions z6 and z7. So to me it looks like the positions are switched and the algorithm is not compatible with the standard.
Refs:
https://en.wikipedia.org/wiki/Salsa20
http://www.ecrypt.eu.org/stream/papersdir/2007/010.pdf

If the same algorithm is used on both sides, the message is encrypted and decrypted correctly. However, the problems may occur if the message encrypted with Cifra implementation is decrypted with some other implementation that handles the nonce/pos as it is described in the documents.

@R2F R2F changed the title Slasa20: handling the POS and NONCE Salsa20: the nonce and the 64-bit counter Apr 24, 2019
@R2F
Copy link
Author

R2F commented Apr 25, 2019

I have just created a pull request with the patch that fixes the issue.

@ctz
Copy link
Owner

ctz commented Apr 29, 2019

Sorry about this, and thanks for the report and patch. I've added an extra test generated with libsodium which confirms this fixes compatibility.

@ctz ctz closed this as completed Apr 29, 2019
@R2F
Copy link
Author

R2F commented Apr 30, 2019

Thanks. I am happy that I could contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants