Skip to content

Commit

Permalink
standardising error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed May 15, 2024
1 parent 649fbb0 commit 5f43d42
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 55 deletions.
2 changes: 1 addition & 1 deletion app/internal/commands/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
pwdLength = 10
)

var errInvalidPassword = fmt.Errorf("xipher: please set a decent password with at least %d characters, including at least one uppercase letter, one lowercase letter, one number, and one of the following special characters: %s", pwdLength, pwdSpecialChars)
var errInvalidPassword = fmt.Errorf("%s: please set a decent password with at least %d characters, including at least one uppercase letter, one lowercase letter, one number, and one of the following special characters: %s", "xipher", pwdLength, pwdSpecialChars)

func pwdCheck(password string) error {
var (
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.2

require (
github.com/cloudflare/circl v1.3.8
github.com/fatih/color v1.16.0
github.com/fatih/color v1.17.0
github.com/spf13/cobra v1.8.0
golang.org/x/crypto v0.23.0
golang.org/x/term v0.20.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/cloudflare/circl v1.3.8 h1:j+V8jJt09PoeMFIu2uh5JUyEaIHTXVOHslFoLNAKqwI=
github.com/cloudflare/circl v1.3.8/go.mod h1:PDRU+oXvdD7KCtgKxW95M5Z8BpSCJXQORiZFnBQS5QU=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
8 changes: 4 additions & 4 deletions internal/asx/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (
)

var (
errInvalidPrivateKeyLength = fmt.Errorf("xipher: invalid private key lengths [please use %d bytes]", PrivateKeyLength)
errInvalidPublicKeyLength = fmt.Errorf("xipher: invalid public key lengths [please use a minimum of %d bytes]", MinPublicKeyLength)
errInvalidPublicKey = fmt.Errorf("xipher: invalid public key")
errInvalidAlgorithm = fmt.Errorf("xipher: invalid algorithm")
errInvalidPrivateKeyLength = fmt.Errorf("%s: invalid private key lengths [please use %d bytes]", "xipher", PrivateKeyLength)
errInvalidPublicKeyLength = fmt.Errorf("%s: invalid public key lengths [please use a minimum of %d bytes]", "xipher", MinPublicKeyLength)
errInvalidPublicKey = fmt.Errorf("%s: invalid public key", "xipher")
errInvalidAlgorithm = fmt.Errorf("%s: invalid algorithm", "xipher")
)
7 changes: 4 additions & 3 deletions internal/asx/crypto.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package asx

import (
"fmt"
"io"
)

// NewEncryptingWriter returns a new WriteCloser that encrypts data with the public key and writes to dst.
func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool) (io.WriteCloser, error) {
if publicKey.ePub != nil {
if _, err := dst.Write([]byte{AlgoECC}); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write algorithm: %w", "xipher", err)
}
return publicKey.ePub.NewEncryptingWriter(dst, compression)
} else if publicKey.kPub != nil {
if _, err := dst.Write([]byte{AlgoKyber}); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write algorithm: %w", "xipher", err)
}
return publicKey.kPub.NewEncryptingWriter(dst, compression)
} else {
Expand All @@ -25,7 +26,7 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
algoBytes := make([]byte, 1)
if _, err := io.ReadFull(src, algoBytes); err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to read algorithm: %w", "xipher", err)
}
var algo uint8 = algoBytes[0]
if algo == AlgoECC {
Expand Down
3 changes: 2 additions & 1 deletion internal/asx/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asx
import (
"crypto/rand"
"crypto/sha256"
"fmt"

"dev.shib.me/xipher/internal/ecc"
"dev.shib.me/xipher/internal/kyb"
Expand Down Expand Up @@ -32,7 +33,7 @@ func (privateKey *PrivateKey) Bytes() []byte {
func NewPrivateKey() (*PrivateKey, error) {
key := make([]byte, PrivateKeyLength)
if _, err := rand.Read(key); err != nil {
return nil, err
return nil, fmt.Errorf("%s: failed to generate private key: %w", "xipher", err)
}
return ParsePrivateKey(key)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/ecc/crypto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ecc

import (
"fmt"
"io"

"dev.shib.me/xipher/internal/xcp"
Expand All @@ -14,7 +15,7 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
return nil, err
}
if _, err = dst.Write(encrypter.ephPubKey); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write ephemeral public key: %w", "xipher", err)
}
return (*encrypter.cipher).NewEncryptingWriter(dst, compression)
}
Expand All @@ -23,11 +24,11 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
ephPubKey := make([]byte, KeyLength)
if _, err := io.ReadFull(src, ephPubKey); err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to read ephemeral public key: %w", "xipher", err)
}
sharedKey, err := curve25519.X25519(*privateKey.key, ephPubKey)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to generate shared key: %w", "xipher", err)
}
decrypter, err := xcp.New(sharedKey)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions internal/ecc/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// KeyLength is the length of the ECC key.
const KeyLength = curve25519.ScalarSize

var errInvalidKeyLength = fmt.Errorf("xipher: invalid key lengths [please use %d bytes]", KeyLength)
var errInvalidKeyLength = fmt.Errorf("%s: invalid key lengths [please use %d bytes]", "xipher", KeyLength)

// PrivateKey represents a private key.
type PrivateKey struct {
Expand Down Expand Up @@ -39,7 +39,7 @@ func (privateKey *PrivateKey) Bytes() []byte {
func NewPrivateKey() (*PrivateKey, error) {
key := make([]byte, KeyLength)
if _, err := rand.Read(key); err != nil {
return nil, err
return nil, fmt.Errorf("%s: failed to generate private key: %w", "xipher", err)
}
return ParsePrivateKey(key)
}
Expand All @@ -59,7 +59,7 @@ func (privateKey *PrivateKey) PublicKey() (*PublicKey, error) {
if privateKey.publicKey == nil {
key, err := curve25519.X25519(*privateKey.key, curve25519.Basepoint)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: failed to generate public key: %w", "xipher", err)
}
privateKey.publicKey = &PublicKey{
key: &key,
Expand Down Expand Up @@ -87,15 +87,15 @@ func (publicKey *PublicKey) getEncrypter() (*encrypter, error) {
if publicKey.encrypter == nil {
ephPrivKey := make([]byte, KeyLength)
if _, err := rand.Read(ephPrivKey); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to generate ephemeral private key: %w", "xipher", err)
}
ephPubKey, err := curve25519.X25519(ephPrivKey, curve25519.Basepoint)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to generate ephemeral public key: %w", "xipher", err)
}
sharedKey, err := curve25519.X25519(ephPrivKey, *publicKey.key)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to generate shared key: %w", "xipher", err)
}
cipher, err := xcp.New(sharedKey)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/kyb/cypto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kyb

import (
"fmt"
"io"

"dev.shib.me/xipher/internal/xcp"
Expand All @@ -14,7 +15,7 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
return nil, err
}
if _, err = dst.Write(encrypter.keyEnc); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write encapsulated key: %w", "xipher", err)
}
return (*encrypter.cipher).NewEncryptingWriter(dst, compression)
}
Expand All @@ -23,11 +24,11 @@ func (publicKey *PublicKey) NewEncryptingWriter(dst io.Writer, compression bool)
func (privateKey *PrivateKey) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
keyEnc := make([]byte, ctLength)
if _, err := io.ReadFull(src, keyEnc); err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to read encapsulated key: %w", "xipher", err)
}
sharedKey, err := kyber1024.Scheme().Decapsulate(privateKey.sk, keyEnc)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to generate shared key: %w", "xipher", err)
}
decrypter, err := xcp.New(sharedKey)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/kyb/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (
)

