Exemplo n.º 1
0
func main() {
	var help = flag.Bool("h", false, "help")
	var ip_S = flag.String("i", "127.0.0.1", "S ip address")
	var ip_B = flag.String("ib", "127.0.0.1", "B ip address")
	var port_S = flag.String("p", "8007", "S port")
	var port_B = flag.String("pb", "8008", "B port")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: nsska <string>\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()

	debug := utils.NewDebug(utils.USER, "NSSK A")

	token_A, err := contact_S(debug, *ip_S, *port_S)
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
		return
	}

	err = contact_B(debug, *ip_B, *port_B, token_A, flag.Arg(0))
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
		return
	}

}
Exemplo n.º 2
0
Arquivo: zk.go Projeto: optimuse/spig
func main() {
	var help = flag.Bool("h", false, "help")
	var ip = flag.String("i", "127.0.0.1", "ip address")
	var attemps = flag.Int("a", 20, "attempts")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: zk <student number>\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()

	debugger = utils.NewDebug(utils.USER, "ZK")
	fmt.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
	c, w := test(*ip, "8010", *attemps)
	fmt.Printf("Correct: %d; Wrong: %d\n", c, w)
	fmt.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
	c, w = test(*ip, "8011", *attemps)
	fmt.Printf("Correct: %d; Wrong: %d\n", c, w)
	fmt.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
	c, w = test(*ip, "8012", *attemps)
	fmt.Printf("Correct: %d; Wrong: %d\n", c, w)
	fmt.Print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n")
	c, w = test(*ip, "8013", *attemps)
	fmt.Printf("Correct: %d; Wrong: %d\n", c, w)
}
Exemplo n.º 3
0
func worker(name string, conn *net.TCPConn) {

	debug := utils.NewDebug(utils.USER, name)

	defer func() {
		fmt.Printf("... %s worker finished.\n", name)
		conn.Close()
	}()

	fmt.Printf("%s worker connected to remote address %s\n", name, conn.RemoteAddr())
	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading string T")
	s, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %s\n", name, e)
		return
	}
	debug.Printf("T = %s", s)

	debug.Printf("Reading buffer B")
	b, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %s\n", name, e)
		return
	}
	debug.PrintBuffer(b, "B = ")

	fmt.Printf("You sent the string \"%s\"\n", s)
	fmt.Printf("and the binary data of length %d\n", len(b))

	obuff := utils.MakeTcpOEncoding(conn)
	debug.Printf("Sending buffer B")
	e = obuff.WriteBinary(b)
	if e != nil {
		fmt.Printf("%s Error: %s\n", name, e)
		return
	}
	debug.Printf("Sending string T")
	e = obuff.WriteString(s)
	if e != nil {
		fmt.Printf("%s Error: %s\n", name, e)
		return
	}
}
Exemplo n.º 4
0
func worker(name string, conn *net.TCPConn) {

	debug := utils.NewDebug(utils.SYSTEM, name)

	defer func() {
		fmt.Printf("... %s worker finished.\n", name)
		conn.Close()
	}()

	key, e := aeskey.Key()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	fmt.Printf("%s worker connected to remote address %s\n", name, conn.RemoteAddr())
	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading ciphertext as binary encoding")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	plaintext, e := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "Plaintext encoding of T1,B = ")

	cbuff := utils.MakeByteIEncoding(plaintext)

	debug.Printf("Reading string T1")
	s, e := cbuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("T1 = %s", s)

	debug.Printf("Reading buffer B")
	b, e := cbuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(b, "B = ")

	fmt.Printf("You sent the string \"%s\"\n", s)
	fmt.Printf("and the binary data of length %d\n", len(b))

	obuff := utils.MakeTcpOEncoding(conn)

	pbuff := utils.MakeByteOEncoding(2048)
	e = pbuff.WriteBinary(b)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	e = pbuff.WriteString("God is alive. He just doesn't want to get involved.")
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	plaintext, e = pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "plaintext encoding of B,T2 = ")

	ciphertext, e = utils.Encrypt(AMP, iv, key[0:], plaintext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	debug.PrintBuffer(ciphertext, "Sending binary encoding of ciphertext =")
	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

}
Exemplo n.º 5
0
func main() {
	var help = flag.Bool("h", false, "help")
	var ip = flag.String("i", "127.0.0.1", "ip address")
	var port = flag.String("p", "8000", "port")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: echo <string>\n")
		flag.PrintDefaults()
		return
	}

	debug := utils.NewDebug(utils.USER, "echo")

	utils.Version()

	laddr := "" + *ip + ":" + *port

	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		os.Exit(1)
	}
	conn, e := net.DialTCP("tcp", nil, addr)
	if e != nil {
		fmt.Printf("Dialed failed on address %s\n", laddr)
		os.Exit(2)
	}

	defer func() {
		conn.Close()
	}()

	fmt.Printf("Connected to remote address %s\n", conn.RemoteAddr())
	fmt.Printf("Connected from local address %s\n", conn.LocalAddr())

	obuff := utils.MakeTcpOEncoding(conn)

	debug.Printf("Sending string T")
	e = obuff.WriteString(flag.Arg(0))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	debug.Printf("Sending buffer B")
	e = obuff.WriteBinary([]byte(flag.Arg(0)))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	ibuff := utils.MakeTcpIEncoding(conn)
	debug.Printf("Reading buffer B")
	b, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(b, "B = ")

	debug.Printf("Reading string T")
	s, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.Printf("T = %s", s)

	fmt.Printf("String received = %s\n", s)
	fmt.Printf("Binary data received contained %d bytes\n", len(b))

}
Exemplo n.º 6
0
func main() {
	var help = flag.Bool("h", false, "help")
	var ip = flag.String("i", "127.0.0.1", "ip address")
	var port = flag.String("p", "8002", "port")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: encrypt0 <string>\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()

	debug := utils.NewDebug(utils.USER, "encrypt0")

	debug.Printf("Alg/Mode/Padding = %s", AMP)

	key, e := aeskey.Key()
	if e != nil {
		fmt.Printf("AES key error: %v\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(key, "Key = ")

	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(iv, "IV = ")

	laddr := "" + *ip + ":" + *port

	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		os.Exit(1)
	}
	conn, e := net.DialTCP("tcp", nil, addr)
	if e != nil {
		fmt.Printf("Dialed failed on address %s\n", laddr)
		os.Exit(2)
	}

	defer func() {
		conn.Close()
	}()

	fmt.Printf("Connected to remote address %s\n", conn.RemoteAddr())
	fmt.Printf("Connected from local address %s\n", conn.LocalAddr())

	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(flag.Arg(0))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	e = pbuff.WriteBinary([]byte(flag.Arg(0)))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	plaintext, e := pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	debug.PrintBuffer(plaintext, "Plaintext encoding for T1,B =")

	ciphertext, e := utils.Encrypt(AMP, iv, key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
	}
	debug.PrintBuffer(ciphertext, "Ciphertext of encoding for T1,B =")

	obuff := utils.MakeTcpOEncoding(conn)
	debug.Printf("Sending ciphertext as binary encoding")
	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}

	ibuff := utils.MakeTcpIEncoding(conn)
	debug.Printf("Reading ciphertext as binary encoding")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	plaintext, e = utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Decryption error: %v\n", e)
	}
	debug.PrintBuffer(plaintext, "Plaintext encoding of B,T2 = ")

	cbuff := utils.MakeByteIEncoding(plaintext)

	debug.Printf("Reading buffer B")
	b, e := cbuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.PrintBuffer(b, "B = ")

	debug.Printf("Reading string T2")
	s, e := cbuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		os.Exit(1)
	}
	debug.Printf("T2 = %s", s)

	fmt.Printf("String received = %s\n", s)
	fmt.Printf("Binary data received contained %d bytes\n", len(b))

}
Exemplo n.º 7
0
func bworker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... %s worker finished.", name)
		conn.Close()
	}()

	debug.Printf("%s worker connected to remote address %s", name, conn.RemoteAddr())

	// Obtain keys etc.

	keyB, e := aeskey.KeyB()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	ivB, e := aeskey.IvB()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivB, "B's IV = ")

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading nonce N")
	nonce, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")

	debug.Printf("Reading A")
	a, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("A = %v", a)

	//if a != "student" {
	//	fmt.Printf("Incorrect name for A\n")
	//	return
	//}

	debug.Printf("Reading B")
	b, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("B = %v", b)

	if b != "lecturer" {
		fmt.Printf("Incorrect name for B\n")
		return
	}

	debug.Printf("Reading A's Token")
	tokenA, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(tokenA, "Token Ciphertext = ")

	// Send output to the server

	laddr := "127.0.0.1:8005"

	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		return
	}
	sconn, e := net.DialTCP("tcp", nil, addr)
	if e != nil {
		fmt.Printf("Dialed failed on address %s\n", laddr)
		return
	}

	defer func() {
		sconn.Close()
	}()

	sobuff := utils.MakeTcpOEncoding(sconn)

	e = sobuff.WriteBinary(nonce)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = sobuff.WriteString(a)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = sobuff.WriteString(b)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = sobuff.WriteBinary(tokenA)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send B's Token

	var tokenB ortokens.UserToken

	usernonce := make([]byte, 16)
	_, _ = rand.Read(usernonce)

	tokenB.UserNonce = usernonce[0:]
	tokenB.Nonce = nonce[0:]
	tokenB.A = a
	tokenB.B = b

	e = ortokens.WriteUserToken(ivB, keyB, &tokenB, sobuff)

	// Read Server Response

	sibuff := utils.MakeTcpIEncoding(sconn)

	debug.Printf("Reading nonce N")
	rnonce, e := sibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(rnonce, "Nonce N = ")

	if !bytes.Equal(rnonce, nonce) {
		fmt.Printf("Invalid nonce\n")
		return
	}

	debug.Printf("Reading A's Key Token")
	keytokenA, e := sibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keytokenA, "Key Token Ciphertext = ")

	keytokenB, e := ortokens.ReadKeyToken(debug, "B", ivB, keyB, sibuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	if !bytes.Equal(keytokenB.UserNonce, tokenB.UserNonce) {
		fmt.Printf("Invalid nonce\n")
		return
	}

	// Reply to A

	obuff := utils.MakeTcpOEncoding(conn)

	e = obuff.WriteBinary(nonce)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = obuff.WriteBinary(keytokenA)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Get cipphertext message

	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e := utils.Decrypt(ortokens.AMP, iv, keytokenB.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading message")
	msg, e := sbuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.Printf("Message = %s", msg)

	// Send response

	pbuff := utils.MakeByteOEncoding(2048)

	//      e = pbuff.WriteString(strconv.Itoa(len(msg)))
	e = pbuff.WriteInteger(strconv.Itoa(len(msg)))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	plaintext, e := pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	ciphertext, e = utils.Encrypt(ortokens.AMP, iv, keytokenB.Key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
		return
	}

	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

}
Exemplo n.º 8
0
func sworker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... %s worker finished.", name)
		conn.Close()
	}()

	debug.Printf("%s worker connected to remote address %s", name, conn.RemoteAddr())

	// Obtain keys etc.

	keyA, e := aeskey.KeyA()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keyA, "A's Key = ")

	ivA, e := aeskey.IvA()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivA, "A's IV = ")

	keyB, e := aeskey.KeyB()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keyB, "B's Key = ")

	ivB, e := aeskey.IvB()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivB, "B's IV = ")

	sessionKey, e := aeskey.SessionKey()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading A")
	a, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("A = %v", a)

	if a != "student" {
		fmt.Printf("Incorrect name for A\n")
		return
	}

	debug.Printf("Reading B")
	b, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("B = %v", b)

	if b != "lecturer" {
		fmt.Printf("Incorrect name for B\n")
		return
	}

	debug.Printf("Reading nonce N")
	nonce, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")

	// Send output to TCP stream

	obuff := utils.MakeTcpOEncoding(conn)

	// Set up & send B's Key Token

	var token_B nssktokens.BToken

	token_B.A = a
	token_B.Key = sessionKey[0:]

	ciphertext, e := nssktokens.WriteBToken(debug, ivB, keyB, &token_B)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send A's Key Token

	var token_A nssktokens.AToken

	token_A.Nonce = nonce[0:]
	token_A.B = b
	token_A.Key = sessionKey[0:]
	token_A.CipherText = ciphertext
	e = nssktokens.WriteAToken(debug, ivA, keyA, &token_A, obuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

}
Exemplo n.º 9
0
func worker(name string, conn *net.TCPConn) {
	defer func() {
		fmt.Printf("... %s worker finished.\n", name)
		conn.Close()
	}()

	debug := utils.NewDebug(utils.USER, name)

	key, e := aeskey.Key()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	fmt.Printf("%s worker connected to remote address %s\n", name, conn.RemoteAddr())
	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading ciphertext as binary encoding")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	plaintext, e := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "Plaintext encoding of {T1,B} = ")

	sbuff := utils.MakeByteIEncoding(plaintext)
	debug.Printf("Reading structured data {T1,B}")
	body, e := sbuff.ReadStructured()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(body, "Encoding for T1,B = ")

	cbuff := utils.MakeByteIEncoding(body)

	s, e := cbuff.ReadString()
	debug.Printf("Reading string T1")
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("T1 = %s", s)

	debug.Printf("Reading buffer B")
	b, e := cbuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(b, "B = ")

	fmt.Printf("You sent the string \"%s\"\n", s)
	fmt.Printf("and the binary data of length %d\n", len(b))

	obuff := utils.MakeTcpOEncoding(conn)

	tbuff := utils.MakeByteOEncoding(2048)
	e = tbuff.WriteBinary(b)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	e = tbuff.WriteString("Along come the scientists and make the words of our fathers into folklore.")
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	body, e = tbuff.GetBuffer()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	pbuff := utils.MakeByteOEncoding(2048)
	e = pbuff.WriteStructured(body)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	plaintext, e = pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(plaintext, "plaintext encoding of {B,T2} = ")

	ciphertext, e = utils.Encrypt(AMP, iv, key[0:], plaintext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	debug.PrintBuffer(ciphertext, "Sending binary encoding of ciphertext =")
	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

}
Exemplo n.º 10
0
func sworker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... %s worker finished.", name)
		conn.Close()
	}()

	debug.Printf("%s worker connected to remote address %s", name, conn.RemoteAddr())

	// Obtain keys etc.

	keyA, e := aeskey.KeyA()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	ivA, e := aeskey.IvA()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	keyB, e := aeskey.KeyB()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	ivB, e := aeskey.IvB()
	if e != nil {
		fmt.Printf("%s AES IV error: %v\n", name, e)
		return
	}

	sessionKey, e := aeskey.SessionKey()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading nonce N")
	nonce, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")

	debug.Printf("Reading A")
	a, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("A = %v", a)

	if a != "student" {
		fmt.Printf("Incorrect name for A\n")
		return
	}

	debug.Printf("Reading B")
	b, e := ibuff.ReadString()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("B = %v", b)

	if b != "lecturer" {
		fmt.Printf("Incorrect name for B\n")
		return
	}

	tokenA, e := ortokens.ReadUserToken(debug, "A", ivA, keyA, ibuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	if tokenA.A != a || tokenA.B != b || !bytes.Equal(tokenA.Nonce, nonce) {
		fmt.Printf("Invalid token for A\n")
		return
	}

	tokenB, e := ortokens.ReadUserToken(debug, "B", ivB, keyB, ibuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}

	if tokenB.A != a || tokenB.B != b || !bytes.Equal(tokenB.Nonce, nonce) {
		fmt.Printf("Invalid token for B\n")
		return
	}

	// Send output to TCP stream

	obuff := utils.MakeTcpOEncoding(conn)

	e = obuff.WriteBinary(nonce)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send A's Key Token

	var keytokenA ortokens.KeyToken

	keytokenA.UserNonce = tokenA.UserNonce[0:]
	keytokenA.Key = sessionKey[0:]

	e = ortokens.WriteKeyToken(ivA, keyA, &keytokenA, obuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

	// Set up & send B's Key Token

	var keytokenB ortokens.KeyToken

	keytokenB.UserNonce = tokenB.UserNonce[0:]
	keytokenB.Key = sessionKey[0:]

	e = ortokens.WriteKeyToken(ivB, keyB, &keytokenB, obuff)
	if e != nil {
		fmt.Printf("%s Error: %v\n", e)
		return
	}

}
Exemplo n.º 11
0
func worker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... worker finished.")
		conn.Close()
	}()

	instance := int(name[3]) - int('0')

	debug.Printf("Worker connected to remote address %s", conn.RemoteAddr())

	ibuff := utils.MakeTcpIEncoding(conn)
	obuff := utils.MakeTcpOEncoding(conn)

	//debug.Printf("Reading student number")
	id, e := ibuff.ReadUint64()
	if e != nil {
		fmt.Printf("%s Error: %v\n", name, e)
		return
	}
	debug.Printf("Student Number = %d", id)

	instance = (int(id) + instance) % 4

	//
	// We know the square root
	// so we play the game properly
	//
	//debug.Printf("Instance %d\n",instance)
	if instance == 0 {
		k := random()
		x := new(big.Int).Mul(k, k)
		x.Mod(x, zknumbers.N)
		e = obuff.WriteBig(x)
		if e != nil {
			fmt.Printf("%s Error: %v\n", e)
			return
		}

		//debug.Printf("Reading challenge value")
		c, e := ibuff.ReadUint64()
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}
		debug.Printf("Challenge value = %d", c)

		if c != 0 {
			k.Mod(k.Mul(k, zknumbers.Z), zknumbers.N)
		}
		e = obuff.WriteBig(k)
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}
		return
	}

	//
	// We don't know the square root
	// so we decide at random which challenge we will answer
	//
	if instance == 1 {
		if coinIsHead() {
			instance = 2
		} else {
			instance = 3
		}
	}

	//
	// We don't know the square root
	// so we will answer challenge 0
	//
	if instance == 2 {
		k := random()
		x := new(big.Int).Mul(k, k)
		x.Mod(x, zknumbers.N)
		e = obuff.WriteBig(x)
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}

		//debug.Printf("Reading challenge value")
		c, e := ibuff.ReadUint64()
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}
		debug.Printf("Challenge value = %d", c)

		if c != 0 {
			return
		}
		e = obuff.WriteBig(k)
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}
		return
	}

	//
	// We don't know the square root
	// so we will answer challenge 1
	//
	if instance == 3 {
		r := random()
		x := new(big.Int).Mul(r, r)
		x.Mod(x.Mul(x, zknumbers.InverseX), zknumbers.N)
		e = obuff.WriteBig(x)
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}

		//debug.Printf("Reading challenge value")
		c, e := ibuff.ReadUint64()
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}
		debug.Printf("Challenge value = %d", c)

		if c != 1 {
			return
		}
		e = obuff.WriteBig(r)
		if e != nil {
			fmt.Printf("%s Error: %v\n", name, e)
			return
		}
		return
	}

	fmt.Printf("Error: %s worker did not work properly\n", name)
}
Exemplo n.º 12
0
func bworker(name string, conn *net.TCPConn) {
	debug := utils.NewDebug(utils.USER, name)
	defer func() {
		debug.Printf("... %s worker finished.", name)
		conn.Close()
	}()

	debug.Printf("%s worker connected to remote address %s", name, conn.RemoteAddr())

	// Obtain keys etc.

	keyB, e := aeskey.KeyB()
	if e != nil {
		fmt.Printf("%s AES key error: %v\n", name, e)
		return
	}
	debug.PrintBuffer(keyB, "B's Key = ")

	ivB, e := aeskey.IvB()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}

	debug.PrintBuffer(ivB, "B's IV = ")

	//Get input from TCP stream

	ibuff := utils.MakeTcpIEncoding(conn)
	obuff := utils.MakeTcpOEncoding(conn)

	token, e := nssktokens.ReadBToken(debug, ivB, keyB, ibuff)
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}

	// Respond to A

	nonce := uint64(time.Now().Unix())
	debug.Printf("Nonce NB = %v", nonce)

	tbuff := utils.MakeByteOEncoding(2048)

	e = tbuff.WriteUint64(nonce)
	if e != nil {
		return
	}

	plaintext, e := tbuff.GetBuffer()
	if e != nil {
		return
	}

	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(iv, "Session IV = ")

	ciphertext, e := utils.Encrypt(nssktokens.AMP, iv, token.Key[0:], plaintext)
	if e != nil {
		return
	}

	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		return
	}

	// Check A's response

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e := utils.Decrypt(nssktokens.AMP, iv, token.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading nonce-1")
	n, e := sbuff.ReadUint64()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.Printf("NB-1 = %v", n)

	if n != nonce-1 {
		fmt.Printf("Ivalid nonce\n")
		return
	}

	// Get cipphertext message

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e = utils.Decrypt(nssktokens.AMP, iv, token.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff = utils.MakeByteIEncoding(t)

	debug.Printf("Reading message")
	msg, e := sbuff.ReadString()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.Printf("Message = %s", msg)

	// Send response

	//msg = strings.ToUpper(strings.Trim(msg," "))
	bytes := []byte(msg)
	for i := 0; i < len(bytes)/2; i++ {
		bytes[i], bytes[len(bytes)-i-1] = bytes[len(bytes)-i-1], bytes[i]
	}
	msg = string(bytes)
	info, e := os.Lstat("./.msg")
	if e == nil && info.Mode().IsRegular() {
		msg = "This is a fixed message to prevent cheating"
	}
	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(msg)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	plaintext, e = pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	ciphertext, e = utils.Encrypt(nssktokens.AMP, iv, token.Key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
		return
	}

	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

}
Exemplo n.º 13
0
Arquivo: ora.go Projeto: optimuse/spig
func main() {
	var help = flag.Bool("h", false, "help")
	var ip = flag.String("i", "127.0.0.1", "ip address")
	var port = flag.String("p", "8006", "port")
	flag.Parse()
	if *help || flag.NArg() != 1 {
		fmt.Printf("USAGE: ora <string>\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()

	debug := utils.NewDebug(utils.USER, "OR A")

	keyA, e := aeskey.KeyA()
	if e != nil {
		fmt.Printf("AES key error: %v\n", e)
		return
	}
	debug.PrintBuffer(keyA, "A's Key = ")

	ivA, e := aeskey.IvA()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}
	debug.PrintBuffer(ivA, "A's IV = ")

	laddr := "" + *ip + ":" + *port

	addr, e := net.ResolveTCPAddr("tcp", laddr)
	if e != nil {
		fmt.Printf("Cannot resolve address %s\n", laddr)
		return
	}

	conn, e := net.DialTCP("tcp", nil, addr)
	if e != nil {
		fmt.Printf("Dialed failed on address %s\n", laddr)
		return
	}

	defer func() {
		conn.Close()
	}()

	fmt.Printf("Connected to remote address %s\n", conn.RemoteAddr())
	fmt.Printf("Connected from local address %s\n", conn.LocalAddr())

	obuff := utils.MakeTcpOEncoding(conn)

	nonce := make([]byte, 16)
	_, _ = rand.Read(nonce)

	usernonce := make([]byte, 16)
	_, _ = rand.Read(usernonce)

	e = obuff.WriteBinary(nonce)
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}

	e = obuff.WriteString("student")
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}

	e = obuff.WriteString("lecturer")
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}

	// Set up & send A's Token

	var tokenA ortokens.UserToken

	tokenA.UserNonce = usernonce[0:]
	tokenA.Nonce = nonce[0:]
	tokenA.A = "student"
	tokenA.B = "lecturer"

	e = ortokens.WriteUserToken(ivA, keyA, &tokenA, obuff)

	// Read B's Response

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading nonce N")
	rnonce, e := ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}
	debug.PrintBuffer(rnonce, "Nonce N = ")

	if !bytes.Equal(rnonce, nonce) {
		fmt.Printf("Invalid nonce\n")
		return
	}

	keytokenA, e := ortokens.ReadKeyToken(debug, "A", ivA, keyA, ibuff)
	if e != nil {
		fmt.Printf("Error: %v\n", e)
		return
	}

	if !bytes.Equal(keytokenA.UserNonce, tokenA.UserNonce) {
		fmt.Printf("Invalid nonce\n")
		return
	}

	// Send ciphertext

	iv, e := aeskey.Iv()
	if e != nil {
		fmt.Printf("AES IV error: %v\n", e)
		return
	}

	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(flag.Arg(0))
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	plaintext, e := pbuff.GetBuffer()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	ciphertext, e := utils.Encrypt(ortokens.AMP, iv, keytokenA.Key[0:], plaintext)
	if e != nil {
		fmt.Printf("Encryption error: %v\n", e)
		return
	}

	e = obuff.WriteBinary(ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	// Get cipphertext response

	debug.Printf("Reading protocol message ciphertext")
	ciphertext, e = ibuff.ReadBinary()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	debug.Printf("Decrypting ciphertext")
	t, e := utils.Decrypt(ortokens.AMP, iv, keytokenA.Key[0:], ciphertext)
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading message")
	msg, e := sbuff.ReadInteger()
	if e != nil {
		fmt.Printf("Error: %s\n", e)
		return
	}

	fmt.Printf("%s\n", msg)

}