tel / saltine

Cryptography that's easy to digest (NaCl/libsodium bindings)
https://github.com/tel/saltine
MIT License
61 stars 29 forks source link

Unable to construct own Nonce #16

Closed JanAhrens closed 10 years ago

JanAhrens commented 10 years ago

I'm trying to implement a NaCl based protocol. It requires to use nonces that are generated by concatenating a nonce-prefix and a counter. For example:

noncePrefix = "abcdefghijklmnop"
counter = 1
nonce = generateNonce noncePrefix counter # => "abcdefghijklmnop00000001"

My problem is that I'm unable to use my own nonces with the secretbox function, because I can't create the Nonce type. Internally Nonce is implemented as a newtype for ByteString, but the constructor can only be used inside the Crypto.Saltine.Core.SecretBox package.

If you agree that this is a valid use case, I'd like to send a pull-request that exposes the Nonce type constructor for the Nonce types in Crypto.Saltine.Core.SecretBox, Crypto.Saltine.Core.Box and Crypto.Saltine.Core.Stream.

tel commented 10 years ago

The Nonce interface was a little experimental. The thought was that it's broadly really important to ensure uniqueness, so the use of nonces would somehow drive that.

But I wouldn't mind relaxing that, at least with a warning. Perhaps it should just be that the type constructors are exported in an Internal module.

If you'd like to experiment with this, please feel free to send pull requests.

On Sat, Jun 28, 2014 at 8:34 AM, JanAhrens notifications@github.com wrote:

I'm trying to implement a NaCl based protocol. It requires to use nonces that are generated by concatenating a nonce-prefix and a counter. For example:

noncePrefix = "abcdefghijklmnop"counter = 1nonce = generateNonce noncePrefix counter # => "abcdefghijklmnop00000001"

My problem is that I'm unable to use my own nonces with the secretbox function, because I can't create the Nonce type. Internally Nonce is implemented https://github.com/tel/saltine/blob/master/src/Crypto/Saltine/Core/SecretBox.hs#L71 as a newtype for ByteString, but the constructor can only be used inside the Crypto.Saltine.Core.SecretBox package.

If you agree that this is a valid use case, I'd like to send a pull-request that exposes the Nonce type constructor for the Nonce types in Crypto.Saltine.Core.SecretBox, Crypto.Saltine.Core.Box and Crypto.Saltine.Core.Stream.

— Reply to this email directly or view it on GitHub https://github.com/tel/saltine/issues/16.

JanAhrens commented 10 years ago

Thanks for your quick response!

The Nonce interface was a little experimental. The thought was that it's broadly really important to ensure uniqueness, so the use of nonces would somehow drive that.

That makes sense. I'll try to make that clear when I change the API.

I also saw that in the message branch the internal implementation of Nonce was changed from ByteString to Vector Word8. To make sure that this change can happen in a compatible manner, I'll probably write and export a ByteString -> Nonce function.

tel commented 10 years ago

Message was an old experiment. I don't think it's worth spending effort to be compatible with it.

JanAhrens commented 10 years ago

It actually works already. I don't need to send a pull-request :smiley:

While reading through the tests I learned about the Cypto.Saltine.Class.decode function from the IsEncoding typeclass. Using this I can implement my use-case:

noncePrefix = "abcdefghijklmnop"
counter = 1
nonce = fromJust (encode $ generateNonce noncePrefix counter :: Maybe Nonce)

I'm closing this issue myself.

tel commented 10 years ago

Alright, excellent!

If you feel like it'd be helpful, feel free to patch the documentation as well.