Replace all calls to naked panic() with log.Panic()

We want all panics to show up in the syslog.
This commit is contained in:
Jakob Unterwurzacher 2016-12-10 11:50:16 +01:00
parent 6c86afb5cd
commit c9f4400e6d
11 changed files with 48 additions and 38 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"errors" "errors"
"log"
"github.com/rfjakob/gocryptfs/internal/cryptocore" "github.com/rfjakob/gocryptfs/internal/cryptocore"
"github.com/rfjakob/gocryptfs/internal/tlog" "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. // This function can only be used in SIV mode.
func (be *ContentEnc) EncryptBlockNonce(plaintext []byte, blockNo uint64, fileID []byte, nonce []byte) []byte { func (be *ContentEnc) EncryptBlockNonce(plaintext []byte, blockNo uint64, fileID []byte, nonce []byte) []byte {
if be.cryptoCore.AEADBackend != cryptocore.BackendAESSIV { 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) return be.doEncryptBlock(plaintext, blockNo, fileID, nonce)
} }
@ -166,7 +167,7 @@ func (be *ContentEnc) doEncryptBlock(plaintext []byte, blockNo uint64, fileID []
return plaintext return plaintext
} }
if len(nonce) != be.cryptoCore.IVLen { if len(nonce) != be.cryptoCore.IVLen {
panic("wrong nonce length") log.Panic("wrong nonce length")
} }
// Authenticate block with block number and file ID // Authenticate block with block number and file ID

View File

