crypto icon indicating copy to clipboard operation
crypto copied to clipboard

bcrypt: simplify bcrypt's base64 by using base64.Nopadding

Open kurenaif opened this issue 4 years ago • 7 comments

In the existing bcrypt, pad and unpad by the base64Encode and base64Decode methods of bcrypt/base64. However, in the current encoding/base64 implementation, WithPadding(base64) can omit them.

Also, in the current implementation, if src is 0 bytes at Encode, dst[n-1] == '=' will result in an out-of-range. current base64Encode is not affected by this because it handle either fixed-length salt(16bytes) or hash values(23bytes).

This pull request simplifies the code and reduce the risk of future out-of-range.

kurenaif avatar Apr 13 '21 17:04 kurenaif

This PR (HEAD: 82b4fb45d59cc4003c51dca706a496e0cbc172b1) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/crypto/+/309392 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off) See the Wiki page for more info

gopherbot avatar Apr 13 '21 17:04 gopherbot

Message from Go Bot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps: A maintainer will review your change and provide feedback. See https://golang.org/doc/contribute.html#review for more info and tips to get your patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be surprising to people new to the project. The careful, iterative review process is our way of helping mentor contributors and ensuring that their contributions have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which little code gets reviewed or merged. If a reviewer responds with a comment like R=go1.11 or adds a tag like "wait-release", it means that this CL will be reviewed as part of the next development cycle. See https://golang.org/s/release for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/309392. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Apr 13 '21 17:04 gopherbot

Message from Ian Lance Taylor:

Patch Set 1: Run-TryBot+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/309392. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Apr 13 '21 19:04 gopherbot

Message from Go Bot:

Patch Set 1:

TryBots beginning. Status page: https://farmer.golang.org/try?commit=126f671b


Please don’t reply on this GitHub thread. Visit golang.org/cl/309392. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Apr 13 '21 19:04 gopherbot

Message from Go Bot:

Patch Set 1: TryBot-Result+1

TryBots are happy.


Please don’t reply on this GitHub thread. Visit golang.org/cl/309392. After addressing review feedback, remember to publish your drafts!

gopherbot avatar Apr 13 '21 19:04 gopherbot

I was testing the bcrypt/base64 and discovered the issue with paddings.

If anyone there is not convinced yet by the existence of problem. Take a look to spec of base64: https://datatracker.ietf.org/doc/html/rfc4648#section-9

Current code produces "SGVsbG8gd29ybGQu" for "Hello world." which is correct. But when decoding; it adds 3 equal signs (=) at the end of array. It assumes when the length of input is multiples of 4, it should add (=)s.

https://github.com/golang/crypto/blob/630584e8d5aaa1472863b49679b2d5548d80dcba/bcrypt/base64.go#L24

ufukty avatar Aug 16 '22 11:08 ufukty

Correct operation should calculate number of equals as (4 - len(src)) % 4

number of bytes in the decoded string -> number of equals to add
0 -> 0
1 -> 3
2 -> 2
3 -> 1
4 -> 0 (repeating from top)
5 -> 3
6 -> 2
7 -> 1
8 -> 0

Edit: I realized % operator doesn't work as I thought, it produces negative values.

Edit 2: x = len(src); Subtracting x from the smallest number greater than x that is multiple of 4 seems work (x - x % 4 + 4 - x) % 4. I'm aware it looks ridiculous.

ufukty avatar Aug 16 '22 12:08 ufukty