diff options
Diffstat (limited to 'internal/crypt/sshKey.go')
| -rw-r--r-- | internal/crypt/sshKey.go | 22 |
1 files changed, 19 insertions, 3 deletions
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. |