Esempio n. 1
0
File: box.go Progetto: 4cdn/srndv2
// encrypts a message to a user given their public key is known
// returns an encrypted box
func CryptoBox(msg, nounce, pk, sk []byte) []byte {
	msgbuff := NewBuffer(msg)
	defer msgbuff.Free()

	// check sizes
	if len(pk) != int(C.crypto_box_publickeybytes()) {
		log.Println("len(pk) != crypto_box_publickey_bytes")
		return nil
	}
	if len(sk) != int(C.crypto_box_secretkeybytes()) {
		log.Println("len(sk) != crypto_box_secretkey_bytes")
		return nil
	}
	if len(nounce) != int(C.crypto_box_macbytes()) {
		log.Println("len(nounce) != crypto_box_macbytes()")
		return nil
	}

	pkbuff := NewBuffer(pk)
	defer pkbuff.Free()
	skbuff := NewBuffer(sk)
	defer skbuff.Free()
	nouncebuff := NewBuffer(nounce)
	defer nouncebuff.Free()

	resultbuff := malloc(msgbuff.size + nouncebuff.size)
	defer resultbuff.Free()
	res := C.crypto_box_easy(resultbuff.uchar(), msgbuff.uchar(), C.ulonglong(msgbuff.size), nouncebuff.uchar(), pkbuff.uchar(), skbuff.uchar())
	if res != 0 {
		log.Println("crypto_box_easy failed:", res)
		return nil
	}
	return resultbuff.Bytes()
}
Esempio n. 2
0
func BoxEasy(cypherTextOut []byte, message []byte, nonce, pk, sk []byte) int {
	checkSize(cypherTextOut, BoxMacBytes()+len(message), "cypher text output")
	checkSize(nonce, BoxNonceBytes(), "nonce")
	checkSize(pk, BoxPublicKeyBytes(), "public key")
	checkSize(sk, BoxSecretKeyBytes(), "secret key")

	return int(C.crypto_box_easy((*C.uchar)(&cypherTextOut[0]),
		(*C.uchar)(&message[0]), (C.ulonglong)(len(message)),
		(*C.uchar)(&nonce[0]),
		(*C.uchar)(&pk[0]),
		(*C.uchar)(&sk[0])))
}
Esempio n. 3
0
func CryptoBoxEasy(m []byte, n []byte, pk []byte, sk []byte) ([]byte, int) {
	support.CheckSize(n, CryptoBoxNonceBytes(), "nonce")
	support.CheckSize(pk, CryptoBoxPublicKeyBytes(), "public key")
	support.CheckSize(sk, CryptoBoxSecretKeyBytes(), "secret key")
	c := make([]byte, len(m)+CryptoBoxMacBytes())
	exit := int(C.crypto_box_easy(
		(*C.uchar)(&c[0]),
		(*C.uchar)(&m[0]),
		(C.ulonglong)(len(m)),
		(*C.uchar)(&n[0]),
		(*C.uchar)(&pk[0]),
		(*C.uchar)(&sk[0])))

	return c, exit
}