Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Clean up

Neeraj Gupta0d9de30a1a60
diff --git a/server/pkg/utils/crypto/crypto.go b/server/pkg/utils/crypto/crypto.go
index eb1b40e1f4..415fb979e5 100644
--- a/server/pkg/utils/crypto/crypto.go
+++ b/server/pkg/utils/crypto/crypto.go
@@ -2,14 +2,13 @@ package crypto
import (
"encoding/base64"
- "fmt"
- "github.com/ente-io/stacktrace"
"github.com/GoKillers/libsodium-go/cryptobox"
generichash "github.com/GoKillers/libsodium-go/cryptogenerichash"
cryptosecretbox "github.com/GoKillers/libsodium-go/cryptosecretbox"
"github.com/ente-io/museum/ente"
"github.com/ente-io/museum/pkg/utils/auth"
+ "github.com/ente-io/stacktrace"
)
// Exported constants matching libsodium
@@ -17,11 +16,12 @@ const (
SecretBoxKeyBytes = 32 // crypto_secretbox_KEYBYTES in libsodium
SecretBoxNonceBytes = 24 // crypto_secretbox_NONCEBYTES in libsodium
GenericHashBytes = 32 // crypto_generichash_BYTES in libsodium (BLAKE2b-256)
+ BoxPublicKeyBytes = 32 // crypto_box_publickeybytes in lib-sodium
)
func Encrypt(data string, encryptionKey []byte) (ente.EncryptionResult, error) {
if SecretBoxNonceBytes != cryptosecretbox.CryptoSecretBoxNonceBytes() {
- panic("SecretBoxNonceBytes constant does not match the actual nonce size")
+ return ente.EncryptionResult{}, stacktrace.NewError("SecretBoxNonceBytes constant does not match the actual nonce size")
}
nonce, err := auth.GenerateRandomBytes(SecretBoxNonceBytes)
if err != nil {
@@ -48,8 +48,7 @@ func Decrypt(cipher []byte, key []byte, nonce []byte) (string, error) {
func GetHash(data string, hashKey []byte) (string, error) {
if GenericHashBytes != generichash.CryptoGenericHashBytes() {
- panic(fmt.Sprintf("GenericHashBytes constant %d does not match the actual hash size %d",
- GenericHashBytes, generichash.CryptoGenericHashBytes()))
+ return "", stacktrace.NewError("GenericHashBytes constant does not match the actual hash size")
}
dataHashBytes, err := generichash.CryptoGenericHash(GenericHashBytes, []byte(data), hashKey)
if err != 0 {
diff --git a/server/pkg/utils/crypto/crypto_native.go b/server/pkg/utils/crypto/crypto_native.go
index 81c3feaf6c..a29af731ae 100644
--- a/server/pkg/utils/crypto/crypto_native.go
+++ b/server/pkg/utils/crypto/crypto_native.go
@@ -3,9 +3,9 @@ package crypto
import (
"crypto/rand"
"encoding/base64"
- "github.com/ente-io/museum/pkg/utils/auth"
"github.com/ente-io/museum/ente"
+ "github.com/ente-io/museum/pkg/utils/auth"
"github.com/ente-io/stacktrace"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/nacl/box"
@@ -26,7 +26,7 @@ func encryptWithNonceNative(data string, encryptionKey []byte, nonce []byte) (en
if len(encryptionKey) != SecretBoxKeyBytes {
return ente.EncryptionResult{}, stacktrace.NewError("invalid key length")
}
- var key [32]byte
+ var key [SecretBoxKeyBytes]byte
copy(key[:], encryptionKey)
// Convert nonce to array
@@ -50,7 +50,7 @@ func DecryptNative(cipher []byte, encryptionKey []byte, nonce []byte) (string, e
if len(encryptionKey) != SecretBoxKeyBytes {
return "", stacktrace.NewError("invalid key length")
}
- var key [32]byte
+ var key [SecretBoxKeyBytes]byte
copy(key[:], encryptionKey)
// Convert nonce to array
@@ -82,12 +82,15 @@ func GetHashNative(data string, hashKey []byte) (string, error) {
return base64.StdEncoding.EncodeToString(hashBytes), nil
}
+// GetEncryptedTokenNative encrypts the given token using the recipient's public key.
+// Format matches lib-sodium: https://libsodium.gitbook.io/doc/public-key_cryptography/sealed_boxes#algorithm-details
+// ephemeral_pk β€– box(m, recipient_pk, ephemeral_sk, nonce=blake2b(ephemeral_pk β€– recipient_pk))
func GetEncryptedTokenNative(token string, publicKey string) (string, error) {
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
return "", stacktrace.Propagate(err, "")
}
- if len(publicKeyBytes) != 32 {
+ if len(publicKeyBytes) != BoxPublicKeyBytes {
return "", stacktrace.NewError("invalid public key length")
}
@@ -97,7 +100,7 @@ func GetEncryptedTokenNative(token string, publicKey string) (string, error) {
}
// Convert slice to array for nacl/box
- var recipientPublicKey [32]byte
+ var recipientPublicKey [BoxPublicKeyBytes]byte
copy(recipientPublicKey[:], publicKeyBytes)
// Generate ephemeral keypair
@@ -121,11 +124,8 @@ func GetEncryptedTokenNative(token string, publicKey string) (string, error) {
copy(nonce[:], hash.Sum(nil))
// Encrypt the message (ephemeral public key + ciphertext)
- // Format matches libsodium: ephemeral_pk || box(m, nonce, recipient_pk, ephemeral_sk)
- out := make([]byte, 32) // just ephemeral public key
+ out := make([]byte, BoxPublicKeyBytes) // just ephemeral public key
copy(out, ephemeralPublicKey[:])
-
encrypted := box.Seal(out, tokenBytes, &nonce, &recipientPublicKey, ephemeralPrivateKey)
-
return base64.StdEncoding.EncodeToString(encrypted), nil
}
diff --git a/server/pkg/utils/crypto/crypto_test.go b/server/pkg/utils/crypto/crypto_test.go
index 870025a238..71a8f0aa3a 100644
--- a/server/pkg/utils/crypto/crypto_test.go
+++ b/server/pkg/utils/crypto/crypto_test.go
@@ -1,11 +1,13 @@
package crypto
import (
+ "crypto/rand"
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "golang.org/x/crypto/nacl/box"
)
func init() {
@@ -283,17 +285,13 @@ func generateTestKey() []byte {
}
func generateTestKeyPair() ([32]byte, [32]byte) {
- // Generate a deterministic test keypair for testing
- var publicKey, privateKey [32]byte
- // Use a fixed seed for deterministic testing
- seed := make([]byte, 32)
- for i := range seed {
- seed[i] = byte(i * 2)
+ // Generate a valid test keypair using nacl/box
+ // For tests that need actual crypto operations
+ publicKey, privateKey, err := box.GenerateKey(rand.Reader)
+ if err != nil {
+ panic(err)
}
- copy(privateKey[:], seed)
- // This is a simplified test keypair - in real usage use box.GenerateKey
- copy(publicKey[:], append([]byte("public"), seed[:26]...))
- return publicKey, privateKey
+ return *publicKey, *privateKey
}
func generateLargeText(size int) string {