aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt/rawDecrypt.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/crypt/rawDecrypt.go')
-rw-r--r--internal/crypt/rawDecrypt.go166
1 files changed, 0 insertions, 166 deletions
diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go
deleted file mode 100644
index ede063c..0000000
--- a/internal/crypt/rawDecrypt.go
+++ /dev/null
@@ -1,166 +0,0 @@
-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)
- 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)
- }
-
- ciphertext := inputBytes[idx+1+headerLen:]
- 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) {
- 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)
- 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)
- }
-
- ciphertext := inputBytes[idx+1+headerLen:]
- 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) {
- 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)
- 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)
- }
-
- ciphertext := inputBytes[idx+1+headerLen:]
- plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
- if err != nil {
- return nil, fmt.Errorf("decrypt: %w", err)
- }
-
- return plaintext, nil
-}