diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-14 19:26:55 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-14 19:26:55 +0530 |
| commit | 6c6c4a05267e93597bb009c7d6c36383b6af7bf7 (patch) | |
| tree | 4c2c85b78b07e877ca18e833d07e8624712e8a4c /internal/crypt | |
| parent | 9eb2b774e9f0522a3547644453bf929dd560f05f (diff) | |
ssh
Diffstat (limited to 'internal/crypt')
| -rw-r--r-- | internal/crypt/headers.go | 19 | ||||
| -rw-r--r-- | internal/crypt/rawDecrypt.go | 70 | ||||
| -rw-r--r-- | internal/crypt/rawEncrypt.go | 49 | ||||
| -rw-r--r-- | internal/crypt/sshKey.go | 22 |
4 files changed, 149 insertions, 11 deletions
diff --git a/internal/crypt/headers.go b/internal/crypt/headers.go index b5addb2..8f9dac2 100644 --- a/internal/crypt/headers.go +++ b/internal/crypt/headers.go @@ -18,15 +18,24 @@ const ( ) type FileHeaders struct { - Salt [16]byte - Nonce [12]byte - Type DataType - Auth AuthMode - Parts int + Salt [16]byte + Nonce [12]byte + Type DataType + Auth AuthMode // "Pass" or "SSH" + Parts int + Recipients []WrappedKey // TODO: Version (string) for the version of .eno file. } +type WrappedKey struct { + PKType string // "ssh-ed25519" + PubKeyBytes []byte + EphemeralPub []byte // of X25519, for ECDH on decrypt (also can replace salt) + Nonce [12]byte // for decrypting EncNoteKey + EncNoteKey []byte // encrypted note key (ciphertext + 16B tag) +} + func GetFileHeaders(b []byte) (FileHeaders, error) { dec := gob.NewDecoder(bytes.NewReader(b)) var headers FileHeaders diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go index 28d7856..b94ea60 100644 --- a/internal/crypt/rawDecrypt.go +++ b/internal/crypt/rawDecrypt.go @@ -8,7 +8,7 @@ import ( "golang.org/x/crypto/chacha20poly1305" ) -func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { +func DecryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) { idx := -1 for i, b := range inputBytes { if b == ':' { @@ -35,6 +35,10 @@ func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { 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) @@ -51,3 +55,67 @@ func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) { 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 +} 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 +} diff --git a/internal/crypt/sshKey.go b/internal/crypt/sshKey.go index 2481275..3b0b344 100644 --- a/internal/crypt/sshKey.go +++ b/internal/crypt/sshKey.go @@ -21,9 +21,17 @@ import ( var curve25519Prime, _ = new(big.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10) var bigOne = big.NewInt(1) -// Ed25519 pubkey -> X25519 pubkey. Both 32b +// Ed25519 pubkey -> X25519 pubkey. Both 32B func edwardsToMontgomery(edPub []byte) []byte { - y := new(big.Int).SetBytes(edPub) + edPubCopy := make([]byte, 32) + copy(edPubCopy, edPub) + edPubCopy[31] &= 0x7F + + for i, j := 0, 31; i < j; i, j = i+1, j-1 { + edPubCopy[i], edPubCopy[j] = edPubCopy[j], edPubCopy[i] + } + + y := new(big.Int).SetBytes(edPubCopy) num := new(big.Int).Add(bigOne, y) @@ -34,7 +42,15 @@ func edwardsToMontgomery(edPub []byte) []byte { u := new(big.Int).Mul(num, denom) u.Mod(u, curve25519Prime) - return u.FillBytes(make([]byte, 32)) + uBE := make([]byte, 32) + u.FillBytes(uBE) + + uLE := make([]byte, 32) + for i := 0; i < 32; i++ { + uLE[i] = uBE[31-i] + } + + return uLE } // Ed25519 seed -> X25519 private key. |