├── go.mod ├── ecdh.go ├── LICENSE └── ecdh_test.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/fd/ecdh 2 | -------------------------------------------------------------------------------- /ecdh.go: -------------------------------------------------------------------------------- 1 | // Package ecdh implments Elliptic curve Diffie–Hellman key sharing 2 | package ecdh 3 | 4 | import ( 5 | "crypto/elliptic" 6 | "math/big" 7 | ) 8 | 9 | // ComputeShared computes the shared key for the private key material priv and 10 | // the x and y public coordinates 11 | func ComputeShared(curve elliptic.Curve, x, y *big.Int, priv []byte) []byte { 12 | x, _ = curve.ScalarMult(x, y, priv) 13 | return x.Bytes() 14 | } 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Simon Menke 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /ecdh_test.go: -------------------------------------------------------------------------------- 1 | package ecdh 2 | 3 | import ( 4 | "bytes" 5 | "crypto/elliptic" 6 | "crypto/rand" 7 | "testing" 8 | 9 | "github.com/fd/secp160r1" 10 | ) 11 | 12 | func Test_ComputeShared_P256(t *testing.T) { 13 | curve := elliptic.P256() 14 | 15 | for i := 100; i > 0; i-- { 16 | prv1, x1, y1, err := elliptic.GenerateKey(curve, rand.Reader) 17 | if err != nil { 18 | t.Fatal(err) 19 | } 20 | if prv1 == nil { 21 | t.Fatal("expected prv1 to be non-nil") 22 | } 23 | if x1 == nil { 24 | t.Fatal("expected x1 to be non-nil") 25 | } 26 | if y1 == nil { 27 | t.Fatal("expected y1 to be non-nil") 28 | } 29 | 30 | prv2, x2, y2, err := elliptic.GenerateKey(curve, rand.Reader) 31 | if err != nil { 32 | t.Fatal(err) 33 | } 34 | if prv2 == nil { 35 | t.Fatal("expected prv2 to be non-nil") 36 | } 37 | if x2 == nil { 38 | t.Fatal("expected x2 to be non-nil") 39 | } 40 | if y2 == nil { 41 | t.Fatal("expected y2 to be non-nil") 42 | } 43 | 44 | shared1 := ComputeShared(curve, x2, y2, prv1) 45 | shared2 := ComputeShared(curve, x1, y1, prv2) 46 | 47 | if !bytes.Equal(shared1, shared2) { 48 | t.Fatal("expected shared1 and shared2 to be equal") 49 | } 50 | } 51 | } 52 | 53 | func Test_ComputeShared_P160(t *testing.T) { 54 | curve := secp160r1.P160() 55 | 56 | for i := 100; i > 0; i-- { 57 | prv1, x1, y1, err := elliptic.GenerateKey(curve, rand.Reader) 58 | if err != nil { 59 | t.Fatal(err) 60 | } 61 | if prv1 == nil { 62 | t.Fatal("expected prv1 to be non-nil") 63 | } 64 | if x1 == nil { 65 | t.Fatal("expected x1 to be non-nil") 66 | } 67 | if y1 == nil { 68 | t.Fatal("expected y1 to be non-nil") 69 | } 70 | 71 | prv2, x2, y2, err := elliptic.GenerateKey(curve, rand.Reader) 72 | if err != nil { 73 | t.Fatal(err) 74 | } 75 | if prv2 == nil { 76 | t.Fatal("expected prv2 to be non-nil") 77 | } 78 | if x2 == nil { 79 | t.Fatal("expected x2 to be non-nil") 80 | } 81 | if y2 == nil { 82 | t.Fatal("expected y2 to be non-nil") 83 | } 84 | 85 | shared1 := ComputeShared(curve, x2, y2, prv1) 86 | shared2 := ComputeShared(curve, x1, y1, prv2) 87 | 88 | if !bytes.Equal(shared1, shared2) { 89 | t.Fatal("expected shared1 and shared2 to be equal") 90 | } 91 | } 92 | } 93 | --------------------------------------------------------------------------------