示例#1
0
func ReadBToken(debug utils.Debug, iv []byte, key []byte, ibuff utils.IEncoding) (tok BToken, err error) {
	err = nil

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

	t, err := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if err != nil {
		return
	}
	debug.PrintBuffer(t, "Token Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading session key")
	skey, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(skey, "Session Key = ")
	tok.Key = skey[0:]

	debug.Printf("Reading A")
	a, err := sbuff.ReadString()
	if err != nil {
		return
	}
	debug.Printf("A = %v", a)
	tok.A = a

	return
}
示例#2
0
文件: ortokens.go 项目: optimuse/spig
func ReadKeyToken(debug utils.Debug, user string, iv []byte, key []byte, ibuff utils.IEncoding) (tok KeyToken, err error) {
	err = nil

	debug.Printf("Reading %s's key token", user)
	ciphertext, err := ibuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(ciphertext, "Key Token Ciphertext = ")

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

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading nonce N%s", user)
	usernonce, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(usernonce, "Nonce = ")
	tok.UserNonce = usernonce[0:]

	debug.Printf("Reading session key")
	skey, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(skey, "Session Key = ")
	tok.Key = skey[0:]

	return
}
示例#3
0
func ReadAToken(debug utils.Debug, iv []byte, key []byte, ibuff utils.IEncoding) (tok AToken, err error) {
	err = nil

	debug.Printf("Reading A's token")
	ciphertext, err := ibuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(ciphertext, "Token Ciphertext = ")

	t, err := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if err != nil {
		return
	}
	debug.PrintBuffer(t, "Token Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading nonce N")
	nonce, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")
	tok.Nonce = nonce[0:]

	debug.Printf("Reading B")
	b, err := sbuff.ReadString()
	if err != nil {
		return
	}
	debug.Printf("B = %v", b)
	tok.B = b

	debug.Printf("Reading session key")
	skey, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(skey, "Session Key = ")
	tok.Key = skey[0:]

	debug.Printf("Reading ciphertext of B's token")
	ct, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(ct, "Ciphertext of B's token = ")
	tok.CipherText = ct[0:]
	return
}
示例#4
0
文件: nsska.go 项目: optimuse/spig
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
}
示例#5
0
文件: ortokens.go 项目: optimuse/spig
func ReadUserToken(debug utils.Debug, user string, iv []byte, key []byte, ibuff utils.IEncoding) (tok UserToken, err error) {
	err = nil

	debug.Printf("Reading %s's token", user)
	ciphertext, err := ibuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(ciphertext, "Token Ciphertext = ")

	t, err := utils.Decrypt(AMP, iv, key[0:], ciphertext)
	if err != nil {
		return
	}
	debug.PrintBuffer(t, "Token Plaintext = ")

	sbuff := utils.MakeByteIEncoding(t)

	debug.Printf("Reading nonce N%s", user)
	usernonce, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(usernonce, "Nonce = ")
	tok.UserNonce = usernonce[0:]

	debug.Printf("Reading nonce N")
	nonce, err := sbuff.ReadBinary()
	if err != nil {
		return
	}
	debug.PrintBuffer(nonce, "Nonce N = ")
	tok.Nonce = nonce[0:]

	debug.Printf("Reading A")
	a, err := sbuff.ReadString()
	if err != nil {
		return
	}
	debug.Printf("A = %v", a)
	tok.A = a

	debug.Printf("Reading B")
	b, err := sbuff.ReadString()
	if err != nil {
		return
	}
	debug.Printf("B = %v", b)
	tok.B = b
	return
}