Example #1
0
File: zk.go Project: 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)
}
Example #2
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
	}

}
Example #3
0
func main() {

	var help = flag.Bool("h", false, "help")
	flag.Parse()
	if *help || flag.NArg() != 0 {
		fmt.Printf("Usage: showor\n")
		flag.PrintDefaults()
		return
	}

	utils.Version()
	fmt.Printf("NSSK Key for A = %s\n", aeskey.KeyAString())
	fmt.Printf("NSSK  IV for A = %s\n", aeskey.IvAString())

	fmt.Printf("NSSK Key for B = %s\n", aeskey.KeyBString())
	fmt.Printf("NSSK  IV for B = %s\n", aeskey.IvBString())

	fmt.Printf("NSSK  IV for Session = %s\n", aeskey.IvString())
}
Example #4
0
func Start() {
	var p = flag.Int("p", 8000, "base port")
	var help = flag.Bool("h", false, "help")
	flag.Parse()
	if *help || flag.NArg() != 0 {
		fmt.Printf("Usage: server\n")
		flag.PrintDefaults()
		return
	}

	log := utils.NewLog("server")
	log.Printf("Server started.")
	utils.Version()
	workers.Apply(listener, *p)
	for {
		fmt.Printf("++++ %s\n", time.Now().Local().Format(time.UnixDate))
		time.Sleep(1800000000000) // 30 min
	}
}
Example #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))

}
Example #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))

}
Example #7
0
File: ora.go Project: 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)

}