Skip to content

armgit / ente

Public Imported from GitHub
0

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

Remove obsolete native crypto layer

Manav Rathi22c143c8c5d3
diff --git a/server/pkg/controller/user/twofactor.go b/server/pkg/controller/user/twofactor.go
index 28162fd062..042d53e09c 100644
--- a/server/pkg/controller/user/twofactor.go
+++ b/server/pkg/controller/user/twofactor.go
@@ -213,7 +213,7 @@ func (c *UserController) GetKeyAttributeAndToken(context *gin.Context, userID in
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
token := auth.GenerateURLSafeRandomString(TokenLength)
- encryptedToken, err := crypto.GetEncryptedTokenNative(token, keyAttributes.PublicKey)
+ encryptedToken, err := crypto.GetEncryptedToken(token, keyAttributes.PublicKey)
if err != nil {
return ente.TwoFactorAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/controller/user/user_delete.go b/server/pkg/controller/user/user_delete.go
index 0cf51b9944..498c6ba27c 100644
--- a/server/pkg/controller/user/user_delete.go
+++ b/server/pkg/controller/user/user_delete.go
@@ -45,7 +45,7 @@ func (c *UserController) GetDeleteChallengeToken(ctx *gin.Context) (*ente.Delete
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
- encryptedToken, err := crypto.GetEncryptedTokenNative(base64.StdEncoding.EncodeToString([]byte(token)), keyAttributes.PublicKey)
+ encryptedToken, err := crypto.GetEncryptedToken(base64.StdEncoding.EncodeToString([]byte(token)), keyAttributes.PublicKey)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index 8a87644aad..bd45e928ff 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -699,7 +699,7 @@ func (c *UserController) onVerificationSuccess(context *gin.Context, email strin
}
}
var encryptedToken string
- encryptedToken, err = crypto.GetEncryptedTokenNative(token, keyAttributes.PublicKey)
+ encryptedToken, err = crypto.GetEncryptedToken(token, keyAttributes.PublicKey)
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/utils/crypto/crypto.go b/server/pkg/utils/crypto/crypto.go
index 3e5692859d..4a9427fec3 100644
--- a/server/pkg/utils/crypto/crypto.go
+++ b/server/pkg/utils/crypto/crypto.go
@@ -1,12 +1,17 @@
package crypto
import (
+ "crypto/rand"
"encoding/base64"
"encoding/hex"
"github.com/ente/museum/ente"
+ "github.com/ente/museum/pkg/utils/auth"
"github.com/ente/stacktrace"
+ "golang.org/x/crypto/blake2b"
"golang.org/x/crypto/curve25519"
+ "golang.org/x/crypto/nacl/box"
+ "golang.org/x/crypto/nacl/secretbox"
)
// Exported constants matching libsodium
@@ -18,23 +23,116 @@ const (
)
func Encrypt(data string, encryptionKey []byte) (ente.EncryptionResult, error) {
- return EncryptNative(data, encryptionKey)
+ return encryptWithNonce(data, encryptionKey, auth.GenerateRandomBytes(SecretBoxNonceBytes))
}
func encryptWithNonce(data string, encryptionKey []byte, nonce []byte) (ente.EncryptionResult, error) {
- return encryptWithNonceNative(data, encryptionKey, nonce)
+ // Convert key to array
+ if len(encryptionKey) != SecretBoxKeyBytes {
+ return ente.EncryptionResult{}, stacktrace.NewError("invalid key length")
+ }
+ var key [SecretBoxKeyBytes]byte
+ copy(key[:], encryptionKey)
+
+ // Convert nonce to array
+ if len(nonce) != SecretBoxNonceBytes {
+ return ente.EncryptionResult{}, stacktrace.NewError("invalid nonce length")
+ }
+ var nonceArray [SecretBoxNonceBytes]byte
+ copy(nonceArray[:], nonce)
+
+ // Encrypt using secretbox (XSalsa20-Poly1305)
+ encrypted := secretbox.Seal(nil, []byte(data), &nonceArray, &key)
+
+ return ente.EncryptionResult{
+ Cipher: encrypted,
+ Nonce: nonce,
+ }, nil
}
-func Decrypt(cipher []byte, key []byte, nonce []byte) (string, error) {
- return DecryptNative(cipher, key, nonce)
+func Decrypt(cipher []byte, encryptionKey []byte, nonce []byte) (string, error) {
+ // Convert key to array
+ if len(encryptionKey) != SecretBoxKeyBytes {
+ return "", stacktrace.NewError("invalid key length")
+ }
+ var key [SecretBoxKeyBytes]byte
+ copy(key[:], encryptionKey)
+
+ // Convert nonce to array
+ if len(nonce) != SecretBoxNonceBytes {
+ return "", stacktrace.NewError("invalid nonce length")
+ }
+ var nonceArray [SecretBoxNonceBytes]byte
+ copy(nonceArray[:], nonce)
+
+ // Decrypt using secretbox
+ decrypted, ok := secretbox.Open(nil, cipher, &nonceArray, &key)
+ if !ok {
+ return "", stacktrace.NewError("decryption failed")
+ }
+
+ return string(decrypted), nil
}
func GetHash(data string, hashKey []byte) (string, error) {
- return GetHashNative(data, hashKey)
+ // BLAKE2b-256 hash with optional key
+ hash, err := blake2b.New256(hashKey)
+ if err != nil {
+ return "", stacktrace.Propagate(err, "failed to create blake2b hasher")
+ }
+
+ hash.Write([]byte(data))
+ hashBytes := hash.Sum(nil)
+
+ return base64.StdEncoding.EncodeToString(hashBytes), nil
}
+// GetEncryptedToken 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 GetEncryptedToken(token string, publicKey string) (string, error) {
- return GetEncryptedTokenNative(token, publicKey)
+ publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKey)
+ if err != nil {
+ return "", stacktrace.Propagate(err, "")
+ }
+ if len(publicKeyBytes) != BoxPublicKeyBytes {
+ return "", stacktrace.NewError("invalid public key length")
+ }
+
+ tokenBytes, err := base64.URLEncoding.DecodeString(token)
+ if err != nil {
+ return "", stacktrace.Propagate(err, "")
+ }
+
+ // Convert slice to array for nacl/box
+ var recipientPublicKey [BoxPublicKeyBytes]byte
+ copy(recipientPublicKey[:], publicKeyBytes)
+
+ // Generate ephemeral keypair
+ ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
+ if err != nil {
+ return "", stacktrace.Propagate(err, "failed to generate ephemeral keypair")
+ }
+
+ // Derive nonce deterministically like libsodium's crypto_box_seal
+ // nonce = BLAKE2b-192(ephemeral_pk || recipient_pk)
+ nonceInput := make([]byte, 64)
+ copy(nonceInput[:32], ephemeralPublicKey[:])
+ copy(nonceInput[32:], recipientPublicKey[:])
+
+ hash, err := blake2b.New(24, nil) // 24 bytes = 192 bits for nonce
+ if err != nil {
+ return "", stacktrace.Propagate(err, "failed to create blake2b hasher")
+ }
+ hash.Write(nonceInput)
+ var nonce [24]byte
+ copy(nonce[:], hash.Sum(nil))
+
+ // Encrypt the message (ephemeral public key + ciphertext)
+ 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
}
func ValidateSealedBoxPublicKey(publicKey string) error {
diff --git a/server/pkg/utils/crypto/crypto_native.go b/server/pkg/utils/crypto/crypto_native.go
deleted file mode 100644
index 3cdec85294..0000000000
--- a/server/pkg/utils/crypto/crypto_native.go
+++ /dev/null
@@ -1,126 +0,0 @@
-package crypto
-
-import (
- "crypto/rand"
- "encoding/base64"
-
- "github.com/ente/museum/ente"
- "github.com/ente/museum/pkg/utils/auth"
- "github.com/ente/stacktrace"
- "golang.org/x/crypto/blake2b"
- "golang.org/x/crypto/nacl/box"
- "golang.org/x/crypto/nacl/secretbox"
-)
-
-func EncryptNative(data string, encryptionKey []byte) (ente.EncryptionResult, error) {
- return encryptWithNonceNative(data, encryptionKey, auth.GenerateRandomBytes(SecretBoxNonceBytes))
-}
-
-func encryptWithNonceNative(data string, encryptionKey []byte, nonce []byte) (ente.EncryptionResult, error) {
- // Convert key to array
- if len(encryptionKey) != SecretBoxKeyBytes {
- return ente.EncryptionResult{}, stacktrace.NewError("invalid key length")
- }
- var key [SecretBoxKeyBytes]byte
- copy(key[:], encryptionKey)
-
- // Convert nonce to array
- if len(nonce) != SecretBoxNonceBytes {
- return ente.EncryptionResult{}, stacktrace.NewError("invalid nonce length")
- }
- var nonceArray [SecretBoxNonceBytes]byte
- copy(nonceArray[:], nonce)
-
- // Encrypt using secretbox (XSalsa20-Poly1305)
- encrypted := secretbox.Seal(nil, []byte(data), &nonceArray, &key)
-
- return ente.EncryptionResult{
- Cipher: encrypted,
- Nonce: nonce,
- }, nil
-}
-
-func DecryptNative(cipher []byte, encryptionKey []byte, nonce []byte) (string, error) {
- // Convert key to array
- if len(encryptionKey) != SecretBoxKeyBytes {
- return "", stacktrace.NewError("invalid key length")
- }
- var key [SecretBoxKeyBytes]byte
- copy(key[:], encryptionKey)
-
- // Convert nonce to array
- if len(nonce) != SecretBoxNonceBytes {
- return "", stacktrace.NewError("invalid nonce length")
- }
- var nonceArray [SecretBoxNonceBytes]byte
- copy(nonceArray[:], nonce)
-
- // Decrypt using secretbox
- decrypted, ok := secretbox.Open(nil, cipher, &nonceArray, &key)
- if !ok {
- return "", stacktrace.NewError("decryption failed")
- }
-
- return string(decrypted), nil
-}
-
-func GetHashNative(data string, hashKey []byte) (string, error) {
- // BLAKE2b-256 hash with optional key
- hash, err := blake2b.New256(hashKey)
- if err != nil {
- return "", stacktrace.Propagate(err, "failed to create blake2b hasher")
- }
-
- hash.Write([]byte(data))
- hashBytes := hash.Sum(nil)
-
- 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) != BoxPublicKeyBytes {
- return "", stacktrace.NewError("invalid public key length")
- }
-
- tokenBytes, err := base64.URLEncoding.DecodeString(token)
- if err != nil {
- return "", stacktrace.Propagate(err, "")
- }
-
- // Convert slice to array for nacl/box
- var recipientPublicKey [BoxPublicKeyBytes]byte
- copy(recipientPublicKey[:], publicKeyBytes)
-
- // Generate ephemeral keypair
- ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
- if err != nil {
- return "", stacktrace.Propagate(err, "failed to generate ephemeral keypair")
- }
-
- // Derive nonce deterministically like libsodium's crypto_box_seal
- // nonce = BLAKE2b-192(ephemeral_pk || recipient_pk)
- nonceInput := make([]byte, 64)
- copy(nonceInput[:32], ephemeralPublicKey[:])
- copy(nonceInput[32:], recipientPublicKey[:])
-
- hash, err := blake2b.New(24, nil) // 24 bytes = 192 bits for nonce
- if err != nil {
- return "", stacktrace.Propagate(err, "failed to create blake2b hasher")
- }
- hash.Write(nonceInput)
- var nonce [24]byte
- copy(nonce[:], hash.Sum(nil))
-
- // Encrypt the message (ephemeral public key + ciphertext)
- 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
-}