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

Migrate to a cross-platform, well-established crypto library #23

Closed
lsd-cat opened this issue Oct 29, 2023 · 5 comments
Closed

Migrate to a cross-platform, well-established crypto library #23

lsd-cat opened this issue Oct 29, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@lsd-cat
Copy link
Member

lsd-cat commented Oct 29, 2023

Currently we are using python-ecdsa for the DH operations, while we are using libsodium for the symmetric encryption. We originally chose a native python library so that we could edit and debug the crypto primitives. With the recent changes, and the protocol now using only established and available primitives, we should be able to use libsodium (or even openssl) only.

A lot of porting is already in the libsodium-only branch.

Currently, the message fetching mechanism does not work: I tried to implement it using only the higher level functions in the following way:

re = nacl.public.PrivateKey.generate()
me = nacl.public.PrivateKey.generate()
jc = nacl.public.PrivateKey.generate()

nacl.public.Box(re, nacl.public.PublicKey(nacl.public.Box(me, jc.public_key).shared_key())).shared_key() == nacl.public.Box(jc, nacl.public.PublicKey(nacl.public.Box(re, me.public_key).shared_key())).shared_key()

However, the comparison returns False, meaning that the server and the fetching party are currently not able to compute the same shared secret. It can be worth investigating why, or try to implement the mechanism using the lower level crypto_scalarmult().

Edit: it is probably because the result of the multiplication is hashed or expanded (with also other parameters) before the usage in Box, losing the commutative property of DH.

@lsd-cat
Copy link
Member Author

lsd-cat commented Oct 29, 2023

import nacl.bindings.crypto_scalarmult as scalarmult

re = nacl.public.PrivateKey.generate()
me = nacl.public.PrivateKey.generate()
jc = nacl.public.PrivateKey.generate()

assert(scalarmult(re.encode(), scalarmult(me.encode(), jc.public_key.encode())) == scalarmult(jc.encode(), scalarmult(me.encode(), re.public_key.encode())))

assert(nacl.public.Box(re, nacl.public.PublicKey(scalarmult(me.encode(), jc.public_key.encode()))).shared_key() == nacl.public.Box(jc, nacl.public.PublicKey(scalarmult(me.encode(), re.public_key.encode()))).shared_key())

The manual version seems to work, even if just in the intermediate step.

@lsd-cat
Copy link
Member Author

lsd-cat commented Oct 30, 2023

With 41594ec the core parts now fully work with libsodium only. Also we can drop both ecdsa and gmpy2 as requirements.

@lsd-cat
Copy link
Member Author

lsd-cat commented Oct 31, 2023

Just realized that libsodium crypto-box automatically sign the messages that it encrypts. While we might want to do that from journalist->source, we probably do not want that on submission as it provides non-repudiability, which in case of a source it is not a desirable property.

@lsd-cat
Copy link
Member Author

lsd-cat commented Nov 2, 2023

I have ported the server to OpenResty, using luasodium. Reusing the same bindings in different languages is quitye fast, even if there are sometimes syntax or abstraction differences. For istance, luasodium always expects a nonce and expect the programmer to share it, while PyNaCl automatically does that and assume the nonce is prepended to the ciphertext.

https://gist.github.com/lsd-cat/721313dc54578f04553ffa9c39852f9d

@lsd-cat lsd-cat added the enhancement New feature or request label Dec 22, 2023
@lsd-cat
Copy link
Member Author

lsd-cat commented Mar 27, 2024

Completed with #24. Sample OpenResty server will be added to the repo soon.

@lsd-cat lsd-cat closed this as completed Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant