aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt/rawEncrypt.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-14 19:26:55 +0530
committerishaan <contact@ishaan.cc>2026-07-14 19:26:55 +0530
commit6c6c4a05267e93597bb009c7d6c36383b6af7bf7 (patch)
tree4c2c85b78b07e877ca18e833d07e8624712e8a4c /internal/crypt/rawEncrypt.go
parent9eb2b774e9f0522a3547644453bf929dd560f05f (diff)
ssh
Diffstat (limited to 'internal/crypt/rawEncrypt.go')
-rw-r--r--internal/crypt/rawEncrypt.go49
1 files changed, 47 insertions, 2 deletions
diff --git a/internal/crypt/rawEncrypt.go b/internal/crypt/rawEncrypt.go
index 135efdf..bf4b82f 100644
--- a/internal/crypt/rawEncrypt.go
+++ b/internal/crypt/rawEncrypt.go
@@ -1,6 +1,7 @@
package crypt
import (
+ "crypto/rand"
"fmt"
"strconv"
@@ -8,7 +9,7 @@ import (
"golang.org/x/crypto/chacha20poly1305"
)
-func EncryptRawBytes(inputBytes []byte, password []byte, authMode AuthMode) ([]byte, error) {
+func EncryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) {
salt, err := NewSalt()
if err != nil {
return nil, fmt.Errorf("encrypt: %w", err)
@@ -19,7 +20,7 @@ func EncryptRawBytes(inputBytes []byte, password []byte, authMode AuthMode) ([]b
return nil, fmt.Errorf("encrypt: %w", err)
}
- headers := FileHeaders{Salt: salt, Nonce: nonce, Type: File, Parts: 1, Auth: authMode}
+ headers := FileHeaders{Salt: salt, Nonce: nonce, Type: File, Parts: 1, Auth: Pass, Recipients: []WrappedKey{}}
key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32)
TryMlock(key)
@@ -44,3 +45,47 @@ func EncryptRawBytes(inputBytes []byte, password []byte, authMode AuthMode) ([]b
return target, nil
}
+
+func EncryptBytesSSH(inputBytes []byte, pubKeys [][]byte) ([]byte, error) {
+ salt, err := NewSalt()
+ if err != nil {
+ return nil, fmt.Errorf("encrypt: %w", err)
+ }
+
+ nonce, err := NewNonce()
+ if err != nil {
+ return nil, fmt.Errorf("encrypt: %w", err)
+ }
+
+ key := [32]byte{}
+ _, err = rand.Read(key[:])
+ if err != nil {
+ return nil, fmt.Errorf("encrypt: failed to generate key: %w", err)
+ }
+ TryMlock(key[:])
+ defer clear(key[:])
+
+ recipients, err := WrapNoteKeyForRecipients(key[:], pubKeys)
+
+ headers := FileHeaders{Salt: salt, Nonce: nonce, Type: File, Parts: 1, Auth: SSH, Recipients: recipients}
+
+ aead, err := chacha20poly1305.New(key[:])
+ if err != nil {
+ return nil, fmt.Errorf("encrypt: failed to create cipher: %w", err)
+ }
+
+ encrypted := aead.Seal(nil, headers.Nonce[:], inputBytes, nil)
+
+ headerBytes, err := FileHeadersBytes(headers)
+ if err != nil {
+ return nil, fmt.Errorf("encrypt: %w", err)
+ }
+
+ lenLen := len(strconv.Itoa(len(headerBytes)))
+ target := make([]byte, 0, lenLen+1+len(headerBytes)+len(encrypted))
+ target = fmt.Appendf(target, "%d:", len(headerBytes))
+ target = append(target, headerBytes...)
+ target = append(target, encrypted...)
+
+ return target, nil
+}