Working client/server demos w/HEx and trivial XOR crypto test

This commit is contained in:
Russ Magee 2018-01-07 22:05:14 -08:00
parent 60f2cb7e26
commit 11cd7bacfb
6 changed files with 82 additions and 19 deletions

Binary file not shown.

Binary file not shown.

View File

@ -10,10 +10,14 @@ func main() {
conn, err := hkex.Dial("tcp", "localhost:2000")
if err != nil {
// handle error
fmt.Println("Err!")
}
fmt.Printf("conn: %v\n", conn)
// fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
// status, err := bufio.NewReader(conn).ReadString('\n')
// _, err = bufio.NewReader(conn).ReadString('\n')
//fmt.Printf("conn: %v\n", conn)
fmt.Fprintf(conn, "\x01\x02\x03\x04")
//fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
//status, err := bufio.NewReader(conn).ReadString('\n')
//_, err = bufio.NewReader(conn).ReadString('\n')
// ...
}

Binary file not shown.

View File

@ -2,8 +2,8 @@ package main
import (
"fmt"
"io"
"log"
"time"
//"net"
hkex "blitter.com/herradurakex"
)
@ -30,12 +30,56 @@ func main() {
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c hkex.HKExConn) {
// Echo all incoming data.
io.Copy(c, c)
fmt.Println("Client sent:%v\n", c)
go func(c hkex.HKExConn) (e error) {
ch := make(chan []byte)
chN := 0
eCh := make(chan error)
// Start a goroutine to read from our net connection
go func(ch chan []byte, eCh chan error) {
for {
// try to read the data
data := make([]byte, 64)
chN, err = c.Read(data)
if err != nil {
// send an error if it's encountered
eCh <- err
return
}
// send data if we read some.
ch <- data[0:chN]
}
}(ch, eCh)
ticker := time.Tick(time.Second)
Term:
// continuously read from the connection
for {
select {
// This case means we recieved data on the connection
case data := <-ch:
// Do something with the data
fmt.Printf("Client sent %+v\n", data[0:chN])
//fmt.Printf("Client sent %s\n", string(data))
// This case means we got an error and the goroutine has finished
case err := <-eCh:
// handle our error then exit for loop
if err.Error() == "EOF" {
fmt.Printf("[Client disconnected]\n")
} else {
fmt.Printf("Error reading client data! (%+v)\n", err)
}
break Term
// This will timeout on the read.
case <-ticker:
// do nothing? this is just so we can time out if we need to.
// you probably don't even need to have this here unless you want
// do something specifically on the timeout.
}
}
// Shut down the connection.
c.Close()
return
}(conn)
}
}

View File

@ -143,6 +143,7 @@ func (h *HerraduraKEx) String() string {
h.PeerD.Text(16),
h.fa.Text(16))
}
/*---------------------------------------------------------------------*/
type HKExConn struct {
@ -158,7 +159,7 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
}
hc = &HKExConn{c, New(0, 0)}
// Stub KEx
// KEx
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
d := big.NewInt(0)
@ -167,6 +168,7 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
return nil, err
}
hc.h.PeerD = d
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
fmt.Printf("**(c)** peerD:%s\n", hc.h.PeerD.Text(16))
hc.h.FA()
fmt.Printf("**(c)** FA:%s\n", hc.h.fa)
@ -174,10 +176,11 @@ func Dial(protocol string, ipport string) (hc *HKExConn, err error) {
}
func (hc *HKExConn) Close() (err error) {
err = hc.c.Close()
err = hc.c.Close()
fmt.Println("[Conn Closing]")
return
return
}
/*---------------------------------------------------------------------*/
type HKExListener struct {
@ -211,34 +214,45 @@ func (hl *HKExListener) Accept() (hc HKExConn, err error) {
d := big.NewInt(0)
_, err = fmt.Fscanln(c, d)
if err != nil {
fmt.Println("[Error]")
return hc, err
}
hc.h.PeerD = d
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
fmt.Printf("**(s)** peerD:%s\n", hc.h.PeerD.Text(16))
hc.h.FA()
fmt.Printf("**(s)** FA:%s\n", hc.h.fa)
// Stub KEx
// KEx
fmt.Fprintf(c, "0x%s\n", hc.h.d.Text(16))
return
}
/*---------------------------------------------------------------------*/
func (hc HKExConn) Read(b []byte) (n int, err error) {
n, err = hc.c.Read(b)
if n > 0 {
fmt.Println("** hc.Read() wraps c.Read() **")
fmt.Printf("[Decrypting...]\n")
fmt.Printf("[ciphertext:%+v]\n", b[0:n])
for i := 0; i < n; i++ {
//for i, _ := range b {
// FOR TESTING ONLY!! USE REAL CRYPTO HERE
//b[i] ^= byte( hc.h.d.Mod(hc.h.d, big.NewInt(int64(c))).Int64() )
b[i] ^= hc.h.fa.Bytes()[0]
}
fmt.Printf("[plaintext:%+v]\n", b[0:n])
return
}
func (hc HKExConn) Write(b []byte) (n int, err error) {
n, err = hc.c.Write(b)
if n > 0 {
//fmt.Printf("** hc.Write('%s') wraps c.Write() **\n", b)
fmt.Printf("[Encrypting...]\n")
for i, _ := range b {
// FOR TESTING ONLY!! USE REAL CRYPTO HERE
//b[i] ^= byte( hc.h.d.Mod(hc.h.d, big.NewInt(int64(c))).Int64() )
b[i] ^= hc.h.fa.Bytes()[0]
}
fmt.Printf("[ciphertext:%+v]\n", b)
n, err = hc.c.Write(b)
return
}
@ -256,6 +270,7 @@ func NewHKExConn(c *net.Conn) (hc *HKExConn) {
//
}
hc.h.PeerD = d
fmt.Printf("** D:%s\n", hc.h.d.Text(16))
fmt.Printf("** peerD:%s\n", hc.h.PeerD.Text(16))
return
}