From c9f4400e6dc71a36df5dc9725f52a8968f5f9803 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sat, 10 Dec 2016 11:50:16 +0100 Subject: [PATCH] Replace all calls to naked panic() with log.Panic() We want all panics to show up in the syslog. --- internal/contentenc/content.go | 5 ++-- internal/contentenc/file_header.go | 3 +- internal/cryptocore/cryptocore.go | 13 ++++---- internal/cryptocore/nonce.go | 5 ++-- .../fusefrontend_reverse/reverse_longnames.go | 3 +- internal/fusefrontend_reverse/rfs.go | 3 +- internal/nametransform/pad16.go | 3 +- internal/siv_aead/siv_aead.go | 7 +++-- internal/stupidgcm/stupidgcm.go | 30 +++++++++---------- internal/stupidgcm/stupidgcm_test.go | 3 +- internal/stupidgcm/without_openssl.go | 11 +++---- 11 files changed, 48 insertions(+), 38 deletions(-) diff --git a/internal/contentenc/content.go b/internal/contentenc/content.go index c42a844..322163a 100644 --- a/internal/contentenc/content.go +++ b/internal/contentenc/content.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "encoding/hex" "errors" + "log" "github.com/rfjakob/gocryptfs/internal/cryptocore" "github.com/rfjakob/gocryptfs/internal/tlog" @@ -152,7 +153,7 @@ func (be *ContentEnc) EncryptBlock(plaintext []byte, blockNo uint64, fileID []by // This function can only be used in SIV mode. func (be *ContentEnc) EncryptBlockNonce(plaintext []byte, blockNo uint64, fileID []byte, nonce []byte) []byte { if be.cryptoCore.AEADBackend != cryptocore.BackendAESSIV { - panic("deterministic nonces are only secure in SIV mode") + log.Panic("deterministic nonces are only secure in SIV mode") } return be.doEncryptBlock(plaintext, blockNo, fileID, nonce) } @@ -166,7 +167,7 @@ func (be *ContentEnc) doEncryptBlock(plaintext []byte, blockNo uint64, fileID [] return plaintext } if len(nonce) != be.cryptoCore.IVLen { - panic("wrong nonce length") + log.Panic("wrong nonce length") } // Authenticate block with block number and file ID diff --git a/internal/contentenc/file_header.go b/internal/contentenc/file_header.go index 9ae5ae9..5c05818 100644 --- a/internal/contentenc/file_header.go +++ b/internal/contentenc/file_header.go @@ -7,6 +7,7 @@ package contentenc import ( "encoding/binary" "fmt" + "log" "github.com/rfjakob/gocryptfs/internal/cryptocore" ) @@ -30,7 +31,7 @@ type FileHeader struct { // Pack - serialize fileHeader object func (h *FileHeader) Pack() []byte { if len(h.ID) != headerIDLen || h.Version != CurrentVersion { - panic("FileHeader object not properly initialized") + log.Panic("FileHeader object not properly initialized") } buf := make([]byte, HeaderLen) binary.BigEndian.PutUint16(buf[0:headerVersionLen], h.Version) diff --git a/internal/cryptocore/cryptocore.go b/internal/cryptocore/cryptocore.go index db82f56..13b278f 100644 --- a/internal/cryptocore/cryptocore.go +++ b/internal/cryptocore/cryptocore.go @@ -7,6 +7,7 @@ import ( "crypto/cipher" "crypto/sha512" "fmt" + "log" "github.com/rfjakob/gocryptfs/internal/siv_aead" "github.com/rfjakob/gocryptfs/internal/stupidgcm" @@ -50,7 +51,7 @@ type CryptoCore struct { // key in gocryptfs.conf. func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore { if len(key) != KeyLen { - panic(fmt.Sprintf("Unsupported key length %d", len(key))) + log.Panic(fmt.Sprintf("Unsupported key length %d", len(key))) } // We want the IV size in bytes IVLen := IVBitLen / 8 @@ -59,14 +60,14 @@ func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore { // Content encryption uses BlockCipher only if useOpenssl=false. blockCipher, err := aes.NewCipher(key) if err != nil { - panic(err) + log.Panic(err) } var aeadCipher cipher.AEAD switch backend { case BackendOpenSSL: if IVLen != 16 { - panic("stupidgcm only supports 128-bit IVs") + log.Panic("stupidgcm only supports 128-bit IVs") } aeadCipher = stupidgcm.New(key) case BackendGoGCM: @@ -74,7 +75,7 @@ func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore { case BackendAESSIV: if IVLen != 16 { // SIV supports any nonce size, but we only use 16. - panic("AES-SIV must use 16-byte nonces") + log.Panic("AES-SIV must use 16-byte nonces") } // AES-SIV uses 1/2 of the key for authentication, 1/2 for // encryption, so we need a 64-bytes key for AES-256. Derive it from @@ -82,10 +83,10 @@ func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore { key64 := sha512.Sum512(key) aeadCipher = siv_aead.New(key64[:]) default: - panic("unknown backend cipher") + log.Panic("unknown backend cipher") } if err != nil { - panic(err) + log.Panic(err) } return &CryptoCore{ diff --git a/internal/cryptocore/nonce.go b/internal/cryptocore/nonce.go index 973d2d8..3f56cc9 100644 --- a/internal/cryptocore/nonce.go +++ b/internal/cryptocore/nonce.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "encoding/hex" "fmt" + "log" "github.com/rfjakob/gocryptfs/internal/tlog" ) @@ -15,7 +16,7 @@ func RandBytes(n int) []byte { b := make([]byte, n) _, err := rand.Read(b) if err != nil { - panic("Failed to read random bytes: " + err.Error()) + log.Panic("Failed to read random bytes: " + err.Error()) } return b } @@ -37,7 +38,7 @@ func (n *nonceGenerator) Get() []byte { tlog.Debug.Printf("nonceGenerator.Get(): %s\n", hex.EncodeToString(nonce)) if bytes.Equal(nonce, n.lastNonce) { m := fmt.Sprintf("Got the same nonce twice: %s. This should never happen!", hex.EncodeToString(nonce)) - panic(m) + log.Panic(m) } n.lastNonce = nonce return nonce diff --git a/internal/fusefrontend_reverse/reverse_longnames.go b/internal/fusefrontend_reverse/reverse_longnames.go index 0234397..24335bd 100644 --- a/internal/fusefrontend_reverse/reverse_longnames.go +++ b/internal/fusefrontend_reverse/reverse_longnames.go @@ -1,6 +1,7 @@ package fusefrontend_reverse import ( + "log" "os" "path/filepath" "sync" @@ -65,7 +66,7 @@ func (rfs *ReverseFS) findLongnameParent(dir string, dirIV []byte, longname stri } cName := rfs.nameTransform.EncryptName(plaintextName, dirIV) if len(cName) <= syscall.NAME_MAX { - panic("logic error or wrong shortNameMax constant?") + log.Panic("logic error or wrong shortNameMax constant?") } hName := nametransform.HashLongName(cName) longnameParentCache[hName] = plaintextName diff --git a/internal/fusefrontend_reverse/rfs.go b/internal/fusefrontend_reverse/rfs.go index aca4b90..4a2cc06 100644 --- a/internal/fusefrontend_reverse/rfs.go +++ b/internal/fusefrontend_reverse/rfs.go @@ -3,6 +3,7 @@ package fusefrontend_reverse import ( "encoding/base64" "fmt" + "log" "os" "path/filepath" "sync" @@ -53,7 +54,7 @@ var _ pathfs.FileSystem = &ReverseFS{} // ReverseFS provides an encrypted view. func NewFS(args fusefrontend.Args) *ReverseFS { if args.CryptoBackend != cryptocore.BackendAESSIV { - panic("reverse mode must use AES-SIV, everything else is insecure") + log.Panic("reverse mode must use AES-SIV, everything else is insecure") } initLongnameCache() cryptoCore := cryptocore.New(args.Masterkey, args.CryptoBackend, contentenc.DefaultIVBits) diff --git a/internal/nametransform/pad16.go b/internal/nametransform/pad16.go index e512828..2b30bcb 100644 --- a/internal/nametransform/pad16.go +++ b/internal/nametransform/pad16.go @@ -4,6 +4,7 @@ import ( "crypto/aes" "errors" "fmt" + "log" ) // pad16 - pad data to AES block size (=16 byte) using standard PKCS#7 padding @@ -11,7 +12,7 @@ import ( func pad16(orig []byte) (padded []byte) { oldLen := len(orig) if oldLen == 0 { - panic("Padding zero-length string makes no sense") + log.Panic("Padding zero-length string makes no sense") } padLen := aes.BlockSize - oldLen%aes.BlockSize if padLen == 0 { diff --git a/internal/siv_aead/siv_aead.go b/internal/siv_aead/siv_aead.go index c68ecdb..6cfa937 100644 --- a/internal/siv_aead/siv_aead.go +++ b/internal/siv_aead/siv_aead.go @@ -4,6 +4,7 @@ package siv_aead import ( "crypto/cipher" + "log" "github.com/jacobsa/crypto/siv" ) @@ -34,7 +35,7 @@ func (s *sivAead) Overhead() int { func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte { if len(nonce) != 16 { // SIV supports any nonce size, but in gocryptfs we exclusively use 16. - panic("nonce must be 16 bytes long") + log.Panic("nonce must be 16 bytes long") } // https://github.com/jacobsa/crypto/blob/master/siv/encrypt.go#L48: // As per RFC 5297 section 3, you may use this function for nonce-based @@ -43,7 +44,7 @@ func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte { associated := [][]byte{authData, nonce} out, err := siv.Encrypt(dst, s.key, plaintext, associated) if err != nil { - panic(err) + log.Panic(err) } return out } @@ -52,7 +53,7 @@ func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte { func (s *sivAead) Open(dst, nonce, ciphertext, authData []byte) ([]byte, error) { if len(nonce) != 16 { // SIV supports any nonce size, but in gocryptfs we exclusively use 16. - panic("nonce must be 16 bytes long") + log.Panic("nonce must be 16 bytes long") } associated := [][]byte{authData, nonce} dec, err := siv.Decrypt(s.key, ciphertext, associated) diff --git a/internal/stupidgcm/stupidgcm.go b/internal/stupidgcm/stupidgcm.go index 6e551f5..a1a5a14 100644 --- a/internal/stupidgcm/stupidgcm.go +++ b/internal/stupidgcm/stupidgcm.go @@ -62,28 +62,28 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte { // Create scratch space "context" ctx := C.EVP_CIPHER_CTX_new() if ctx == nil { - panic("EVP_CIPHER_CTX_new failed") + log.Panic("EVP_CIPHER_CTX_new failed") } // Set cipher to AES-256 if C.EVP_EncryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 { - panic("EVP_EncryptInit_ex I failed") + log.Panic("EVP_EncryptInit_ex I failed") } // Use 16-byte IV if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 { - panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed") + log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed") } // Set key and IV if C.EVP_EncryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 { - panic("EVP_EncryptInit_ex II failed") + log.Panic("EVP_EncryptInit_ex II failed") } // Provide authentication data var resultLen C.int if C.EVP_EncryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 { - panic("EVP_EncryptUpdate authData failed") + log.Panic("EVP_EncryptUpdate authData failed") } if int(resultLen) != len(authData) { log.Panicf("Unexpected length %d", resultLen) @@ -91,7 +91,7 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte { // Encrypt "in" into "buf" if C.EVP_EncryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&in[0]), C.int(len(in))) != 1 { - panic("EVP_EncryptUpdate failed") + log.Panic("EVP_EncryptUpdate failed") } if int(resultLen) != len(in) { log.Panicf("Unexpected length %d", resultLen) @@ -101,7 +101,7 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte { // Because GCM is a stream encryption, this will not write out any data. dummy := make([]byte, 16) if C.EVP_EncryptFinal_ex(ctx, (*C.uchar)(&dummy[0]), &resultLen) != 1 { - panic("EVP_EncryptFinal_ex failed") + log.Panic("EVP_EncryptFinal_ex failed") } if resultLen != 0 { log.Panicf("Unexpected length %d", resultLen) @@ -109,7 +109,7 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte { // Get GMAC tag and append it to the ciphertext in "buf" if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_GET_TAG, tagLen, (unsafe.Pointer)(&buf[len(in)])) != 1 { - panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_GET_TAG failed") + log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_GET_TAG failed") } // Free scratch space @@ -135,33 +135,33 @@ func (g stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) { // Create scratch space "context" ctx := C.EVP_CIPHER_CTX_new() if ctx == nil { - panic("EVP_CIPHER_CTX_new failed") + log.Panic("EVP_CIPHER_CTX_new failed") } // Set cipher to AES-256 if C.EVP_DecryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 { - panic("EVP_DecryptInit_ex I failed") + log.Panic("EVP_DecryptInit_ex I failed") } // Use 16-byte IV if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 { - panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed") + log.Panic("EVP_CIPHER_CTX_ctrl EVP_CTRL_GCM_SET_IVLEN failed") } // Set key and IV if C.EVP_DecryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 { - panic("EVP_DecryptInit_ex II failed") + log.Panic("EVP_DecryptInit_ex II failed") } // Set expected GMAC tag if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_TAG, tagLen, (unsafe.Pointer)(&tag[0])) != 1 { - panic("EVP_CIPHER_CTX_ctrl failed") + log.Panic("EVP_CIPHER_CTX_ctrl failed") } // Provide authentication data var resultLen C.int if C.EVP_DecryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 { - panic("EVP_DecryptUpdate authData failed") + log.Panic("EVP_DecryptUpdate authData failed") } if int(resultLen) != len(authData) { log.Panicf("Unexpected length %d", resultLen) @@ -169,7 +169,7 @@ func (g stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) { // Decrypt "ciphertext" into "buf" if C.EVP_DecryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&ciphertext[0]), C.int(len(ciphertext))) != 1 { - panic("EVP_DecryptUpdate failed") + log.Panic("EVP_DecryptUpdate failed") } if int(resultLen) != len(ciphertext) { log.Panicf("Unexpected length %d", resultLen) diff --git a/internal/stupidgcm/stupidgcm_test.go b/internal/stupidgcm/stupidgcm_test.go index 3c11dfe..d6c0714 100644 --- a/internal/stupidgcm/stupidgcm_test.go +++ b/internal/stupidgcm/stupidgcm_test.go @@ -13,6 +13,7 @@ import ( "crypto/cipher" "crypto/rand" "encoding/hex" + "log" "testing" // For benchmark comparison @@ -24,7 +25,7 @@ func randBytes(n int) []byte { b := make([]byte, n) _, err := rand.Read(b) if err != nil { - panic("Failed to read random bytes: " + err.Error()) + log.Panic("Failed to read random bytes: " + err.Error()) } return b } diff --git a/internal/stupidgcm/without_openssl.go b/internal/stupidgcm/without_openssl.go index 91dd33c..9fb245e 100644 --- a/internal/stupidgcm/without_openssl.go +++ b/internal/stupidgcm/without_openssl.go @@ -3,6 +3,7 @@ package stupidgcm import ( + "log" "os" "github.com/rfjakob/gocryptfs/internal/tlog" @@ -24,25 +25,25 @@ func New(_ []byte) stupidGCM { errExit() // This panic is never reached, but having it here stops the Go compiler // from complaining about the missing return code. - panic("") + log.Panic("") } func (g stupidGCM) NonceSize() int { errExit() - panic("") + log.Panic("") } func (g stupidGCM) Overhead() int { errExit() - panic("") + log.Panic("") } func (g stupidGCM) Seal(_, _, _, _ []byte) []byte { errExit() - panic("") + log.Panic("") } func (g stupidGCM) Open(_, _, _, _ []byte) ([]byte, error) { errExit() - panic("") + log.Panic("") }