mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
ECDH
This commit is contained in:
parent
df3bd6edf6
commit
1aa20a1e2b
@ -1,8 +1,10 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"crypto/md5"
|
||||||
|
"crypto/rand"
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EncryptECDH struct {
|
type EncryptECDH struct {
|
||||||
@ -12,10 +14,31 @@ type EncryptECDH struct {
|
|||||||
|
|
||||||
var ECDH = &EncryptECDH{}
|
var ECDH = &EncryptECDH{}
|
||||||
|
|
||||||
|
var tenKeyX = new(big.Int).SetBytes([]byte{ // pubkey[1:24]
|
||||||
|
0x92, 0x8d, 0x88, 0x50, 0x67, 0x30, 0x88, 0xb3, 0x43, 0x26, 0x4e, 0x0c,
|
||||||
|
0x6b, 0xac, 0xb8, 0x49, 0x6d, 0x69, 0x77, 0x99, 0xf3, 0x72, 0x11, 0xde,
|
||||||
|
})
|
||||||
|
|
||||||
|
var tenKeyY = new(big.Int).SetBytes([]byte{ // pubkey[25:48]
|
||||||
|
0xb2, 0x5b, 0xb7, 0x39, 0x06, 0xcb, 0x08, 0x9f, 0xea, 0x96, 0x39, 0xb4,
|
||||||
|
0xe0, 0x26, 0x04, 0x98, 0xb5, 0x1a, 0x99, 0x2d, 0x50, 0x81, 0x3d, 0xa8,
|
||||||
|
})
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
//TODO: Keygen
|
key, sx, sy, err := secp192k1.GenerateKey(rand.Reader)
|
||||||
ECDH.InitialShareKey, _ = hex.DecodeString("41d0d17c506a5256d0d08d7aac133c70")
|
if err != nil {
|
||||||
ECDH.PublicKey, _ = hex.DecodeString("049fb03421ba7ab5fc91c2d94a7657fff7ba8fe09f08a22951a24865212cbc45aff1b5125188fa8f0e30473bc55d54edc2")
|
panic("Can't Create ECDH key pair")
|
||||||
|
}
|
||||||
|
x, _ := secp192k1.ScalarMult(tenKeyX, tenKeyY, key)
|
||||||
|
hash := md5.Sum(x.Bytes())
|
||||||
|
ECDH.InitialShareKey = hash[:]
|
||||||
|
ECDH.PublicKey = make([]byte, 49)[:0]
|
||||||
|
ECDH.PublicKey = append(ECDH.PublicKey, 0x04)
|
||||||
|
ECDH.PublicKey = append(ECDH.PublicKey, sx.Bytes()...)
|
||||||
|
ECDH.PublicKey = append(ECDH.PublicKey, sy.Bytes()...)
|
||||||
|
|
||||||
|
//ECDH.InitialShareKey, _ = hex.DecodeString("41d0d17c506a5256d0d08d7aac133c70")
|
||||||
|
//ECDH.PublicKey, _ = hex.DecodeString("049fb03421ba7ab5fc91c2d94a7657fff7ba8fe09f08a22951a24865212cbc45aff1b5125188fa8f0e30473bc55d54edc2")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EncryptECDH) DoEncrypt(d, k []byte) []byte {
|
func (e *EncryptECDH) DoEncrypt(d, k []byte) []byte {
|
||||||
|
@ -1,5 +1,24 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Copyright 2011 ThePiachu. All rights reserved.
|
||||||
|
// Copyright 2020 LXY1226. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package bitelliptic implements several Koblitz elliptic curves over prime
|
||||||
|
// fields.
|
||||||
|
|
||||||
|
// Origin File at:
|
||||||
|
// https://github.com/ThePiachu/Split-Vanity-Miner-Golang/blob/03677bc96ff4f5c2771e528562360ccbc513db8d/src/pkg/bitelliptic/bitelliptic.go
|
||||||
|
|
||||||
|
// This package operates, internally, on Jacobian coordinates. For a given
|
||||||
|
// (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)
|
||||||
|
// where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole
|
||||||
|
// calculation can be performed within the transform (as in ScalarMult and
|
||||||
|
// ScalarBaseMult). But even for Add and Double, it's faster to apply and
|
||||||
|
// reverse the transform than to operate in affine coordinates.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
@ -37,6 +56,9 @@ var secp192k1 = &BitCurve{
|
|||||||
BitSize: 192,
|
BitSize: 192,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: double check if the function is okay
|
||||||
|
// affineFromJacobian reverses the Jacobian transform. See the comment at the
|
||||||
|
// top of the file.
|
||||||
func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
||||||
zinv := new(big.Int).ModInverse(z, BitCurve.P)
|
zinv := new(big.Int).ModInverse(z, BitCurve.P)
|
||||||
zinvsq := new(big.Int).Mul(zinv, zinv)
|
zinvsq := new(big.Int).Mul(zinv, zinv)
|
||||||
@ -165,19 +187,19 @@ func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.
|
|||||||
z := Bz
|
z := Bz
|
||||||
|
|
||||||
seenFirstTrue := false
|
seenFirstTrue := false
|
||||||
for _, byte := range k {
|
for _, b := range k {
|
||||||
for bitNum := 0; bitNum < 8; bitNum++ {
|
for bitNum := 0; bitNum < 8; bitNum++ {
|
||||||
if seenFirstTrue {
|
if seenFirstTrue {
|
||||||
x, y, z = BitCurve.doubleJacobian(x, y, z)
|
x, y, z = BitCurve.doubleJacobian(x, y, z)
|
||||||
}
|
}
|
||||||
if byte&0x80 == 0x80 {
|
if b&0x80 == 0x80 {
|
||||||
if !seenFirstTrue {
|
if !seenFirstTrue {
|
||||||
seenFirstTrue = true
|
seenFirstTrue = true
|
||||||
} else {
|
} else {
|
||||||
x, y, z = BitCurve.addJacobian(Bx, By, Bz, x, y, z)
|
x, y, z = BitCurve.addJacobian(Bx, By, Bz, x, y, z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
byte <<= 1
|
b <<= 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +226,7 @@ func (BitCurve *BitCurve) GenerateKey(rand io.Reader) (priv []byte, x, y *big.In
|
|||||||
priv = make([]byte, byteLen)
|
priv = make([]byte, byteLen)
|
||||||
|
|
||||||
for x == nil {
|
for x == nil {
|
||||||
_, err = io.ReadFull(rand, priv)
|
_, err = rand.Read(priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user