@ -7,6 +7,7 @@ package contentenc
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"log"
"github.com/rfjakob/gocryptfs/internal/cryptocore" "github.com/rfjakob/gocryptfs/internal/cryptocore"
) )
@ -30,7 +31,7 @@ type FileHeader struct {
// Pack - serialize fileHeader object // Pack - serialize fileHeader object
func (h *FileHeader) Pack() []byte { func (h *FileHeader) Pack() []byte {
if len(h.ID) != headerIDLen || h.Version != CurrentVersion { 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) buf := make([]byte, HeaderLen)
binary.BigEndian.PutUint16(buf[0:headerVersionLen], h.Version) binary.BigEndian.PutUint16(buf[0:headerVersionLen], h.Version)

View File

@ -7,6 +7,7 @@ import (
"crypto/cipher" "crypto/cipher"
"crypto/sha512" "crypto/sha512"
"fmt" "fmt"
"log"
"github.com/rfjakob/gocryptfs/internal/siv_aead" "github.com/rfjakob/gocryptfs/internal/siv_aead"
"github.com/rfjakob/gocryptfs/internal/stupidgcm" "github.com/rfjakob/gocryptfs/internal/stupidgcm"
@ -50,7 +51,7 @@ type CryptoCore struct {
// key in gocryptfs.conf. // key in gocryptfs.conf.
func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore { func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore {
if len(key) != KeyLen { 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 // We want the IV size in bytes
IVLen := IVBitLen / 8 IVLen := IVBitLen / 8
@ -59,14 +60,14 @@ func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore {
// Content encryption uses BlockCipher only if useOpenssl=false. // Content encryption uses BlockCipher only if useOpenssl=false.
blockCipher, err := aes.NewCipher(key) blockCipher, err := aes.NewCipher(key)
if err != nil { if err != nil {
panic(err) log.Panic(err)
} }
var aeadCipher cipher.AEAD var aeadCipher cipher.AEAD
switch backend { switch backend {
case BackendOpenSSL: case BackendOpenSSL:
if IVLen != 16 { if IVLen != 16 {
panic("stupidgcm only supports 128-bit IVs") log.Panic("stupidgcm only supports 128-bit IVs")
} }
aeadCipher = stupidgcm.New(key) aeadCipher = stupidgcm.New(key)
case BackendGoGCM: case BackendGoGCM:
@ -74,7 +75,7 @@ func New(key []byte, backend BackendTypeEnum, IVBitLen int) *CryptoCore {
case BackendAESSIV: case BackendAESSIV:
if IVLen != 16 { if IVLen != 16 {
// SIV supports any nonce size, but we only use 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 // 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 // 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) key64 := sha512.Sum512(key)
aeadCipher = siv_aead.New(key64[:]) aeadCipher = siv_aead.New(key64[:])
default: default:
panic("unknown backend cipher") log.Panic("unknown backend cipher")
} }
if err != nil { if err != nil {
panic(err) log.Panic(err)
} }
return &CryptoCore{ return &CryptoCore{

View File

@ -6,6 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log"
"github.com/rfjakob/gocryptfs/internal/tlog" "github.com/rfjakob/gocryptfs/internal/tlog"
) )
@ -15,7 +16,7 @@ func RandBytes(n int) []byte {
b := make([]byte, n) b := make([]byte, n)
_, err := rand.Read(b) _, err := rand.Read(b)
if err != nil { if err != nil {
panic("Failed to read random bytes: " + err.Error()) log.Panic("Failed to read random bytes: " + err.Error())
} }
return b return b
} }
@ -37,7 +38,7 @@ func (n *nonceGenerator) Get() []byte {
tlog.Debug.Printf("nonceGenerator.Get(): %s\n", hex.EncodeToString(nonce)) tlog.Debug.Printf("nonceGenerator.Get(): %s\n", hex.EncodeToString(nonce))
if bytes.Equal(nonce, n.lastNonce) { if bytes.Equal(nonce, n.lastNonce) {
m := fmt.Sprintf("Got the same nonce twice: %s. This should never happen!", hex.EncodeToString(nonce)) m := fmt.Sprintf("Got the same nonce twice: %s. This should never happen!", hex.EncodeToString(nonce))
panic(m) log.Panic(m)
} }
n.lastNonce = nonce n.lastNonce = nonce
return nonce return nonce

View File

@ -1,6 +1,7 @@
package fusefrontend_reverse package fusefrontend_reverse
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
@ -65,7 +66,7 @@ func (rfs *ReverseFS) findLongnameParent(dir string, dirIV []byte, longname stri
} }
cName := rfs.nameTransform.EncryptName(plaintextName, dirIV) cName := rfs.nameTransform.EncryptName(plaintextName, dirIV)
if len(cName) <= syscall.NAME_MAX { 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) hName := nametransform.HashLongName(cName)
longnameParentCache[hName] = plaintextName longnameParentCache[hName] = plaintextName

View File

@ -3,6 +3,7 @@ package fusefrontend_reverse
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
@ -53,7 +54,7 @@ var _ pathfs.FileSystem = &ReverseFS{}
// ReverseFS provides an encrypted view. // ReverseFS provides an encrypted view.
func NewFS(args fusefrontend.Args) *ReverseFS { func NewFS(args fusefrontend.Args) *ReverseFS {
if args.CryptoBackend != cryptocore.BackendAESSIV { 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() initLongnameCache()
cryptoCore := cryptocore.New(args.Masterkey, args.CryptoBackend, contentenc.DefaultIVBits) cryptoCore := cryptocore.New(args.Masterkey, args.CryptoBackend, contentenc.DefaultIVBits)

View File

@ -4,6 +4,7 @@ import (
"crypto/aes" "crypto/aes"
"errors" "errors"
"fmt" "fmt"
"log"
) )
// pad16 - pad data to AES block size (=16 byte) using standard PKCS#7 padding // 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) { func pad16(orig []byte) (padded []byte) {
oldLen := len(orig) oldLen := len(orig)
if oldLen == 0 { 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 padLen := aes.BlockSize - oldLen%aes.BlockSize
if padLen == 0 { if padLen == 0 {

View File

@ -4,6 +4,7 @@ package siv_aead
import ( import (
"crypto/cipher" "crypto/cipher"
"log"
"github.com/jacobsa/crypto/siv" "github.com/jacobsa/crypto/siv"
) )
@ -34,7 +35,7 @@ func (s *sivAead) Overhead() int {
func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte { func (s *sivAead) Seal(dst, nonce, plaintext, authData []byte) []byte {
if len(nonce) != 16 { if len(nonce) != 16 {
// SIV supports any nonce size, but in gocryptfs we exclusively use 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: // 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 // 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} associated := [][]byte{authData, nonce}
out, err := siv.Encrypt(dst, s.key, plaintext, associated) out, err := siv.Encrypt(dst, s.key, plaintext, associated)
if err != nil { if err != nil {
panic(err) log.Panic(err)
} }
return out 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) { func (s *sivAead) Open(dst, nonce, ciphertext, authData []byte) ([]byte, error) {
if len(nonce) != 16 { if len(nonce) != 16 {
// SIV supports any nonce size, but in gocryptfs we exclusively use 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} associated := [][]byte{authData, nonce}
dec, err := siv.Decrypt(s.key, ciphertext, associated) dec, err := siv.Decrypt(s.key, ciphertext, associated)

View File

@ -62,28 +62,28 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte {
// Create scratch space "context" // Create scratch space "context"
ctx := C.EVP_CIPHER_CTX_new() ctx := C.EVP_CIPHER_CTX_new()
if ctx == nil { if ctx == nil {
panic("EVP_CIPHER_CTX_new failed") log.Panic("EVP_CIPHER_CTX_new failed")
} }
// Set cipher to AES-256 // Set cipher to AES-256
if C.EVP_EncryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 { 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 // Use 16-byte IV
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 { 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 // Set key and IV
if C.EVP_EncryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 { 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 // Provide authentication data
var resultLen C.int var resultLen C.int
if C.EVP_EncryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 { 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) { if int(resultLen) != len(authData) {
log.Panicf("Unexpected length %d", resultLen) log.Panicf("Unexpected length %d", resultLen)
@ -91,7 +91,7 @@ func (g stupidGCM) Seal(dst, iv, in, authData []byte) []byte {
// Encrypt "in" into "buf" // Encrypt "in" into "buf"
if C.EVP_EncryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&in[0]), C.int(len(in))) != 1 { 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) { if int(resultLen) != len(in) {
log.Panicf("Unexpected length %d", resultLen) 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. // Because GCM is a stream encryption, this will not write out any data.
dummy := make([]byte, 16) dummy := make([]byte, 16)
if C.EVP_EncryptFinal_ex(ctx, (*C.uchar)(&dummy[0]), &resultLen) != 1 { 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 { if resultLen != 0 {
log.Panicf("Unexpected length %d", resultLen) 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" // 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 { 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 // Free scratch space
@ -135,33 +135,33 @@ func (g stupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
// Create scratch space "context" // Create scratch space "context"
ctx := C.EVP_CIPHER_CTX_new() ctx := C.EVP_CIPHER_CTX_new()
if ctx == nil { if ctx == nil {
panic("EVP_CIPHER_CTX_new failed") log.Panic("EVP_CIPHER_CTX_new failed")
} }
// Set cipher to AES-256 // Set cipher to AES-256
if C.EVP_DecryptInit_ex(ctx, C.EVP_aes_256_gcm(), nil, nil, nil) != 1 { 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 // Use 16-byte IV
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_IVLEN, ivLen, nil) != 1 { 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 // Set key and IV
if C.EVP_DecryptInit_ex(ctx, nil, nil, (*C.uchar)(&g.key[0]), (*C.uchar)(&iv[0])) != 1 { 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 // Set expected GMAC tag
if C.EVP_CIPHER_CTX_ctrl(ctx, C.EVP_CTRL_GCM_SET_TAG, tagLen, (unsafe.Pointer)(&tag[0])) != 1 { 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 // Provide authentication data
var resultLen C.int var resultLen C.int
if C.EVP_DecryptUpdate(ctx, nil, &resultLen, (*C.uchar)(&authData[0]), C.int(len(authData))) != 1 { 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) { if int(resultLen) != len(authData) {
log.Panicf("Unexpected length %d", resultLen) 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" // Decrypt "ciphertext" into "buf"
if C.EVP_DecryptUpdate(ctx, (*C.uchar)(&buf[0]), &resultLen, (*C.uchar)(&ciphertext[0]), C.int(len(ciphertext))) != 1 { 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) { if int(resultLen) != len(ciphertext) {
log.Panicf("Unexpected length %d", resultLen) log.Panicf("Unexpected length %d", resultLen)

View File

@ -13,6 +13,7 @@ import (
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"log"
"testing" "testing"
// For benchmark comparison // For benchmark comparison
@ -24,7 +25,7 @@ func randBytes(n int) []byte {
b := make([]byte, n) b := make([]byte, n)
_, err := rand.Read(b) _, err := rand.Read(b)
if err != nil { if err != nil {
panic("Failed to read random bytes: " + err.Error()) log.Panic("Failed to read random bytes: " + err.Error())
} }
return b return b
} }

View File

@ -3,6 +3,7 @@
package stupidgcm package stupidgcm
import ( import (
"log"
"os" "os"
"github.com/rfjakob/gocryptfs/internal/tlog" "github.com/rfjakob/gocryptfs/internal/tlog"
@ -24,25 +25,25 @@ func New(_ []byte) stupidGCM {
errExit() errExit()
// This panic is never reached, but having it here stops the Go compiler // This panic is never reached, but having it here stops the Go compiler
// from complaining about the missing return code. // from complaining about the missing return code.
panic("") log.Panic("")
} }
func (g stupidGCM) NonceSize() int { func (g stupidGCM) NonceSize() int {
errExit() errExit()
panic("") log.Panic("")
} }
func (g stupidGCM) Overhead() int { func (g stupidGCM) Overhead() int {
errExit() errExit()
panic("") log.Panic("")
} }
func (g stupidGCM) Seal(_, _, _, _ []byte) []byte { func (g stupidGCM) Seal(_, _, _, _ []byte) []byte {
errExit() errExit()
panic("") log.Panic("")
} }
func (g stupidGCM) Open(_, _, _, _ []byte) ([]byte, error) { func (g stupidGCM) Open(_, _, _, _ []byte) ([]byte, error) {
errExit() errExit()
panic("") log.Panic("")
} }