Skip to content

Commit

Permalink
Keccak hash
Browse files Browse the repository at this point in the history
  • Loading branch information
D3vl0per committed May 29, 2024
1 parent 71b44a8 commit 5caec40
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 6 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ require (
github.com/cloudflare/circl v1.3.8
github.com/klauspost/compress v1.17.8
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.22.0
golang.org/x/crypto v0.23.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
107 changes: 107 additions & 0 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

var ErrHmacSecretNil = errors.New("HMAC secret is nil")
var ErrBlake2s128Key = errors.New("blake2s: a key is required for a 128-bit hash")
var ErrHmacIsNotSupported = errors.New("HMAC is not supported for this algorithm")

type Algorithms interface {
Hash(plaintext []byte) ([]byte, error)
Expand Down Expand Up @@ -86,6 +87,14 @@ type Shake256 struct {
Encoder generic.Encoder
}

type NewLegacyKeccak256 struct {
Encoder generic.Encoder
}

type NewLegacyKeccak512 struct {
Encoder generic.Encoder
}

///
/// Blake2b-256
///
Expand Down Expand Up @@ -919,6 +928,104 @@ func (s *Sha3512) GetEncoder() generic.Encoder {
return s.Encoder
}

///
/// NewLegacyKeccak256
///

func (n *NewLegacyKeccak256) Hash(plaintext []byte) ([]byte, error) {
hash, err := hashNewLegacyKeccak256(plaintext)
if err != nil {
return nil, err
}

Check warning on line 939 in hash/hash.go

View check run for this annotation

Codecov / codecov/patch

hash/hash.go#L938-L939

Added lines #L938 - L939 were not covered by tests
return encoder(n.Encoder, hash), nil
}

func (n *NewLegacyKeccak256) ValidateHash(plaintext, expectedHash []byte) (bool, error) {
hashed, err := hashNewLegacyKeccak256(plaintext)
if err != nil {
return false, err
}

Check warning on line 947 in hash/hash.go

View check run for this annotation

Codecov / codecov/patch

hash/hash.go#L946-L947

Added lines #L946 - L947 were not covered by tests

return generic.Compare(hashed, expectedHash), nil
}

func (n *NewLegacyKeccak256) Hmac(plaintext []byte) ([]byte, error) {
return nil, ErrHmacIsNotSupported
}

func (n *NewLegacyKeccak256) ValidateHmac(plaintext, expectedHash []byte) (bool, error) {
return false, ErrHmacIsNotSupported
}

func hashNewLegacyKeccak256(plaintext []byte) ([]byte, error) {
hash := sha3.NewLegacyKeccak256()
if _, err := hash.Write(plaintext); err != nil {
return nil, err
}

Check warning on line 964 in hash/hash.go

View check run for this annotation

Codecov / codecov/patch

hash/hash.go#L963-L964

Added lines #L963 - L964 were not covered by tests

return hash.Sum(nil), nil
}

func (n *NewLegacyKeccak256) SetEncoder(encoder generic.Encoder) {
n.Encoder = encoder
}

func (n *NewLegacyKeccak256) GetEncoder() generic.Encoder {
if n.Encoder == nil {
return nil
}
return n.Encoder
}

///
/// NewLegacyKeccak512
///

func (n *NewLegacyKeccak512) Hash(plaintext []byte) ([]byte, error) {
hash, err := hashNewLegacyKeccak512(plaintext)
if err != nil {
return nil, err
}

Check warning on line 988 in hash/hash.go

View check run for this annotation

Codecov / codecov/patch

hash/hash.go#L987-L988

Added lines #L987 - L988 were not covered by tests
return encoder(n.Encoder, hash), nil
}

func (n *NewLegacyKeccak512) ValidateHash(plaintext, expectedHash []byte) (bool, error) {
hashed, err := hashNewLegacyKeccak512(plaintext)
if err != nil {
return false, err
}

Check warning on line 996 in hash/hash.go

View check run for this annotation

Codecov / codecov/patch

hash/hash.go#L995-L996

Added lines #L995 - L996 were not covered by tests

return generic.Compare(hashed, expectedHash), nil
}

func (n *NewLegacyKeccak512) Hmac(plaintext []byte) ([]byte, error) {
return nil, ErrHmacIsNotSupported
}

func (n *NewLegacyKeccak512) ValidateHmac(plaintext, expectedHash []byte) (bool, error) {
return false, ErrHmacIsNotSupported
}

func hashNewLegacyKeccak512(plaintext []byte) ([]byte, error) {
hash := sha3.NewLegacyKeccak512()
if _, err := hash.Write(plaintext); err != nil {
return nil, err
}

Check warning on line 1013 in hash/hash.go

View check run for this annotation

Codecov / codecov/patch

hash/hash.go#L1012-L1013

Added lines #L1012 - L1013 were not covered by tests

return hash.Sum(nil), nil
}

func (n *NewLegacyKeccak512) SetEncoder(encoder generic.Encoder) {
n.Encoder = encoder
}

func (n *NewLegacyKeccak512) GetEncoder() generic.Encoder {
if n.Encoder == nil {
return nil
}
return n.Encoder
}

/// utils

func encoder(encoder generic.Encoder, hash []byte) []byte {
Expand Down
26 changes: 26 additions & 0 deletions hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ func TestE2EEHash(t *testing.T) {
//nolint:lll
expected: []byte{62, 90, 106, 154, 156, 175, 100, 184, 63, 15, 68, 163, 213, 182, 24, 243, 36, 68, 68, 37, 103, 225, 4, 104, 209, 65, 10, 211, 59, 49, 142, 76, 158, 209, 197, 144, 65, 227, 33, 54, 201, 149, 142, 218, 4, 93, 7, 57, 232, 25, 39, 234, 202, 216, 103, 197, 113, 123, 113, 85, 208, 28, 237, 36},
},
{
name: "Keccak-256",
algo: &hasher.NewLegacyKeccak256{},
data: data,
//nolint:lll
expected: []byte{37, 215, 211, 209, 110, 60, 70, 74, 213, 160, 52, 165, 229, 85, 223, 120, 27, 220, 108, 45, 118, 134, 146, 214, 44, 165, 33, 167, 60, 73, 180, 81},
},
{
name: "Keccak-512",
algo: &hasher.NewLegacyKeccak512{},
data: data,
//nolint:lll
expected: []byte{84, 132, 62, 233, 6, 185, 221, 191, 149, 86, 16, 66, 56, 234, 18, 175, 12, 76, 53, 175, 195, 148, 157, 10, 25, 133, 130, 216, 107, 161, 27, 127, 178, 240, 117, 12, 177, 50, 113, 186, 59, 74, 8, 98, 83, 225, 178, 19, 44, 195, 116, 224, 199, 77, 33, 25, 36, 212, 69, 44, 70, 142, 75, 184},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -368,6 +382,18 @@ func TestHashErrors(t *testing.T) {
data: []byte("aing7jei3eebeaMohjeesheeph0ichaiXual4vah1Eeg3eikai7aichoeliej1da"),
err: hasher.ErrHmacSecretNil,
},
{
name: "Keccak-256 HMAC",
algo: &hasher.NewLegacyKeccak256{},
data: []byte("aing7jei3eebeaMohjeesheeph0ichaiXual4vah1Eeg3eikai7aichoeliej1da"),
err: hasher.ErrHmacIsNotSupported,
},
{
name: "Keccak-512 HMAC",
algo: &hasher.NewLegacyKeccak512{},
data: []byte("aing7jei3eebeaMohjeesheeph0ichaiXual4vah1Eeg3eikai7aichoeliej1da"),
err: hasher.ErrHmacIsNotSupported,
},
}

for _, test := range tests {
Expand Down
19 changes: 17 additions & 2 deletions vendor/golang.org/x/crypto/sha3/sha3_s390x.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/golang.org/x/sys/cpu/cpu.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_arm64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/golang.org/x/sys/cpu/cpu_gc_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/golang.org/x/sys/cpu/cpu_linux_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ github.com/pmezard/go-difflib/difflib
## explicit; go 1.17
github.com/stretchr/testify/assert
github.com/stretchr/testify/require
# golang.org/x/crypto v0.22.0
# golang.org/x/crypto v0.23.0
## explicit; go 1.18
golang.org/x/crypto/argon2
golang.org/x/crypto/blake2b
Expand All @@ -59,7 +59,7 @@ golang.org/x/crypto/poly1305
golang.org/x/crypto/salsa20/salsa
golang.org/x/crypto/scrypt
golang.org/x/crypto/sha3
# golang.org/x/sys v0.19.0
# golang.org/x/sys v0.20.0
## explicit; go 1.18
golang.org/x/sys/cpu
# gopkg.in/yaml.v3 v3.0.1
Expand Down

0 comments on commit 5caec40

Please sign in to comment.