diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/crud/cli_edit.go (renamed from internal/crud/handler_edit.go) | 8 | ||||
| -rw-r--r-- | internal/crud/cli_new.go | 158 | ||||
| -rw-r--r-- | internal/crud/handler_new.go | 57 | ||||
| -rw-r--r-- | internal/crypt/headers.go (renamed from internal/utils/headers.go) | 19 | ||||
| -rw-r--r-- | internal/crypt/mem.go | 16 | ||||
| -rw-r--r-- | internal/crypt/rawDecrypt.go | 5 | ||||
| -rw-r--r-- | internal/crypt/rawEncrypt.go | 9 | ||||
| -rw-r--r-- | internal/utils/utils.go | 9 |
8 files changed, 196 insertions, 85 deletions
diff --git a/internal/crud/handler_edit.go b/internal/crud/cli_edit.go index 0e13be3..5e2cb8d 100644 --- a/internal/crud/handler_edit.go +++ b/internal/crud/cli_edit.go @@ -20,7 +20,7 @@ func EditNote(notePath, editorString string) error { if err != nil { return fmt.Errorf("failed to read passphrase: %v", err) } - utils.TryMlock(password) + crypt.TryMlock(password) defer clear(password) ciphertext, err := os.ReadFile(notePath) @@ -31,7 +31,7 @@ func EditNote(notePath, editorString string) error { plaintext, err := crypt.DecryptRawBytes(ciphertext, password) if err != nil { return err } - utils.TryMlock(plaintext) + crypt.TryMlock(plaintext) defer clear(plaintext) defer deferredCleanTmpFile(tmpFile, len(plaintext)) @@ -46,12 +46,12 @@ func EditNote(notePath, editorString string) error { newContent, err := os.ReadFile(tmpFile.Name()) if err != nil { return err } - utils.TryMlock(newContent) + crypt.TryMlock(newContent) defer clear(newContent) os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) - encrypted, err := crypt.EncryptRawBytes(newContent, password) + encrypted, err := crypt.EncryptRawBytes(newContent, password, crypt.Pass) if err != nil { return err } err = os.WriteFile(notePath, encrypted, noteInfo.Mode().Perm()) diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go new file mode 100644 index 0000000..2b6e507 --- /dev/null +++ b/internal/crud/cli_new.go @@ -0,0 +1,158 @@ +package crud + +import ( + "fmt" + "os" + + "gitlab.com/whiteboardguy/eno/internal/crypt" + "gitlab.com/whiteboardguy/eno/internal/editor" + "gitlab.com/whiteboardguy/eno/internal/utils" +) + +func NewNote(output string) error { + fmt.Printf("Enter the passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(password) + defer clear(password) + + fmt.Printf("Confirm the passphrase: ") + passwordConfirm, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(passwordConfirm) + defer clear(passwordConfirm) + + if string(password) != string(passwordConfirm) { + return fmt.Errorf("passphrases do not match") + } + + encrypted, err := crypt.EncryptRawBytes( + []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), + password, + crypt.Pass, + ) + if err != nil { + return err + } + + fmt.Println(output) + file, err := os.Create(output) + if err != nil { + return err + } + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + + // utils.ClearTerm() + fmt.Println("New note saved at:", file.Name()) + + return nil +} + +func NewNoteEdit(output, editorString string) error { + fmt.Printf("Enter the passphrase: ") + password, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(password) + defer clear(password) + + fmt.Printf("Confirm the passphrase: ") + passwordConfirm, err := utils.TakeSecureUserInput() + if err != nil { + return fmt.Errorf("failed to read passphrase: %v", err) + } + crypt.TryMlock(passwordConfirm) + defer clear(passwordConfirm) + + if string(password) != string(passwordConfirm) { + return fmt.Errorf("passphrases do not match") + } + + noteInfo, err := os.Stat(output) + preExisting := err == nil + + initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + encrypted, err := crypt.EncryptRawBytes(initialContent, password, crypt.Pass) + if err != nil { + return err + } + + file, err := os.Create(output) + if err != nil { + return err + } + + _, err = file.Write(encrypted) + if err != nil { + return err + } + + file.Sync() + file.Close() + + ciphertext, err := os.ReadFile(output) + if err != nil { + return err + } + + tmpFile, err := os.CreateTemp("", "eno-*") + if err != nil { + return err + } + + plaintext, err := crypt.DecryptRawBytes(ciphertext, password) + if err != nil { + return err + } + crypt.TryMlock(plaintext) + defer clear(plaintext) + + defer deferredCleanTmpFile(tmpFile, len(plaintext)) + defer os.Remove(tmpFile.Name()) + + _, err = tmpFile.Write(plaintext) + if err != nil { + return err + } + + err = editor.OpenWithEditor(tmpFile.Name(), editorString) + if err != nil { + return err + } + + newContent, err := os.ReadFile(tmpFile.Name()) + if err != nil { + return err + } + crypt.TryMlock(newContent) + defer clear(newContent) + + os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + + encrypted, err = crypt.EncryptRawBytes(newContent, password, crypt.Pass) + if err != nil { + return err + } + + perm := os.FileMode(0600) + if preExisting { + perm = noteInfo.Mode().Perm() + } + + err = os.WriteFile(output, encrypted, perm) + + fmt.Println("New note saved at:", output) + + return err +} diff --git a/internal/crud/handler_new.go b/internal/crud/handler_new.go deleted file mode 100644 index f949969..0000000 --- a/internal/crud/handler_new.go +++ /dev/null @@ -1,57 +0,0 @@ -package crud - -import ( - "fmt" - "os" - - "gitlab.com/whiteboardguy/eno/internal/crypt" - "gitlab.com/whiteboardguy/eno/internal/utils" -) - -func NewNote(output string) error { - fmt.Printf("Enter the passphrase: ") - password, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - utils.TryMlock(password) - defer clear(password) - - fmt.Printf("Confirm the passphrase: ") - passwordConfirm, err := utils.TakeSecureUserInput() - if err != nil { - return fmt.Errorf("failed to read passphrase: %v", err) - } - utils.TryMlock(passwordConfirm) - defer clear(passwordConfirm) - - if string(password) != string(passwordConfirm) { - return fmt.Errorf("passphrases do not match") - } - - encrypted, err := crypt.EncryptRawBytes( - []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), - password, - ) - if err != nil { - return err - } - - fmt.Println(output) - file, err := os.Create(output) - if err != nil { - return err - } - - _, err = file.Write(encrypted) - if err != nil { - return err - } - - file.Sync() - - // utils.ClearTerm() - fmt.Println("New note saved at:", file.Name()) - - return nil -} diff --git a/internal/utils/headers.go b/internal/crypt/headers.go index a4c8e10..b5addb2 100644 --- a/internal/utils/headers.go +++ b/internal/crypt/headers.go @@ -1,4 +1,4 @@ -package utils +package crypt import ( "bytes" @@ -7,19 +7,24 @@ import ( ) type DataType string +type AuthMode string + const ( File DataType = "File" Dir DataType = "Dir" + + Pass AuthMode = "Pass" + SSH AuthMode = "SSH" ) type FileHeaders struct { - Salt [16]byte - Nonce [12]byte - Type DataType // `Type` for file vs directory storage + Salt [16]byte + Nonce [12]byte + Type DataType + Auth AuthMode + Parts int - // TODO: `Parts (int)` for the number of encrypted parts the file is divided into. - // TODO: `Version (string)` for the version of .eno file. - // TODO: `EncryptMethod (string)` for the mode of encryption (gpg or passphrase). Maybe even stdout command. + // TODO: Version (string) for the version of .eno file. } func GetFileHeaders(b []byte) (FileHeaders, error) { diff --git a/internal/crypt/mem.go b/internal/crypt/mem.go new file mode 100644 index 0000000..9c1d21c --- /dev/null +++ b/internal/crypt/mem.go @@ -0,0 +1,16 @@ +package crypt + +import ( + "fmt" + "os" + "syscall" +) + +func TryMlock(b []byte) { + if len(b) == 0 { + return + } + if err := syscall.Mlock(b); err != nil { + fmt.Fprintf(os.Stderr, "mlock warning: %v\n", err) + } +} diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go index fe190a5..28d7856 100644 --- a/internal/crypt/rawDecrypt.go +++ b/internal/crypt/rawDecrypt.go @@ -4,7 +4,6 @@ import ( "fmt" "strconv" - "gitlab.com/whiteboardguy/eno/internal/utils" "golang.org/x/crypto/argon2" "golang.org/x/crypto/chacha20poly1305" ) @@ -31,13 +30,13 @@ func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { } headerBytes := inputBytes[idx+1 : idx+1+headerLen] - headers, err := utils.GetFileHeaders(headerBytes) + headers, err := GetFileHeaders(headerBytes) if err != nil { return nil, fmt.Errorf("decrypt: %w", err) } key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32) - utils.TryMlock(key) + TryMlock(key) defer clear(key) aead, err := chacha20poly1305.New(key) if err != nil { diff --git a/internal/crypt/rawEncrypt.go b/internal/crypt/rawEncrypt.go index f197650..135efdf 100644 --- a/internal/crypt/rawEncrypt.go +++ b/internal/crypt/rawEncrypt.go @@ -4,12 +4,11 @@ import ( "fmt" "strconv" - "gitlab.com/whiteboardguy/eno/internal/utils" "golang.org/x/crypto/argon2" "golang.org/x/crypto/chacha20poly1305" ) -func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { +func EncryptRawBytes(inputBytes []byte, password []byte, authMode AuthMode) ([]byte, error) { salt, err := NewSalt() if err != nil { return nil, fmt.Errorf("encrypt: %w", err) @@ -20,10 +19,10 @@ func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { return nil, fmt.Errorf("encrypt: %w", err) } - headers := utils.FileHeaders{Salt: salt, Nonce: nonce} + headers := FileHeaders{Salt: salt, Nonce: nonce, Type: File, Parts: 1, Auth: authMode} key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32) - utils.TryMlock(key) + TryMlock(key) defer clear(key) aead, err := chacha20poly1305.New(key) if err != nil { @@ -32,7 +31,7 @@ func EncryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { encrypted := aead.Seal(nil, headers.Nonce[:], inputBytes, nil) - headerBytes, err := utils.FileHeadersBytes(headers) + headerBytes, err := FileHeadersBytes(headers) if err != nil { return nil, fmt.Errorf("encrypt: %w", err) } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 96ca0f0..a8437b9 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -10,15 +10,6 @@ import ( "github.com/charmbracelet/x/term" ) -func TryMlock(b []byte) { - if len(b) == 0 { - return - } - if err := syscall.Mlock(b); err != nil { - fmt.Fprintf(os.Stderr, "mlock warning: %v\n", err) - } -} - func TakeSecureUserInput() ([]byte, error) { fd := os.Stdin.Fd() |