Skip to content

Commit

Permalink
Add SHA3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wussler committed Jan 17, 2023
1 parent cf6655e commit 06d97d9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions openpgp/clearsign/clearsign.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ func nameOfHash(h crypto.Hash) string {
return "SHA384"
case crypto.SHA512:
return "SHA512"
case crypto.SHA3_256:
return "SHA3-256"
case crypto.SHA3_512:
return "SHA3-512"
}
return ""
}
Expand All @@ -457,6 +461,10 @@ func nameToHash(h string) crypto.Hash {
return crypto.SHA384
case "SHA512":
return crypto.SHA512
case "SHA3-256":
return crypto.SHA3_256
case "SHA3-512":
return crypto.SHA3_512
}
return crypto.Hash(0)
}
6 changes: 6 additions & 0 deletions openpgp/internal/algorithm/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var (
SHA384 Hash = cryptoHash{9, crypto.SHA384}
SHA512 Hash = cryptoHash{10, crypto.SHA512}
SHA224 Hash = cryptoHash{11, crypto.SHA224}
SHA3_256 Hash = cryptoHash{12, crypto.SHA3_256}
SHA3_512 Hash = cryptoHash{14, crypto.SHA3_512}
)

// HashById represents the different hash functions specified for OpenPGP. See
Expand All @@ -52,6 +54,8 @@ var (
SHA384.Id(): SHA384,
SHA512.Id(): SHA512,
SHA224.Id(): SHA224,
SHA3_256.Id(): SHA3_256,
SHA3_512.Id(): SHA3_512,
}
)

Expand All @@ -75,6 +79,8 @@ var hashNames = map[uint8]string{
SHA384.Id(): "SHA384",
SHA512.Id(): "SHA512",
SHA224.Id(): "SHA224",
SHA3_256.Id(): "SHA3-256",
SHA3_512.Id(): "SHA3-512",
}

func (h cryptoHash) String() string {
Expand Down
2 changes: 2 additions & 0 deletions openpgp/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var hashes = []crypto.Hash{
crypto.SHA256,
crypto.SHA384,
crypto.SHA512,
crypto.SHA3_256,
crypto.SHA3_512,
}

var ciphers = []packet.CipherFunction{
Expand Down
2 changes: 2 additions & 0 deletions openpgp/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ package openpgp // import "github.com/ProtonMail/go-crypto/openpgp"
import (
"crypto"
_ "crypto/sha256"
_ "crypto/sha512"
"hash"
"io"
"strconv"

"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/go-crypto/openpgp/errors"
"github.com/ProtonMail/go-crypto/openpgp/packet"
_ "golang.org/x/crypto/sha3"
)

// SignatureType is the armor type for a PGP signature.
Expand Down
2 changes: 1 addition & 1 deletion openpgp/s2k/s2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func HashIdToString(id byte) (name string, ok bool) {
return "", false
}

// HashIdToHash returns an OpenPGP hash id which corresponds the given Hash.
// HashToHashId returns an OpenPGP hash id which corresponds the given Hash.
func HashToHashId(h crypto.Hash) (id byte, ok bool) {
for id, hash := range algorithm.HashById {
if hash.HashFunc() == h {
Expand Down
4 changes: 3 additions & 1 deletion openpgp/s2k/s2k_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

_ "golang.org/x/crypto/ripemd160"
_ "golang.org/x/crypto/sha3"
)

var saltedTests = []struct {
Expand Down Expand Up @@ -136,7 +137,8 @@ func TestParseIntoParams(t *testing.T) {
}

func TestSerializeOK(t *testing.T) {
hashes := []crypto.Hash{crypto.SHA1, crypto.RIPEMD160, crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA224}
hashes := []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA224, crypto.SHA3_256,
crypto.SHA3_512, crypto.SHA1, crypto.RIPEMD160}
testCounts := []int{-1, 0, 1024, 65536, 4063232, 65011712}
for _, h := range hashes {
for _, c := range testCounts {
Expand Down
4 changes: 4 additions & 0 deletions openpgp/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ func encrypt(keyWriter io.Writer, dataWriter io.Writer, to []*Entity, signed *En
hashToHashId(crypto.SHA256),
hashToHashId(crypto.SHA384),
hashToHashId(crypto.SHA512),
hashToHashId(crypto.SHA3_256),
hashToHashId(crypto.SHA3_512),
hashToHashId(crypto.SHA1),
hashToHashId(crypto.RIPEMD160),
}
Expand Down Expand Up @@ -458,6 +460,8 @@ func Sign(output io.Writer, signed *Entity, hints *FileHints, config *packet.Con
hashToHashId(crypto.SHA256),
hashToHashId(crypto.SHA384),
hashToHashId(crypto.SHA512),
hashToHashId(crypto.SHA3_256),
hashToHashId(crypto.SHA3_512),
hashToHashId(crypto.SHA1),
hashToHashId(crypto.RIPEMD160),
}
Expand Down

0 comments on commit 06d97d9

Please sign in to comment.