Esempio n. 1
0
func CreateACMessageNACL(context *ackp.SecKey, rnd, msg, myNick []byte) (out []byte, err error) {
	//var noncebyte [24]byte

	/* lets build our header */
	myHdr, intHdr, err := BuildHeader([]byte(msgHdrAC))
	if err != nil {
		return nil, &acutl.AcError{Value: -1, Msg: "CreateACMessageNACL().BuildHeader(): ", Err: err}
	}

	// first let's compress
	myBody, err := acutl.CompressData(msg)
	if err != nil {
		return nil, &acutl.AcError{Value: -2, Msg: "CreateACMessageNACL().CompressData(): ", Err: err}
	}

	//BuildNonceAC(inonce uint32, bob, mynick, myhdr []byte) (nonce []byte, noncebyte *[24]byte, err error)
	_, noncebyte, err := BuildNonceAC(context.GetNonce(), context.GetBob(), myNick, myHdr)

	// OPEN the key
	// XXX new to check rnd and context.key are the same size
	/*
		for j := 0; j < len(rnd); j++ {
			context.key[j] = context.key[j] ^ rnd[j]
		}
	*/
	context.RndKey(rnd)

	// encrypt
	myCipher := secretbox.Seal(nil, myBody, noncebyte, context.GetSealKey())

	// close the key
	context.RndKey(rnd)
	/*
		for j := 0; j < len(rnd); j++ {
			context.key[j] = context.key[j] ^ rnd[j]
		}
	*/

	// XXX error checking
	out, err = packMessageAC(&intHdr, context.GetNonce(), &myCipher)

	//fmt.Fprintf(os.Stderr, "NACL PB == AC MSG OUT[%d]: %s\n", len(out), out)
	//context.nonce++
	context.IncNonce(0)

	return out, nil
}
Esempio n. 2
0
func OpenACMessageNACL(context *ackp.SecKey, rnd, cmsg, peerNick, myNick []byte) (out []byte, err error) {
	fmt.Fprintf(os.Stderr, "OpenACMessageNACL()\n")
	b64, err := acutl.B64DecodeData(cmsg)
	if err != nil {
		return nil, &acutl.AcError{Value: -1, Msg: "OpenACMessageNACL(): ", Err: err}
	}

	cnonce, myHdr, ciphertext, err := unpackMessageAC(b64)
	if err != nil {
		return nil, &acutl.AcError{Value: -2, Msg: "OpenACMessageNACL(): ", Err: err}
	}

	// XXX this is to handle private message instead of channel communication
	// as the destination are assymetrical eau's dst is frl and frl's dst is eau
	//
	ac_bob := context.GetBob()
	ok_bob, _ := IsValidChannelName(ac_bob)
	fmt.Fprintf(os.Stderr, "[+] OpenACMessage: is %s a valid channel: %t\n", ac_bob, ok_bob)
	if ok_bob == false && len(myNick) > 0 {
		ac_bob = myNick
	}

	/* let's build the nonce */
	//BuildNonceAC(inonce uint32, bob, mynick, myhdr []byte) (nonce []byte, noncebyte *[24]byte, err error)
	_, noncebyte, err := BuildNonceAC(cnonce, ac_bob, peerNick, myHdr)

	// OPEN the key
	context.RndKey(rnd)
	// XXX new to check rnd and context.key are the same size
	/*
		for j := 0; j < len(rnd); j++ {
			context.key[j] = context.key[j] ^ rnd[j]
		}
	*/

	packed, ok := secretbox.Open(nil, ciphertext, noncebyte, context.GetSealKey())

	// Close the key
	context.RndKey(rnd)
	/*
		for j := 0; j < len(rnd); j++ {
			context.key[j] = context.key[j] ^ rnd[j]
		}
	*/

	if ok == false {
		//return nil, acprotoError(1, "OpenACMessage().SecretOpen(): false ", nil)
		return nil, &acutl.AcError{Value: -3, Msg: "OpenACMessageNACL().SecretOpen(): ", Err: nil}
	}

	out, err = acutl.DecompressData(packed)
	if err != nil {
		return nil, &acutl.AcError{Value: -3, Msg: "OpenACMessageNACL().DecompressData(): ", Err: err}
	}

	//nonceval = acIn.GetNonce()
	// update the nonce value
	/*
		if cnonce > context.nonce {
			context.nonce = cnonce + 1
		} else {
			context.nonce++
		}
	*/
	context.IncNonce(cnonce)
	return out, nil
	//return nil, nil
}