var (
errInvalidPrivateKeyLength = fmt.Errorf("xipher: invalid private key lengths [please use %d bytes]", PrivateKeyLength)
errInvalidPublicKeyLength = fmt.Errorf("xipher: invalid public key lengths [please use %d bytes]", PublicKeyLength)
errInvalidPrivateKeyLength = fmt.Errorf("%s: invalid private key lengths [please use %d bytes]", "xipher", PrivateKeyLength)
errInvalidPublicKeyLength = fmt.Errorf("%s: invalid public key lengths [please use %d bytes]", "xipher", PublicKeyLength)
)

// PrivateKey represents a private key.
Expand Down Expand Up @@ -48,7 +48,7 @@ func (privateKey *PrivateKey) Bytes() []byte {
func NewPrivateKey() (*PrivateKey, error) {
key := make([]byte, PrivateKeyLength)
if _, err := rand.Read(key); err != nil {
return nil, err
return nil, fmt.Errorf("%s: failed to generate private key: %w", "xipher", err)
}
return NewPrivateKeyForSeed(key)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (publicKey *PublicKey) getEncrypter() (*encrypter, error) {
if publicKey.encrypter == nil {
keyEnc, sharedKey, err := kyber1024.Scheme().Encapsulate(publicKey.pk)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: failed to encapsulate shared key: %w", "xipher", err)
}
cipher, err := xcp.New(sharedKey)
if err != nil {
Expand Down
27 changes: 14 additions & 13 deletions internal/xcp/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/zlib"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
)

Expand All @@ -20,10 +21,10 @@ type Writer struct {
func (cipher *SymmetricCipher) NewEncryptingWriter(dst io.Writer, compress bool) (io.WriteCloser, error) {
nonce := make([]byte, nonceLength)
if _, err := rand.Read(nonce); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to generate nonce: %w", "xipher", err)
}
if _, err := dst.Write(nonce); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write nonce: %w", "xipher", err)
}
return cipher.newWriter(nonce, dst, compress)
}
Expand All @@ -37,16 +38,16 @@ func (cipher *SymmetricCipher) newWriter(nonce []byte, dst io.Writer, compress b
}
if compress {
if _, err := dst.Write([]byte{1}); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write compress flag: %w", "xipher", err)
}
zWriter, err := zlib.NewWriterLevel(&ciphWriter.buf, zlib.BestCompression)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to create compressed writer: %w", "xipher", err)
}
ciphWriter.zWriter = zWriter
} else {
if _, err := dst.Write([]byte{0}); err != nil {
return nil, err
return nil, fmt.Errorf("%s: encrypter failed to write compression flag: %w", "xipher", err)
}
}
return ciphWriter, nil
Expand All @@ -59,7 +60,7 @@ func (w *Writer) Write(p []byte) (n int, err error) {
n, err = w.zWriter.Write(p)
}
if err != nil {
return n, err
return n, fmt.Errorf("%s: encrypter failed to write: %w", "xipher", err)
}
return n, w.flush(ptBlockSize)
}
Expand All @@ -69,7 +70,7 @@ func (w *Writer) flush(minBufSize int) error {
block := w.buf.Next(ptBlockSize)
ct := w.aead.Seal(nil, w.nonce, block, nil)
if _, err := w.dst.Write(ct); err != nil {
return err
return fmt.Errorf("%s: encrypter failed to write: %w", "xipher", err)
}
}
return nil
Expand All @@ -79,7 +80,7 @@ func (w *Writer) flush(minBufSize int) error {
func (w *Writer) Close() error {
if w.zWriter != nil {
if err := w.zWriter.Close(); err != nil {
return err
return fmt.Errorf("%s: encrypter failed to close compressed writer: %w", "xipher", err)
}
}
return w.flush(1)
Expand All @@ -96,7 +97,7 @@ type Reader struct {
func (cipher *SymmetricCipher) NewDecryptingReader(src io.Reader) (io.ReadCloser, error) {
nonce := make([]byte, nonceLength)
if _, err := io.ReadFull(src, nonce); err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to read nonce: %w", "xipher", err)
}
return cipher.newReader(nonce, src)
}
Expand All @@ -110,14 +111,14 @@ func (cipher *SymmetricCipher) newReader(nonce []byte, src io.Reader) (io.ReadCl
}
compressFlag := make([]byte, 1)
if _, err := io.ReadFull(src, compressFlag); err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to read compress flag: %w", "xipher", err)
}
if compressFlag[0] == 0 {
return io.NopCloser(ciphReader), nil
}
zReader, err := zlib.NewReader(ciphReader)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: decrypter failed to create compressed reader: %w", "xipher", err)
}
return zReader, nil
}
Expand All @@ -131,13 +132,13 @@ func (r *Reader) Read(p []byte) (int, error) {
if err == nil || err == io.ErrUnexpectedEOF {
pt, err := r.aead.Open(nil, r.nonce, block[:n], nil)
if err != nil {
return 0, err
return 0, fmt.Errorf("%s: decrypter failed to decrypt: %w", "xipher", err)
}
r.buf.Write(pt)
return r.buf.Read(p)
} else if err == io.EOF {
return r.buf.Read(p)
} else {
return 0, err
return 0, fmt.Errorf("%s: decrypter failed to read: %w", "xipher", err)
}
}
3 changes: 2 additions & 1 deletion internal/xcp/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xcp

import (
"crypto/cipher"
"fmt"

"golang.org/x/crypto/chacha20poly1305"
)
Expand All @@ -23,7 +24,7 @@ type SymmetricCipher struct {
func New(key []byte) (*SymmetricCipher, error) {
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
return nil, fmt.Errorf("%s: failed to create new symmetric cipher: %w", "xipher", err)
}
return &SymmetricCipher{
aead: &aead,
Expand Down
Loading

0 comments on commit 5f43d42

Please sign in to comment.