From 15053202a13877ca1f03e4266d5a8d61d7c282b1 Mon Sep 17 00:00:00 2001 From: Sudhi Herle Date: Sun, 7 Jan 2024 11:55:21 -0800 Subject: [PATCH] Updated sigtool to use portable mmap lib --- build | 7 ++++++ go.mod | 11 ++++++---- go.sum | 16 +++++++------- sign/iomisc.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ sign/keys.go | 40 ---------------------------------- 5 files changed, 81 insertions(+), 52 deletions(-) create mode 100644 sign/iomisc.go diff --git a/build b/build index 8b7dc80..755108d 100755 --- a/build +++ b/build @@ -416,6 +416,13 @@ case $Tool in out=$p dir=$p fi + + # Add .exe suffix to out if needed + if [ "$GOOS" = "windows" ]; then + base=${out%%.exe} + out="${base}.exe" + fi + echo " $dir: $out .. " $e eval $Go build $vflag -o $Bindir/$out $isuffix "$ldflags" ./$dir || exit 1 done diff --git a/go.mod b/go.mod index 84c9706..e377147 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,20 @@ module github.com/opencoff/sigtool -go 1.21 +go 1.21.1 require ( github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a - github.com/opencoff/go-utils v0.8.0 + github.com/opencoff/go-mmap v0.1.1 + github.com/opencoff/go-utils v0.9.0 github.com/opencoff/pflag v1.0.6-sh1 golang.org/x/crypto v0.17.0 - google.golang.org/protobuf v1.31.0 + google.golang.org/protobuf v1.32.0 gopkg.in/yaml.v2 v2.4.0 ) require ( - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.15.0 // indirect ) + +//replace github.com/opencoff/go-mmap => ../go-mmap diff --git a/go.sum b/go.sum index 2df2afa..c92d66e 100644 --- a/go.sum +++ b/go.sum @@ -1,23 +1,23 @@ github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a h1:saTgr5tMLFnmy/yg3qDTft4rE5DY2uJ/cCxCe3q0XTU= github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a/go.mod h1:Bw9BbhOJVNR+t0jCqx2GC6zv0TGBsShs56Y3gfSCvl0= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/opencoff/go-utils v0.8.0 h1:8p1C5DrjMCczxFg9LcT9KmdahksS6Ox8zgh7O2rZuqo= -github.com/opencoff/go-utils v0.8.0/go.mod h1:8hMC3uc+N0wUMxpmW2yzM6FuhlCCxRST1Wj+cYntqFA= +github.com/opencoff/go-mmap v0.1.1 h1:Z1B8CECT+5Tf6tcqWgf2BR0ufFnQeziNlcI8CzCt4o0= +github.com/opencoff/go-mmap v0.1.1/go.mod h1:+UjRnKQ3l5dLqSNAczz7zKI8LJ7mBhJhaSqU4S91tFs= +github.com/opencoff/go-utils v0.9.0 h1:qJcRsjLHADmej3/BkH6cLX3pxapLwHf9uSN0E7vvZf4= +github.com/opencoff/go-utils v0.9.0/go.mod h1:IHjYSeM+bOKhCD2rBEba4/0C7Apko+KZKKyFgDF43Sc= github.com/opencoff/pflag v1.0.6-sh1 h1:6RO8GgnpH928yu6earGDD01FnFT//bDJ1hCovcVVqY4= github.com/opencoff/pflag v1.0.6-sh1/go.mod h1:2bXtpAD/5h/2LarkbsRwiUxqnvB1nZBzn9Xjad1P41A= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= 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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/sign/iomisc.go b/sign/iomisc.go new file mode 100644 index 0000000..b405b4f --- /dev/null +++ b/sign/iomisc.go @@ -0,0 +1,59 @@ +// iomisc.go -- misc i/o functions +// +// (c) 2016 Sudhi Herle +// +// Licensing Terms: GPLv2 +// +// If you need a commercial license for this work, please contact +// the author. +// +// This software does not come with any express or implied +// warranty; it is provided "as is". No claim is made to its +// suitability for any purpose. + +package sign + +import ( + "encoding/binary" + "fmt" + "github.com/opencoff/go-mmap" + "github.com/opencoff/go-utils" + "hash" + "os" +) + +// Simple function to reliably write data to a file. +// Does MORE than ioutil.WriteFile() - in that it doesn't trash the +// existing file with an incomplete write. +func writeFile(fn string, b []byte, ovwrite bool, mode uint32) error { + sf, err := utils.NewSafeFile(fn, ovwrite, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode)) + if err != nil { + return err + } + sf.Write(b) + return sf.Close() +} + +// Generate file checksum out of hash function h +func fileCksum(fn string, h hash.Hash) ([]byte, error) { + fd, err := os.Open(fn) + if err != nil { + return nil, fmt.Errorf("can't open %s: %s", fn, err) + } + + defer fd.Close() + + sz, err := mmap.Reader(fd, func(b []byte) error { + h.Write(b) + return nil + }) + if err != nil { + return nil, err + } + + var b [8]byte + binary.BigEndian.PutUint64(b[:], uint64(sz)) + h.Write(b[:]) + + return h.Sum(nil)[:], nil +} diff --git a/sign/keys.go b/sign/keys.go index 4a32d45..f0d0c93 100644 --- a/sign/keys.go +++ b/sign/keys.go @@ -25,18 +25,13 @@ import ( "crypto/sha256" "crypto/sha512" "encoding/base64" - "encoding/binary" "fmt" - "hash" "io/ioutil" "math/big" - "os" Ed "crypto/ed25519" "golang.org/x/crypto/scrypt" "gopkg.in/yaml.v2" - - "github.com/opencoff/go-utils" ) // Private Ed25519 key @@ -499,41 +494,6 @@ func (pk *PublicKey) UnmarshalBinary(yml []byte) error { // -- Internal Utility Functions -- -// Simple function to reliably write data to a file. -// Does MORE than ioutil.WriteFile() - in that it doesn't trash the -// existing file with an incomplete write. -func writeFile(fn string, b []byte, ovwrite bool, mode uint32) error { - sf, err := utils.NewSafeFile(fn, ovwrite, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(mode)) - if err != nil { - return err - } - defer sf.Abort() // always cleanup on error - - sf.Write(b) - return sf.Close() -} - -// Generate file checksum out of hash function h -func fileCksum(fn string, h hash.Hash) ([]byte, error) { - fd, err := os.Open(fn) - if err != nil { - return nil, fmt.Errorf("can't open %s: %s", fn, err) - } - - defer fd.Close() - - sz, err := utils.MmapReader(fd, 0, 0, h) - if err != nil { - return nil, err - } - - var b [8]byte - binary.BigEndian.PutUint64(b[:], uint64(sz)) - h.Write(b[:]) - - return h.Sum(nil)[:], nil -} - func clamp(k []byte) []byte { k[0] &= 248 k[31] &= 127