Skip to content

Switch to SHA256 and generate a nonce for AES operations - #119

Closed
aaronjwood wants to merge 3 commits into
go-macaron:masterfrom
aaronjwood:master
Closed

Switch to SHA256 and generate a nonce for AES operations#119
aaronjwood wants to merge 3 commits into
go-macaron:masterfrom
aaronjwood:master

Conversation

@aaronjwood

@aaronjwood aaronjwood commented Feb 13, 2017

Copy link
Copy Markdown
Contributor

@leonklingele

Copy link
Copy Markdown

Wow, this is just bad. ;) Calling SetSuperSecureCookie twice with the same key will reuse the same nonce + key combination, making the system utterly broken.

Comment thread context.go
// This method would need to take in a salt and pass it to PBKDF2 if we want to switch to PBKDF2.
hash := sha256.Sum256([]byte(secret))
secret = hex.EncodeToString(hash[:])
text, err := com.AESEncrypt([]byte(secret), ctx.nonce, []byte(value)) // The nonce should really be unique per cookie.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wait. You hash the secret, encode it to a HEX string, then use the byte repesentation of that HEX string?
Why not plug in hash directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It should be as you say, unless there are dependencies on other libraries/packages that are expecting it in this way. Then they'd all need to be changed. @unknwon would know more about that than me.

@aaronjwood

Copy link
Copy Markdown
Contributor Author

@leonklingele I agree, we really need to switch to PBKDF2 and change the API to fix the issues you're mentioning. If the API can't be changed and we can't move to PBKDF2 then I don't know what else to do.

@leonklingele

Copy link
Copy Markdown

What you just said doesn't make sense at all. You're already breaking the API by switching from MD5 to SHA256. Then why not do it properly (i.e. without HEX encoding / decoding)?

@aaronjwood

aaronjwood commented Feb 13, 2017

Copy link
Copy Markdown
Contributor Author

@leonklingele I don't think this breaks the API. It's internal to Context. If someone upgrades the framework then there would be all new cookies getting set, no? Also, I'm not adding all of the hex encoding/decoding, it was already there. If @unknwon doesn't want to change the API then maybe this should just be closed. Like you said, it really is just reusing the nonce and key over and over...

@leonklingele

Copy link
Copy Markdown

It breaks the API because everyone who still has one of the old MD5-stuff can't use it anymore.

@aaronjwood

Copy link
Copy Markdown
Contributor Author

@unknwon I think it's best to close this out if the API can't be changed. I don't think what I've done here really helps anything. At the very least you can keep the changes to work with the new AES nonce parameter.

@leonklingele

Copy link
Copy Markdown

Why? IMO it's fine to break the API if the old one was broken security-wise. But then please do it properly.

@aaronjwood

Copy link
Copy Markdown
Contributor Author

@unknwon how about changing the API and have something in the next release notes about a bunch of flawed crypto getting fixed which causes incompatibility?

@unknwon

unknwon commented Feb 13, 2017

Copy link
Copy Markdown
Contributor

OK, I agree to break SetSuperSecureCookie's API as long as it allows accept arbitrary length of salt, and use it to generate desired nonce.

The other big use case of this function is https://github.com/grafana/grafana/tree/master/vendor/gopkg.in/macaron.v1, but it is in vendor so shouldn't break their build. But we should warn them (maybe file an issue) about this.

@aaronjwood

Copy link
Copy Markdown
Contributor Author

Okay, let me close this out and open up a new, proper one.

@aaronjwood aaronjwood closed this Feb 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants