mirror of https://github.com/schollz/croc.git
use elliptic curve interface
This commit is contained in:
parent
d1d5b02f27
commit
a9969b161f
|
@ -1,7 +1,6 @@
|
||||||
package pake
|
package pake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/elliptic"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -20,6 +19,13 @@ import (
|
||||||
// http://www.lothar.com/~warner/MagicWormhole-PyCon2016.pdf
|
// http://www.lothar.com/~warner/MagicWormhole-PyCon2016.pdf
|
||||||
// Slide 11
|
// Slide 11
|
||||||
|
|
||||||
|
type EllipticCurve interface {
|
||||||
|
Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)
|
||||||
|
ScalarBaseMult(k []byte) (*big.Int, *big.Int)
|
||||||
|
ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int)
|
||||||
|
IsOnCurve(x, y *big.Int) bool
|
||||||
|
}
|
||||||
|
|
||||||
type Pake struct {
|
type Pake struct {
|
||||||
// Public variables
|
// Public variables
|
||||||
Role int
|
Role int
|
||||||
|
@ -30,7 +36,7 @@ type Pake struct {
|
||||||
HkA, HkB []byte
|
HkA, HkB []byte
|
||||||
|
|
||||||
// Private variables
|
// Private variables
|
||||||
curve elliptic.Curve
|
curve EllipticCurve
|
||||||
pw []byte
|
pw []byte
|
||||||
vpwᵤ, vpwᵥ *big.Int
|
vpwᵤ, vpwᵥ *big.Int
|
||||||
upwᵤ, upwᵥ *big.Int
|
upwᵤ, upwᵥ *big.Int
|
||||||
|
@ -42,7 +48,7 @@ type Pake struct {
|
||||||
isVerified bool
|
isVerified bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(pw []byte, role int, curve elliptic.Curve) (p *Pake, err error) {
|
func Init(pw []byte, role int, curve EllipticCurve) (p *Pake, err error) {
|
||||||
p = new(Pake)
|
p = new(Pake)
|
||||||
if role == 1 {
|
if role == 1 {
|
||||||
p.Role = 1
|
p.Role = 1
|
||||||
|
|
|
@ -8,13 +8,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPake(t *testing.T) {
|
func TestPake(t *testing.T) {
|
||||||
|
curve := elliptic.P521() //siec.SIEC255()
|
||||||
// successful (both have same k)
|
// successful (both have same k)
|
||||||
// initialize A
|
// initialize A
|
||||||
A, err := Init([]byte{1, 2, 3}, 0, elliptic.P256())
|
A, err := Init([]byte{1, 2, 3}, 0, curve)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, A.IsVerified())
|
assert.False(t, A.IsVerified())
|
||||||
// initialize B
|
// initialize B
|
||||||
B, err := Init([]byte{1, 2, 3}, 1, elliptic.P256())
|
B, err := Init([]byte{1, 2, 3}, 1, curve)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, B.IsVerified())
|
assert.False(t, B.IsVerified())
|
||||||
// send A's stuff to B
|
// send A's stuff to B
|
||||||
|
@ -32,11 +33,11 @@ func TestPake(t *testing.T) {
|
||||||
|
|
||||||
// failure (both have different k)
|
// failure (both have different k)
|
||||||
// initialize A
|
// initialize A
|
||||||
A, err = Init([]byte{1, 2, 3}, 0, elliptic.P256())
|
A, err = Init([]byte{1, 2, 3}, 0, curve)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, A.IsVerified())
|
assert.False(t, A.IsVerified())
|
||||||
// initialize B
|
// initialize B
|
||||||
B, err = Init([]byte{4, 5, 6}, 1, elliptic.P256())
|
B, err = Init([]byte{4, 5, 6}, 1, curve)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.False(t, B.IsVerified())
|
assert.False(t, B.IsVerified())
|
||||||
// send A's stuff to B
|
// send A's stuff to B
|
||||||
|
|
Loading…
Reference in New Issue