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

Add support for CSHAKE #80

Merged
merged 12 commits into from
Jan 7, 2025
Merged

Add support for CSHAKE #80

merged 12 commits into from
Jan 7, 2025

Conversation

qmuntal
Copy link
Member

@qmuntal qmuntal commented Dec 18, 2024

Go 1.24 will ship with CSHAKE support. We need to follow suit.

Tests cases are taken from the standard library.

All SHA3 algorithms, which includes CSHAKE, are available since Windows 11, build 25324: https://blogs.windows.com/windows-insider/2023/03/23/announcing-windows-11-insider-preview-build-25324/#:~:text=Introducing%20SHA%2D3%20Support

While here, I've refactor the NewSHA3_256, NewSHA3_384, and NewSHA3_512 functions to return a concret type (SHA3) instead of the interface hash.Hash. This aligns with how upstream implemented crypto/sha3, making the integration easier.

For microsoft/go#1448.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

cng/hash.go Outdated Show resolved Hide resolved
@qmuntal qmuntal requested a review from Copilot December 18, 2024 17:59

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

@qmuntal qmuntal requested a review from Copilot December 20, 2024 11:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

cng/hash_test.go:35

  • The return type should be consistent with the new return type of DigestSHA3.
return func() hash.Hash { return cng.NewSHA3_256() }

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

// SumSHA3_256 returns the SHA3-256 checksum of the data.
func SumSHA3_256(p []byte) (sum [32]byte) {
if err := hashOneShot(bcrypt.SHA3_256_ALGORITHM, p, sum[:]); err != nil {
panic("bcrypt: SHA3_256 failed")
Copy link
Preview

Copilot AI Dec 20, 2024

Choose a reason for hiding this comment

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

The error message should include the algorithm name to make it clear which algorithm failed.

Suggested change
panic("bcrypt: SHA3_256 failed")
panic("bcrypt: SHA3_256 failed for algorithm " + bcrypt.SHA3_256_ALGORITHM)

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
// SumSHA3_384 returns the SHA3-384 checksum of the data.
func SumSHA3_384(p []byte) (sum [48]byte) {
if err := hashOneShot(bcrypt.SHA3_384_ALGORITHM, p, sum[:]); err != nil {
panic("bcrypt: SHA3_384 failed")
Copy link
Preview

Copilot AI Dec 20, 2024

Choose a reason for hiding this comment

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

The error message should include the algorithm name to make it clear which algorithm failed.

Suggested change
panic("bcrypt: SHA3_384 failed")
panic("bcrypt: SHA3_384 failed for algorithm " + bcrypt.SHA3_384_ALGORITHM)

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
// SumSHA3_512 returns the SHA3-512 checksum of the data.
func SumSHA3_512(p []byte) (sum [64]byte) {
if err := hashOneShot(bcrypt.SHA3_512_ALGORITHM, p, sum[:]); err != nil {
panic("bcrypt: SHA3_512 failed")
Copy link
Preview

Copilot AI Dec 20, 2024

Choose a reason for hiding this comment

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

The error message should include the algorithm name to make it clear which algorithm failed.

Suggested change
panic("bcrypt: SHA3_512 failed")
panic("bcrypt: SHA3_512 failed for algorithm " + bcrypt.SHA3_512_ALGORITHM)

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
cng/hash.go Outdated Show resolved Hide resolved
type hashX struct {
alg *hashAlgorithm
_ctx bcrypt.HASH_HANDLE // access it using withCtx
Copy link
Member

Choose a reason for hiding this comment

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

This pattern seems like it makes it easier to spot bad access vs. sprinkling runtime.KeepAlive, why the change? (Also IMO would be good for golang-fips/openssl#238 (review).)

Copy link
Member Author

Choose a reason for hiding this comment

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

It is easy to forget to use withCtx (it happened in the OpenSSL module), so it is not a silver bullet. I prefer the runtime.KeepAlive pattern because it doesn't add a nested block on each function that access the context (thus the code is simpler), and also to keep consistency with other algorithms, which tend to use the runtime.KeepAlive rather than custom withX functions.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, if withCtx didn't help in practice, I agree that simplifying to runtime.KeepAlive makes sense. (If it did help, I would have said that it would make sense to expand that style to other algorithms as the way to get to consistency. 😄)

Co-authored-by: Copilot <[email protected]>
cng/sha3.go Show resolved Hide resolved
cng/sha3.go Show resolved Hide resolved

// sequentialBytes produces a buffer of size consecutive bytes 0x00, 0x01, ..., used for testing.
func sequentialBytes(size int) []byte {
alignmentOffset := rand.Intn(8)
Copy link
Member

Choose a reason for hiding this comment

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

Intentional randomness in tests doesn't feel right... using a fixed seed or looping through possibilities would seem better to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is from the standard library, but I missed to copy some relevant comment about why the intentional randomness. Adding it back,

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, thanks. The technique in general still rubs me the wrong way, but it would be more complicated to implement it differently and it does seem good enough in this case.

@qmuntal qmuntal merged commit 75e3e1b into main Jan 7, 2025
19 checks passed
@qmuntal qmuntal deleted the cshake branch January 7, 2025 08:57
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.

3 participants