aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt
diff options
context:
space:
mode:
Diffstat (limited to 'internal/crypt')
-rw-r--r--internal/crypt/headers.go33
-rw-r--r--internal/crypt/raw_decrypt.go (renamed from internal/crypt/rawDecrypt.go)73
-rw-r--r--internal/crypt/raw_encrypt.go (renamed from internal/crypt/rawEncrypt.go)0
-rw-r--r--internal/crypt/ssh_key.go (renamed from internal/crypt/sshKey.go)32
-rw-r--r--internal/crypt/ssh_key_io.go39
5 files changed, 75 insertions, 102 deletions
diff --git a/internal/crypt/headers.go b/internal/crypt/headers.go
index 8f9dac2..c47d39c 100644
--- a/internal/crypt/headers.go
+++ b/internal/crypt/headers.go
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/gob"
"fmt"
+ "strconv"
)
type DataType string
@@ -55,3 +56,35 @@ func FileHeadersBytes(headers FileHeaders) ([]byte, error) {
return buf.Bytes(), nil
}
+
+func ParseFileHeaders(data []byte) (FileHeaders, []byte, error) {
+ idx := -1
+ for i, b := range data {
+ if b == ':' {
+ idx = i
+ break
+ }
+ }
+ if idx == -1 {
+ return FileHeaders{}, nil, fmt.Errorf("invalid file format: no header length prefix found")
+ }
+
+ headerLen, err := strconv.Atoi(string(data[:idx]))
+ if err != nil {
+ return FileHeaders{}, nil, fmt.Errorf("failed to parse header length: %w", err)
+ }
+
+ if headerLen <= 0 || idx+1+headerLen > len(data) {
+ return FileHeaders{}, nil, fmt.Errorf("invalid header length %d", headerLen)
+ }
+
+ headerBytes := data[idx+1 : idx+1+headerLen]
+ headers, err := GetFileHeaders(headerBytes)
+ if err != nil {
+ return FileHeaders{}, nil, fmt.Errorf("failed to decode headers: %w", err)
+ }
+
+ ciphertext := data[idx+1+headerLen:]
+
+ return headers, ciphertext, nil
+}
diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/raw_decrypt.go
index ede063c..050a4ec 100644
--- a/internal/crypt/rawDecrypt.go
+++ b/internal/crypt/raw_decrypt.go
@@ -2,35 +2,13 @@ package crypt
import (
"fmt"
- "strconv"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/chacha20poly1305"
)
func DecryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) {
- idx := -1
- for i, b := range inputBytes {
- if b == ':' {
- idx = i
- break
- }
- }
- if idx == -1 {
- return nil, fmt.Errorf("decrypt: invalid file format: no header length prefix found")
- }
-
- headerLen, err := strconv.Atoi(string(inputBytes[:idx]))
- if err != nil {
- return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err)
- }
-
- if headerLen <= 0 || idx+1+headerLen > len(inputBytes) {
- return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen)
- }
-
- headerBytes := inputBytes[idx+1 : idx+1+headerLen]
- headers, err := GetFileHeaders(headerBytes)
+ headers, ciphertext, err := ParseFileHeaders(inputBytes)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
}
@@ -47,7 +25,6 @@ func DecryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) {
return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
}
- ciphertext := inputBytes[idx+1+headerLen:]
plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
@@ -57,28 +34,7 @@ func DecryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) {
}
func DecryptBytesSSH(inputBytes []byte, privKeyPath string) ([]byte, error) {
- idx := -1
- for i, b := range inputBytes {
- if b == ':' {
- idx = i
- break
- }
- }
- if idx == -1 {
- return nil, fmt.Errorf("decrypt: invalid file format: no header length prefix found")
- }
-
- headerLen, err := strconv.Atoi(string(inputBytes[:idx]))
- if err != nil {
- return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err)
- }
-
- if headerLen <= 0 || idx+1+headerLen > len(inputBytes) {
- return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen)
- }
-
- headerBytes := inputBytes[idx+1 : idx+1+headerLen]
- headers, err := GetFileHeaders(headerBytes)
+ headers, ciphertext, err := ParseFileHeaders(inputBytes)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
}
@@ -111,7 +67,6 @@ func DecryptBytesSSH(inputBytes []byte, privKeyPath string) ([]byte, error) {
return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
}
- ciphertext := inputBytes[idx+1+headerLen:]
plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
@@ -121,28 +76,7 @@ func DecryptBytesSSH(inputBytes []byte, privKeyPath string) ([]byte, error) {
}
func DecryptBytesSSHWithKey(inputBytes []byte, noteKey []byte) ([]byte, error) {
- idx := -1
- for i, b := range inputBytes {
- if b == ':' {
- idx = i
- break
- }
- }
- if idx == -1 {
- return nil, fmt.Errorf("decrypt: invalid file format: no header length prefix found")
- }
-
- headerLen, err := strconv.Atoi(string(inputBytes[:idx]))
- if err != nil {
- return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err)
- }
-
- if headerLen <= 0 || idx+1+headerLen > len(inputBytes) {
- return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen)
- }
-
- headerBytes := inputBytes[idx+1 : idx+1+headerLen]
- headers, err := GetFileHeaders(headerBytes)
+ headers, ciphertext, err := ParseFileHeaders(inputBytes)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
}
@@ -156,7 +90,6 @@ func DecryptBytesSSHWithKey(inputBytes []byte, noteKey []byte) ([]byte, error) {
return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
}
- ciphertext := inputBytes[idx+1+headerLen:]
plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("decrypt: %w", err)
diff --git a/internal/crypt/rawEncrypt.go b/internal/crypt/raw_encrypt.go
index 03f742f..03f742f 100644
--- a/internal/crypt/rawEncrypt.go
+++ b/internal/crypt/raw_encrypt.go
diff --git a/internal/crypt/sshKey.go b/internal/crypt/ssh_key.go
index 2062ebd..a043f75 100644
--- a/internal/crypt/sshKey.go
+++ b/internal/crypt/ssh_key.go
@@ -10,11 +10,8 @@ import (
"crypto/sha512"
"fmt"
"os"
- "os/signal"
- "syscall"
"filippo.io/edwards25519/field"
- "github.com/charmbracelet/x/term"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/ssh"
@@ -54,35 +51,6 @@ func ed25519SeedToX25519Priv(seed []byte) []byte {
return scalar
}
-func promptSSHKeyPassphrase(privKeyPath string) []byte {
- fd := os.Stdin.Fd()
- oldState, err := term.GetState(fd)
- if err != nil {
- fmt.Fprintf(os.Stderr, "eno: failed to get terminal state: %v\n", err)
- return nil
- }
-
- sigCh := make(chan os.Signal, 1)
- signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
- defer signal.Stop(sigCh)
-
- go func() {
- <-sigCh
- term.Restore(fd, oldState)
- os.Exit(130)
- }()
-
- fmt.Fprintf(os.Stderr, "Enter passphrase for SSH key %s: ", privKeyPath)
- passphrase, err := term.ReadPassword(fd)
- fmt.Fprintln(os.Stderr)
- if err != nil {
- fmt.Fprintf(os.Stderr, "eno: failed to read passphrase: %v\n", err)
- return nil
- }
-
- return passphrase
-}
-
// Wraps noteKey once per PubKey. Returns WrappedKey for each.
func WrapNoteKeyForRecipients(noteKey []byte, pubKeys [][]byte) ([]WrappedKey, error) {
recipients := make([]WrappedKey, 0, len(pubKeys))
diff --git a/internal/crypt/ssh_key_io.go b/internal/crypt/ssh_key_io.go
new file mode 100644
index 0000000..59ea448
--- /dev/null
+++ b/internal/crypt/ssh_key_io.go
@@ -0,0 +1,39 @@
+package crypt
+
+import (
+ "fmt"
+ "os"
+ "os/signal"
+ "syscall"
+
+ "github.com/charmbracelet/x/term"
+)
+
+func promptSSHKeyPassphrase(privKeyPath string) []byte {
+ fd := os.Stdin.Fd()
+ oldState, err := term.GetState(fd)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "eno: failed to get terminal state: %v\n", err)
+ return nil
+ }
+
+ sigCh := make(chan os.Signal, 1)
+ signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
+ defer signal.Stop(sigCh)
+
+ go func() {
+ <-sigCh
+ term.Restore(fd, oldState)
+ os.Exit(130)
+ }()
+
+ fmt.Fprintf(os.Stderr, "Enter passphrase for SSH key %s: ", privKeyPath)
+ passphrase, err := term.ReadPassword(fd)
+ fmt.Fprintln(os.Stderr)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "eno: failed to read passphrase: %v\n", err)
+ return nil
+ }
+
+ return passphrase
+}