dhkx

package
v0.0.0-...-642df0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause, Apache-2.0 Imports: 5 Imported by: 0

README

dhkx

Diffie Hellman Key-exchange algorithm in Go

Based on: github.com/monnand/dhkx

Documentation

Overview

REQUIRE: go 1.16 or later

This is an implementation of Diffie-Hellman Key Exchange algorithm. The algorithm is used to establish a shared key between two communication peers without sharing secrete information.

Typical process:

First, Alice and Bob should agree on which group to use. If you are not sure, choose group 14. GetGroup() will return the desired group by a given id. GetGroup(0) will return a default group, which is usually safe enough to use this group. It is totally safe to share the group's information.

NOTE: The code below will skip error-checking part for the sake of simplicity.

Here is the code on Alice's side:

// Get a group. Use the default one would be enough.
g, _ := GetGroup(0)

// Generate a private key from the group.
// Use the default random number generator.
priv, _ := g.GeneratePrivateKey(nil)

// Get the public key from the private key.
pub := priv.Bytes()

// Send the public key to Bob.
Send("Bob", pub)

// Receive a slice of bytes from Bob, which contains Bob's public key
b := Recv("Bob")

// Recover Bob's public key
bobPubKey := NewPublicKey(b)

// Compute the key
k, _ := group.ComputeKey(bobPubKey, priv)

// Get the key in the form of []byte
key := k.Bytes()

Similarly, here is the code on Bob's side:

// Get a group. Use the default one would be enough.
g, _ := GetGroup(0)

// Generate a private key from the group.
// Use the default random number generator.
priv, _ := g.GeneratePrivateKey(nil)

// Get the public key from the private key.
pub := priv.Bytes()

// Receive a slice of bytes from Alice, which contains Alice's public key
a := Recv("Alice")

// Send the public key to Alice.
Send("Alice", pub)

// Recover Alice's public key
alicePubKey := NewPublicKey(a)

// Compute the key
k, _ := group.ComputeKey(alicePubKey, priv)

// Get the key in the form of []byte
key := k.Bytes()

To this point, the variables ”key” on both Alice and Bob side are same. It could be used as the secrete key for the later communication.

Index

Constants

View Source
const (
	VERSION = "r.20230928.2358"
)

Variables

This section is empty.

Functions

func DhKxClientExchange

func DhKxClientExchange(handleDhkxCliRecvFunc HandleDhkxCliRecvFunc, handleDhkxCliSendFunc HandleDhkxCliSendFunc) (e string, g *DHGroup, priv *DHKey, pub []byte, pubSrv []byte, shardSec string, shardExc string)

func DhKxGetRandomGroup

func DhKxGetRandomGroup(highOnly bool) int

func DhKxServerFinalizeExchange

func DhKxServerFinalizeExchange(g *DHGroup, priv *DHKey, handleDhkxSrvRecvFunc HandleDhkxSrvRecvFunc) (e string, pubCli []byte, shardSec string)

func DhKxServerInitExchange

func DhKxServerInitExchange(grpID int, handleDhkxSrvSendFunc HandleDhkxSrvSendFunc) (e string, g *DHGroup, priv *DHKey, pub []byte)

func DhKxValidateGroup

func DhKxValidateGroup(grpID int) bool

Types

type DHGroup

type DHGroup struct {
	// contains filtered or unexported fields
}

func CreateGroup

func CreateGroup(prime, generator *big.Int) (group DHGroup)

This function enables users to create their own custom DHGroup. Most users will not however want to use this function, and should prefer the use of GetGroup which supplies DHGroups defined in RFCs 2409 and 3526

WARNING! You should only use this if you know what you are doing. The behavior of the group returned by this function is not defined if prime is not in fact prime.

func GetGroup

func GetGroup(groupID int) (group DHGroup)

This function fetches a DHGroup by its ID as defined in either RFC 2409 or RFC 3526. Extra Groups added by unixman: 5, 15, 16, 17, 18 ; 101..107 If you are unsure what to use use group ID 0 for a sensible default value

func (*DHGroup) ComputeKey

func (self *DHGroup) ComputeKey(pubkey *DHKey, privkey *DHKey) (key *DHKey, err error)

func (*DHGroup) G

func (self *DHGroup) G() *big.Int

func (*DHGroup) GeneratePrivateKey

func (self *DHGroup) GeneratePrivateKey(randReader io.Reader) (key *DHKey, err error)

func (*DHGroup) P

func (self *DHGroup) P() *big.Int

type DHKey

type DHKey struct {
	// contains filtered or unexported fields
}

func NewPublicKey

func NewPublicKey(s []byte) *DHKey

func (*DHKey) Bytes

func (self *DHKey) Bytes() []byte

func (*DHKey) IsPrivateKey

func (self *DHKey) IsPrivateKey() bool

func (*DHKey) String

func (self *DHKey) String() string

type HandleDhkxCliRecvFunc

type HandleDhkxCliRecvFunc func() (string, []byte, int) // () : (err string, srvPubKey []byte, grpId, int) 		# Client Recv from Server

type HandleDhkxCliSendFunc

type HandleDhkxCliSendFunc func([]byte, []byte) string // (cliPubKey []byte, cliExch []byte) : (err string) 	# Client Send to Server

type HandleDhkxSrvRecvFunc

type HandleDhkxSrvRecvFunc func([]byte) (string, []byte, []byte) // () : (err string, cliPubKey []byte, cliExch []byte) 	# Server Recv from Client

type HandleDhkxSrvSendFunc

type HandleDhkxSrvSendFunc func([]byte, int) string // (srvPubKey []byte, grpID int) : (err string) 		# Server Send to Client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL