Пример #1
0
// open an encrypted box
func CryptoBoxOpen(box, nounce, sk, pk []byte) ([]byte, error) {
	boxbuff := NewBuffer(box)
	defer boxbuff.Free()

	// check sizes
	if len(pk) != int(C.crypto_box_publickeybytes()) {
		err := errors.New("len(pk) != crypto_box_publickey_bytes")
		return nil, err
	}
	if len(sk) != int(C.crypto_box_secretkeybytes()) {
		err := errors.New("len(sk) != crypto_box_secretkey_bytes")
		return nil, err
	}
	if len(nounce) != int(C.crypto_box_macbytes()) {
		err := errors.New("len(nounce) != crypto_box_macbytes()")
		return nil, err
	}

	pkbuff := NewBuffer(pk)
	defer pkbuff.Free()
	skbuff := NewBuffer(sk)
	defer skbuff.Free()
	nouncebuff := NewBuffer(nounce)
	defer nouncebuff.Free()
	resultbuff := malloc(boxbuff.size - nouncebuff.size)
	defer resultbuff.Free()

	// decrypt
	res := C.crypto_box_open_easy(resultbuff.uchar(), boxbuff.uchar(), C.ulonglong(boxbuff.size), nouncebuff.uchar(), pkbuff.uchar(), skbuff.uchar())
	if res != 0 {
		return nil, errors.New("crypto_box_open_easy failed")
	}
	// return result
	return resultbuff.Bytes(), nil
}
Пример #2
0
Файл: box.go Проект: 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()
}
Пример #3
0
func GenBoxKeypair() *KeyPair {
	sk_len := C.crypto_box_secretkeybytes()
	sk := malloc(sk_len)
	pk_len := C.crypto_box_publickeybytes()
	pk := malloc(pk_len)
	res := C.crypto_box_keypair(pk.uchar(), sk.uchar())
	if res == 0 {
		return &KeyPair{pk, sk}
	}
	pk.Free()
	sk.Free()
	return nil
}
Пример #4
0
Файл: key.go Проект: 4cdn/srndv2
func GenBoxKeypair() *KeyPair {
	sk_len := C.crypto_box_secretkeybytes()
	sk := malloc(sk_len)
	pk_len := C.crypto_box_publickeybytes()
	pk := malloc(pk_len)
	res := C.crypto_box_keypair(pk.uchar(), sk.uchar())
	if res == 0 {
		return &KeyPair{pk, sk}
	}
	log.Println("nacl.GenBoxKeyPair() failed to generate keypair")
	pk.Free()
	sk.Free()
	return nil
}
Пример #5
0
// get public key from secret key
func GetBoxPubkey(sk []byte) []byte {
	sk_len := C.crypto_box_seedbytes()
	if C.size_t(len(sk)) != sk_len {
		return nil
	}

	pk_len := C.crypto_box_publickeybytes()
	pkbuff := malloc(pk_len)
	defer pkbuff.Free()

	skbuff := NewBuffer(sk)
	defer skbuff.Free()

	// compute the public key
	C.crypto_scalarmult_base(pkbuff.uchar(), skbuff.uchar())

	return pkbuff.Bytes()
}
Пример #6
0
// make keypair from seed
func SeedBoxKey(seed []byte) *KeyPair {
	seed_len := C.crypto_box_seedbytes()
	if C.size_t(len(seed)) != seed_len {
		return nil
	}
	seedbuff := NewBuffer(seed)
	defer seedbuff.Free()
	pk_len := C.crypto_box_publickeybytes()
	sk_len := C.crypto_box_secretkeybytes()
	pkbuff := malloc(pk_len)
	skbuff := malloc(sk_len)
	res := C.crypto_box_seed_keypair(pkbuff.uchar(), skbuff.uchar(), seedbuff.uchar())
	if res != 0 {
		pkbuff.Free()
		skbuff.Free()
		return nil
	}
	return &KeyPair{pkbuff, skbuff}
}
Пример #7
0
Файл: key.go Проект: 4cdn/srndv2
// make keypair from seed
func SeedBoxKey(seed []byte) *KeyPair {
	seed_len := C.crypto_box_seedbytes()
	if C.size_t(len(seed)) != seed_len {
		log.Println("nacl.SeedBoxKey() invalid seed size", len(seed))
		return nil
	}
	seedbuff := NewBuffer(seed)
	defer seedbuff.Free()
	pk_len := C.crypto_box_publickeybytes()
	sk_len := C.crypto_box_secretkeybytes()
	pkbuff := malloc(pk_len)
	skbuff := malloc(sk_len)
	res := C.crypto_box_seed_keypair(pkbuff.uchar(), skbuff.uchar(), seedbuff.uchar())
	if res != 0 {
		pkbuff.Free()
		skbuff.Free()
		log.Println("nacl.SeedBoxKey cannot derive keys from seed:", res)
		return nil
	}
	return &KeyPair{pkbuff, skbuff}
}
Пример #8
0
// BoxPublicKeyBytes returns the expected size, in bytes, of the public keys for the box functions.
func BoxPublicKeyBytes() int {
	return int(C.crypto_box_publickeybytes())
}
Пример #9
0
// size of crypto_box public keys
func CryptoBoxPubKeySize() int {
	return int(C.crypto_box_publickeybytes())
}