aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-07-15 06:38:49 +0530
committerishaan <contact@ishaan.cc>2026-07-15 06:38:49 +0530
commit2cc09ca2977cafc9da939907b9088dcfba14e685 (patch)
tree0b840d1d239278238e9f0081efe6f96c1bc80281 /internal
parent1363cb249c2f9ca0159fd336179653243b6fa0be (diff)
improve ed->mont conversion to be longer but near-constant time
Diffstat (limited to 'internal')
-rw-r--r--internal/crypt/sshKey.go36
1 files changed, 10 insertions, 26 deletions
diff --git a/internal/crypt/sshKey.go b/internal/crypt/sshKey.go
index e36d6fd..1328e59 100644
--- a/internal/crypt/sshKey.go
+++ b/internal/crypt/sshKey.go
@@ -9,48 +9,32 @@ import (
"crypto/sha256"
"crypto/sha512"
"fmt"
- "math/big"
"os"
+ "filippo.io/edwards25519/field"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/ssh"
)
-// j*vascript could never
-var curve25519Prime, _ = new(big.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10)
-var bigOne = big.NewInt(1)
-
// Ed25519 pubkey -> X25519 pubkey. Both 32B
func edwardsToMontgomery(edPub []byte) []byte {
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)
+ y := new(field.Element)
+ y.SetBytes(edPubCopy)
- denom := new(big.Int).Sub(bigOne, y)
- denom.Mod(denom, curve25519Prime)
- denom.ModInverse(denom, curve25519Prime)
+ one := new(field.Element).One()
- u := new(big.Int).Mul(num, denom)
- u.Mod(u, curve25519Prime)
-
- uBE := make([]byte, 32)
- u.FillBytes(uBE)
-
- uLE := make([]byte, 32)
- for i := 0; i < 32; i++ {
- uLE[i] = uBE[31-i]
- }
+ var num, denom, inv, u field.Element
+ num.Add(one, y)
+ denom.Subtract(one, y)
+ inv.Invert(&denom)
+ u.Multiply(&num, &inv)
- return uLE
+ return u.Bytes()
}
// Ed25519 seed -> X25519 private key.