Ejemplo n.º 1
0
func openBox(box []byte, key PrivateKey) (btype byte, message []byte, ok bool) {
	if box == nil {
		return 0, nil, false
	} else if !KeyIsSuitable(key, nil) {
		return 0, nil, false
	}
	btype = box[0]
	unpacker := newbr(box[1:])
	eph_pub := unpacker.Next()
	sbox := unpacker.Next()

	shared, ok := ecdh(key, eph_pub)
	if !ok {
		return 0, nil, false
	}

	message, ok = strongbox.Open(sbox, shared)
	if !ok {
		return 0, nil, false
	}

	return btype, message, true
}
Ejemplo n.º 2
0
func unpackSharedBox(box []byte, key PrivateKey, public PublicKey) (btype byte, message []byte, ok bool) {
	if box == nil {
		return 0, nil, false
	} else if !KeyIsSuitable(key, public) {
		return 0, nil, false
	}
	btype = box[0]

	unpacker := newbr(box[1:])
	e_pub := unpacker.Next()
	if e_pub == nil {
		return 0, nil, false
	}

	packedPeers := unpacker.Next()
	if packedPeers == nil {
		return 0, nil, false
	} else if packedPeers[0] != peerList {
		return 0, nil, false
	}
	peerUnpack := newbr(packedPeers[1:])
	peerCount, ok := peerUnpack.NextU32()
	if !ok {
		return 0, nil, false
	}

	var shared []byte = nil
	defer zero(shared)

	for i := uint32(0); i < peerCount; i++ {
		peer := peerUnpack.Next()
		if peer == nil {
			return 0, nil, false
		}
		sbox := peerUnpack.Next()
		if sbox == nil {
			return 0, nil, false
		} else if !bytes.Equal(peer, public) {
			continue
		}
		skey, ok := ecdh(key, e_pub)
		if !ok {
			return 0, nil, false
		}
		shared, ok = strongbox.Open(sbox, skey)
		if !ok {
			return 0, nil, false
		}
		break
	}
	if shared == nil {
		return 0, nil, false
	}
	sbox := unpacker.Next()
	if sbox == nil {
		return 0, nil, false
	}
	message, ok = strongbox.Open(sbox, shared)
	if !ok {
	}
	return btype, message, ok
}