Skip to content

Commit

Permalink
Add decode support for files with SHA-512 MACs
Browse files Browse the repository at this point in the history
Fixes: #59

Co-authored-by: charalampos.kailantzis <[email protected]>
  • Loading branch information
AGWA and charalampos.kailantzis committed Aug 30, 2024
1 parent b59d8eb commit 133d36f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mac.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"crypto/x509/pkix"
"encoding/asn1"
"hash"
Expand All @@ -29,6 +30,7 @@ type digestInfo struct {
var (
oidSHA1 = asn1.ObjectIdentifier([]int{1, 3, 14, 3, 2, 26})
oidSHA256 = asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 1})
oidSHA512 = asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 3})
)

func doMac(macData *macData, message, password []byte) ([]byte, error) {
Expand All @@ -41,6 +43,9 @@ func doMac(macData *macData, message, password []byte) ([]byte, error) {
case macData.Mac.Algorithm.Algorithm.Equal(oidSHA256):
hFn = sha256.New
key = pbkdf(sha256Sum, 32, 64, macData.MacSalt, password, macData.Iterations, 3, 32)
case macData.Mac.Algorithm.Algorithm.Equal(oidSHA512):
hFn = sha512.New
key = pbkdf(sha512Sum, 64, 128, macData.MacSalt, password, macData.Iterations, 3, 64)
default:
return nil, NotImplementedError("unknown digest algorithm: " + macData.Mac.Algorithm.Algorithm.String())
}
Expand Down
7 changes: 7 additions & 0 deletions pbkdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"math/big"
)

Expand All @@ -27,6 +28,12 @@ func sha256Sum(in []byte) []byte {
return sum[:]
}

// sha512Sum returns the SHA-512 hash of in.
func sha512Sum(in []byte) []byte {
sum := sha512.Sum512(in)
return sum[:]
}

// fillWithRepeats returns v*ceiling(len(pattern) / v) bytes consisting of
// repeats of pattern.
func fillWithRepeats(pattern []byte, v int) []byte {
Expand Down

0 comments on commit 133d36f

Please sign in to comment.