From 387c75e7910d9785aac811a42774b5f48912927f Mon Sep 17 00:00:00 2001 From: Sudhi Herle Date: Sat, 19 Oct 2019 21:12:57 -0500 Subject: [PATCH] Ran gofmt on all files; forgot to do that after merging. --- crypt.go | 3 --- sign/encrypt.go | 68 ++++++++++++++++++++--------------------------- sign/sign.go | 6 +---- sign/sign_test.go | 1 - version | 2 +- 5 files changed, 31 insertions(+), 49 deletions(-) diff --git a/crypt.go b/crypt.go index e02376a..ebcb23e 100644 --- a/crypt.go +++ b/crypt.go @@ -126,7 +126,6 @@ func encrypt(args []string) { } } - err = en.Encrypt(infd, outfd) if err != nil { die("%s", err) @@ -220,7 +219,6 @@ func decrypt(args []string) { outfd = outf } - d, err := sign.NewDecryptor(infd) if err != nil { die("%s", err) @@ -237,7 +235,6 @@ func decrypt(args []string) { } } - func encryptUsage(fs *flag.FlagSet) { fmt.Printf(`%s encrypt: Encrypt a file to one or more recipients. diff --git a/sign/encrypt.go b/sign/encrypt.go index 454200a..a421287 100644 --- a/sign/encrypt.go +++ b/sign/encrypt.go @@ -14,22 +14,21 @@ package sign import ( + "bytes" "crypto/aes" "crypto/cipher" "crypto/ed25519" "crypto/sha256" "crypto/sha512" "crypto/subtle" + "encoding/binary" "fmt" "golang.org/x/crypto/curve25519" "golang.org/x/crypto/hkdf" "io" "math/big" - "bytes" - "encoding/binary" ) - // Encryption chunk size = 4MB const chunkSize int = 4 * 1048576 @@ -37,20 +36,19 @@ const _Magic = "SigTool" const _MagicLen = len(_Magic) const _AEADNonceLen = 32 - // Encryptor holds the encryption context type Encryptor struct { Header - key [32]byte // file encryption key + key [32]byte // file encryption key - ae cipher.AEAD - sender *PrivateKey + ae cipher.AEAD + sender *PrivateKey started bool buf []byte } -// Create a new Encryption context and use the optional private key 'sk' for +// Create a new Encryption context and use the optional private key 'sk' for // signing any recipient keys. If 'sk' is nil, then ephmeral Curve25519 keys // are generated and used with recipient's public key. func NewEncryptor(sk *PrivateKey) (*Encryptor, error) { @@ -58,7 +56,7 @@ func NewEncryptor(sk *PrivateKey) (*Encryptor, error) { e := &Encryptor{ Header: Header{ ChunkSize: uint32(chunkSize), - Salt: make([]byte, _AEADNonceLen), + Salt: make([]byte, _AEADNonceLen), }, sender: sk, @@ -77,11 +75,10 @@ func NewEncryptor(sk *PrivateKey) (*Encryptor, error) { return nil, fmt.Errorf("encrypt: %s", err) } - e.buf = make([]byte, chunkSize + 4 + e.ae.Overhead()) + e.buf = make([]byte, chunkSize+4+e.ae.Overhead()) return e, nil } - // Add a new recipient to this encryption context. func (e *Encryptor) AddRecipient(pk *PublicKey) error { if e.started { @@ -104,7 +101,6 @@ func (e *Encryptor) AddRecipient(pk *PublicKey) error { return nil } - // Begin the encryption process by writing the header func (e *Encryptor) start(wr io.Writer) error { msize := e.Size() @@ -112,16 +108,16 @@ func (e *Encryptor) start(wr io.Writer) error { // marshal the header and recipients hdrlen := _MagicLen + 1 + 4 + sha256.Size - buf := make([]byte, hdrlen + msize) + buf := make([]byte, hdrlen+msize) hdrbuf := buf[hdrlen:] copy(buf[:], []byte(_Magic)) - buf[_MagicLen] = 1 // file version# + buf[_MagicLen] = 1 // file version# // The fixed header is the magic _and _ the length of the variable segment. // So, we capture the length of the variable portion first. - binary.BigEndian.PutUint32(buf[_MagicLen + 1:], uint32(sha256.Size + msize)) + binary.BigEndian.PutUint32(buf[_MagicLen+1:], uint32(sha256.Size+msize)) // Now marshal the variable portion _, err := e.MarshalToSizedBuffer(hdrbuf) @@ -130,7 +126,7 @@ func (e *Encryptor) start(wr io.Writer) error { } // and calculate the header checksum - cksum := buf[_MagicLen + 1 + 4:] + cksum := buf[_MagicLen+1+4:] h := sha256.New() h.Write(hdrbuf) h.Sum(cksum[:0]) @@ -161,7 +157,6 @@ func fullwrite(buf []byte, wr io.Writer) error { return nil } - // Encrypt the input stream 'rd' and write encrypted stream to 'wr' func (e *Encryptor) Encrypt(rd io.Reader, wr io.Writer) error { if !e.started { @@ -204,7 +199,7 @@ func (e *Encryptor) encrypt(buf []byte, wr io.Writer, i int) error { var b [8]byte var noncebuf [32]byte - binary.BigEndian.PutUint32(b[:4], uint32(e.ae.Overhead() + len(buf))) + binary.BigEndian.PutUint32(b[:4], uint32(e.ae.Overhead()+len(buf))) binary.BigEndian.PutUint32(b[4:], uint32(i)) h := sha256.New() @@ -225,26 +220,24 @@ func (e *Encryptor) encrypt(buf []byte, wr io.Writer, i int) error { return nil } - // Decryptor holds the decryption context type Decryptor struct { Header - ae cipher.AEAD - rd io.Reader + ae cipher.AEAD + rd io.Reader buf []byte // Decrypted key key []byte } - // Create a new decryption context and if 'pk' is given, check that it matches // the sender func NewDecryptor(rd io.Reader) (*Decryptor, error) { - var b [12]byte + var b [12]byte - _, err := io.ReadFull(rd, b[:]) + _, err := io.ReadFull(rd, b[:]) if err != nil { return nil, err } @@ -281,7 +274,7 @@ func NewDecryptor(rd io.Reader) (*Decryptor, error) { } d := &Decryptor{ - rd: rd, + rd: rd, } err = d.Header.Unmarshal(hdr) @@ -289,7 +282,7 @@ func NewDecryptor(rd io.Reader) (*Decryptor, error) { return nil, fmt.Errorf("decrypt: decode error: %s", err) } - if d.ChunkSize == 0 || d.ChunkSize > (16 * 1048576) { + if d.ChunkSize == 0 || d.ChunkSize > (16*1048576) { return nil, fmt.Errorf("decrypt: invalid chunkSize %d", d.ChunkSize) } @@ -343,7 +336,6 @@ func (d *Decryptor) SetPrivateKey(sk *PrivateKey, senderPk *PublicKey) error { return fmt.Errorf("decrypt: Can't find any public key to match the given private key") - havekey: aes, err := aes.NewCipher(d.key) if err != nil { @@ -354,7 +346,7 @@ havekey: if err != nil { return fmt.Errorf("decrypt: %s", err) } - d.buf = make([]byte, int(d.ChunkSize) + d.ae.Overhead()) + d.buf = make([]byte, int(d.ChunkSize)+d.ae.Overhead()) return nil } @@ -363,7 +355,6 @@ func (d *Decryptor) WrappedKeys() []*WrappedKey { return d.Keys } - // Decrypt the file and write to 'wr' func (d *Decryptor) Decrypt(wr io.Writer) error { if d.key == nil { @@ -378,7 +369,7 @@ func (d *Decryptor) Decrypt(wr io.Writer) error { if len(c) == 0 { return nil } - + if len(c) > 0 { err = fullwrite(c, wr) if err != nil { @@ -403,8 +394,13 @@ func (d *Decryptor) decrypt(i int) ([]byte, error) { return nil, fmt.Errorf("decrypt: can't read chunk %d length: %s", i, err) } - chunklen := int(binary.BigEndian.Uint32(b[:4])) + + // Sanity check - in case of corrupt header + if chunklen > (d.ae.Overhead()+chunkSize) { + return nil, fmt.Errorf("decrypt: chunksize is too large (%d)", chunklen) + } + binary.BigEndian.PutUint32(b[4:], uint32(i)) h := sha256.New() h.Write(d.Salt) @@ -448,7 +444,6 @@ func (sk *PrivateKey) WrapKey(pk *PublicKey, key []byte) (*WrappedKey, error) { return wrapKey(pk, key, &ourSK) } - func wrapKey(pk *PublicKey, k []byte, ourSK *[32]byte) (*WrappedKey, error) { var curvePK, theirPK, shared [32]byte @@ -469,8 +464,6 @@ func wrapKey(pk *PublicKey, k []byte, ourSK *[32]byte) (*WrappedKey, error) { }, nil } - - // Unwrap a wrapped key using the private key 'sk' func (w *WrappedKey) UnwrapKey(sk *PrivateKey, senderPk *PublicKey) ([]byte, error) { var shared, theirPK, ourSK [32]byte @@ -501,7 +494,6 @@ func (w *WrappedKey) UnwrapKey(sk *PrivateKey, senderPk *PublicKey) ([]byte, err return key, nil } - // Convert an Ed25519 Private Key to Curve25519 Private key func (sk *PrivateKey) toCurve25519SK() []byte { if sk.ck == nil { @@ -565,8 +557,7 @@ func expand(shared, pk []byte) ([]byte, error) { return kek, err } - -// seal the data via AEAD after suitably expanding 'shared' +// seal the data via AEAD after suitably expanding 'shared' func aeadSeal(data, shared, pk []byte) ([]byte, []byte, error) { kek, err := expand(shared[:], pk) if err != nil { @@ -586,7 +577,7 @@ func aeadSeal(data, shared, pk []byte) ([]byte, []byte, error) { noncesize := ae.NonceSize() tagsize := ae.Overhead() - buf := make([]byte, tagsize + len(kek)) + buf := make([]byte, tagsize+len(kek)) nonce := make([]byte, noncesize) randread(nonce) @@ -624,7 +615,6 @@ func aeadOpen(data, nonce, shared, pk []byte) ([]byte, error) { return c, nil } - func clamp(k []byte) []byte { k[0] &= 248 k[31] &= 127 diff --git a/sign/sign.go b/sign/sign.go index daaca11..ad70313 100644 --- a/sign/sign.go +++ b/sign/sign.go @@ -223,7 +223,6 @@ func MakePrivateKey(yml []byte, pw string) (*PrivateKey, error) { return nil, fmt.Errorf("can't decode YAML:Verify: %s", err) } - // We take short passwords and extend them pwb := sha512.Sum512([]byte(pw)) @@ -250,7 +249,6 @@ func MakePrivateKey(yml []byte, pw string) (*PrivateKey, error) { return PrivateKeyFromBytes(skb) } - // Make a private key from 64-bytes of extended Ed25519 key func PrivateKeyFromBytes(skb []byte) (*PrivateKey, error) { if len(skb) != 64 { @@ -272,7 +270,6 @@ func PrivateKeyFromBytes(skb []byte) (*PrivateKey, error) { return sk, nil } - // Given a secret key, return the corresponding Public Key func (sk *PrivateKey) PublicKey() *PublicKey { return sk.pk @@ -481,7 +478,6 @@ func MakePublicKey(yml []byte) (*PublicKey, error) { return PublicKeyFromBytes(pk) } - // Make a public key from a byte string func PublicKeyFromBytes(b []byte) (*PublicKey, error) { if len(b) != 32 { @@ -489,7 +485,7 @@ func PublicKeyFromBytes(b []byte) (*PublicKey, error) { } pk := &PublicKey{ - Pk: make([]byte, 32), + Pk: make([]byte, 32), hash: pkhash(b), } diff --git a/sign/sign_test.go b/sign/sign_test.go index b29de39..54fb509 100644 --- a/sign/sign_test.go +++ b/sign/sign_test.go @@ -90,7 +90,6 @@ r: 8 p: 1 ` - // #1. Create new key pair, and read them back. func Test0(t *testing.T) { assert := newAsserter(t) diff --git a/version b/version index 0ea3a94..0c62199 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.2.0 +0.2.1