Esempio n. 1
0
// ReceiveFromMix decrypts a message received from the mix
func ReceiveFromMix(receiveTemplate nymaddr.AddressTemplate, MailboxAddress, msg []byte) (decMessage, Nym []byte, err error) {
	if len(msg) < 2 {
		return nil, nil, ErrTooShort
	}
	headerLen := int(binary.BigEndian.Uint16(msg[0:2]))
	if len(msg) < 2+headerLen {
		return nil, nil, ErrTooShort
	}
	headerContent := msg[2 : 2+headerLen]
	nym, secret, err := receiveTemplate.GetPrivate(headerContent, MailboxAddress)
	if err != nil {
		return nil, nil, err
	}
	nonce := sha256.Sum256(headerContent)
	decMessage, err = GCMDecrypt(nonce[:], secret, msg[2+headerLen:])
	if err != nil {
		return nil, nil, err
	}
	return decMessage, nym, nil
}
Esempio n. 2
0
// NewNymAddress generates a new nym address.
func NewNymAddress(
	domain string,
	secret []byte,
	expire int64,
	singleUse bool,
	minDelay, maxDelay int32,
	id string,
	pubkey *[ed25519.PublicKeySize]byte,
	server string,
	caCert []byte,
) (mixaddress, nymaddress string, err error) {
	if err := identity.IsMapped(id); err != nil {
		return "", "", log.Error(err)
	}
	if MixAddress == "" {
		return "", "", log.Error("util: MixAddress undefined")
	}
	mixAddresses, err := client.GetMixKeys(MixAddress, caCert)
	if err != nil {
		return "", "", log.Error(err)
	}
	tmp := nymaddr.AddressTemplate{
		Secret:        secret,
		System:        0,
		MixCandidates: mixAddresses.Addresses,
		Expire:        expire,
		SingleUse:     singleUse,
		MinDelay:      minDelay,
		MaxDelay:      maxDelay,
	}
	nymAddress, err := tmp.NewAddress(MailboxAddress(pubkey, server),
		cipher.SHA256([]byte(id)))
	if err != nil {
		return "", "", log.Error(err)
	}
	addr, err := nymaddr.ParseAddress(nymAddress)
	if err != nil {
		return "", "", log.Error(err)
	}
	return string(addr.MixAddress), base64.Encode(nymAddress), nil
}
Esempio n. 3
0
func TestSendReceiveRelay(t *testing.T) {
	_, privkey, _ := ed25519.GenerateKey(rand.Reader)
	mixAddress := "*****@*****.**"
	recAddress := "mailbox001@001."
	pseudonym := []byte("Pseudonym001")
	pseudoHash := sha256.Sum256(pseudonym)
	kl := mixaddr.New(privkey, mixAddress, 7200, 24*3600, "/tmp/mixkeydir")
	kl.AddKey()
	stmt := kl.GetStatement()
	// AddressTemplate contains parameters for address creation
	addressTemplate := nymaddr.AddressTemplate{
		Secret: []byte("something super-secret"),

		MixCandidates: stmt.Addresses,

		Expire:    times.Now() + 3600,
		SingleUse: true,
		MinDelay:  10,
		MaxDelay:  30,
	}
	NymAddress, err := addressTemplate.NewAddress([]byte(recAddress), pseudoHash[:])
	if err != nil {
		t.Fatalf("NewAddress: %s", err)
	}
	clientHeader := ClientMixHeader{
		SenderMinDelay: 10,
		SenderMaxDelay: 30,
		Token:          []byte("Example token"),
	}
	encMessage, nextaddress, err := clientHeader.NewRelayMessage(NymAddress, testMessage)
	if err != nil {
		t.Fatalf("NewRelayMessage: %s", err)
	}
	if nextaddress != mixAddress {
		t.Error("Bad NextHop")
	}
	receiveData, err := ReceiveMessage(kl.GetPrivateKey, encMessage)
	if err != nil {
		t.Fatalf("ReceiveMessage: %s", err)
	}
	if len(receiveData.UniqueTest) != 2 {
		t.Error("SingleUse nymaddress, exactly two uniquetests necessary")
	}
	if !bytes.Equal(receiveData.Message, testMessage) {
		t.Error("Messages dont match")
	}
	if !bytes.Equal(receiveData.MixHeader.Token, clientHeader.Token) {
		t.Error("Tokens dont match")
	}
	if receiveData.MixHeader.SenderMaxDelay != clientHeader.SenderMaxDelay {
		t.Error("Max delay does not match")
	}
	if receiveData.MixHeader.SenderMinDelay != clientHeader.SenderMinDelay {
		t.Error("Min delay does not match")
	}
	newMessage, nextaddress, err := receiveData.Send()
	if err != nil {
		t.Fatalf("Send-Along: %s", err)
	}
	if nextaddress != "*****@*****.**" {
		t.Error("Bad address expansion")
	}
	decMessage, nym, err := ReceiveFromMix(addressTemplate, []byte(recAddress), newMessage)
	if err != nil {
		t.Fatalf("ReceiveFromMix: %s", err)
	}
	if !bytes.Equal(nym, pseudoHash[:]) {
		t.Error("Nyms do not match")
	}
	if !bytes.Equal(decMessage, testMessage) {
		t.Error("Message decryption failed")
	}
}