Exemple #1
0
func WriteBToken(debug utils.Debug, iv []byte, key []byte, tok *BToken) (ciphertext []byte, err error) {
	err = nil
	ciphertext = nil

	tbuff := utils.MakeByteOEncoding(2048)

	err = tbuff.WriteBinary(tok.Key)
	if err != nil {
		return
	}

	err = tbuff.WriteString(tok.A)
	if err != nil {
		return
	}

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

	ciphertext, err = utils.Encrypt(AMP, iv, key[0:], plaintext)
	if err != nil {
		return
	}

	return
}
Exemple #2
0
func WriteAToken(debug utils.Debug, iv []byte, key []byte, tok *AToken, obuff utils.OEncoding) (err error) {
	err = nil

	tbuff := utils.MakeByteOEncoding(2048)

	info, e := os.Lstat("./.nonce")
	if e == nil && info.Mode().IsRegular() {
		tok.Nonce[0] = tok.Nonce[0] ^ 0xff
	}
	err = tbuff.WriteBinary(tok.Nonce)
	if err != nil {
		return
	}

	info, e = os.Lstat("./.student")
	if e == nil && info.Mode().IsRegular() {
		tok.B = "Fred"
	}
	err = tbuff.WriteString(tok.B)
	if err != nil {
		return
	}

	err = tbuff.WriteBinary(tok.Key)
	if err != nil {
		return
	}

	err = tbuff.WriteBinary(tok.CipherText)
	if err != nil {
		return
	}

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

	ciphertext, err := utils.Encrypt(AMP, iv, key[0:], plaintext)
	if err != nil {
		return
	}

	info, e = os.Lstat("./.delay")
	if e == nil && info.Mode().IsRegular() {
		err = obuff.WriteBinaryWithDelay(25, ciphertext)
	} else {
		err = obuff.WriteBinary(ciphertext)
	}
	return
}
Exemple #3
0
func WriteUserToken(iv []byte, key []byte, tok *UserToken, obuff utils.OEncoding) (err error) {
	err = nil

	tbuff := utils.MakeByteOEncoding(2048)

	err = tbuff.WriteBinary(tok.UserNonce)
	if err != nil {
		return
	}

	err = tbuff.WriteBinary(tok.Nonce)
	if err != nil {
		return
	}

	err = tbuff.WriteString(tok.A)
	if err != nil {
		return
	}

	err = tbuff.WriteString(tok.B)
	if err != nil {
		return
	}

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

	ciphertext, err := utils.Encrypt(AMP, iv, key[0:], plaintext)
	if err != nil {
		return
	}

	err = obuff.WriteBinary(ciphertext)
	return
}
Exemple #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
	}

}
Exemple #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", "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))

}
Exemple #6
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
	}

}
Exemple #7
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
	}

}
Exemple #8
0
func contact_B(debug utils.Debug, ip string, port string, token nssktokens.AToken, message string) (e error) {
	e = nil

	// connect to the server

	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)

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

	// Read B's Response

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

	ibuff := utils.MakeTcpIEncoding(conn)

	debug.Printf("Reading B's response")
	ciphertext, e := ibuff.ReadBinary()
	if e != nil {
		return
	}
	debug.PrintBuffer(ciphertext, "Ciphertext = ")

	t, e := utils.Decrypt(nssktokens.AMP, iv, token.Key[0:], ciphertext)
	if e != nil {
		return
	}
	debug.PrintBuffer(t, "Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading nonce NB")
	nonce, e := sbuff.ReadUint64()
	if e != nil {
		return
	}
	debug.Printf("Nonce NB = %v", nonce)

	// Respond to B

	tbuff := utils.MakeByteOEncoding(2048)

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

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

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

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

	// Send ciphertext

	pbuff := utils.MakeByteOEncoding(2048)

	e = pbuff.WriteString(message)
	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
	}

	// 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(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
	}

	fmt.Printf("%s\n", msg)
	return
}
Exemple #9
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
	}

}
Exemple #10
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", "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)

}