aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt/raw_decrypt.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-19 18:33:40 +0530
committerishaan <contact@ishaan.cc>2026-07-19 18:33:40 +0530
commite48a02646f60b6fdff8db5c4105896756584ac63 (patch)
tree3371785e68bfe6b14065589ba7fd417d75742595 /internal/crypt/raw_decrypt.go
parentd6449a232e6bd9eb46132c7403a05ac1a756b619 (diff)
reorganize
Diffstat (limited to 'internal/crypt/raw_decrypt.go')
-rw-r--r--internal/crypt/raw_decrypt.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/internal/crypt/raw_decrypt.go b/internal/crypt/raw_decrypt.go
new file mode 100644
index 0000000..050a4ec
--- /dev/null
+++ b/internal/crypt/raw_decrypt.go
@@ -0,0 +1,99 @@
+package crypt
+
+import (
+ "fmt"
+
+ "golang.org/x/crypto/argon2"
+ "golang.org/x/crypto/chacha20poly1305"
+)
+
+func DecryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) {
+ headers, ciphertext, err := ParseFileHeaders(inputBytes)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ if headers.Auth != Pass {
+ return nil, fmt.Errorf("decrypt: expected password auth, got %s", headers.Auth)
+ }
+
+ key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32)
+ TryMlock(key)
+ defer clear(key)
+ aead, err := chacha20poly1305.New(key)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
+ }
+
+ plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ return plaintext, nil
+}
+
+func DecryptBytesSSH(inputBytes []byte, privKeyPath string) ([]byte, error) {
+ headers, ciphertext, err := ParseFileHeaders(inputBytes)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ if headers.Auth != SSH {
+ return nil, fmt.Errorf("decrypt: expected SSH auth, got %s", headers.Auth)
+ }
+
+ if len(headers.Recipients) == 0 {
+ return nil, fmt.Errorf("decrypt: no recipients found in headers")
+ }
+
+ key := []byte{}
+ TryMlock(key)
+ defer clear(key)
+ if len(headers.Recipients) == 1 {
+ key, err = UnwrapNoteKey(headers.Recipients[0], privKeyPath)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to unwrap note key: %w", err)
+ }
+ } else {
+ key, err = FindAndUnwrapNoteKey(headers.Recipients, privKeyPath)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to unwrap note key: %w", err)
+ }
+ }
+
+ aead, err := chacha20poly1305.New(key)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
+ }
+
+ plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ return plaintext, nil
+}
+
+func DecryptBytesSSHWithKey(inputBytes []byte, noteKey []byte) ([]byte, error) {
+ headers, ciphertext, err := ParseFileHeaders(inputBytes)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ if headers.Auth != SSH {
+ return nil, fmt.Errorf("decrypt: expected SSH auth, got %s", headers.Auth)
+ }
+
+ aead, err := chacha20poly1305.New(noteKey)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
+ }
+
+ plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ return plaintext, nil
+}