Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add assert for constants value

Neeraj Guptaa806135fc85b
diff --git a/server/pkg/utils/crypto/crypto.go b/server/pkg/utils/crypto/crypto.go
index cb0f9fc302..06913a7329 100644
--- a/server/pkg/utils/crypto/crypto.go
+++ b/server/pkg/utils/crypto/crypto.go
@@ -1,9 +1,9 @@
package crypto
import (
- "github.com/ente-io/stacktrace"
-
"encoding/base64"
+ "fmt"
+ "github.com/ente-io/stacktrace"
"github.com/GoKillers/libsodium-go/cryptobox"
generichash "github.com/GoKillers/libsodium-go/cryptogenerichash"
@@ -12,11 +12,24 @@ import (
"github.com/ente-io/museum/pkg/utils/auth"
)
+const (
+ cryptoSecretBoxNonceBytes = 24 // Nonce size for secret box
+ // Generic hash (BLAKE2b) constants
+ cryptoGenericHashBytes = 32 // Default output size
+)
+
func Encrypt(data string, encryptionKey []byte) (ente.EncryptionResult, error) {
- nonce, err := auth.GenerateRandomBytes(cryptosecretbox.CryptoSecretBoxNonceBytes())
+ if cryptoSecretBoxNonceBytes != cryptosecretbox.CryptoSecretBoxNonceBytes() {
+ panic("cryptoSecretBoxNonceBytes constant does not match the actual nonce size")
+ }
+ nonce, err := auth.GenerateRandomBytes(cryptoSecretBoxNonceBytes)
if err != nil {
return ente.EncryptionResult{}, stacktrace.Propagate(err, "")
}
+ return encryptWithNonce(data, encryptionKey, nonce)
+}
+
+func encryptWithNonce(data string, encryptionKey []byte, nonce []byte) (ente.EncryptionResult, error) {
encryptedEmailBytes, errCode := cryptosecretbox.CryptoSecretBoxEasy([]byte(data), nonce, encryptionKey)
if errCode != 0 {
return ente.EncryptionResult{}, stacktrace.NewError("encryption failed")
@@ -33,7 +46,11 @@ func Decrypt(cipher []byte, key []byte, nonce []byte) (string, error) {
}
func GetHash(data string, hashKey []byte) (string, error) {
- dataHashBytes, err := generichash.CryptoGenericHash(generichash.CryptoGenericHashBytes(), []byte(data), hashKey)
+ if cryptoGenericHashBytes != generichash.CryptoGenericHashBytes() {
+ panic(fmt.Sprintf("cryptoGenericHashBytes constant %d does not match the actual hash size %d",
+ cryptoGenericHashBytes, generichash.CryptoGenericHashBytes()))
+ }
+ dataHashBytes, err := generichash.CryptoGenericHash(cryptoGenericHashBytes, []byte(data), hashKey)
if err != 0 {
return "", stacktrace.NewError("email hash failed")
}