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

Seed math/rand with crypto/rand #359

Merged
merged 3 commits into from
Jun 5, 2016
Merged

Conversation

rolandshoemaker
Copy link
Contributor

The performance arguments given in #48 and #102 are perfectly valid reasons to not use crypto/rand for every message ID but that doesn't mean you have to use the default seed (which is literally just 1) when using math/rand.

This patch simply reads out a random int64 using crypto/rand on package initialization and uses it to seed the default math/rand source. While there are obviously other more viable attacks this should make message ID spoofing/prediction much harder when using the default Id.

This also changes id() to just use a rand int32 and bound it to the max size of a uint16 instead of using time.Now since it's a relatively easy to guess variable so it shouldn't actually provide that many useful bits (since someone analyzing the messages will generally know the period the messages were generated in).

@miekg
Copy link
Owner

miekg commented Jun 4, 2016

Nice!

Don't like the panic though as that is a backwards incomp. change. Can't we just default to the current setup then? I.e. seed with 1?

@rolandshoemaker
Copy link
Contributor Author

Good point, I've also cleaned up the various integer bounding by leaving it to the compiler (it's much cleverer than me anyway).

if err != nil {
// Failed to read from cryptographic source, warn user and
// fallback to default initial seed (1) by returning early
log.Printf("Failed to seed math/rand with crypto/rand.Read for message ID generation: %s\n", err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prolly nobody ever will read this and the random is no worse then now, so I would just drop the log - leave the comment though!

@miekg miekg merged commit 3f53d75 into miekg:master Jun 5, 2016
@rolandshoemaker rolandshoemaker deleted the crypto-seed branch June 5, 2016 19:54
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

Successfully merging this pull request may close these issues.

2 participants